Repository navigation
Fix ApacheModRewrite tokenizer throwing on regex shorthand escapes - #68788
SergioAlmeida29 wants to merge 4 commits into
Conversation
Tokenizer.RemoveQuotesAndEscapeCharacters ran every token through Regex.Unescape, which throws ArgumentException for valid regex escape sequences such as \d, \w or \s, making AddApacheModRewrite unusable for standard mod_rewrite rules using shorthand character classes. It also silently corrupted patterns like \b by decoding it to a backspace character instead of a word boundary. Replace Regex.Unescape with a dedicated unescape step that keeps the previous decoding for everything it accepted (escaped spaces, quotes, backslashes, control characters, \xHH, \uHHHH and octal escapes) while leaving regex escape sequences untouched so they are interpreted by the regex engine. Fixes dotnet#18555 Signed-off-by: Sergio Almeida <sergioalmeida29.05@gmail.com>
|
Thanks for your PR, @SergioAlmeida29. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
|
@dotnet-policy-service agree |
|
/review |
There was a problem hiding this comment.
Review of frozen head fc35db986d89fb8de0bba8d2244ac74d369b0f37 found one compatibility regression in the replacement for Regex.Unescape: previously decoded alphabetic escapes such as \e and \cA now change non-regex replacement targets to /e and /cA. Targeted execution through the tokenizer and middleware confirmed the before/after behavior while showing regex-role behavior remains unchanged.
Panel accounting: expected=14, launched=14, returned=14, retried=0, fallback=0; path: subagent-per-topic (n=14). All 14 topics from dotnet/aspnetcore/docs/CrossCuttingGuidance.md@c6971b1955cd7396de752bf4b6556d802b0aaa4f were covered using default target-base guidance and its delegated policy inputs. Middleware has no included specialist guide, so this review provides cross-cutting coverage only.
The tokenizer and middleware tests are at the correct ownership layers and directly observe the intended shorthand behavior, but they do not cover this legacy replacement compatibility boundary. Existing feedback contained no duplicate finding, and the live head matched the reviewed SHA before publication.
Generated by ASP.NET Core Pull Request Review for #68788 · copilot · gpt56
Comment /review to run again
| { | ||
| // Preserve regex shorthand escapes and backreferences so they are | ||
| // interpreted by the regex engine rather than unescaped here. | ||
| builder.Append(Escape).Append(escaped); |
There was a problem hiding this comment.
\e and \cA change behavior in non-regex replacement tokens. The old Regex.Unescape path decoded them to U+001B and U+0001, while this code preserves them through tokenization and the replacement pipeline produces /e and /cA. Although that aligns with Apache's token handling, it changes longstanding ASP.NET Core behavior. Since retaining these legacy escapes does not conflict with preserving regex shorthand escapes, please preserve the existing replacement behavior or explicitly account for the compatibility change, with replacement-path regression coverage.
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
| .Configure(app => | ||
| { | ||
| app.UseRewriter(options); | ||
| app.Run(context => context.Response.WriteAsync(context.Request.Path)); |
There was a problem hiding this comment.
The three Invoke_PreservesLegacyReplacementEscapes failures already shown in the CI console log come from this observation boundary: passing Request.Path to WriteAsync(string) invokes PathString.ToString() / ToUriComponent(). The response is /%1B for \e and /%01 for \cA and \ca, while line 246 expects the raw control characters.
Could we write the raw value here so the existing assertion checks the decoded replacement rather than its URI-formatted representation? Previously retained execution at this exact commit reproduced each failing row, and a real TestHost consumer probe confirmed the expected raw value for all three; the replacement decoding itself is correct.
| app.Run(context => context.Response.WriteAsync(context.Request.Path)); | |
| app.Run(context => context.Response.WriteAsync(context.Request.Path.Value!)); |
Summary of the changes (Less than 80 chars)
Stop unescaping regex shorthand sequences like
\din ApacheModRewrite tokensDescription
Tokenizer.RemoveQuotesAndEscapeCharactersran every token throughRegex.Unescape, which throwsArgumentExceptionfor valid regex escapes such as\d,\wor\sand silently corrupted\binto a backspace character. Replaced it with an unescape step that preserves the previous decoding for everything it accepted (escaped spaces/quotes/backslashes, control characters,\xHH,\uHHHH, octal escapes,\e, and\cXcontrol escapes) while passing regex escape sequences through to the regex engine unchanged. All previously-accepted inputs decode identically.Fixes #18555