Skip to content

Commit

Permalink
ensure parser has default regexp timeout, cleanup instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Jul 29, 2023
1 parent 37988ad commit 260e2c6
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 26 deletions.
15 changes: 10 additions & 5 deletions Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ namespace Jint
/// </summary>
public sealed partial class Engine : IDisposable
{
private static readonly ParserOptions _defaultParserOptions = ParserOptions.Default with
{
AllowReturnOutsideFunction = true
};
private readonly JavaScriptParser _defaultParser = new(_defaultParserOptions);
private readonly ParserOptions _defaultParserOptions;
private readonly JavaScriptParser _defaultParser;

internal readonly ExecutionContextStack _executionContexts;
private JsValue _completionValue = JsValue.Undefined;
Expand Down Expand Up @@ -131,6 +128,14 @@ public Engine(Action<Engine, Options> options)

CallStack = new JintCallStack(Options.Constraints.MaxRecursionDepth >= 0);
_stackGuard = new StackGuard(this);

_defaultParserOptions = ParserOptions.Default with
{
AllowReturnOutsideFunction = true,
RegexTimeout = Options.Constraints.RegexTimeout
};

_defaultParser = new JavaScriptParser(_defaultParserOptions);
}

private void Reset()
Expand Down
5 changes: 4 additions & 1 deletion Jint/ModuleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ internal ModuleBuilder(Engine engine, string specifier)
{
_engine = engine;
_specifier = specifier;
_options = new ParserOptions();
_options = new ParserOptions
{
RegexTimeout = engine.Options.Constraints.RegexTimeout
};
}

public ModuleBuilder AddSource(string code)
Expand Down
6 changes: 5 additions & 1 deletion Jint/Native/Function/FunctionInstance.Dynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ internal FunctionInstance CreateDynamicFunction(
}
}

JavaScriptParser parser = new(new ParserOptions { Tolerant = false });
JavaScriptParser parser = new(new ParserOptions
{
Tolerant = false,
RegexTimeout = _engine.Options.Constraints.RegexTimeout
});
function = (IFunction) parser.ParseScript(functionExpression, source: null, _engine._isStrict).Body[0];
}
catch (ParserException ex)
Expand Down
17 changes: 3 additions & 14 deletions Jint/Native/RegExp/RegExpConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,10 @@ private JsRegExp RegExpAlloc(JsValue newTarget)

public JsRegExp Construct(Regex regExp, string source, string flags)
{
var r = new JsRegExp(Engine);
r._prototype = PrototypeObject;

r.Flags = flags;
var r = RegExpAlloc(this);
r.Value = regExp;
r.Source = source;

var timeout = _engine.Options.Constraints.RegexTimeout;
if (timeout.Ticks > 0)
{
r.Value = new Regex(regExp.ToString(), regExp.Options, timeout);
}
else
{
r.Value = regExp;
}
r.Flags = flags;

RegExpInitialize(r);

Expand Down
7 changes: 6 additions & 1 deletion Jint/Native/ShadowRealm/ShadowRealm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ namespace Jint.Native.ShadowRealm;
/// </summary>
public sealed class ShadowRealm : ObjectInstance
{
private readonly JavaScriptParser _parser = new(new ParserOptions { Tolerant = false });
private readonly JavaScriptParser _parser;
internal readonly Realm _shadowRealm;
private readonly ExecutionContext _executionContext;

internal ShadowRealm(Engine engine, ExecutionContext executionContext, Realm shadowRealm) : base(engine)
{
_parser = new(new ParserOptions
{
Tolerant = false,
RegexTimeout = engine.Options.Constraints.RegexTimeout
});
_executionContext = executionContext;
_shadowRealm = shadowRealm;
}
Expand Down
8 changes: 4 additions & 4 deletions Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ private JsValue ResolveValue(EvaluationContext context)
if (expression.TokenType == TokenType.RegularExpression)
{
var regExpLiteral = (RegExpLiteral) _expression;
if (regExpLiteral.Value is not System.Text.RegularExpressions.Regex regex)
if (regExpLiteral.Value is System.Text.RegularExpressions.Regex regex)
{
ExceptionHelper.ThrowSyntaxError(context.Engine.Realm, $"Unsupported regular expression: '{regExpLiteral.Regex.Pattern}/{regExpLiteral.Regex.Flags}'");
return JsValue.Undefined;
return context.Engine.Realm.Intrinsics.RegExp.Construct(regex, regExpLiteral.Regex.Pattern, regExpLiteral.Regex.Flags);
}
return context.Engine.Realm.Intrinsics.RegExp.Construct(regex, regExpLiteral.Regex.Pattern, regExpLiteral.Regex.Flags);

ExceptionHelper.ThrowSyntaxError(context.Engine.Realm, $"Unsupported regular expression: '{regExpLiteral.Regex.Pattern}/{regExpLiteral.Regex.Flags}'");
}

return JsValue.FromObject(context.Engine, expression.Value);
Expand Down

0 comments on commit 260e2c6

Please sign in to comment.