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

fix: do not re-escape cookies collected from fetch calls during SSR #11904

Merged
merged 4 commits into from
Apr 12, 2024
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/fair-suns-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

fix: avoid incorrectly un- and re-escaping cookies collected during a server-side `fetch`
5 changes: 4 additions & 1 deletion packages/kit/src/runtime/server/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,16 @@ export function create_fetch({ event, options, manifest, state, get_cookie_heade
const set_cookie = response.headers.get('set-cookie');
if (set_cookie) {
for (const str of set_cookie_parser.splitCookiesString(set_cookie)) {
const { name, value, ...options } = set_cookie_parser.parseString(str);
const { name, value, ...options } = set_cookie_parser.parseString(str, {
decodeValues: false
});

const path = options.path ?? (url.pathname.split('/').slice(0, -1).join('/') || '/');

// options.sameSite is string, something more specific is required - type cast is safe
set_internal(name, value, {
path,
encode: (value) => value,
.../** @type {import('cookie').CookieSerializeOptions} */ (options)
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { browser } from '$app/environment';

/** @type {import('@sveltejs/kit').Load}*/
export async function load({ fetch }) {
if (!browser) {
// We don't want the client-side collected cookie to clobber the
// server-side collected cookie that we're actually testing.
await fetch('/cookies/collect-without-re-escaping/set-cookie');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import { browser } from '$app/environment';
</script>

<p>{browser && document.cookie}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export async function GET() {
return new Response(null, { headers: { 'set-cookie': 'cookie-special-characters="foo"' } });
}
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@
await expect(page.locator('input')).toBeFocused();
});

test('scroll positions are recovered on reloading the page', async ({

Check warning on line 420 in packages/kit/test/apps/basics/test/cross-platform/client.test.js

View workflow job for this annotation

GitHub Actions / test-kit-cross-browser (18, ubuntu-latest, firefox, dev)

flaky test: scroll positions are recovered on reloading the page

retries: 2
page,
app,
browserName
Expand Down Expand Up @@ -827,6 +827,11 @@
await page.locator('button').click();
await expect(page.locator('p')).toHaveText('foo=bar');
});

test("fetch during SSR doesn't un- and re-escape cookies", async ({ page }) => {
await page.goto('/cookies/collect-without-re-escaping');
await expect(page.locator('p')).toHaveText('cookie-special-characters="foo"');
});
});

test.describe('Interactivity', () => {
Expand Down
Loading