dotnet/aspnetcore

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

Open

#18.555 aberto em 24 de jan. de 2020

Ver no GitHub
 (3 comments) (0 reactions) (0 assignees)C# (10.653 forks)batch import
area-middlewaregood first issuehelp wantedinvestigate

Métricas do repositório

Stars
 (37.933 stars)
Métricas de merge de PR
 (Mesclagem média 16d 9h) (258 fundiu PRs em 30d)

Description

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.

Guia do colaborador