Skip to content

Conversation

@simonw
Copy link
Owner

@simonw simonw commented Dec 27, 2025

Fixes #13 - Regex operations now respect the Context's time_limit.

Changes:

  • Pass poll_callback to JSRegExp in BUILD_REGEX opcode
  • Pass poll_callback to JSRegExp in RegExp constructor
  • Convert RegexTimeoutError to TimeLimitError in regex methods
  • Use time.monotonic() instead of time.time() for reliable timeout checks
  • Add parameterized tests for regex literal and constructor timeout
  • Add docs/regex.md with Python API documentation for direct regex usage

https://gistpreview.github.io/?6f81b908faf7ca6e5e0fd901cf49599d/index.html

claude and others added 2 commits December 27, 2025 16:22
Fixes #13 - Regex operations now respect the Context's time_limit.

Changes:
- Pass poll_callback to JSRegExp in BUILD_REGEX opcode
- Pass poll_callback to JSRegExp in RegExp constructor
- Convert RegexTimeoutError to TimeLimitError in regex methods
- Use time.monotonic() instead of time.time() for reliable timeout checks
- Add parameterized tests for regex literal and constructor timeout
- Add docs/regex.md with Python API documentation for direct regex usage
@simonw
Copy link
Owner Author

simonw commented Dec 27, 2025

Here's the new test:

@pytest.mark.parametrize(
"regex_code",
[
# Regex literal
"var re = /(a+)+b/;",
# RegExp constructor
'var re = new RegExp("(a+)+b");',
],
ids=["literal", "constructor"],
)
def test_regex_respects_time_limit(self, regex_code):
"""Test that regex operations respect Context time_limit.
This uses a pattern known to cause catastrophic backtracking:
(a+)+b matching against 'aaa...c' causes exponential backtracking.
"""
from microjs import TimeLimitError
ctx = Context(time_limit=0.1)
with pytest.raises(TimeLimitError):
ctx.eval(
f"""
{regex_code}
re.test("aaaaaaaaaaaaaaaaaaaaaaaaaaac");
"""
)

@simonw simonw merged commit b320fb4 into main Dec 27, 2025
10 checks passed
@simonw simonw deleted the claude/add-arrow-functions-gNwf8 branch December 27, 2025 16:29
@simonw simonw added the sandbox Sandboxing to safely run untrusted code label Dec 27, 2025
simonw added a commit that referenced this pull request Dec 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sandbox Sandboxing to safely run untrusted code

Projects

None yet

3 participants