Skip to content

Conversation

patchback[bot]
Copy link
Contributor

@patchback patchback bot commented Jun 10, 2025

This is a backport of PR #11178 as merged into master (915338c).

What do these changes do?

This PR fixes the Cookie header parser to correctly handle reserved attribute names (like path, domain, secure) as regular cookies, per RFC 6265 Section 5.4.

Previously, Cookie headers like session=abc123; path=/api; secure=true would only parse session=abc123, incorrectly ignoring path and secure. Now all three are correctly parsed as cookies.

The fix:

  • Adds a new parse_cookie_header() function specifically for RFC 6265 compliant Cookie header parsing
  • Renames the existing parse_cookie_headers() to parse_set_cookie_headers() for clarity
  • Updates all Cookie header parsing locations to use the new parser

Are there changes in behavior for the user?

Yes, Cookie headers containing reserved attribute names will now be parsed differently:

Before:

# Cookie: "session=abc123; path=/api; secure=true"
request.cookies == {"session": "abc123"}  # path and secure ignored

After:

# Cookie: "session=abc123; path=/api; secure=true"
request.cookies == {"session": "abc123", "path": "/api", "secure": "true"}

This is the correct behavior per RFC 6265 and matches what web browsers do.

Is it a substantial burden for the maintainers to support this?

No. This change:

  • Simplifies the cookie parsing logic by having separate parsers for different header types
  • Makes the code more RFC compliant and easier to understand
  • Uses the same robust regex-based parsing approach as the existing parser
  • Has comprehensive test coverage

Related issue number

This has likely been an issue since the beginning, as we previously used Python's SimpleCookie which has the same incorrect behavior. Now that we have our own parser (from PR #11112), we can fix this RFC compliance issue.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES/ folder
    • name it <issue_or_pr_num>.<type>.rst (e.g. 588.bugfix.rst)

    • if you don't have an issue number, change it to the pull request
      number after creating the PR

      • .bugfix: A bug fix for something the maintainers deemed an
        improper undesired behavior that got corrected to match
        pre-agreed expectations.
      • .feature: A new behavior, public APIs. That sort of stuff.
      • .deprecation: A declaration of future API removals and breaking
        changes in behavior.
      • .breaking: When something public is removed in a breaking way.
        Could be deprecated in an earlier release.
      • .doc: Notable updates to the documentation structure or build
        process.
      • .packaging: Notes for downstreams about unobvious side effects
        and tooling. Changes in the test invocation considerations and
        runtime assumptions.
      • .contrib: Stuff that affects the contributor experience. e.g.
        Running tests, building the docs, setting up the development
        environment.
      • .misc: Changes that are hard to assign to any of the above
        categories.
    • Make sure to use full sentences with correct case and punctuation,
      for example:

      Fixed issue with non-ascii contents in doctest text files
      -- by :user:`contributor-gh-handle`.

      Use the past tense or the present tense a non-imperative mood,
      referring to what's changed compared to the last released version
      of this project.

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
(cherry picked from commit 915338c)
@bdraco bdraco enabled auto-merge (squash) June 10, 2025 03:56
Copy link

codspeed-hq bot commented Jun 10, 2025

CodSpeed Performance Report

Merging #11181 will not alter performance

Comparing patchback/backports/3.12/915338c7b02d843114e05a94508e6d91921e88d7/pr-11178 (b313c74) with 3.12 (608d8ff)

Summary

✅ 59 untouched benchmarks

Copy link

codecov bot commented Jun 10, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.31%. Comparing base (608d8ff) to head (b313c74).
⚠️ Report is 64 commits behind head on 3.12.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff            @@
##             3.12   #11181    +/-   ##
========================================
  Coverage   98.30%   98.31%            
========================================
  Files         132      132            
  Lines       43289    43499   +210     
  Branches     2376     2380     +4     
========================================
+ Hits        42555    42765   +210     
  Misses        558      558            
  Partials      176      176            
Flag Coverage Δ
CI-GHA 98.20% <100.00%> (+<0.01%) ⬆️
OS-Linux 97.95% <100.00%> (+<0.01%) ⬆️
OS-Windows 95.66% <100.00%> (+0.02%) ⬆️
OS-macOS 97.21% <100.00%> (+0.02%) ⬆️
Py-3.10.11 96.84% <100.00%> (+0.01%) ⬆️
Py-3.10.17 97.34% <100.00%> (+0.01%) ⬆️
Py-3.11.12 97.50% <100.00%> (-0.03%) ⬇️
Py-3.11.9 97.04% <100.00%> (+0.01%) ⬆️
Py-3.12.10 97.86% <100.00%> (+<0.01%) ⬆️
Py-3.12.11 97.05% <100.00%> (?)
Py-3.13.3 97.86% <100.00%> (+0.01%) ⬆️
Py-3.9.13 96.74% <100.00%> (+0.01%) ⬆️
Py-3.9.22 97.22% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.16 87.91% <99.08%> (-0.14%) ⬇️
VM-macos 97.21% <100.00%> (+0.02%) ⬆️
VM-ubuntu 97.95% <100.00%> (+<0.01%) ⬆️
VM-windows 95.66% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bdraco bdraco merged commit e2b24d7 into 3.12 Jun 10, 2025
35 checks passed
@bdraco bdraco deleted the patchback/backports/3.12/915338c7b02d843114e05a94508e6d91921e88d7/pr-11178 branch June 10, 2025 04:15
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