From 3fd9912363570e3a8a436150a69c02cd47ce5a47 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Thu, 2 Jan 2025 18:09:52 +0000 Subject: [PATCH 1/2] Update pre-commit hook DavidAnson/markdownlint-cli2 to v0.17.1 (#2960) | datasource | package | from | to | | ----------- | ---------------------------- | ------- | ------- | | github-tags | DavidAnson/markdownlint-cli2 | v0.17.0 | v0.17.1 | Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d41790d866..a7d54e2d77 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -53,7 +53,7 @@ repos: # Rules are in .markdownlint-cli2.yaml file # See https://github.com/DavidAnson/markdownlint/blob/main/doc/Rules.md for rule descriptions - repo: https://github.com/DavidAnson/markdownlint-cli2 - rev: v0.17.0 + rev: v0.17.1 hooks: - id: markdownlint-cli2 From 3683b584fb781134021c90c86823fee4e196866a Mon Sep 17 00:00:00 2001 From: salonichf5 <146118978+salonichf5@users.noreply.github.com> Date: Thu, 2 Jan 2025 11:37:38 -0700 Subject: [PATCH 2/2] Fix rewrite path for ReplacePrefixMatch to parse request arguments correctly (#2951) fix rewrite path for ReplacePrefixMatch --- internal/mode/static/nginx/config/servers.go | 14 ++++++----- .../mode/static/nginx/config/servers_test.go | 14 +++++------ .../redirects-and-rewrites.md | 23 +++++++++++++++++++ 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/internal/mode/static/nginx/config/servers.go b/internal/mode/static/nginx/config/servers.go index e7ccef05fc..d387404f51 100644 --- a/internal/mode/static/nginx/config/servers.go +++ b/internal/mode/static/nginx/config/servers.go @@ -606,22 +606,24 @@ func createRewritesValForRewriteFilter(filter *dataplane.HTTPURLRewriteFilter, p filterPrefix = "/" } - // capture everything after the configured prefix - regex := fmt.Sprintf("^%s(.*)$", path) - // replace the configured prefix with the filter prefix and append what was captured - replacement := fmt.Sprintf("%s$1", filterPrefix) + // capture everything following the configured prefix up to the first ?, if present. + regex := fmt.Sprintf("^%s([^?]*)?", path) + // replace the configured prefix with the filter prefix, append the captured segment, + // and include the request arguments stored in nginx variable $args. + // https://nginx.org/en/docs/http/ngx_http_core_module.html#var_args + replacement := fmt.Sprintf("%s$1?$args?", filterPrefix) // if configured prefix does not end in /, but replacement prefix does end in /, // then make sure that we *require* but *don't capture* a trailing slash in the request, // otherwise we'll get duplicate slashes in the full replacement if strings.HasSuffix(filterPrefix, "/") && !strings.HasSuffix(path, "/") { - regex = fmt.Sprintf("^%s(?:/(.*))?$", path) + regex = fmt.Sprintf("^%s(?:/([^?]*))?", path) } // if configured prefix ends in / we won't capture it for a request (since it's not in the regex), // so append it to the replacement prefix if the replacement prefix doesn't already end in / if strings.HasSuffix(path, "/") && !strings.HasSuffix(filterPrefix, "/") { - replacement = fmt.Sprintf("%s/$1", filterPrefix) + replacement = fmt.Sprintf("%s/$1?$args?", filterPrefix) } rewrites.MainRewrite = fmt.Sprintf("%s %s break", regex, replacement) diff --git a/internal/mode/static/nginx/config/servers_test.go b/internal/mode/static/nginx/config/servers_test.go index 24b40d0f60..1a92090a26 100644 --- a/internal/mode/static/nginx/config/servers_test.go +++ b/internal/mode/static/nginx/config/servers_test.go @@ -1307,7 +1307,7 @@ func TestCreateServers(t *testing.T) { }, { Path: "/_ngf-internal-rule8-route0", - Rewrites: []string{"^ $request_uri", "^/rewrite-with-headers(.*)$ /prefix-replacement$1 break"}, + Rewrites: []string{"^ $request_uri", "^/rewrite-with-headers([^?]*)? /prefix-replacement$1?$args? break"}, ProxyPass: "http://test_foo_80", ProxySetHeaders: rewriteProxySetHeaders, Type: http.InternalLocationType, @@ -2427,7 +2427,7 @@ func TestCreateRewritesValForRewriteFilter(t *testing.T) { }, expected: &rewriteConfig{ InternalRewrite: "^ $request_uri", - MainRewrite: "^/original(.*)$ /prefix-path$1 break", + MainRewrite: "^/original([^?]*)? /prefix-path$1?$args? break", }, msg: "prefix path no trailing slashes", }, @@ -2441,7 +2441,7 @@ func TestCreateRewritesValForRewriteFilter(t *testing.T) { }, expected: &rewriteConfig{ InternalRewrite: "^ $request_uri", - MainRewrite: "^/original(?:/(.*))?$ /$1 break", + MainRewrite: "^/original(?:/([^?]*))? /$1?$args? break", }, msg: "prefix path empty string", }, @@ -2455,7 +2455,7 @@ func TestCreateRewritesValForRewriteFilter(t *testing.T) { }, expected: &rewriteConfig{ InternalRewrite: "^ $request_uri", - MainRewrite: "^/original(?:/(.*))?$ /$1 break", + MainRewrite: "^/original(?:/([^?]*))? /$1?$args? break", }, msg: "prefix path /", }, @@ -2469,7 +2469,7 @@ func TestCreateRewritesValForRewriteFilter(t *testing.T) { }, expected: &rewriteConfig{ InternalRewrite: "^ $request_uri", - MainRewrite: "^/original(?:/(.*))?$ /trailing/$1 break", + MainRewrite: "^/original(?:/([^?]*))? /trailing/$1?$args? break", }, msg: "prefix path replacement with trailing /", }, @@ -2483,7 +2483,7 @@ func TestCreateRewritesValForRewriteFilter(t *testing.T) { }, expected: &rewriteConfig{ InternalRewrite: "^ $request_uri", - MainRewrite: "^/original/(.*)$ /prefix-path/$1 break", + MainRewrite: "^/original/([^?]*)? /prefix-path/$1?$args? break", }, msg: "prefix path original with trailing /", }, @@ -2497,7 +2497,7 @@ func TestCreateRewritesValForRewriteFilter(t *testing.T) { }, expected: &rewriteConfig{ InternalRewrite: "^ $request_uri", - MainRewrite: "^/original/(.*)$ /trailing/$1 break", + MainRewrite: "^/original/([^?]*)? /trailing/$1?$args? break", }, msg: "prefix path both with trailing slashes", }, diff --git a/site/content/how-to/traffic-management/redirects-and-rewrites.md b/site/content/how-to/traffic-management/redirects-and-rewrites.md index d5ca7b63da..8785da32aa 100644 --- a/site/content/how-to/traffic-management/redirects-and-rewrites.md +++ b/site/content/how-to/traffic-management/redirects-and-rewrites.md @@ -188,6 +188,17 @@ Server name: coffee-6b8b6d6486-7fc78 URI: /beans ``` +```shell +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee/mocha\?test\=v1\&test\=v2 +``` + +```text +Server address: 10.244.0.235:8080 +Server name: coffee-6db967495b-twn6x +... +URI: /beans?test=v1&test=v2 +``` + ```shell curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/latte/prices ``` @@ -199,6 +210,18 @@ Server name: coffee-6b8b6d6486-7fc78 URI: /prices ``` +```shell +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/latte/prices\?test\=v1\&test\=v2 +``` + +```text +curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/latte/prices\?test\=v1\&test\=v2 +Server address: 10.244.0.235:8080 +Server name: coffee-6db967495b-twn6x +... +URI: /prices?test=v1&test=v2 +``` + ## Further reading To learn more about redirects and rewrites using the Gateway API, see the following resource: