Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/single-flight-cookie-header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": patch
---

Send the single-flight re-run request a single `; `-separated Cookie header with values kept as the browser sent them, so cookie parsers on Cloudflare Workers no longer read every cookie as one.
40 changes: 32 additions & 8 deletions packages/start/src/fns/handler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import { parseCookies } from "h3";
import type { FetchEvent } from "../server/types.ts";
import { getFetchEvent, mergeResponseHeaders } from "../server/fetchEvent.ts";
import { getServerFunction } from "./registration.ts";

vi.mock("h3", () => ({
parseCookies: vi.fn(() => ({})),
}));

vi.mock("solid-js/web", () => ({
renderToString: vi.fn(),
}));
Expand Down Expand Up @@ -69,7 +64,6 @@ describe("createSingleFlightHeaders", () => {

beforeEach(async () => {
vi.clearAllMocks();
vi.mocked(parseCookies).mockReturnValue({});
const module = await import("./handler.ts");
createSingleFlightHeaders = module.createSingleFlightHeaders;
});
Expand Down Expand Up @@ -137,7 +131,6 @@ describe("createSingleFlightHeaders", () => {
});

it("should let response cookies win over ones already on the request", () => {
vi.mocked(parseCookies).mockReturnValue({ session: "old" });
const sourceEvent = createMockFetchEvent({ cookie: "session=old" });
const redirect = new Response(null, {
status: 302,
Expand All @@ -150,7 +143,6 @@ describe("createSingleFlightHeaders", () => {
});

it("should remove cookies cleared by the response", () => {
vi.mocked(parseCookies).mockReturnValue({ session: "abc123" });
const sourceEvent = createMockFetchEvent({ cookie: "session=abc123" });
const redirect = new Response(null, {
status: 302,
Expand All @@ -162,6 +154,38 @@ describe("createSingleFlightHeaders", () => {
expect(result.get("cookie")).toBe(null);
});

it("should join multiple cookies into a single semicolon-separated header", () => {
const sourceEvent = createMockFetchEvent({ cookie: "theme=dark; session=abc" }, [
"val=1234; Path=/",
]);

const append = vi.spyOn(Headers.prototype, "append");

const result = createSingleFlightHeaders(sourceEvent);

expect(append).not.toHaveBeenCalledWith("cookie", expect.anything());
expect(result.get("cookie")).toBe("theme=dark; session=abc; val=1234");
append.mockRestore();
});

it("should keep cookie values encoded as they were sent", () => {
const sourceEvent = createMockFetchEvent({ cookie: "token=a%2Bb%3D%3D; other=x" }, [
"data=c%3Bd; Path=/",
]);

const result = createSingleFlightHeaders(sourceEvent);

expect(result.get("cookie")).toBe("token=a%2Bb%3D%3D; other=x; data=c%3Bd");
});

it("should send a nameless response cookie as its bare value", () => {
const sourceEvent = createMockFetchEvent({ cookie: "a=1" }, ["abc"]);

const result = createSingleFlightHeaders(sourceEvent);

expect(result.get("cookie")).toBe("a=1; abc");
});

it("should not copy non-cookie response headers onto the request", () => {
const sourceEvent = createMockFetchEvent();
const redirect = new Response(null, {
Expand Down
24 changes: 10 additions & 14 deletions packages/start/src/fns/handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseSetCookie } from "cookie-es";
import { type H3Event, parseCookies } from "h3";
import { parseCookie, parseSetCookie } from "cookie-es";
import type { H3Event } from "h3";
import { sharedConfig } from "solid-js";
import { renderToString } from "solid-js/web";
import { provideRequestEvent } from "solid-js/web/storage";
Expand Down Expand Up @@ -431,24 +431,20 @@ export function createSingleFlightHeaders(sourceEvent: FetchEvent, result?: unkn
// update request headers on the underlying H3 event.

const headers = new Headers(sourceEvent.request.headers);
const cookies = parseCookies(sourceEvent.nativeEvent);
// keep values encoded: a decoded `%3B` or `%25` would split or re-decode differently
const cookies = parseCookie(headers.get("cookie") ?? "", { decode: value => value });
const SetCookies = sourceEvent.response.headers.getSetCookie();
// cookies attached to the returned/thrown response (eg. `redirect(to, { headers })`)
// haven't been merged onto the event response yet, but a browser round trip would
// have sent them back with the next request, so apply them here too. They come after
// the ones on the event response, so they win on conflict.
if (result instanceof Response) SetCookies.push(...result.headers.getSetCookie());
headers.delete("cookie");
// let useH3Internals = false;
// if (sourceEvent.nativeEvent.node?.req) {
// useH3Internals = true;
// sourceEvent.nativeEvent.node.req.headers.cookie = "";
// }
SetCookies.forEach(cookie => {
if (!cookie) return;
// `parseSetCookie` returns undefined for forbidden cookie names and for
// cookies with an empty name and value.
const parsed = parseSetCookie(cookie);
const parsed = parseSetCookie(cookie, { decode: false });
if (!parsed) return;
const { maxAge, expires, name, value } = parsed;
if (maxAge != null && maxAge <= 0) {
Expand All @@ -461,11 +457,11 @@ export function createSingleFlightHeaders(sourceEvent: FetchEvent, result?: unkn
}
cookies[name] = value;
});
Object.entries(cookies).forEach(([key, value]) => {
headers.append("cookie", `${key}=${value}`);
// useH3Internals &&
// (sourceEvent.nativeEvent.node.req.headers.cookie += `${key}=${value};`);
});
// a single header: repeated `append` joins with ", " outside Node, which parsers read as one cookie
const cookie = Object.entries(cookies)
.map(([key, value]) => (key ? `${key}=${value}` : value))
.join("; ");
if (cookie) headers.set("cookie", cookie);

return headers;
}
Expand Down
Loading