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

only forward cookies for internal fetches - #6923

Merged
merged 1 commit into from
Sep 21, 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/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