Repository navigation
fix(python): eliminate ReDoS in chat parser role-boundary regex #457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3349b8b
3c82064
e7d62f1
e967c0a
ac431ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -28,9 +28,15 @@ | |||
|
|
||||
| # Role boundary regex — matches lines like ``system:`` or ``user[name="Alice"]:`` | ||||
| _ROLE_NAMES = "|".join(sorted(ROLES)) | ||||
| # Possessive quantifiers + a quoted/unquoted alternation (no shared chars between | ||||
| # branches) keep this linear-time — a plain `\w+\s*=\s*"?[^"]*"?` value class lets | ||||
| # unquoted attributes overlap with the separator/`]`, causing catastrophic | ||||
| # backtracking on malformed input. See issue #446. | ||||
| # Possessive quantifiers (`++`, `*+`, `?+`) require Python 3.11+, which pyproject.toml | ||||
| # already sets as this package's floor (`requires-python = ">=3.11"`). | ||||
| _BOUNDARY_RE = re.compile( | ||||
| r"(?im)^\s*#?\s*(" + _ROLE_NAMES + r")" | ||||
| r"(\[((\w+\s*=\s*\"?[^\"]*\"?\s*,?\s*)+)\])?\s*:\s*$" | ||||
| r"(\[(?:\w++\s*+=\s*+(?:\"[^\"]*\"|[^\",\]]*+)\s*+,?+\s*+)+\])?\s*:\s*$" | ||||
| ) | ||||
|
Comment on lines
37
to
40
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is pre-existing, not new to this PR:
requires-python = ">=3.11" on main, unchanged by this diff. Possessive quantifiers are safe on the package's actual floor.
Comment on lines
+31
to
40
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same point as above (already answered on the earlier duplicate at #discussion_r3732083116 and r3732328648) — pyproject.toml has pinned requires-python = ">=3.11" on main since before this PR; unaffected by this diff.
Comment on lines
+31
to
40
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same already-answered point (see r3732083116, r3732328648, r3732454506) — pinned by pyproject.toml's requires-python = ">=3.11" on main, unaffected by this diff. Pushed e967c0a to state the floor directly in the code comment next to the regex so it's visible without cross-referencing pyproject.toml. |
||||
|
|
||||
|
|
||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already covered —
pyproject.tomlhasrequires-python = ">=3.11", so the possessive-quantifier syntax can't hit an older interpreter. No change needed here.