Skip to content
9 changes: 9 additions & 0 deletions .changeset/fix-miniflare-websocket-set-cookie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"miniflare": patch
---

Preserve multiple `Set-Cookie` headers on WebSocket 101 upgrade responses

Iterating a `Headers` object with `for…of` collapses all `Set-Cookie` values into a single comma-joined string — a known web-platform limitation. This corrupted cookies whose attributes contain commas (e.g. `Expires=Wed, 09 Jun 2026 …`), causing clients to receive a single mangled `Set-Cookie` line instead of separate cookies when a Worker's WebSocket response included more than one.

Fixes [#14145](https://github.com/cloudflare/workers-sdk/issues/14145).
12 changes: 12 additions & 0 deletions packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,19 @@ export class Miniflare {
const extra = this.#webSocketExtraHeaders.get(req);
this.#webSocketExtraHeaders.delete(req);
if (extra) {
// Preserve multiple Set-Cookie values verbatim — iterating `Headers`
// with `for…of` collapses them into a single comma-joined string
// (web-platform limitation), which corrupts cookies containing commas
// (e.g. `Expires=Wed, 09 Jun 2026 …`). `getSetCookie` is not part of
// the standard Fetch API, so guard before calling it.
Comment on lines +1102 to +1106

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚩 Undici 7.x may already handle Set-Cookie separately in iteration

The changeset and code comment state that iterating a Headers object with for...of collapses Set-Cookie values into a single comma-joined string. This was true for older implementations, but undici 7.x follows the updated Fetch spec (since March 2023) where Headers[Symbol.iterator]() yields each Set-Cookie value as a separate entry. This means the bug being fixed might only have manifested with older undici versions or specific workerd Headers implementations. The fix using getSetCookie() is still the most robust approach and explicitly correct regardless of iteration behavior, so this doesn't affect correctness — but the comment's characterization of the problem may not accurately describe the current runtime behavior.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

const hasGetSetCookie = typeof extra.getSetCookie === "function";
if (hasGetSetCookie) {
for (const cookie of extra.getSetCookie()) {
headers.push(`Set-Cookie: ${cookie}`);
}
}
Comment thread
matingathani marked this conversation as resolved.
for (const [key, value] of extra) {
if (hasGetSetCookie && key.toLowerCase() === "set-cookie") continue;
if (!restrictedWebSocketUpgradeHeaders.includes(key.toLowerCase())) {
headers.push(`${key}: ${value}`);
}
Expand Down
55 changes: 55 additions & 0 deletions packages/miniflare/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,61 @@ test("Miniflare: web socket kitchen sink", async ({
expect(serverCloseEvent.reason).toBe("Test Closure");
});

// https://github.com/cloudflare/workers-sdk/issues/14145
test("Miniflare: preserves multiple Set-Cookie headers on WebSocket 101 upgrade response", async ({
expect,
onTestFinished,
}) => {
// External WebSocket origin server that returns two Set-Cookie headers on the
// 101 upgrade response. The first cookie contains a comma in its Expires
// attribute — the exact value that gets mangled when the Headers object is
// iterated with `for…of` instead of `getSetCookie()`.
Comment thread
matingathani marked this conversation as resolved.
const server = http.createServer();
const wss = new WebSocketServer({ server });
wss.on("headers", (headers) => {
headers.push(
"Set-Cookie: session=abc; Path=/; Expires=Wed, 09 Jun 2026 10:18:14 GMT"
);
headers.push("Set-Cookie: theme=dark; Path=/; HttpOnly");
});
const port = await new Promise<number>((resolve) => {
server.listen(0, () => {
onTestFinished(() => {
wss.close();
server.close();
});
resolve((server.address() as AddressInfo).port);
});
});
Comment thread
matingathani marked this conversation as resolved.

const mf = new Miniflare({
script: `addEventListener("fetch", (event) => {
event.respondWith(CUSTOM.fetch(event.request));
})`,
serviceBindings: {
CUSTOM(request) {
return fetch(`http://localhost:${port}`, request);
},
},
});
useDispose(mf);

const res = await mf.dispatchFetch("http://localhost", {
headers: {
Upgrade: "websocket",
},
});

// Both cookies must be returned as separate entries, not collapsed into a
// single comma-joined string (which would corrupt the Expires date).
const cookies = res.headers.getSetCookie();
expect(cookies).toHaveLength(2);
expect(cookies[0]).toBe(
"session=abc; Path=/; Expires=Wed, 09 Jun 2026 10:18:14 GMT"
);
expect(cookies[1]).toBe("theme=dark; Path=/; HttpOnly");
});

test("Miniflare: custom service binding to another Miniflare instance", async ({
expect,
}) => {
Expand Down
Loading