Skip to content

Commit

Permalink
only forward cookies for internal fetches - closes #6604 (#6923)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Sep 21, 2022
1 parent 2776f33 commit 73ea7b4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-shoes-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Only forward set-cookie headers for internal fetches
22 changes: 11 additions & 11 deletions packages/kit/src/runtime/server/page/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,21 @@ export function create_fetch({ event, options, state, route, prerender_default,
state.prerendering.dependencies.set(url.pathname, dependency);
}

const set_cookie = response.headers.get('set-cookie');
if (set_cookie) {
set_cookies.push(
...set_cookie_parser.splitCookiesString(set_cookie).map((str) => {
const { name, value, ...options } = set_cookie_parser.parseString(str);
// options.sameSite is string, something more specific is required - type cast is safe
return /** @type{import('./types').Cookie} */ ({ name, value, options });
})
);
}

return response;
}
});

const set_cookie = response.headers.get('set-cookie');
if (set_cookie) {
set_cookies.push(
...set_cookie_parser.splitCookiesString(set_cookie).map((str) => {
const { name, value, ...options } = set_cookie_parser.parseString(str);
// options.sameSite is string, something more specific is required - type cast is safe
return /** @type{import('./types').Cookie} */ ({ name, value, options });
})
);
}

const proxy = new Proxy(response, {
get(response, key, _receiver) {
async function text() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('./$types').PageLoad} */
export async function load({ fetch, url }) {
const port = url.searchParams.get('port');
const res = await fetch(`http://localhost:${port}`);

return await res.json();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>hello</h1>
25 changes: 24 additions & 1 deletion packages/kit/test/apps/basics/test/server.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@playwright/test';
import { test } from '../../../utils.js';
import { start_server, test } from '../../../utils.js';
import { fetch } from 'undici';
import { createHash, randomBytes } from 'node:crypto';

Expand All @@ -23,6 +23,29 @@ test.describe('Content-Type', () => {
});
});

test.describe('Cookies', () => {
test('does not forward cookies from external domains', async ({ request }) => {
const { close, port } = await start_server(async (req, res) => {
if (req.url === '/') {
res.writeHead(200, {
'set-cookie': 'external=true',
'access-control-allow-origin': '*'
});

res.end('ok');
} else {
res.writeHead(404);
res.end('not found');
}
});

const response = await request.get(`/load/fetch-external-no-cookies?port=${port}`);
expect(response.headers()['set-cookie']).not.toContain('external=true');

close();
});
});

test.describe('CSRF', () => {
test('Blocks requests with incorrect origin', async ({ baseURL }) => {
const res = await fetch(`${baseURL}/csrf`, {
Expand Down

0 comments on commit 73ea7b4

Please sign in to comment.