Browsers now receive all cookies, not just the last one#59
Merged
dcrockwell merged 1 commit intodevelopfrom Mar 7, 2026
Merged
Browsers now receive all cookies, not just the last one#59dcrockwell merged 1 commit intodevelopfrom
dcrockwell merged 1 commit intodevelopfrom
Conversation
## Why This Change Was Made - The mist response converter used `http_response.set_header` for every header, which calls `list.key_set` and replaces any existing header with the same name - This is correct for most headers but violates RFC 6265 for `Set-Cookie` — each cookie MUST be sent as a separate `Set-Cookie` header - Browsers do not parse comma-separated `Set-Cookie` values, so when a Dream response had multiple cookies, only the last one survived the conversion to mist format - This meant authentication flows setting both a session cookie and a CSRF cookie (or any multi-cookie scenario) would silently lose all cookies except the last one ## What Was Changed - `src/dream/servers/mist/response.gleam`: `add_header` now uses `prepend_header` (which allows duplicates) for `set-cookie` headers, and `set_header` (which replaces) for everything else — matching the Gleam standard library's own `set_cookie` convention - `src/dream/http/response.gleam`: Updated `Response` type hexdoc to clarify each cookie becomes a separate `Set-Cookie` header - `test/dream/servers/mist/response_test.gleam`: Added 7 tests covering RFC 6265 multi-cookie compliance - `test/matchers/count_mist_headers.gleam`: New matcher for verifying header counts by name - `test/matchers/extract_all_mist_header_values.gleam`: New matcher for extracting all values of a header - `CHANGELOG.md`: Added 2.3.3 entry - `gleam.toml`: Bumped version to 2.3.3 - `releases/release-2.3.3.md`: New release notes ## Note to Future Engineer - The string match on `"set-cookie"` looks fragile but is safe: `convert_header_to_tuple` lowercases all header names, and `add_cookie_header` hardcodes lowercase `"set-cookie"` — plus `prepend_header` itself lowercases the key internally, so you'd have to try pretty hard to break this - If you're wondering why `Set-Cookie` is special: RFC 7230 §3.2.2 says you can comma-fold duplicate headers into one, EXCEPT for `Set-Cookie` which RFC 6265 says you absolutely cannot — because cookie values can contain commas (in Expires dates), so browsers gave up trying to parse them that way circa 2011 and never looked back - Yes, this means the entire fix is a three-line case expression. The other 250 lines are tests. You're welcome.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
When a Dream response contained multiple cookies — for example, a session cookie and a CSRF token — only the last cookie was actually received by the browser. The rest were silently dropped during the conversion from Dream's response format to Mist's wire format.
This happened because the response converter treated
Set-Cookielike any other HTTP header and replaced earlier values with later ones. ButSet-Cookieis special: RFC 6265 requires each cookie to be sent as its own separate header. Browsers don't try to split a single comma-separatedSet-Cookievalue — they just see one cookie and ignore the rest.Any application setting more than one cookie per response (auth + preferences, session + CSRF, etc.) was affected.
What
The mist response converter's
add_headerfunction now distinguishes betweenSet-Cookieand all other headers:Set-Cookie→ usesprepend_header, which allows duplicate header names (one per cookie)set_header, which replaces duplicates (correct for most headers)This matches the convention used by the Gleam standard library's own
set_cookiefunction.Additionally:
convertandResponsetype to document the per-cookie header behaviorcount_mist_headers,extract_all_mist_header_values)How
The fix is a 3-line
caseexpression inadd_headerthat checks whether the header name is"set-cookie". Case sensitivity is safe because all header names are lowercased before reaching this function (viaconvert_header_to_tupleandadd_cookie_header).Test plan
multiple Set-Cookie headers (RFC 6265)groupSet-CookieheadersSet-Cookiein headers coexists with cookies fromcookiesfield