Skip to content

Commit

Permalink
fix: do not re-escape cookies collected from fetch calls during SSR (#…
Browse files Browse the repository at this point in the history
…11904)

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

* add test

* add changeset
  • Loading branch information
Conduitry authored Apr 12, 2024
1 parent ed8970e commit bbab296
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 1 deletion.
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 @@ -827,6 +827,11 @@ test.describe('cookies', () => {
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

0 comments on commit bbab296

Please # to comment.