Safety wrapper for user-supplied regex compilation and matching.
ReDoS is mitigated by two complementary defences:
A cheap pre-screen (safeCompile) rejects null/empty/oversized patterns and a set of well-known catastrophic-backtracking shapes before they are ever compiled.
A hard execution bound (safeFind) caps the number of character accesses the backtracking engine may perform for a single match, without relying on thread interruption or wall-clock timeouts.
The step bound is the real security boundary, and safeCompile's structural check is what makes it sound (#3051): the bound counts reads, and the engine could otherwise do unbounded work without reading, in many required iterations of a loop whose body matches the empty string ((){2000000000}b) or across many ways of matching the empty string at the end of the input ((?:|)(?:|)...b). With every pattern refused that can take more than 1000 steps at one position without a read, the work between two reads is bounded. The shape blocklist is only a fast early-reject and must not be relied on for safety.
NOTE: kept intentionally in sync with org.llm4s.runner.WorkspaceRegexSafetyManager, which duplicates this logic because the workspace runner module cannot depend on core (and so returns Either[String, _], not Result). Changes to the heuristics or bounds here should be mirrored there.
Compiles text as a literal pattern: it is quoted with Pattern.quote, so regex metacharacters in it match themselves.
Compiles text as a literal pattern: it is quoted with Pattern.quote, so regex metacharacters in it match themselves.
It returns the Pattern directly and does not apply the checks of safeCompile. A quoted pattern has no quantifiers or alternation, so the shapes those checks look for cannot occur in it.
Compiles a user-supplied pattern after the pre-screen, without throwing.
Compiles a user-supplied pattern after the pre-screen, without throwing.
Returns a Left when the pattern is null, empty or blank, is longer than 1000 characters, matches one of the known catastrophic-backtracking shapes (nested quantifiers such as (a+)+, or a quantified alternation whose alternatives overlap, such as (a|ab)*), or is not valid regex syntax. A quantified alternation whose alternatives do not overlap, such as (foo|bar)+, compiles. The pre-screen is only an early reject: bound matching with safeFind.
It also returns a Left for a pattern whose matching the step bound of safeFind could not bound (#3051): one that can take more than 1000 steps at one position without reading the input, such as many required iterations of something that can match the empty string ((){5000}) or many ways in a row to match it ((?:|), (\s*)? or (?:a?)* repeated); a lookbehind longer than 64 characters, unbounded, or holding another lookaround, which the engine retries from each earlier position without reading; and comments mode or canonical equivalence ((?x), Pattern.COMMENTS, Pattern.CANON_EQ), whose syntax the check does not follow. One empty-matching part, such as (\s*)?, (a|)? or (a?)*, or a few in a row, compiles.
Value parameters
flags
java.util.regex.Pattern flags, such as Pattern.CASE_INSENSITIVE; 0 by default
pattern
the regular expression to compile
Attributes
Returns
the compiled Pattern, or a org.llm4s.error.ValidationError for the field pattern whose message holds the reason it was rejected
Tells whether the pattern is found anywhere in the input (Matcher.find), within a budget of 10000000 character accesses.
Tells whether the pattern is found anywhere in the input (Matcher.find), within a budget of 10000000 character accesses.
Returns a Left when the input is null or longer than 100000 characters, when matching exceeds the budget (likely catastrophic backtracking), when the pattern recurses too deeply for the input, or when matching fails for another non-fatal reason.
Right(true) if a match is found, Right(false) if not, or a org.llm4s.error.ValidationError whose message holds the reason matching stopped: for the field input when the input was refused, for pattern when the match was