Skip to content

Fix ReDoS in email update regex (challenge 7, API4:2019) - #70

Closed
beanbeah wants to merge 1 commit into
OWASP-CTF:dc34-ctffrom
beanbeah:ctf/challenge-7-redos-email-update
Closed

beanbeah wants to merge 1 commit into
OWASP-CTF:dc34-ctffrom
beanbeah:ctf/challenge-7-redos-email-update

Conversation

@beanbeah

@beanbeah beanbeah commented Aug 9, 2026

Copy link
Copy Markdown

Vulnerability

PUT /users/v1/{username}/email validated the new email against a catastrophic-backtracking regex:

^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@{1}([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$

The nested (X*Y)* construction over overlapping character classes is classic ReDoS territory: a crafted input with no @ (e.g. a long run of alphanumerics) forces the engine into exponential backtracking, hanging the worker thread. Locally, this pattern took 60+ seconds on a 30-character crafted payload before I gave up waiting.

Fix

Replaced the regex in the if vuln: branch of update_email() (api_views/users.py) with an equivalent-intent email pattern where no unbounded quantifier is nested inside another — every repetition is capped to a small explicit bound, so backtracking work stays bounded/linear in input length regardless of content. Added a hard 254-character length cap on the input as defense in depth, since no regex-only fix should be trusted to stay cheap for arbitrarily long input.

Testing (local, WSL)

  • Ran the app locally (vulnerable=1), seeded via /createdb, logged in as name1.
  • Exploit before fix (isolated regex test, not against a live server crash): the original pattern against 'a'*30 + '!' did not return within 60 seconds.
  • Exploit after fix: the same class of payload (up to 250+ chars, no @) is now rejected with 400 in ~10ms.
  • Legitimate use still works: PUT /users/v1/name1/email with {"email": "name1@example.com"} returns 204 and updates the email as before. Additional valid formats (john.doe@sub.domain.co, a_b-c@x-y.io) also match correctly.

PUT /users/v1/{username}/email validated the new email against a
catastrophic-backtracking pattern: ^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@{1}...)$
The nested (X*Y)* construction over overlapping character classes lets a
crafted string with no '@' drive exponential backtracking, hanging the
worker thread (ReDoS / API4:2019 Lack of Resources & Rate Limiting).

Replace it with an equivalent-intent email pattern where every repetition
is bounded (no unbounded quantifier is nested inside another), so
backtracking work stays linear in input length, plus a hard 254-char
length cap on the input as defense in depth. Verified locally that a
crafted payload that hung the original regex for 60+ seconds at n=30 now
gets rejected in milliseconds even at 250+ chars, while legitimate email
updates (e.g. name1@example.com) still succeed with HTTP 204.
@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown

🏆 VAmPI — CTF Patch Score

████░░░░░░░░░░░░░░░░  3 / 16 pts  (19%)

1 / 9 challenges patched

Per-challenge detail is withheld — it would reveal the rubric.

Commit: 2ebae70 · scoring run

🎉 Your result is on the leaderboard — see where you rank! 🏆

@beanbeah

beanbeah commented Aug 9, 2026

Copy link
Copy Markdown
Author

Superseded by consolidated PR #74 (#74), which merges all validated challenge fixes into one branch. Closing this individual PR.

@beanbeah beanbeah closed this Aug 9, 2026
@beanbeah
beanbeah deleted the ctf/challenge-7-redos-email-update branch August 9, 2026 13:57
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.

1 participant