Skip to content

fix(bridge): bound regex search work - #105

Merged
morluto merged 1 commit into
mainfrom
fix/bounded-hopper-regex-search
Jul 13, 2026
Merged

morluto merged 1 commit into
mainfrom
fix/bounded-hopper-regex-search

Conversation

@N0zoM1z0

Copy link
Copy Markdown
Collaborator

Summary

Harden Hopper-backed procedure and string regex searches against bounded-but-catastrophic Python regular expressions.

  • replace the previous structural blacklist with a conservative AST work model;
  • cap static backtracking paths at 10,000;
  • cap each regex candidate at 4,096 characters;
  • cap cumulative regex work across the full search inventory at 1,000,000 units;
  • normalize Python parser overflow into a stable invalid_request response;
  • document the exact limits in the public search_procedures and search_strings tool descriptions;
  • preserve exhaustive literal matching and the existing deterministic pagination behavior.

This is an independent regex-boundedness contribution toward #29. It does not claim to close the broader issue.

Root cause

The existing validator rejected lookarounds, backreferences, nested repetitions, and unbounded repetitions, but it treated every permitted bounded repetition as safe. Patterns such as:

(a|aa){1,35}b

were therefore accepted even though Python's backtracking engine can explore an exponential number of paths. Candidate length and inventory size could multiply that cost while the bridge handled the request synchronously.

Very large repetition counts could also make sre_parse raise OverflowError; that escaped the validation boundary as bridge_exception instead of a stable caller error.

Implementation

The bridge now derives two conservative values from the parsed regex AST:

  • the number of possible backtracking paths, including branch products, adjacent optional atoms, and bounded repetition alternatives;
  • the maximum matching steps along a path, including fixed and variable repeat counts.

The matcher combines those values with each candidate length and consumes a per-search work budget before calling re.search. This bounds both a single expensive candidate and cost accumulated while scanning an inventory. Unsupported AST operations fail closed.

Boundary behavior is intentional:

Boundary Behavior
Pattern length Existing 1-256 character contract remains unchanged
Bounded repetition Up to 1,000 repetitions
Static backtracking paths Up to 10,000 paths
Regex candidate length Up to 4,096 characters
Cumulative regex work Up to 1,000,000 units per search request

Regex safety failures are projected as invalid_request with stable messages. Literal mode does not use Python regex and continues matching the complete value even when returned text must be truncated.

Test coverage

A new executable probe loads the real bridge/hopper_bridge.py without opening a Hopper socket. The focused suite covers:

  • useful bounded regexes and case-insensitive matching;
  • deterministic regex pagination;
  • lookarounds and backreferences;
  • nested, unbounded, and excessive repetitions;
  • invalid syntax and repetition-count parser overflow;
  • exact maximum bounded-repeat and work-budget acceptance;
  • near-budget acceptance and over-budget rejection;
  • ambiguous bounded repetition and adjacent optional-atom path explosion;
  • cumulative work across multiple inventory entries;
  • exact 4,096-character candidate acceptance and oversized rejection;
  • exact 256-character pattern acceptance and oversized rejection;
  • literal matching of values longer than the returned-value limit;
  • stable invalid_request diagnostic projection.

Package verification now extracts the generated npm tarball and executes the same catastrophic-pattern probe against the packed production bridge. This prevents the fake launcher used by other package smoke tests from masking a missing bridge hardening change.

Verification

  • npm run check - 58 test files and 375 tests passed; typecheck, oxlint, Prettier, build, and coverage run passed
  • npm run knip - passed
  • npm run jscpd - passed; no new duplicate block introduced
  • npm run scan:todos - passed
  • npm run verify:package - passed with 68 MCP tools and boundedRegexBridge: true
  • pre-commit lint-staged, Prettier, oxlint, and typecheck - passed

Real Hopper verification was not run because this environment does not provide a Hopper executable and two target binaries. The tests execute the production bridge source and packed bridge directly, but they do not replace the repository's real-Hopper workflow.

@N0zoM1z0
N0zoM1z0 requested a review from morluto July 13, 2026 14:23
@morluto
morluto merged commit 733a382 into main Jul 13, 2026
11 checks passed
@morluto
morluto deleted the fix/bounded-hopper-regex-search branch July 28, 2026 17:39
darsrc pushed a commit to darsrc/rea that referenced this pull request Oct 10, 2026
…search

fix(bridge): bound regex search work
@morluto morluto added bug Something isn't working area: bridge Hopper bridge (Python) related changes labels Oct 14, 2026
DrAbdulmalek pushed a commit to DrAbdulmalek/rea that referenced this pull request Oct 26, 2026
Rebase of useful morluto#105 content onto main after morluto#104.
Keep uv/ZIP security in UV-AND-DOWNLOAD-SECURITY.md; do not close morluto#51.
mvanhorn added a commit to mvanhorn/rea that referenced this pull request Nov 1, 2026
The cyclonedx analysis view projects one retained Go build-information
parent or one Rust dependency parent into one deterministic CycloneDX
1.6 JSON document from that parent alone. Go modules become pkg:golang
Package URLs; a local replacement has no Package URL and records the
replaced module as rea:go:original, and an h1 sum is the property
rea:go:sum. Rust crates.io packages become pkg:cargo Package URLs, other
sources have no Package URL, and a build-kind package has scope
excluded. Compositions are incomplete or unknown, the document has no
timestamp, a document larger than 1,048,576 bytes is refused by naming
export_evidence_bundle, and the tool text, docs, service tests, and
verify:cyclonedx:view cover the projection.

Fixes morluto#105
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: bridge Hopper bridge (Python) related changes bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants