Skip to content

Lightpanda: the headless browser designed for AI and automation

perf: apply backtrack limit on user-supplied regex

Karl Seguin2333e17f7e21
diff --git a/src/browser/js/RegExp.zig b/src/browser/js/RegExp.zig
index 010cf0277..7066ea16c 100644
--- a/src/browser/js/RegExp.zig
+++ b/src/browser/js/RegExp.zig
@@ -39,10 +39,18 @@ pub const Flag = struct {
pub const unicode_sets: c_int = v8.kRegExpUnicodeSets;
};
-pub fn init(local: *const js.Local, pattern: []const u8, flags: c_int) !RegExp {
+pub const InitOpts = struct {
+ flags: c_int = Flag.none,
+ backtrack_limit: ?u32 = 1_000_000,
+};
+
+pub fn init(local: *const js.Local, pattern: []const u8, opts: InitOpts) !RegExp {
const pattern_handle = local.isolate.initStringHandle(pattern);
- const handle = v8.v8__RegExp__New(local.handle, pattern_handle, flags) orelse return error.JsException;
- return .{ .local = local, .handle = handle };
+ const handle = if (opts.backtrack_limit) |limit|
+ v8.v8__RegExp__NewWithBacktrackLimit(local.handle, pattern_handle, opts.flags, limit)
+ else
+ v8.v8__RegExp__New(local.handle, pattern_handle, opts.flags);
+ return .{ .local = local, .handle = handle orelse return error.JsException };
}
// Runs the pattern against `subject`. Returns the result Array (as a generic
diff --git a/src/browser/tests/element/html/input.html b/src/browser/tests/element/html/input.html
index b3b70dc95..4d8d76bd8 100644
--- a/src/browser/tests/element/html/input.html
+++ b/src/browser/tests/element/html/input.html
@@ -619,3 +619,16 @@
testing.expectEqual(0, date_selects);
}
</script>
+
+<script id="pattern_catastrophic_backtracking">
+ {
+ // A nested quantifier backtracks exponentially on a value that almost
+ // matches. Chrome bounds the match and returns at once, treating a
+ // pattern that gives up as not matching.
+ const input = document.createElement('input');
+ input.pattern = '(a+)+';
+ input.value = 'a'.repeat(40) + 'b';
+ testing.expectEqual(true, input.validity.patternMismatch);
+ testing.expectEqual(false, input.checkValidity());
+ }
+</script>
diff --git a/src/browser/tests/urlpattern.html b/src/browser/tests/urlpattern.html
index 9640368cd..0befed59b 100644
--- a/src/browser/tests/urlpattern.html
+++ b/src/browser/tests/urlpattern.html
@@ -261,3 +261,15 @@
testing.expectError('TypeError', () => new URLPattern({ pathname: '/*' }).generate('pathname', {}));
testing.expectError('TypeError', () => new URLPattern({ pathname: '/foo' }).generate('invalid', {}));
</script>
+
+<script id=catastrophic_backtracking>
+ {
+ // A regexp group that backtracks exponentially on a near miss. Chrome
+ // bounds the match and returns at once, treating a pattern that gives up
+ // as not matching, even where the second alternative would match.
+ const pattern = new URLPattern({ pathname: '/((?:a+)+b|a+c)' });
+ testing.expectEqual(true, pattern.test({ pathname: '/' + 'a'.repeat(10) + 'c' }));
+ testing.expectEqual(false, pattern.test({ pathname: '/' + 'a'.repeat(40) + 'c' }));
+ testing.expectEqual(null, pattern.exec({ pathname: '/' + 'a'.repeat(40) + 'c' }));
+ }
+</script>
diff --git a/src/browser/webapi/URLPattern.zig b/src/browser/webapi/URLPattern.zig
index 7c2fc1b11..622d42cc9 100644
--- a/src/browser/webapi/URLPattern.zig
+++ b/src/browser/webapi/URLPattern.zig
@@ -1165,7 +1165,7 @@ const Env = struct {
// needs the Unicode ID_Start / ID_Continue tables. V8 has them; a RegExp
// over the remaining input tells us where the identifier ends.
fn nameLenUnicode(self: Env, s: []const u8) Error!usize {
- const re = js.RegExp.init(self.local, "^[\\p{ID_Start}$_][\\p{ID_Continue}$\\u200C\\u200D]*", js.RegExp.Flag.unicode) catch return error.JsException;
+ const re = js.RegExp.init(self.local, "^[\\p{ID_Start}$_][\\p{ID_Continue}$\\u200C\\u200D]*", .{ .flags = js.RegExp.Flag.unicode, .backtrack_limit = null }) catch return error.JsException;
const result = (re.exec(s) catch return error.JsException) orelse return 0;
const matched = result.get("0") catch return error.JsException;
const str = matched.isString() orelse return 0;
@@ -1186,7 +1186,7 @@ const Env = struct {
return false;
}
const source = if (first) "^[\\p{ID_Start}$_]" else "^[\\p{ID_Continue}$\\u200C\\u200D]";
- const re = js.RegExp.init(self.local, source, js.RegExp.Flag.unicode) catch return error.JsException;
+ const re = js.RegExp.init(self.local, source, .{ .flags = js.RegExp.Flag.unicode, .backtrack_limit = null }) catch return error.JsException;
return re.match(s[0..cp_len]) catch return error.JsException;
}
};
@@ -1810,7 +1810,7 @@ const Component = struct {
if (self.options.ignore_case) {
flags |= js.RegExp.Flag.ignore_case;
}
- return js.RegExp.init(local, self.regexp_source, flags);
+ return js.RegExp.init(local, self.regexp_source, .{ .flags = flags });
}
/// Runs the component against `input`; null when it doesn't match.
diff --git a/src/browser/webapi/element/html/Input.zig b/src/browser/webapi/element/html/Input.zig
index bdde7395a..54f75eb4c 100644
--- a/src/browser/webapi/element/html/Input.zig
+++ b/src/browser/webapi/element/html/Input.zig
@@ -470,7 +470,7 @@ pub fn suffersPatternMismatch(self: *const Input, frame: *Frame) bool {
defer try_catch.deinit();
const wrapped = frame.call_arena.print("^(?:{s})$", .{pattern}) catch return false;
- const re = js.RegExp.init(&ls.local, wrapped, js.RegExp.Flag.unicode_sets) catch return false;
+ const re = js.RegExp.init(&ls.local, wrapped, .{ .flags = js.RegExp.Flag.unicode_sets }) catch return false;
const matched = re.match(value) catch return false;
return !matched;