Skip to content

Browsers now receive all cookies, not just the last one#59

Merged
dcrockwell merged 1 commit intodevelopfrom
fix/set-cookie-multiple-headers
Mar 7, 2026
Merged

Browsers now receive all cookies, not just the last one#59
dcrockwell merged 1 commit intodevelopfrom
fix/set-cookie-multiple-headers

Conversation

@dcrockwell
Copy link
Copy Markdown
Contributor

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-Cookie like any other HTTP header and replaced earlier values with later ones. But Set-Cookie is special: RFC 6265 requires each cookie to be sent as its own separate header. Browsers don't try to split a single comma-separated Set-Cookie value — 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_header function now distinguishes between Set-Cookie and all other headers:

  • Set-Cookie → uses prepend_header, which allows duplicate header names (one per cookie)
  • Everything else → uses set_header, which replaces duplicates (correct for most headers)

This matches the convention used by the Gleam standard library's own set_cookie function.

Additionally:

  • Updated hexdocs on convert and Response type to document the per-cookie header behavior
  • Added 7 new tests covering multi-cookie scenarios, RFC 6265 compliance, and edge cases
  • Added 2 new test matchers (count_mist_headers, extract_all_mist_header_values)
  • Bumped version to 2.3.3 with changelog and release notes

How

The fix is a 3-line case expression in add_header that checks whether the header name is "set-cookie". Case sensitivity is safe because all header names are lowercased before reaching this function (via convert_header_to_tuple and add_cookie_header).

Test plan

  • 7 new tests in multiple Set-Cookie headers (RFC 6265) group
  • Multiple cookies produce separate Set-Cookie headers
  • Each cookie value is individually present
  • Three cookies produce three headers
  • Manual Set-Cookie in headers coexists with cookies from cookies field
  • Duplicate non-cookie headers are still deduplicated
  • Cookies with attributes each get their own header
  • Cookies alongside other headers don't interfere
  • All 247 tests passing
  • Pre-commit hooks passing (format, build, all modules, all examples)

## 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.
@dcrockwell dcrockwell self-assigned this Mar 7, 2026
@dcrockwell dcrockwell added the bug Something isn't working label Mar 7, 2026
@dcrockwell dcrockwell merged commit 1954291 into develop Mar 7, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant