Skip to content

Security: harden regex usage against ReDoS (S5852)#281

Merged
NickLetts2 merged 3 commits into
mainfrom
copilot/harden-regex-against-redos
May 15, 2026
Merged

Security: harden regex usage against ReDoS (S5852)#281
NickLetts2 merged 3 commits into
mainfrom
copilot/harden-regex-against-redos

Conversation

Copilot AI commented May 15, 2026

Copy link
Copy Markdown
Contributor

SonarCloud flagged five regex hotspots (S5852) across the codebase. The content-sanitiser is the highest-priority target since it scans untrusted uploaded document text; the remainder are public auth input or admin/CI-only contexts.

Content sanitiser (services/content-sanitiser/app/main.py)

  • Added MAX_SCAN_CHARS = 200_000; injection pattern scan now operates on text[:MAX_SCAN_CHARS] rather than the full raw input
  • Simplified the delete-files pattern to eliminate adjacent optional \s+ groups that created ambiguous whitespace ownership:
    # Before — adjacent optional groups, potential super-linear backtracking
    r"delete\s+(?:any|all|the|these)?(?:\s+uploaded)?(?:\s+)?files?"
    # After — unambiguous, each group owns its own trailing space
    r"delete\s+(?:(?:any|all|the|these)\s+)?(?:uploaded\s+)?files?"

Content sanitiser tests

  • TestReDoSAdversarialInput: 5 regression tests with wall-clock budget assertions against pathological inputs (500 k-char word-space strings, trigger phrase inside/outside scan window, repeated delete words)
  • TestPromptInjectionDeleteFiles: 5 additional variant tests confirming the simplified regex still detects all intended phrasings

EmailContinueForm.tsx

  • Replaced unbounded EMAIL_REGEX with explicit RFC-aligned bounds: /^[^\s@]{1,64}@[^\s@]{1,253}\.[^\s@]{1,63}$/
  • Added MAX_EMAIL_LENGTH = 254, maxLength={254} on the input, and length guards in both isEmailValid and handleSubmit

BlogPostForm.tsx

  • Added maxLength={200} on the title input to bound the slug derivation
  • Split .replace(/^-+|-+$/g, '') into two separate anchored replaces to remove the alternation Sonar flags

check-naming-conventions.js

  • Added // NOSONAR: javascript:S5852 with written justification on both flagged lines — these patterns run only against trusted repository source in CI, never against attacker-controlled input

Copilot AI linked an issue May 15, 2026 that may be closed by this pull request
7 tasks
Copilot AI and others added 2 commits May 15, 2026 20:11
- content-sanitiser: add MAX_SCAN_CHARS=200_000 guard before injection
  pattern scan; simplify delete-files regex to remove adjacent optional
  whitespace groups that could cause super-linear backtracking
- content-sanitiser tests: add TestReDoSAdversarialInput with 5 regression
  tests for long/pathological inputs; all 44 tests pass
- EmailContinueForm: replace unbounded EMAIL_REGEX with bounded-quantifier
  version, add MAX_EMAIL_LENGTH=254 constant, maxLength={254} on input,
  and explicit length guard in both isEmailValid and handleSubmit
- BlogPostForm: add maxLength={200} to title input; split slug cleanup into
  two linear anchored replaces to eliminate alternation flagged by Sonar S5852
- check-naming-conventions.js: add NOSONAR:javascript:S5852 comments with
  written justification for both CI-only regex patterns (lines 35, 50)
  that run only against trusted repository source

Agent-Logs-Url: https://github.com/NickLetts2/Curvit/sessions/992012cf-81a3-4603-a017-531d943556a4

Co-authored-by: NickLetts2 <90337962+NickLetts2@users.noreply.github.com>
… tests

Agent-Logs-Url: https://github.com/NickLetts2/Curvit/sessions/992012cf-81a3-4603-a017-531d943556a4

Co-authored-by: NickLetts2 <90337962+NickLetts2@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix regex usage to harden against ReDoS vulnerabilities Security: harden regex usage against ReDoS (S5852) May 15, 2026
Copilot AI requested a review from NickLetts2 May 15, 2026 20:14
@NickLetts2 NickLetts2 marked this pull request as ready for review May 15, 2026 20:37
Copilot AI review requested due to automatic review settings May 15, 2026 20:37
@NickLetts2 NickLetts2 merged commit e2174e2 into main May 15, 2026
6 checks passed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens regex usage flagged by SonarCloud S5852 across the content sanitiser, frontend forms, and a contracts CI script. The main intent is reducing ReDoS risk for untrusted uploaded text while preserving existing prompt-injection and form behavior.

Changes:

  • Bounds prompt-injection regex scanning in the content sanitiser and simplifies one risky pattern.
  • Adds frontend input bounds and adjusts slug/email regex handling.
  • Adds regression tests for adversarial sanitiser inputs and documents CI-only regex suppressions.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
services/content-sanitiser/app/main.py Adds bounded injection scanning and updates the delete-files regex.
services/content-sanitiser/tests/test_injection_patterns.py Adds delete-files variants and long-input ReDoS regression tests.
apps/app-frontend/src/components/auth/EmailContinueForm.tsx Adds email length limits and bounded validation regex.
apps/app-frontend/src/components/admin/blog/BlogPostForm.tsx Adds title length limit and splits slug trim regex.
shared/contracts/scripts/check-naming-conventions.js Adds Sonar suppression comments for CI-only regex checks.

Comment on lines +142 to +146
scan_text = text[:MAX_SCAN_CHARS]
detected: list[str] = [
label
for pattern, label in _INJECTION_PATTERNS
if pattern.search(text)
if pattern.search(scan_text)
Comment on lines +35 to +36
// NOSONAR: javascript:S5852 — runs only against trusted repository source in
// local/CI contract checks, never against attacker-controlled runtime input.
Comment on lines +52 to +53
// NOSONAR: javascript:S5852 — runs only against trusted repository source in
// local/CI contract checks, never against attacker-controlled runtime input.
NickLetts2 added a commit that referenced this pull request Jun 1, 2026
…redos

Security: harden regex usage against ReDoS (S5852)
@NickLetts2 NickLetts2 deleted the copilot/harden-regex-against-redos branch June 2, 2026 09:46
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.

Security Hotspots: Harden regex usage against ReDoS

3 participants