Skip to content

security(detector): ReDoS guard on third-party regex overrides (#829) - #842

Merged
FabioLeitao merged 1 commit into
mainfrom
fix/redos-guard
Jun 11, 2026
Merged

FabioLeitao merged 1 commit into
mainfrom
fix/redos-guard

Conversation

@FabioLeitao

Copy link
Copy Markdown
Collaborator

ReDoS guard (#829)

Threat model (per operator decision on #829): ReDoS is an external-input vector. Enforcement happens at the trust boundary — _load_regex_overrides (third-party plugin/override path) — while curated built-in DEFAULT_PATTERNS are exempt. Calibration tests keep the heuristic honest: every built-in must pass the guard, so it never drifts into rejecting curated patterns again (the PHONE_BR false-positive class).

Design

  • config/plugin_validator.py — _has_nested_quantifier: char-by-char scanner, "star height > 1" criterion (same as safe-regex/eslint). Replaces a regex-over-regex approach that false-positived on PHONE_BR's escaped literals (\+55, \(, \)). Handles:
    • escaped metacharacters → literals
    • quantifier chars inside character classes ([+*]) → literals
    • bounded ? and {2,5} never trigger; unbounded *, +, {2,} do
    • unbounded content propagates to enclosing groups (((a+)b)+ flagged, conservative by design)
  • core/detector.py — _load_regex_overrides: per-item skip with PluginValidationWarning for nested-quantifier patterns and re.error invalid regex. Skip is per-item — a good pattern in the same plugin file still loads; the scan never crashes.
  • __init__ compile loop unchanged — built-ins trusted, overrides pre-filtered.

Tests (8, all passing)

  • Dangerous shapes flagged: (a+)+, (\w*)*, ([a-z]+)*, (x{2,})+, ((a+)b)+
  • Safe shapes allowed: CPF, e-mail, PHONE_BR (escaped \+/\(), (a?)+, ([+*]\d)+, (a{2,5})+
  • Validator issue emission + detector skip (ReDoS and invalid-regex paths)
  • Calibration: all DEFAULT_PATTERNS pass the heuristic and compile

Local gate

./scripts/check-all.sh: 1294 passed, 5 skipped ✅

Closes #829

Made with Cursor

Enforcement at the trust boundary — _load_regex_overrides (plugin/override
path) only; curated built-in DEFAULT_PATTERNS are exempt per threat model
(ReDoS is an external-input vector; built-ins are the defense line, not the
threat).

- config/plugin_validator.py: _has_nested_quantifier — char-by-char scanner
  (star height > 1, same criterion as safe-regex/eslint). Escape-aware
  (\+, \(, \)), character-class-aware ([+*]), bounded-brace-aware
  ({2,5} vs {2,}). Replaces regex-over-regex which false-positived on
  PHONE_BR's escaped literals. _validate_item also reports ReDoS issues.
- core/detector.py: _load_regex_overrides skips (with PluginValidationWarning)
  override patterns with nested quantifiers or re.error; skip is per-item,
  scan continues. __init__ compile loop stays simple — built-ins trusted.
- tests/test_redos_guard.py: 8 tests — dangerous/safe heuristic shapes,
  validator issue, detector skip (ReDoS + invalid regex, good pattern in same
  file still loads), and CALIBRATION: every DEFAULT_PATTERN must pass the
  heuristic so it never drifts into rejecting curated patterns again.

Closes #829
@FabioLeitao
FabioLeitao merged commit 2dfccd6 into main Jun 11, 2026
15 checks passed
@FabioLeitao
FabioLeitao deleted the fix/redos-guard branch June 11, 2026 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[P2·G2·U1][security] core/detector.py:1128 — regex de override de 3º compilada sem guard ReDoS (auto-DoS via plugin pattern)

1 participant