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

Fixes adding set-cookie headers multiple times #3026

Merged
merged 2 commits into from
Apr 7, 2022
Merged
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/calm-dolphins-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix for adding set-cookie multiple times
19 changes: 18 additions & 1 deletion packages/astro/src/vite-plugin-astro-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,24 @@ function writeHtmlResponse(res: http.ServerResponse, statusCode: number, html: s

async function writeWebResponse(res: http.ServerResponse, webResponse: Response) {
const { status, headers, body } = webResponse;
res.writeHead(status, Object.fromEntries(headers.entries()));

let _headers = {};
if('raw' in headers) {
// Node fetch allows you to get the raw headers, which includes multiples of the same type.
// This is needed because Set-Cookie *must* be called for each cookie, and can't be
// concatenated together.
type HeadersWithRaw = Headers & {
raw: () => Record<string, string[]>
};

for(const [key, value] of Object.entries((headers as HeadersWithRaw).raw())) {
res.setHeader(key, value);
}
} else {
_headers = Object.fromEntries(headers.entries());
}

res.writeHead(status, _headers);
if (body) {
if (body instanceof Readable) {
body.pipe(res);
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/ssr-api-route/src/pages/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

export function post() {
const headers = new Headers();
headers.append('Set-Cookie', `foo=foo; HttpOnly`);
headers.append('Set-Cookie', `bar=bar; HttpOnly`);

return new Response('', {
status: 201,
headers,
});
}
8 changes: 8 additions & 0 deletions packages/astro/test/ssr-api-route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,13 @@ describe('API routes in SSR', () => {
const text = await response.text();
expect(text).to.equal(`ok`);
});

it('Can set multiple headers of the same type', async () => {
const response = await fixture.fetch('/login', {
method: 'POST',
});
const setCookie = response.headers.get('set-cookie');
expect(setCookie).to.equal('foo=foo; HttpOnly, bar=bar; HttpOnly');
});
});
});