Skip to content

Fix ArgumentException for regex shorthand classes in Apache mod_rewrite tokenizer#66354

Open
slymanmrcan wants to merge 2 commits intodotnet:mainfrom
slymanmrcan:fix/18555-apache-modrewrite-regex-shorthands
Open

Fix ArgumentException for regex shorthand classes in Apache mod_rewrite tokenizer#66354
slymanmrcan wants to merge 2 commits intodotnet:mainfrom
slymanmrcan:fix/18555-apache-modrewrite-regex-shorthands

Conversation

@slymanmrcan
Copy link
Copy Markdown

@slymanmrcan slymanmrcan commented Apr 17, 2026

Fixes #18555

Problem

RewriteOptions.AddApacheModRewrite() throws ArgumentException when
rewrite rules contain regex shorthands like \d, \w, \s.
RewriteRule ^/(\d) /?num=\1
// → System.ArgumentException: Unrecognized escape sequence \d

Cause

Tokenizer.RemoveQuotesAndEscapeCharacters() calls Regex.Unescape()
which doesn't recognize regex shorthand classes.

Fix

Replaced Regex.Unescape() with a custom method that only unescapes
Apache-specific sequences (spaces, quotes, backslashes) and leaves
regex patterns untouched.

Tests

  • \d, \w, \s no longer throw
  • Escaped spaces still work
  • Mixed regex + escaped space works
  • Regex inside quotes works
  • All existing tests pass

…te tokenizer

Replace Regex.Unescape() with custom UnescapeApacheString() that only
handles Apache-specific escape sequences (quotes, spaces, backslashes)
while preserving regex patterns like \d, \w, \s intact.

Fixes dotnet#18555
@github-actions github-actions Bot added the area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares label Apr 17, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Apr 17, 2026
@slymanmrcan
Copy link
Copy Markdown
Author

@dotnet-policy-service agree

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

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)

1 participant