Skip to content

Commit 1f50628

Browse files
authored
fix: return 404 with invalid URL ✌️ (#7902)
* docs: return 404 with new URL exception 🤙 * docs: return 404 with invalid URL ✌️ * docs: return 404 with invalid URL ✌️ * fix: fix up typo * fix: fix up typo * fix: fix up error response * chore: add changeset
1 parent f166db4 commit 1f50628

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

.changeset/petite-flies-pay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@builder.io/qwik-city': patch
3+
---
4+
5+
FIX: return 404 with invalid URL.

packages/qwik-city/src/middleware/request-handler/user-response.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ async function runNext(
7272
rebuildRouteInfo: RebuildRouteInfoInternal,
7373
resolve: (value: any) => void
7474
) {
75+
try {
76+
const isValidURL = (url: URL) => new URL(url.pathname + url.search, url);
77+
isValidURL(requestEv.originalUrl);
78+
} catch {
79+
const status = 404;
80+
const message = 'Resource Not Found';
81+
requestEv.status(status);
82+
const html = getErrorHtml(status, message);
83+
requestEv.html(status, html);
84+
return new ServerError(status, message);
85+
}
86+
7587
let rewriteAttempt = 1;
7688

7789
async function _runNext() {

packages/qwik/src/optimizer/src/plugins/image-size-server.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,14 @@ export const getImageSizeServer = (
115115
const fs: typeof import('fs') = await sys.dynamicImport('node:fs');
116116
const path: typeof import('path') = await sys.dynamicImport('node:path');
117117

118-
const url = new URL(req.url!, 'http://localhost:3000/');
118+
let url;
119+
try {
120+
url = new URL(req.url!, 'http://localhost:3000/');
121+
} catch {
122+
res.statusCode = 404;
123+
res.end();
124+
return;
125+
}
119126
if (req.method === 'GET' && url.pathname === '/__image_info') {
120127
const imageURL = url.searchParams.get('url');
121128
res.setHeader('content-type', 'application/json');

starters/dev-server.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ Error.stackTraceLimit = 1000;
6060
const cache = new Map<string, Promise<QwikManifest>>();
6161
async function handleApp(req: Request, res: Response, next: NextFunction) {
6262
try {
63-
const url = new URL(req.url, address);
63+
let url;
64+
try {
65+
url = new URL(req.url, address);
66+
} catch {
67+
res.status(404).send();
68+
return;
69+
}
6470
if (existsSync(url.pathname)) {
6571
const relPath = relative(startersAppsDir, url.pathname);
6672
if (!relPath.startsWith(".")) {

0 commit comments

Comments
 (0)