Skip to content

Commit

Permalink
[fix] ensure serialized headers check is always applied
Browse files Browse the repository at this point in the history
Closes #7112
  • Loading branch information
dummdidumm committed Oct 11, 2022
1 parent bf7a6c3 commit 85cc43d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-experts-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] ensure serialized headers check is always applied
34 changes: 17 additions & 17 deletions packages/kit/src/runtime/server/page/load_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,6 @@ export async function load_data({
response_body: body,
response: response
});

// ensure that excluded headers can't be read
const get = response.headers.get;
response.headers.get = (key) => {
const lower = key.toLowerCase();
const value = get.call(response.headers, lower);
if (value && !lower.startsWith('x-sveltekit-')) {
const included = resolve_opts.filterSerializedResponseHeaders(lower, value);
if (!included) {
throw new Error(
`Failed to get response header "${lower}" — it must be included by the \`filterSerializedResponseHeaders\` option: https://kit.svelte.dev/docs/hooks#handle`
);
}
}

return value;
};
}

if (dependency) {
Expand Down Expand Up @@ -186,6 +169,23 @@ export async function load_data({
}
});

// ensure that excluded headers can't be read
const get = response.headers.get;
response.headers.get = (key) => {
const lower = key.toLowerCase();
const value = get.call(response.headers, lower);
if (value && !lower.startsWith('x-sveltekit-')) {
const included = resolve_opts.filterSerializedResponseHeaders(lower, value);
if (!included) {
throw new Error(
`Failed to get response header "${lower}" — it must be included by the \`filterSerializedResponseHeaders\` option: https://kit.svelte.dev/docs/hooks#handle`
);
}
}

return value;
};

return proxy;
},
setHeaders: event.setHeaders,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('./$types').PageLoad} */
export async function load({ fetch }) {
let res = await fetch('./irrelevant', {
method: 'GET'
});

res.headers.get('content-type');
}
11 changes: 11 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,17 @@ test.describe('Load', () => {
}
});

test('errors when trying to access non-serialized request headers on the server', async ({
page,
read_errors
}) => {
await page.goto('/load/fetch-request-headers-invalid-access');

expect(read_errors(`/load/fetch-request-headers-invalid-access`).message).toContain(
'Failed to get response header "content-type" — it must be included by the `filterSerializedResponseHeaders` option'
);
});

test('exposes rawBody as a DataView to endpoints', async ({ page, clicknav }) => {
await page.goto('/load/raw-body');
await clicknav('[href="/load/raw-body/dataview"]');
Expand Down

0 comments on commit 85cc43d

Please sign in to comment.