Repository navigation
internal/dag: give each HTTPRoute match its own prefix removal regex - #7847
BahaaMattar wants to merge 2 commits into
Conversation
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>
Signed-off-by: Bahaa Mattar <bahaa.kmattar@gmail.com>
Sun-Wukhan
left a comment
There was a problem hiding this comment.
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:
-
The changelog entry has lost the backticked terms. As committed,
changelogs/unreleased/7847-BahaaMattar-small.mdreads "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? -
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, sincehandlePathRewritePrefixRemovalstill returns the shared pointer there and the routes now rely on nobody mutating it downstream.
Fixes #7846
When an HTTPRoute rule has a URLRewrite or RequestRedirect filter that replaces the matched prefix with "/",
handlePathRewritePrefixRemovalturns 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:clusterRoutesandredirectRoutescall it once per match with the same policy. After the first PathPrefix match the policy no longer hasPrefixRewrite == "/", so the other matches kept the regex of the first one. A rule with matches/fooand/bargot^/foo/*on both routes, and a request to/bar/xwas 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
TestDAGInsertGatewayAPIcases, one for URLRewrite and one for RequestRedirect, with matches/fooand/barand ReplacePrefixMatch/. They check that the/barroute has^/bar/*. Both fail on main (the/barroute 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.