Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly copy headers to preserve duplicate set-cookie headers. #872

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions examples/pages-functions-app/functions/_middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const onRequest = async ({ next }) => {
const response = await next();
response.headers.set("x-custom", "header value");
response.headers.set("set-cookie", "numberOne=Riker");
response.headers.append("set-cookie", "otherNumberOne=NumberOne");
return response;
};
40 changes: 39 additions & 1 deletion packages/wrangler/pages/functions/template-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ declare type PagesFunction<
P extends string = string,
Data extends Record<string, unknown> = Record<string, unknown>
> = (context: EventContext<Env, P, Data>) => Response | Promise<Response>;

declare class Headers {
constructor(init?: HeadersInit);
get(name: string): string | null;
getAll(name: string): string[];
has(name: string): boolean;
set(name: string, value: string): void;
append(name: string, value: string): void;
delete(name: string): void;
forEach<This = unknown>(
callback: (this: This, key: string, value: string, parent: Headers) => void,
thisArg?: This
): void;
entries(): IterableIterator<[key: string, value: string]>;
keys(): IterableIterator<string>;
values(): IterableIterator<string>;
[Symbol.iterator](): IterableIterator<[key: string, value: string]>;
}

/* end @cloudflare/workers-types */

type RouteHandler = {
Expand Down Expand Up @@ -91,6 +110,25 @@ function* executeRequest(request: Request) {
}
}

// With a regular get on multiple set-cookies headers, they get concatenated
// which browsers have a very hard time parsing. The fetch spec has added getAll
// to Headers, which only works on set-cookie headers, but will return them
// as an array, so they can be passed on as multiple headers.
function copyHeaders(headers: Headers) {
const cookies = headers.getAll("set-cookie");
const newHeaders = new Headers([...headers.entries()]);
if (cookies.length > 1) {
cookies.forEach((cookie, index) => {
if (index === 0) {
newHeaders.set("set-cookie", cookie);
} else {
newHeaders.append("set-cookie", cookie);
}
});
}
return newHeaders;
}

export default {
async fetch(request: Request, env: FetchEnv, workerContext: WorkerContext) {
const handlerIterator = executeRequest(request);
Expand Down Expand Up @@ -123,7 +161,7 @@ export default {
// https://fetch.spec.whatwg.org/#null-body-status
return new Response(
[101, 204, 205, 304].includes(response.status) ? null : response.body,
{ ...response, headers: new Headers([...response.headers.entries()]) }
{ ...response, headers: copyHeaders(response.headers as Headers) }
Copy link
Member

Choose a reason for hiding this comment

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

I actually think we can simplify this. I must have been tired when I first wrote this, because apparently just doing new Headers(response.headers) is enough to correctly make a mutable copy of headers.

As explained to me by the ever-helpful champs: https://discord.com/channels/595317990191398933/910978223968518144/970105175924690994

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
{ ...response, headers: copyHeaders(response.headers as Headers) }
{ ...response, headers: new Headers(response.headers) }

Copy link
Member

Choose a reason for hiding this comment

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

the new way:

image

image

vs. the old way:

image

image

);
} else if (__FALLBACK_SERVICE__) {
// There are no more handlers so finish with the fallback service (`env.ASSETS.fetch` in Pages' case)
Expand Down