소스 검색

Don't compile Regex instances containing negative lookahead (#1761)

Marko Lahma 1 년 전
부모
커밋
1ddfad3515
2개의 변경된 파일15개의 추가작업 그리고 1개의 파일을 삭제
  1. 11 0
      Jint.Tests/Runtime/EngineTests.ScriptPreparation.cs
  2. 4 1
      Jint/Engine.Ast.cs

+ 11 - 0
Jint.Tests/Runtime/EngineTests.ScriptPreparation.cs

@@ -65,4 +65,15 @@ public partial class EngineTests
         var result = builtStatement.Execute(new EvaluationContext(_engine)).Value;
         Assert.Equal(JsBoolean.False, result);
     }
+
+    [Fact]
+    public void CompiledRegexShouldProduceSameResultAsNonCompiled()
+    {
+        const string Script = """JSON.stringify(/(.*?)a(?!(a+)b\2c)\2(.*)/.exec("baaabaac"))""";
+
+        var nonCompiled = _engine.Evaluate(Script);
+        var compiled = _engine.Evaluate(Engine.PrepareScript(Script));
+
+        Assert.Equal(nonCompiled, compiled);
+    }
 }

+ 4 - 1
Jint/Engine.Ast.cs

@@ -92,7 +92,10 @@ public partial class Engine
                     {
                         var regExpLiteral = (RegExpLiteral) literal;
                         var regExpParseResult = regExpLiteral.ParseResult;
-                        if (regExpParseResult.Success)
+
+                        // only compile if there's no negative lookahead, it works incorrectly under NET 7 and NET 8
+                        // https://github.com/dotnet/runtime/issues/97455
+                        if (regExpParseResult.Success && !regExpLiteral.Raw.Contains("(?!"))
                         {
                             if (!_regexes.TryGetValue(regExpLiteral.Raw, out var regex))
                             {