@ifc-lite/regex-guard
v0.3.0
Published
A shared, dependency-free guard against catastrophic-backtracking (ReDoS) regex patterns compiled from untrusted input
Readme
@ifc-lite/regex-guard
Dependency-free validation for regular-expression patterns supplied by users or model files. Shared by IFC pattern-validation and editing tools.
Usage
import { compileGuardedRegex } from '@ifc-lite/regex-guard';
const pattern = compileGuardedRegex('^Wall-[0-9]+$', 'i');
console.log(pattern.test('Wall-42')); // truecompileGuardedRegex(pattern, flags?, { maxLength? }?) validates the pattern
before constructing a JavaScript RegExp. Rejected patterns throw
UnsafeRegexPatternError, which exposes pattern and reason. A malformed
pattern that passes the guard can still throw the native SyntaxError.
Other exports:
compileNameMatcher(pattern)/isNamePattern(pattern): cached exact-or-/regex/flagsname matching shared by Lists and Rules; guarded patterns retain the Lists API behavior.unsafeNamePatternReason(pattern): inspect a name-pattern rejection without compiling it.assertGuardedRegexPattern(pattern, { maxLength? }?): validate without compiling.hasCatastrophicBacktrackingShape(pattern): detect the nested-quantifier shapes covered by the heuristic.MAX_GUARDED_REGEX_PATTERN_LENGTH: the default limit of 256 characters.
Limits
This is a heuristic, not a guarantee of bounded execution time. It rejects
known nested-quantifier patterns such as (a+)+ and caps pattern length, but
it does not analyze every possible backtracking shape. It does not move matches
to a worker, enforce a match timeout, or replace JavaScript's regex engine.
License
MPL-2.0.
