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

Fix rewrite path for ReplacePrefixMatch to parse request arguments correctly #2947

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

salonichf5
Copy link
Contributor

@salonichf5 salonichf5 commented Dec 23, 2024

Proposed changes

Write a clear and concise description that helps reviewers understand the purpose and impact of your changes. Use the
following format:

Problem: Users want query params to be parsed correctly when using ReplacePrefixMatch.

Solution: Updates the regex to parse query params to be parsed separately using the $args variable.

I updated the regex to capture everything from the matching variable to a ? found (if present) and then use nginx variable $args to append the request arguments. This avoids processing the entire variable after the prefix via nginx, leading to encoded characters in the resulting path.

  1. Regex update explanation

^%s(.*)$ --> ^%s([^?]*)? - Captures the path till a ? is found. Using ? at the end makes the capture group optional
%s$1 --> %s$1?$args? - $1 indicates the path captured above till a ? is found. Concatenating the path captured + ? + args associated with the request. The ? at the end, tells Nginx to not double append args.

  1. Updated documentation in Redirects and Rewrite example to showcase expected behavior when using query parameters when using ReplaceFullPath and ReplacePrefixMatch for users.

Testing:

  1. Updated existing unit tests
  2. Manual testing with Redirects and Redirects example with HTTPRoute slightly modified by adding method GET
kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: coffee
spec:
  parentRefs:
  - name: cafe
    sectionName: http
  hostnames:
  - "cafe.example.com"
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /latte
      method: GET
    filters:
    - type: URLRewrite
      urlRewrite:
        path:
          type: ReplacePrefixMatch
          replacePrefixMatch: /
    backendRefs:
    - name: coffee
      port: 80
EOF

Results

curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/latte/prices
Handling connection for 8080
Server address: 10.244.0.235:8080
Server name: coffee-6db967495b-twn6x
Date: 23/Dec/2024:20:46:15 +0000
URI: /prices
Request ID: 235d5fb72bf8c4be343926d27da0c996
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/latte
Handling connection for 8080
Server address: 10.244.0.235:8080
Server name: coffee-6db967495b-twn6x
Date: 23/Dec/2024:20:46:48 +0000
URI: /
Request ID: a7f8c33333984f922090f9ae672db40f
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/latte/prices\?test\=v1\&test\=v2
Handling connection for 8080
Server address: 10.244.0.235:8080
Server name: coffee-6db967495b-twn6x
Date: 23/Dec/2024:20:47:11 +0000
URI: /prices?test=v1&test=v2
Request ID: b21f0fd7005760a338eb99dfd6595af1
  1. Testing with the POST method
kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: coffee
spec:
  parentRefs:
  - name: cafe
    sectionName: http
  hostnames:
  - "cafe.example.com"
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /latte
      method: POST
    filters:
    - type: URLRewrite
      urlRewrite:
        path:
          type: ReplacePrefixMatch
          replacePrefixMatch: /
    backendRefs:
    - name: coffee
      port: 80
EOF

Results

curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/latte\?test\=v1 -X POST
Server address: 10.244.0.235:8080
Server name: coffee-6db967495b-twn6x
Date: 23/Dec/2024:18:39:38 +0000
URI: /?test=v1
Request ID: e2231bc3899fa29e1ce1e345c40cfa9b
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/latte/prices\?test\=v1 -X POST
Server address: 10.244.0.235:8080
Server name: coffee-6db967495b-twn6x
Date: 23/Dec/2024:18:40:27 +0000
URI: /prices?test=v1
Request ID: 72068bd237709ec7d4a7a02558cb461a
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/latte/ -X POST                
Server address: 10.244.0.235:8080
Server name: coffee-6db967495b-twn6x
Date: 23/Dec/2024:18:40:43 +0000
URI: /
Request ID: bcfb4194707d4c38f35e4e68987745fe

Please focus on (optional): If you any specific areas where you would like reviewers to focus their attention or provide
specific feedback, add them here.

Closes #2862

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked that all unit tests pass after adding my changes
  • I have updated necessary documentation
  • I have rebased my branch onto main
  • I will ensure my PR is targeting the main branch and pulling from my branch from my own fork

Release notes

If this PR introduces a change that affects users and needs to be mentioned in the release notes,
please add a brief note that summarizes the change.

NONE

@github-actions github-actions bot added bug Something isn't working documentation Improvements or additions to documentation labels Dec 23, 2024
@salonichf5 salonichf5 marked this pull request as ready for review December 23, 2024 21:03
@salonichf5 salonichf5 requested review from a team as code owners December 23, 2024 21:03
Copy link

codecov bot commented Dec 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.93%. Comparing base (938b7ff) to head (0befa29).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2947      +/-   ##
==========================================
+ Coverage   89.74%   89.93%   +0.18%     
==========================================
  Files         109      111       +2     
  Lines       11150    11423     +273     
  Branches       50       50              
==========================================
+ Hits        10007    10273     +266     
- Misses       1083     1089       +6     
- Partials       60       61       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@salonichf5 salonichf5 marked this pull request as draft December 23, 2024 21:06
@salonichf5 salonichf5 force-pushed the bug/url-rewrite branch 2 times, most recently from 70366fe to ab6d8ff Compare December 23, 2024 23:17
@salonichf5 salonichf5 marked this pull request as ready for review December 23, 2024 23:18
@salonichf5 salonichf5 changed the title Fix rewrite path for ReplacePrefixMatch to parse query parameters correctly Fix rewrite path for ReplacePrefixMatch to parse request arguments correctly Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
Status: 🆕 New
Development

Successfully merging this pull request may close these issues.

URLRewrite filter does not rewrite query parameters correctly
1 participant