Skip to content

fix(middleware): read $10 and above as one capture in rewrite targets - #3114

Open
SulimanAbdulrazzaq wants to merge 1 commit into
labstack:masterfrom
SulimanAbdulrazzaq:fix/rewrite-two-digit-captures
Open

SulimanAbdulrazzaq wants to merge 1 commit into
labstack:masterfrom
SulimanAbdulrazzaq:fix/rewrite-two-digit-captures

Conversation

@SulimanAbdulrazzaq

Copy link
Copy Markdown

captureTokens passes "$1", "$2", ... to strings.NewReplacer in ascending order. A Replacer tries its old strings in argument order, so once a rule has ten or more captures, $10 in the target is read as $1 followed by 0.

With the rule "/t/*/*/*/*/*/*/*/*/*/*/*": "/r/$11/$10/$1", a request for /t/a/b/c/d/e/f/g/h/i/j/k is rewritten to /r/a1/a0/a instead of /r/k/j/a. The same happens with a RegexRules entry that has ten or more groups. rewriteURL is shared by the Rewrite and Proxy middlewares, so both are affected.

The fix builds the replacement pairs from the highest index down, so the longer token is matched first.

TestEchoRewriteTwoDigitCaptures covers a wildcard rule and a regex rule with 11 captures. Without the fix it fails with expected: "/r/k/j/a", actual: "/r/a1/a0/a"; with it, go test -race ./middleware/... passes.

captureTokens passed "$1", "$2", ... to strings.NewReplacer in ascending
order. A Replacer tries its old strings in argument order, so once a rule
has ten or more captures, "$10" in the target was read as "$1" followed by
"0". With the rule "/t/*/*/*/*/*/*/*/*/*/*/*": "/r/$11/$10/$1", a request
for /t/a/b/c/d/e/f/g/h/i/j/k was rewritten to /r/a1/a0/a instead of
/r/k/j/a. This affects both the Rewrite and the Proxy middleware.

List the replacement pairs from the highest index down so the longer
token is matched first.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant