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 server crash when accessing a malformed URI #5246

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spotty-ladybugs-teach.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix server crash when accessing a malformed URI
9 changes: 7 additions & 2 deletions packages/kit/src/runtime/server/index.js
Expand Up @@ -41,7 +41,12 @@ export async function respond(request, options, state) {
}
}

let decoded = decodeURI(url.pathname);
let decoded;
try {
decoded = decodeURI(url.pathname);
} catch {
return new Response('Malformed URI', { status: 400 });
}

/** @type {import('types').SSRRoute | null} */
let route = null;
Expand All @@ -51,7 +56,7 @@ export async function respond(request, options, state) {

if (options.paths.base && !state.prerendering?.fallback) {
if (!decoded.startsWith(options.paths.base)) {
return new Response(undefined, { status: 404 });
return new Response('Not found', { status: 404 });
}
decoded = decoded.slice(options.paths.base.length) || '/';
}
Expand Down
16 changes: 16 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Expand Up @@ -1134,6 +1134,22 @@ test.describe.parallel('Errors', () => {
'500: Cannot prerender pages that have endpoints with mutative methods'
);
});

test('returns 400 when accessing a malformed URI', async ({ page, javaScriptEnabled }) => {
if (javaScriptEnabled) {
// the JS tests will look for body.started which won't be present
return;
}

const response = await page.goto('/%c0%ae%c0%ae/etc/passwd');
if (process.env.DEV) {
// Vite will return a 500 error code
// We mostly want to make sure malformed requests don't bring down the whole server
expect(/** @type {Response} */ (response).status()).toBeGreaterThanOrEqual(400);
} else {
expect(/** @type {Response} */ (response).status()).toBe(400);
}
});
});

test.describe.parallel('ETags', () => {
Expand Down