Skip to content

internal/dag: give each HTTPRoute match its own prefix removal regex - #7847

Open
BahaaMattar wants to merge 2 commits into
projectcontour:mainfrom
BahaaMattar:fix-gwapi-prefix-rewrite-multi-match
Open

BahaaMattar wants to merge 2 commits into
projectcontour:mainfrom
BahaaMattar:fix-gwapi-prefix-rewrite-multi-match

Conversation

@BahaaMattar

Copy link
Copy Markdown

Fixes #7846

When an HTTPRoute rule has a URLRewrite or RequestRedirect filter that replaces the matched prefix with "/", handlePathRewritePrefixRemoval turns the PathRewritePolicy into a regex for the prefix of the match (^/foo/*). It did that by changing the policy in place, and the policy is shared by all matches of the rule: clusterRoutes and redirectRoutes call it once per match with the same policy. After the first PathPrefix match the policy no longer has PrefixRewrite == "/", so the other matches kept the regex of the first one. A rule with matches /foo and /bar got ^/foo/* on both routes, and a request to /bar/x was not rewritten (or redirected) without its prefix.

This returns a modified copy of the policy for each match instead of changing the shared one. For redirect routes each route also gets its own copy of the Redirect, so the per-match policy is not written back to the shared object. Rules with one match, ReplaceFullPath, a different ReplacePrefixMatch value, or an Exact/Regex match produce the same result as before.

Tests: two new TestDAGInsertGatewayAPI cases, one for URLRewrite and one for RequestRedirect, with matches /foo and /bar and ReplacePrefixMatch /. They check that the /bar route has ^/bar/*. Both fail on main (the /bar route has ^/foo/*) and pass with this change.

Checked locally: gofmt, go vet, golangci-lint (0 issues), go test ./internal/dag ./internal/featuretests/v3 ./internal/envoy/...

A changelogs/unreleased/<PR#>-BahaaMattar-small.md file is included.

For an HTTPRoute rule whose URLRewrite or RequestRedirect filter replaces
the matched prefix with "/", handlePathRewritePrefixRemoval turns the
PathRewritePolicy into a regex for the prefix of the match, and it did so
by changing the policy in place. That policy is shared by every match of
the rule, so once the first PathPrefix match had set the regex, the other
matches reused it. A rule matching /foo and /bar ended up with the regex
"^/foo/*" on both routes, and requests to /bar/x were neither rewritten
nor redirected without the prefix.

Return a modified copy of the policy for each match instead of changing
the shared one, and give each redirect route its own copy of the Redirect
so the per-match policy is not written back to the shared Redirect.

Signed-off-by: Bahaa Mattar <bahaa.kmattar@gmail.com>
@BahaaMattar
BahaaMattar requested a review from a team as a code owner November 20, 2026 13:55
@BahaaMattar
BahaaMattar requested review from tsaarni and removed request for a team November 20, 2026 13:55
Signed-off-by: Bahaa Mattar <bahaa.kmattar@gmail.com>

@Sun-Wukhan Sun-Wukhan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The fix looks right. Returning a copy from handlePathRewritePrefixRemoval once the prefix is known, instead of mutating the policy shared by every match of the rule, addresses the root cause, and copying the redirect per match in redirectRoutes closes the second leak where every route pointed at the same Redirect. The two new builder cases, one per filter type with two matches, cover exactly the reported shape.

Two smaller points:

  1. The changelog entry has lost the backticked terms. As committed, changelogs/unreleased/7847-BahaaMattar-small.md reads "an HTTPRoute rule that has several matches and a or filter replacing the matched prefix with now removes the prefix of each match", with double spaces where the match and filter names should be. Could you restore the missing words so the release note parses?

  2. Worth pinning the non removal path as well: both new tests use ReplacePrefixMatch: "/". A case where several matches share a policy with a non root replacement (for example "/new") would confirm the per match copy did not change behaviour for the path that never entered the removal branch, since handlePathRewritePrefixRemoval still returns the shared pointer there and the routes now rely on nobody mutating it downstream.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants