Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
Dawnflash opened this issue May 10, 2024 · 4 comments · Fixed by #13024
Closed
1 task done
Labels
good first issue Issues that beginners/volunteers can easily help with.

Comments

@Dawnflash
Copy link

Dawnflash commented May 10, 2024

Is there an existing issue for this?

  • 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"
@StarlightIbuki
Copy link
Contributor

StarlightIbuki commented May 11, 2024

I believe this is the reason for this bug:

uri_captures = (captures and captures[1]) and captures or nil,

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

It should be easy to fix.

@StarlightIbuki StarlightIbuki added the good first issue Issues that beginners/volunteers can easily help with. label May 11, 2024
@chronolaw
Copy link
Contributor

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

@StarlightIbuki
Copy link
Contributor

@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.

@samugi
Copy link
Member

samugi commented May 13, 2024

--> KAG-4474

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issues that beginners/volunteers can easily help with.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants