Skip to content

fix(typescript): eliminate ReDoS in chat parser role-boundary regex - #480

Merged
Seth Juarez (sethjuarez) merged 1 commit into
mainfrom
sejuare-microsoft/fix-ts-parser-redos
Aug 18, 2026
Merged

Seth Juarez (sethjuarez) merged 1 commit into
mainfrom
sejuare-microsoft/fix-ts-parser-redos

Conversation

@sethjuarez

Copy link
Copy Markdown
Member

Summary

Eliminates a ReDoS (catastrophic backtracking) vulnerability in the TypeScript chat parser's role-boundary regex — the TypeScript half of #446. The Python half was fixed in #457.

The bug

The old boundary regex nested a repetition whose value class overlapped both the \w+= iteration starter and the separators:

(\[((\w+\s*=\s*"?[^"]*"?\s*,?\s*)+)\])?

A long attribute-like run with no valid terminator (e.g. user[name=aname=a...name=a") can be partitioned exponentially, so the engine hangs. I confirmed this locally: the adversarial input did not terminate.

The fix

JavaScript's RegExp has no possessive quantifiers or atomic groups (so the Python 3.11 approach in #457 doesn't port). Instead the nested + is removed entirely: the bracket now requires an attribute-like start and swallows the remainder of the bracket with a single, non-overlapping class:

(\[\w+\s*=\s*[^\]]*\])?

This is structurally linear (verified: 40,000-repetition adversarial input matches in ~0.2ms). Individual attributes are still split by the unchanged ATTR_RE, so parsing behavior is preserved — including multi-attribute lines like user[name=Bob, id=7]: and quoted commas like user[name="a,b"]:. Parity holds for negatives too (user[garbage]: is still not a boundary).

Tests

  • Added a bounded-runtime regression test (< 1000ms on adversarial input).
  • Added a multi-unquoted-attribute test (user[name=Bob, id=7]:).
  • Full core suite: 1809 passed / 17 skipped, tsc --noEmit clean.

Fixes #446

The role-boundary regex nested a repetition whose value class overlapped
the `\w+=` iteration starter and the separators, so a long attribute run
(e.g. `name=aname=a...`) could be partitioned exponentially, causing
catastrophic backtracking. JavaScript has no possessive quantifiers (unlike
the Python fix in #457), so the nested `+` is removed: the bracket now
requires an attribute-like start and swallows the remainder with a single
non-overlapping `[^]]*` class, which is structurally linear. Individual
attributes are still split by ATTR_RE, so behavior is preserved.

Adds a bounded-runtime regression test and a multi-unquoted-attribute test.

Fixes #446

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@sethjuarez
Seth Juarez (sethjuarez) merged commit a1f7918 into main Aug 18, 2026
16 checks passed
@sethjuarez
Seth Juarez (sethjuarez) deleted the sejuare-microsoft/fix-ts-parser-redos branch August 18, 2026 05:05
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.

ReDoS in chat parser role-boundary regex (Python, TypeScript)

1 participant