Skip to content

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

@xzxzxc

Description

@xzxzxc

Description of the bug

The System.ArgumentException occurs while executing the method AddApacheModRewrite if passed file contains regex rules with shorthand character classes (like \d).

To Reproduce

public static class ApplicationExtensions
{
	public static IApplicationBuilder UseUrlRewriter(this IApplicationBuilder builder)
	{
		using (var fileReader = File.OpenText("apache_rewrite_rules.txt"))
		{
			var rewriteOptions = new RewriteOptions()
				.AddApacheModRewrite(fileReader);

			return builder.UseRewriter(rewriteOptions);
		}
	}
}

Where file apache_rewrite_rules.txt contains:
RewriteRule ^/(\d)$ /?num=$1

Exception message:
System.ArgumentException: 'parsing '^/(\d)$' - Unrecognized escape sequence \\d.'
Stack trace:

   at System.Text.RegularExpressions.RegexParser.ScanCharEscape()
   at System.Text.RegularExpressions.RegexParser.Unescape(String input)
   at System.Text.RegularExpressions.Regex.Unescape(String str)
   at Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite.Tokenizer.RemoveQuotesAndEscapeCharacters(IList`1 tokens)
   at Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite.Tokenizer.Tokenize(String rule)
   at Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite.FileParser.Parse(TextReader input)
   at Microsoft.AspNetCore.Rewrite.ApacheModRewriteOptionsExtensions.AddApacheModRewrite(RewriteOptions options, TextReader reader)
   at Middleware.ApplicationExtensions.UseUrlRewriter(IApplicationBuilder builder, Action`1 configure) in D:\workspace\url-rewriter\UrlRewriter.Middleware\ApplicationExtensions.cs:line 17
   at UrlRewriter.Tests.ApacheModRewriteTests.<>c.<.ctor>b__1_0(IApplicationBuilder app) in D:\workspace\url-rewriter\UrlRewriter.Tests\ApacheModRewriteTests.cs:line 22
   at Microsoft.AspNetCore.Hosting.DelegateStartup.Configure(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

Comments

RewriteRule statement implies to use regular expressions, so it's not clear why there is a call of Regex.Unescape in ApacheModRewrite.Tokenizer.RemoveQuotesAndEscapeCharacters, seems like it's a bug considering that Regex.Unescape is unable to convert sequences such as \w, \d or \s, it throws an ArgumentException.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewaresgood first issueGood for newcomers.help wantedUp for grabs. We would accept a PR to help resolve this issueinvestigate

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions