fix(middleware): read $10 and above as one capture in rewrite targets - #3114
Open
SulimanAbdulrazzaq wants to merge 1 commit into
Open
SulimanAbdulrazzaq wants to merge 1 commit into
SulimanAbdulrazzaq wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
captureTokenspasses"$1", "$2", ...tostrings.NewReplacerin ascending order. AReplacertries its old strings in argument order, so once a rule has ten or more captures,$10in the target is read as$1followed by0.With the rule
"/t/*/*/*/*/*/*/*/*/*/*/*": "/r/$11/$10/$1", a request for/t/a/b/c/d/e/f/g/h/i/j/kis rewritten to/r/a1/a0/ainstead of/r/k/j/a. The same happens with aRegexRulesentry that has ten or more groups.rewriteURLis 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.
TestEchoRewriteTwoDigitCapturescovers a wildcard rule and a regex rule with 11 captures. Without the fix it fails withexpected: "/r/k/j/a",actual: "/r/a1/a0/a"; with it,go test -race ./middleware/...passes.