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

feat(router) add decode_uri_captures parameter #8064

Conversation

yankun-li-kong
Copy link
Contributor

@yankun-li-kong yankun-li-kong commented Nov 10, 2021

Add a new parameter decode_uri_captures to decide whether to normalize the captured request URI after route matching.

New feature for #7913

Summary

Add a new parameter decode_uri_captures to decide whether to normalize the captured request URI after route matching.

Only allow setting on or off:

  • on: Captured request URI decoding is enabled.
  • off: Captured request URI decoding is disabled.

By default, this value is set as on.

Examples of decode_uri_captures = on behavior:

  • Assuming that the route path is /plain/(a%2Eb%20c)/(.*) and the request URI is /plain/a%2Eb%20c/a%2Eb%20c, the captured request URI is decoded as /plain/a.b c/a.b c.
  • Assuming that the route path is /plain/(a.b c)/(.*) and the request URI is /plain/a%2Eb%20c/a%2Eb%20c, the captured request URI is decoded as /plain/a.b c/a.b c.

Examples of decode_uri_captures = off

  • Assuming that the route path is /plain/(a%2Eb%20c)/(.*) and the request URI is /plain/a%2Eb%20c/a%2Eb%20c, the captured request URI is /plain/a%2Eb%20c/a%2Eb%20c without URI decoding.
  • Assuming that the route path is /plain/(a.b c)/(.*) and the request URI is /plain/a%2Eb%20c/a%2Eb%20c, the request URI can not match the route path without URI decoding. As a result, the captured request URI is decoded as /plain/a.b c/a.b c.

Full changelog

  • Add a parameter decode_uri_captures to only decide whether to decode captured request URI or not (Do not have affect for route matching and upstream request)
  • Use a self parameter(decode_uri_captures) of kong.router to load the new parameter.
  • Add unit tests

Issues resolved

Fix #7913

@CLAassistant
Copy link

CLAassistant commented Nov 10, 2021

CLA assistant check
All committers have signed the CLA.

@yankun-li-kong yankun-li-kong changed the title [WIP] feat(router) add normalize-req-uri parameter feat(router) add normalize-req-uri parameter Nov 14, 2021
kong/conf_loader/init.lua Outdated Show resolved Hide resolved
# - `on`: Request URI normalization is enabled.
# - `off`: Request URI normalization is disabled.
#
# By default this value is set to `on`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normalization and decoding are two different things and have different consequences.

this description doesn't explain if the process is done only for route matching or if it affects the upstream request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your advice,
now I am considering only using the unnormalized req uri to get the match_t.matches.uri_captures instead.

Then it should not affect route matching and upstream request

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace normalize_req_uri with normalize_uri_captures to only affect captured request URI.
Please help check again, thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is still calling the process "normalization", but it's decoding. i guess the function was badly named, but this change makes the error visible in the documentation and parameter name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your review!
Just submit a commit to rename normalize_uri_captures to decode_uri_captures.

@yankun-li-kong yankun-li-kong changed the title feat(router) add normalize-req-uri parameter feat(router) add normalize_uri_captures parameter Nov 28, 2021
@yankun-li-kong
Copy link
Contributor Author

yankun-li-kong commented Nov 28, 2021

Replace normalize_req_uri with decode_uri_captures to only affect captured request URI.

When decode_uri_captures is set as on, the behavior is the same as previous.

When decode_uri_captures is set as off,
kong will first try to get undecoded uri_captures by using unnormalized req_uri and unnormalized regex like below example-1:
Regex: /plain/(a%2Eb%20c)/(.*)
Request URI: /plain/a%2Eb%20c/a%2Eb%20c
then the uri_captures is /plain/a%2Eb%20c/a%2Eb%20c without URI decoding.

If kong failed to get uri_captures, it will nomalize req_uri and regex to get decoded uri_captures instead
like below example-2:
Regex: /plain/(a.b c)/(.*)
Request URI: /plain/a%2Eb%20c/a%2Eb%20c
The request URI can not match the regex without URI decoding.
As a result, the uri_captures is decoded as /plain/a.b c/a.b c.

Now customers are able to get undecoded uri_captures by setting decode_uri_captures as off
and using a correct regex for a route path.
It means customers have to consider what is the actual request URI passed to Kong,
then try to match that request URI with route path without URI decoding like the example-1.

Add a new parameter normalize-req-uri to decide whether to normalize the request URI or not.

New feature for Kong#7913
Use a self parameter(normalize_req_uri) of kong.router to load the new parameter.
It will be easier to add a new route parameter to control the URI normalization behavior in the future.

New feature for Kong#7913
Modify the 'if condition' for the case self.normalize_req_uri is nil to pass the original unit test.
Add unit tests.

New feature for Kong#7913
The normalize-req-uri may affect route matching and the upstream request.
Replace normalize-req-uri with normalize_uri_captures to only affect captured request URI.
If normalize_uri_captures is set to on, the behavior is same as previous.
If normalize_uri_captures is set to off, it will try to the capture the request URI without URI normalization.
Check more details in normalize_uri_captures part from kong.conf.default.

New feature for Kong#7913
@yankun-li-kong yankun-li-kong force-pushed the feat/add-normalize-req-uri-param branch from 7edbb03 to f8ceec8 Compare November 28, 2021 12:32
@yankun-li-kong
Copy link
Contributor Author

Rebased

Rename normalize_uri_captures to decode_uri_captures

New feature for Kong#7913
@yankun-li-kong yankun-li-kong changed the title feat(router) add normalize_uri_captures parameter feat(router) add decode_uri_captures parameter Dec 2, 2021
@yankun-li-kong
Copy link
Contributor Author

yankun-li-kong commented Dec 2, 2021

Renamed normalize_uri_captures to decode_uri_captures

@bungle
Copy link
Member

bungle commented Dec 7, 2021

I don't think we want this to be configuration option. We just need to do proper thing.

@guanlan guanlan requested a review from a team as a August 25, 2022 01:38
@hbagdi
Copy link
Member

hbagdi commented Oct 26, 2022

@dndx We need to make a decision on if we want to proceed or not here.

@hanshuebner
Copy link
Contributor

Is this pull request still relevant with Kong Gateway 3.x?

@hanshuebner hanshuebner added the pending author feedback Waiting for the issue author to get back to a maintainer with findings, more details, etc... label Feb 7, 2023
@kikito
Copy link
Member

kikito commented Feb 9, 2023

We decided to not go this way, instead we did #8140

@kikito kikito closed this Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core/router pending author feedback Waiting for the issue author to get back to a maintainer with findings, more details, etc...
Projects
None yet
Development

Successfully merging this pull request may close these issues.

uri_captures are url decoded
7 participants