Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

feat(v8): expose per-regexp backtrack limits

ldm0a0617d37b5c6
diff --git a/moli-v8-test-util/tests/regexp_bindings.rs b/moli-v8-test-util/tests/regexp_bindings.rs
new file mode 100644
index 000000000..1cda4478f
--- /dev/null
+++ b/moli-v8-test-util/tests/regexp_bindings.rs
@@ -0,0 +1,67 @@
+use std::pin::pin;
+
+#[test]
+fn backtrack_limits_bound_rejections_and_preserve_normal_matches() {
+ moli_v8_test_util::ensure_v8();
+ let mut isolate = v8::Isolate::new(v8::CreateParams::default());
+ let scope = pin!(v8::HandleScope::new(&mut isolate));
+ let scope = &mut scope.init();
+ let context = v8::Context::new(scope, Default::default());
+ let scope = &mut v8::ContextScope::new(scope, context);
+ let source = v8::String::new(scope, "^(a+)+$").unwrap();
+ let matching = v8::String::new(scope, &"a".repeat(64)).unwrap();
+ let rejecting = v8::String::new(scope, &format!("{}!", "a".repeat(64))).unwrap();
+ for limit in [1, 1_000, 1_000_000] {
+ let regexp = v8::RegExp::new_with_backtrack_limit(
+ scope,
+ source,
+ v8::RegExpCreationFlags::UNICODE_SETS,
+ limit,
+ )
+ .unwrap();
+ // Without the explicit limit, this rejection requires exponential
+ // backtracking. Do not assume an exact count for an optimizable match.
+ assert!(regexp.exec(scope, rejecting).unwrap().is_null());
+ if limit == 1_000_000 {
+ assert!(!regexp.exec(scope, matching).unwrap().is_null());
+ }
+ }
+ let unbounded = v8::RegExp::new(scope, source, v8::RegExpCreationFlags::UNICODE_SETS).unwrap();
+ assert!(!unbounded.exec(scope, matching).unwrap().is_null());
+}
+
+#[test]
+fn invalid_backtrack_limits_are_rejected_without_a_process_abort() {
+ moli_v8_test_util::ensure_v8();
+ let mut isolate = v8::Isolate::new(v8::CreateParams::default());
+ let scope = pin!(v8::HandleScope::new(&mut isolate));
+ let scope = &mut scope.init();
+ let context = v8::Context::new(scope, Default::default());
+ let scope = &mut v8::ContextScope::new(scope, context);
+ let try_catch = pin!(v8::TryCatch::new(scope));
+ let scope = &mut try_catch.init();
+ let source = v8::String::new(scope, "a").unwrap();
+ for limit in [0, u32::MAX] {
+ assert!(
+ v8::RegExp::new_with_backtrack_limit(
+ scope,
+ source,
+ v8::RegExpCreationFlags::empty(),
+ limit,
+ )
+ .is_none()
+ );
+ assert!(!scope.has_caught());
+ }
+ let invalid_source = v8::String::new(scope, "[").unwrap();
+ assert!(
+ v8::RegExp::new_with_backtrack_limit(
+ scope,
+ invalid_source,
+ v8::RegExpCreationFlags::UNICODE_SETS,
+ 1_000,
+ )
+ .is_none()
+ );
+ assert!(scope.has_caught());
+}
diff --git a/vendor/v8-152.2.0/MOLI_VENDORING.md b/vendor/v8-152.2.0/MOLI_VENDORING.md
index 859f16d78..8aa995e16 100644
--- a/vendor/v8-152.2.0/MOLI_VENDORING.md
+++ b/vendor/v8-152.2.0/MOLI_VENDORING.md
@@ -66,3 +66,13 @@ Implications:
`serde_v8 0.320.0` depends on `deno_v8 0.3.0`, whose published manifest still
selects V8 150. The adjacent slim `vendor/deno_v8-0.3.0` patch changes only that
dependency constraint to V8 152 so serde_v8 and Moli share one V8 type universe.
+
+
+## Bounded regular expressions
+
+Moli adds `RegExp::new_with_backtrack_limit` and the `regexp_ext.cc` bridge to
+V8's fallible `RegExp::NewWithBacktrackLimit` API. Zero and out-of-Smi-range
+limits return `None` before entering V8; invalid syntax retains the pending
+exception. `moli-v8-test-util/tests/regexp_bindings.rs` covers bounded rejection,
+normal matches, invalid limits and invalid syntax. Keep this local bridge when
+refreshing the vendored bindings.
diff --git a/vendor/v8-152.2.0/src/regexp.rs b/vendor/v8-152.2.0/src/regexp.rs
index f0f29ee10..b87dc1e73 100644
--- a/vendor/v8-152.2.0/src/regexp.rs
+++ b/vendor/v8-152.2.0/src/regexp.rs
@@ -28,6 +28,12 @@ unsafe extern "C" {
pattern: *const String,
flags: RegExpCreationFlags,
) -> *const RegExp;
+ fn v8__RegExp__NewWithBacktrackLimit(
+ context: *const Context,
+ pattern: *const String,
+ flags: RegExpCreationFlags,
+ backtrack_limit: u32,
+ ) -> *const RegExp;
fn v8__RegExp__Exec(
this: *const RegExp,
context: *const Context,
@@ -50,6 +56,29 @@ impl RegExp {
}
}
+ /// Creates a regular expression with a per-execution backtrack limit.
+ /// Reaching the limit returns a match failure rather than an exception.
+ /// Returns `None` if the limit is zero or does not fit a V8 Smi. Invalid
+ /// regular expression syntax returns `None` with a pending exception.
+ #[inline(always)]
+ pub fn new_with_backtrack_limit<'s>(
+ scope: &PinScope<'s, '_>,
+ pattern: Local<String>,
+ flags: RegExpCreationFlags,
+ backtrack_limit: u32,
+ ) -> Option<Local<'s, RegExp>> {
+ unsafe {
+ scope.cast_local(|sd| {
+ v8__RegExp__NewWithBacktrackLimit(
+ sd.get_current_context(),
+ &*pattern,
+ flags,
+ backtrack_limit,
+ )
+ })
+ }
+ }
+
#[inline(always)]
pub fn exec<'s>(
&self,
diff --git a/vendor/v8-152.2.0/src/regexp_ext.cc b/vendor/v8-152.2.0/src/regexp_ext.cc
new file mode 100644
index 000000000..f53184491
--- /dev/null
+++ b/vendor/v8-152.2.0/src/regexp_ext.cc
@@ -0,0 +1,25 @@
+// Copyright 2026 the Moli authors. MIT license.
+
+#include "support.h"
+#include "v8-regexp.h"
+
+using namespace support;
+
+extern "C" {
+
+const v8::RegExp* v8__RegExp__NewWithBacktrackLimit(
+ const v8::Context& context,
+ const v8::String& pattern,
+ v8::RegExp::Flags flags,
+ uint32_t backtrack_limit) {
+ // The V8 API aborts on invalid limits. Keep Rust's fallible constructor
+ // fallible for these values as well, including pointer-compressed builds.
+ if (backtrack_limit == 0 ||
+ backtrack_limit > static_cast<uint32_t>(v8::internal::kSmiMaxValue)) {
+ return nullptr;
+ }
+ return maybe_local_to_ptr(v8::RegExp::NewWithBacktrackLimit(
+ ptr_to_local(&context), ptr_to_local(&pattern), flags, backtrack_limit));
+}
+
+} // extern "C"