Opened 10 minutes ago
#37524 assigned Bug
HttpResponse.delete_cookie() matches cookie name prefixes case-sensitively
| Reported by: | Mariano Ignacio Baragiola | Owned by: | Mariano Ignacio Baragiola |
|---|---|---|---|
| Component: | HTTP handling | Version: | dev |
| Severity: | Normal | Keywords: | cookie prefix delete_cookie |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
HttpResponse.delete_cookie() adds the Secure flag when the cookie name starts with __Secure-, __Host- or __Http-, and the HttpOnly flag when it starts with __Http- or __Host-Http- (#28989, #37467). It compares the prefixes case-sensitively:
secure = key.startswith(("__Secure-", "__Host-", "__Http-")) or (...) httponly = key.startswith(("__Http-", "__Host-Http-"))
User agents match these prefixes case-insensitively. draft-ietf-httpbis-rfc6265bis-22, section 5.4 ("Cookie Name Prefixes"): "UAs MUST match the prefix string case-insensitively", and section 5.7 ("Storage Model"), steps 20 and 21: "If the cookie-name begins with a case-insensitive match for the string "Secure-", abort this algorithm and ignore the cookie entirely unless the cookie's secure-only-flag is true." draft-ietf-httpbis-layered-cookies-02, section 5.4.3 ("Store a Cookie"), steps 13 to 16, does the same for __Secure-, __Host-, __Http- and __Host-Http-: "If cookie's name, byte-lowercased, starts with secure- and cookie's secure is false, then return null."
So a browser treats a cookie named __host-sid or __SECURE-sid as prefixed, but Django deletes it without Secure (or without HttpOnly for __http-), and the browser ignores the deletion. The cookie stays.
Reproduction:
from django.conf import settings settings.configure() from django.http import HttpResponse r = HttpResponse() for name in ("__host-sid", "__SECURE-sid", "__http-sid", "__host-http-sid"): r.delete_cookie(name) print(r.cookies[name].output())
Actual:
Set-Cookie: __host-sid=""; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/ Set-Cookie: __SECURE-sid=""; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/ Set-Cookie: __http-sid=""; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/ Set-Cookie: __host-http-sid=""; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/
Expected: Secure on all four, and HttpOnly on the last two, as Django already does for __Host-sid, __Secure-sid, __Http-sid and __Host-Http-sid.
Versions affected: every version since the prefix check was added in #28989 (Django 2.1); #37467 (308b6d6ac0, main only) added __Http- and __Host-Http- with the same case-sensitive match. Not a regression.
set_cookie() has no prefix logic, so only delete_cookie() is affected.
Suggested fix: compare against the lowercased name:
lower_key = key.lower() secure = lower_key.startswith(("__secure-", "__host-", "__http-")) or (...) httponly = lower_key.startswith(("__http-", "__host-http-"))
AI disclosure: I found and reproduced this with Claude Code (Anthropic).