Skip to content

Commit cd89d87

Browse files
authored
prevent server-side fetch from succeeding if filename contains # (#6549)
* prevent server-side fetch from succeeding if filename contains # - closes #2802 * huh i guess this was wrong all along
1 parent 03358fa commit cd89d87

File tree

7 files changed

+33
-3
lines changed

7 files changed

+33
-3
lines changed

.changeset/green-kiwis-roll.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
[breaking] prevent server-side fetch from reading files with # character in the filename

packages/kit/src/runtime/server/page/fetch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function create_fetch({ event, options, state, route, prerender_default }
6363
}
6464
}
6565

66-
const resolved = resolve(event.url.pathname, requested.split('?')[0]);
66+
const resolved = resolve(event.url.pathname, requested.split('?')[0]).replace(/#.+$/, '');
6767

6868
/** @type {Response} */
6969
let response;

packages/kit/test/apps/basics/src/hooks.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ export const handle = sequence(
3737
? ({ html }) => html.replace('__REPLACEME__', 'Worked!')
3838
: undefined
3939
});
40-
response.headers.append('set-cookie', 'name=SvelteKit; path=/; HttpOnly');
40+
41+
try {
42+
// in some tests we fetch stuff with undici, and the headers are immutable.
43+
// we can safely ignore it in those cases
44+
response.headers.append('set-cookie', 'name=SvelteKit; path=/; HttpOnly');
45+
} catch {}
4146

4247
return response;
4348
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** @type {import('./$types').PageLoad} */
2+
export async function load({ fetch }) {
3+
const res = await fetch('/load/assets/a#b.txt');
4+
5+
return {
6+
status: res.status
7+
};
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
/** @type {import('./$types').PageData} */
3+
export let data;
4+
</script>
5+
6+
<h1>status: {data.status}</h1>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nope

packages/kit/test/apps/basics/test/server.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,12 @@ test.describe('Load', () => {
249249
request
250250
}) => {
251251
const response = await request.get('/errors/error-in-layout');
252-
expect(await response.text()).toContain('Error: 500');
252+
expect(await response.text()).toContain('Error: 404');
253+
});
254+
255+
test('fetch does not load a file with a # character', async ({ request }) => {
256+
const response = await request.get('/load/static-file-with-hash');
257+
expect(await response.text()).toContain('status: 404');
253258
});
254259
});
255260

0 commit comments

Comments
 (0)