Opened 3 weeks ago
Closed 3 weeks ago
#37485 closed New feature (needsnewfeatureprocess)
set_cookie() ignores the cookie flags required by name prefixes and SameSite=None
| Reported by: | Knk | Owned by: | |
|---|---|---|---|
| Component: | HTTP handling | Version: | dev |
| Severity: | Normal | Keywords: | cookies samesite secure httponly |
| Cc: | Knk | Triage Stage: | Unreviewed |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
HttpResponse.set_cookie() emits the secure and httponly values exactly as it receives them, while
HttpResponse.delete_cookie() already applies the flags a browser requires before it stores a cookie.
The result is a syntactically valid Set-Cookie header that the browser silently discards.
This affects three documented cases:
| Cookie name / parameters | Required by the browser | Emitted by set_cookie() |
__Secure-x, __Host-x, __Http-x | Secure | nothing, unless the caller passes secure=True
|
__Http-x, __Host-Http-x | HttpOnly | nothing, unless the caller passes httponly=True
|
samesite="None" | Secure | nothing |
References: RFC 6265bis section 4.1.3 (cookie name prefixes) and the SameSite=None requirements. The
__Http- and __Host-Http- prefixes come from CHIPS; delete_cookie() already handles them, so the
inconsistency is inside the same class.
The failure is silent. The response is a normal 200 with a Set-Cookie header, the browser drops the
cookie, and the following request arrives without it. A project that sets
SESSION_COOKIE_NAME = "__Host-sessionid" and keeps SESSION_COOKIE_SECURE = False (local development,
or TLS terminated in a reverse proxy) sees a successful login followed by a request with no session, and
nothing appears in the server logs.
Steps to reproduce, current behavior:
>>> from django.http import HttpResponse
>>> r = HttpResponse()
>>> r.set_cookie("__Host-sessionid", "abc")
>>> r.cookies["__Host-sessionid"]["secure"]
''
>>> r.set_cookie("c", "abc", samesite="None")
>>> r.cookies["c"]["secure"]
''
Expected behavior: secure is True in both cases, because a browser will not store either cookie
otherwise. Cookies without a name prefix and without SameSite=None must keep behaving exactly as
before, with the flags the caller passed and nothing else.
Proposed fix: a module-level helper in django/http/response.py that computes the flags required by the
name and by samesite, used by both set_cookie() and delete_cookie() so the two paths cannot
diverge again. The helper only adds flags a browser already requires for that cookie; it never removes
one.
Patch: https://github.com/django/django/pull/22261 (branch set-cookie-required-flags), with four
regression tests in tests/responses/test_cookie.py and a paragraph in
docs/ref/request-response.txt.
Verification:
$ python tests/runtests.py responses --parallel 1 # with the patch Ran 61 tests OK (skipped=1) $ python tests/runtests.py responses --parallel 1 # django/http/response.py reverted FAILED (failures=7, skipped=1)
The seven failures are exactly the new assertions (the prefixed-name loops count one subtest each), so
they measure the change and not the environment.
Not covered by this patch: __Host- also requires Path=/ and no Domain attribute. Enforcing that
means changing the contract of set_cookie() or adding a security.W0xx deployment check that reads
SESSION_COOKIE_NAME / CSRF_COOKIE_NAME; it can be a follow-up rather than part of this diff.
I searched the existing tickets for cookie prefixes, Secure, HttpOnly and SameSite. The open ones I
found, including 34613 (Partitioned support) and the older cookie parsing tickets, do not cover
set_cookie() applying the flags required by name prefixes or by SameSite=None.
Change History (4)
comment:1 by , 3 weeks ago
| Status: | new → assigned |
|---|
comment:2 by , 3 weeks ago
comment:3 by , 3 weeks ago
Special handling for cookie prefixes in delete_cookie() only was added in #28989 (2018) and updated in #37467 (last week). Those tickets don't mention set_cookie(); it's not entirely clear why this wasn't considered at the same time.
comment:4 by , 3 weeks ago
| Resolution: | → needsnewfeatureprocess |
|---|---|
| Status: | assigned → closed |
| Type: | Bug → New feature |
Thank you for the report!
The current behavior may be desirable: set_cookie() gives callers explicit control over the cookie attributes. Automatically enabling flags that the caller has omitted or explicitly disabled would change that behavior.
We should discuss whether Django should add the required attributes, reject incompatible combinations, or leave this to the caller.
delete_cookie() has a different contract: it supplies attributes needed to perform the deletion, and its API does not expose secure or httponly arguments.
I'll close this as needsnewfeatureprocess so the proposed behavior needs more discussion
Requesting triage for this ticket.
The patch is in PR #22261 (branch set-cookie-required-flags), head commit c43681d:
https://github.com/django/django/pull/22261
It adds a module-level helper in django/http/response.py that computes the flags a browser
requires for a cookie name prefix and for SameSite=None, and both set_cookie() and
delete_cookie() use it. It only adds flags the browser already requires; cookies without a
prefix and without SameSite=None keep exactly the flags the caller passed.
Reviewed state:
With django/http/response.py reverted, the same suite fails on the new assertions only
(7 failures, skipped=1), so the tests measure the change and not the environment.
docs/ref/request-response.txt documents the automatic flags and the docs lint is green.
Known limit, already stated in the ticket: Host- also requires Path=/ and no Domain.
Enforcing that changes the contract of set_cookie() or needs a security.W0xx deployment
check, so it is left as a follow-up instead of being hidden in this diff.
The "Has patch" flag is set. I am happy to adjust the diff, the component or the approach
if triage points elsewhere.