Skip to content

Send cookies back as the server sent them - #14508

Open
tugayabdulla wants to merge 2 commits into
aio-libs:masterfrom
tugayabdulla:cookie-send-as-received
Open

tugayabdulla wants to merge 2 commits into
aio-libs:masterfrom
tugayabdulla:cookie-send-as-received

Conversation

@tugayabdulla

@tugayabdulla tugayabdulla commented Nov 28, 2026 •

Copy link
Copy Markdown

What do these changes do?

With the default quote_cookie=True, the cookie jar re-quoted cookie values received from a server when they contain characters such as =, / or a space, so Set-Cookie: token=YWJjZA== was sent back as Cookie: token="YWJjZA==". Browsers send the value back byte for byte, and some servers reject the quoted form.

Following bdraco's suggestion in the issue, with the default the jar now always sends the stored coded value. For cookies from a server that is the value exactly as received; for values set from plain strings it is the form they were already quoted to when stored, so those are unchanged.

Are there changes in behavior for the user?

Yes: cookies received from a server are no longer quoted when sent back. quote_cookie=False behaves as before. Since this changes a default, I targeted master; if you want a deprecation step on 3.x first, as discussed in the issue, I can add one.

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

No. It removes a branch from _build_morsel and the module-level SimpleCookie that was used for re-encoding.

Related issue number

Fixes #11200

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modifications, please add yourself to
    CONTRIBUTORS.txt
  • Add a new news fragment into the CHANGES/ folder
    (CHANGES/11200.bugfix.rst, with CHANGES/14508.bugfix.rst linking to it)
Test results
$ pytest
5066 passed, 530 skipped, 14 xfailed, 1 warning in 219.10s (0:03:39)

On master, test_set_cookies_sent_back_as_received and the two "received cookie" cases of test_quotes_correctly_based_on_input fail; they pass with this change. test_quotes_cookie_values_set_from_strings passes on both and checks that values set from strings are still quoted. mypy reports the same errors as on master, none in the changed files.

Drafted with Claude Code (Claude Opus 5.5); reviewed by @tugayabdulla.

@agentscanapp

agentscanapp Bot commented Nov 28, 2026

Copy link
Copy Markdown

Insufficient data

Not enough activity yet to make a reliable assessment.

View full analysis →

This is an automated analysis by AgentScan

@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Nov 28, 2026
@codecov

codecov Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.12%. Comparing base (1290e69) to head (a7546a7).
⚠️ Report is 4 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff            @@
##           master   #14508    +/-   ##
========================================
  Coverage   99.12%   99.12%            
========================================
  Files         135      135            
  Lines       54123    54257   +134     
  Branches     2778     2785     +7     
========================================
+ Hits        53648    53782   +134     
  Misses        359      359            
  Partials      116      116            
Flag Coverage Δ
Autobahn 21.60% <8.00%> (-0.03%) ⬇️
CI-GHA 99.05% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.73% <100.00%> (+<0.01%) ⬆️
OS-Windows 97.51% <100.00%> (+<0.01%) ⬆️
OS-macOS 98.35% <100.00%> (-0.01%) ⬇️
Py-3.11 98.30% <100.00%> (+<0.01%) ⬆️
Py-3.12 98.38% <100.00%> (+<0.01%) ⬆️
Py-3.13 98.38% <100.00%> (-0.01%) ⬇️
Py-3.14 98.43% <100.00%> (+<0.01%) ⬆️
Py-3.15 98.43% <100.00%> (+<0.01%) ⬆️
Py-3.15t 98.28% <100.00%> (-0.01%) ⬇️
Py-pypy-3.12 96.97% <100.00%> (+<0.01%) ⬆️
VM-macos-latest 98.35% <100.00%> (-0.01%) ⬇️
VM-ubuntu-latest 98.73% <100.00%> (+<0.01%) ⬆️
VM-windows-latest 97.51% <100.00%> (+<0.01%) ⬆️
cython-coverage 83.95% <90.00%> (+0.05%) ⬆️

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

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

@codspeed

codspeed Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 50 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 102 untouched benchmarks
⏩ 83 skipped benchmarks1


Comparing tugayabdulla:cookie-send-as-received (a7546a7) with master (f6bd242)

Open in CodSpeed

Footnotes

  1. 83 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@tugayabdulla
tugayabdulla marked this pull request as ready for review November 29, 2026 19:41
@greptile-apps

greptile-apps Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

security Security Review

This PR appears safe to merge from a security standpoint.

What was checked
  • Server-controlled Set-Cookie values that get echoed into the Cookie request header cannot contain CR/LF or any control byte: the Set-Cookie parser rejects them, including tab and octal escapes, and _can_send re-checks the app-supplied path.
  • The unquoted comma, space, and quote characters now sent back verbatim match what browsers send, and a malicious server gains nothing it cannot already do by setting a Domain cookie directly, so no forged cookie pair reaches a spec-compliant server.
  • Cookie matching rules (domain, path, secure, host-only) and the logic that strips cookies on cross-origin redirects are untouched.
  • The 150-cookie and 8190-byte Cookie header limits are still measured against the exact bytes sent, since the length is computed after _build_morsel.
  • Cookies set from plain strings keep identical quoting to before, and quote_cookie=False is unchanged.

@greptile-apps

greptile-apps Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge; both previous findings are fixed, and no new actionable issues were found.RetriggerView in GreptileConfidence Score: 5/5 Tier: apex

[Medium impact] The PR appears safe to merge; both previous findings are fixed, and no new actionable issues were found.

Reviews (2) · Last reviewed commit: "Link the changelog to the PR and scope t..." · Reviewed by Greptile

Comment thread CHANGES/11200.bugfix.rst Resolved
Comment thread docs/client_reference.rst Resolved

@atishay-kasliwal atishay-kasliwal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Round trip through an aiohttp server and ClientSession: cookies set as t=YWJjZA==, c=a,b, s=a b, j={"k":1} and x=café now come back exactly as sent. Master quoted or octal-escaped them (t="YWJjZA==", c="a\054b", x="caf\351"). Values set from strings with update_cookies are still quoted (a="val/one").

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Phase out cookie quoting as it only causes problems

2 participants