kong icon indicating copy to clipboard operation
kong copied to clipboard

Optional capture groups are broken with the request-transformer plugin and traditional_compatible router

Open Dawnflash opened this issue 1 year ago • 4 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Kong version ($ kong version)

3.6

Current Behavior

When I

  • use the traditional_compatible router
  • create a regex path ~/(gw/)?api/(?<subpath>htest)$
  • match it against /api/htest
  • add the following request-transformer plugin config:
    replace:
      uri: /api/v2/$(uri_captures.subpath)
    

The route sent upstream is /api/v2/ instead of /api/v2/htest. When I request /gw/api/htest (the gw/ optional group matches) the route sent upstream is correct: /api/v2/htest.

Expected Behavior

The route sent upstream is /api/v2/htest, respecting the subpath capture group even if the optional group doesn't match. The traditional router behaves like this, the bug is only observed with traditional_compatible.

Steps To Reproduce

Use this deck config:

services:
  - name: htest-deck
    url: http://htest.infra
    routes:
      - name: htest-routes
        paths:
          - ~/(gw/)?api/(?<subpath>htest)$
        strip_path: false
        plugins:
          - name: request-transformer
            config:
              replace:
                uri: /api/v2/$(uri_captures["subpath"])

with the traditional_compatible router on Kong 3.6.

Anything else?

debug logs from request-transformer with route /api/htest:

2024/05/10 10:10:05 [debug] 1284#0: *12328 [kong] access.lua:486 [request-transformer] 8[request-transformer] template `/api/v2/$(uri_captures["subpath"])` rendered to `/api/v2/`
172.21.9.0 - - [10/May/2024:10:10:05 +0000] "GET /api/htest HTTP/1.1" 200 409 "-" "curl/8.4.0" kong_request_id: "882af5474b6f451100612f6d94bca01f"

Dawnflash avatar May 10 '24 10:05 Dawnflash

I believe this is the reason for this bug: https://github.com/Kong/kong/blob/78d45822fb4ec456cf8917b0f830e80ef0559625/kong/router/atc.lua#L390

The code assumes that the first capture must appear if the route matches.

It should be easy to fix.

StarlightIbuki avatar May 11 '24 07:05 StarlightIbuki

@StarlightIbuki , could you create a PR to fix it?

chronolaw avatar May 13 '24 01:05 chronolaw

@StarlightIbuki , could you create a PR to fix it?

As this is a relatively simple and not urgent task, I intentionally marked this as a "good first issue", for anyone who wants to contribute to Kong.

StarlightIbuki avatar May 13 '24 03:05 StarlightIbuki

--> KAG-4474

samugi avatar May 13 '24 07:05 samugi