Skip to content

Fix ApacheModRewrite tokenizer throwing on regex shorthand escapes - #68788

Open
SergioAlmeida29 wants to merge 4 commits into
dotnet:mainfrom
SergioAlmeida29:fix/18555-apache-modrewrite-regex-escapes
Open

SergioAlmeida29 wants to merge 4 commits into
dotnet:mainfrom
SergioAlmeida29:fix/18555-apache-modrewrite-regex-escapes

Conversation

@SergioAlmeida29

@SergioAlmeida29 SergioAlmeida29 commented Aug 25, 2026 •

Copy link
Copy Markdown
  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Summary of the changes (Less than 80 chars)

Stop unescaping regex shorthand sequences like \d in ApacheModRewrite tokens

Description

Tokenizer.RemoveQuotesAndEscapeCharacters ran every token through Regex.Unescape, which throws ArgumentException for valid regex escapes such as \d, \w or \s and silently corrupted \b into 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 \cX control escapes) while passing regex escape sequences through to the regex engine unchanged. All previously-accepted inputs decode identically.

Fixes #18555

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>
Copilot AI lite review requested due to automatic review settings August 25, 2026 23:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Aug 25, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @SergioAlmeida29. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@SergioAlmeida29

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

@Youssef1313 Youssef1313 added the area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares label Sep 2, 2026
@PureWeen

Copy link
Copy Markdown
Member

/review

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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);

@github-actions github-actions Bot Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

\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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Oct 1, 2026
.Configure(app =>
{
app.UseRewriter(options);
app.Run(context => context.Response.WriteAsync(context.Request.Path));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Suggested change
app.Run(context => context.Response.WriteAsync(context.Request.Path));
app.Run(context => context.Response.WriteAsync(context.Request.Path.Value!));

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

area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ArgumentException "unrecognized escape sequence" in RewriteOptions.AddApacheModRewrite in case of usage of regex shorthand character classes (like \d)

4 participants