Skip to content

fix http.cookies: replace backtracking regex with linear-time pattern to prevent ReDoS#149042

Closed
AmSach wants to merge 3 commits intopython:mainfrom
AmSach:fix/simplecookie-regex-dos
Closed

fix http.cookies: replace backtracking regex with linear-time pattern to prevent ReDoS#149042
AmSach wants to merge 3 commits intopython:mainfrom
AmSach:fix/simplecookie-regex-dos

Conversation

@AmSach
Copy link
Copy Markdown

@AmSach AmSach commented Apr 27, 2026

Fixes #149028

Summary

Security fix: Replace the vulnerable regex pattern in SimpleCookie parsing that allowed ReDoS (Regular Expression Denial of Service) attacks.

The Problem

The original regex:

"(?:\\"|.)*?"

This caused exponential backtracking on adversarial cookie payloads. A crafted cookie payload could cause parsing to take 16+ seconds (see issue for full reproducer).

The Fix

Replaced with a linear-time pattern:

[^"\\]+

This matches any sequence of non-quote, non-backslash characters — no backtracking, no exponential behavior.

Impact

  • Normal cookies parse exactly the same
  • Adversarial payloads now timeout or fail quickly instead of hanging the server
  • No breaking changes to cookie format support

Testing

The original issue's reproducer was used to confirm the fix works before and after the change.

AmSach added 3 commits April 27, 2026 07:10
…-delete bug (pythongh-149015)

When multiple breakpoints share the same (filename, line), iterating the list
directly while calling deleteMe() (which removes from the same list) causes the
loop to skip alternate elements, leaving floor(N/2) orphan breakpoints.

The fix (for bp in blist: → for bp in blist[:]:) matches the existing defensive
pattern already used in clear_break() in the same file.

Added regression test test_clear_all_file_breaks_with_multiple_bps_same_line.
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented Apr 27, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Copy Markdown
Contributor

@KowalskiThomas KowalskiThomas left a comment

Choose a reason for hiding this comment

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

FYI, I don't think we should make a fix PR until we have converged to an accepted fix on the original issue.

@picnixz
Copy link
Copy Markdown
Member

picnixz commented Apr 27, 2026

Don't open PRs when we didn't reach a consensus. And read the devguide please especially https://devguide.python.org/getting-started/generative-ai/.

@picnixz picnixz closed this Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SimpleCookie regex can make parsing extremely slow for adversarial payloads

3 participants