ArgumentException "unrecognized escape sequence" in RewriteOptions.AddApacheModRewrite in case of usage of regex shorthand character classes (like \d)
#18.555 aperta il 24 gen 2020
Metriche repository
- Star
- (37.933 star)
- Metriche merge PR
- (Merge medio 16g 9h) (258 PR mergiate in 30 g)
Descrizione
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.