Skip to content

Release 2.3.3: Browsers now receive all cookies, not just the last one#60

Merged
dcrockwell merged 2 commits intomainfrom
develop
Mar 7, 2026
Merged

Release 2.3.3: Browsers now receive all cookies, not just the last one#60
dcrockwell merged 2 commits intomainfrom
develop

Conversation

@dcrockwell
Copy link
Copy Markdown
Contributor

Release 2.3.3

Summary

This release fixes a bug where multiple Set-Cookie headers were collapsed into a single header during mist response conversion, causing browsers to only receive the last cookie in multi-cookie responses.

What's Included

Bug Fix — Multiple Set-Cookie headers (PR #59)

The mist response converter used set_header for all headers, which replaces existing headers with the same name. This violates RFC 6265, which requires each cookie to be sent as its own Set-Cookie header. Browsers don't parse comma-separated Set-Cookie values — they only see the first (or last) cookie.

The fix uses prepend_header (which allows duplicates) for set-cookie headers, and set_header (which replaces) for everything else. This matches the Gleam standard library's own set_cookie convention.

Test Coverage

  • 7 new tests covering RFC 6265 multi-cookie compliance
  • 2 new test matchers (count_mist_headers, extract_all_mist_header_values)
  • All 247 tests passing

Documentation

  • Updated hexdocs on convert and Response type to document per-cookie header behavior
  • Changelog and release notes for 2.3.3

Upgrade

[dependencies]
dream = ">= 2.3.3 and < 3.0.0"

Full Changelog

dcrockwell and others added 2 commits March 7, 2026 02:52
## 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.
Browsers now receive all cookies, not just the last one
@dcrockwell dcrockwell self-assigned this Mar 7, 2026
@dcrockwell dcrockwell added bug Something isn't working release Official public releases labels Mar 7, 2026
@dcrockwell dcrockwell merged commit 728187c into main 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 release Official public releases

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant