diff options
author | 2023-04-04 15:48:28 +0200 | |
---|---|---|
committer | 2023-04-04 15:48:28 +0200 | |
commit | 5ce1264c981015c732cdc85d0a451d990d02a561 (patch) | |
tree | a70e386a61e47298414180f88804da67d634edd2 /packages/integrations/node/src | |
parent | c0de5ac2d886858a4155a589fc1ec0395f391c48 (diff) | |
download | astro-5ce1264c981015c732cdc85d0a451d990d02a561.tar.gz astro-5ce1264c981015c732cdc85d0a451d990d02a561.tar.zst astro-5ce1264c981015c732cdc85d0a451d990d02a561.zip |
fix(node): Fix malformed URLs crashing the server in certain cases (#6746)
Diffstat (limited to 'packages/integrations/node/src')
-rw-r--r-- | packages/integrations/node/src/http-server.ts | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/packages/integrations/node/src/http-server.ts b/packages/integrations/node/src/http-server.ts index f0dde82d5..850d61bbb 100644 --- a/packages/integrations/node/src/http-server.ts +++ b/packages/integrations/node/src/http-server.ts @@ -12,16 +12,32 @@ interface CreateServerOptions { removeBase: (pathname: string) => string; } +function parsePathname(pathname: string, host: string | undefined, port: number) { + try { + const urlPathname = new URL(pathname, `http://${host}:${port}`).pathname; + return decodeURI(encodeURI(urlPathname)); + } catch (err) { + return undefined; + } +} + export function createServer( { client, port, host, removeBase }: CreateServerOptions, handler: http.RequestListener ) { const listener: http.RequestListener = (req, res) => { if (req.url) { - let pathname = removeBase(req.url); + let pathname: string | undefined = removeBase(req.url); pathname = pathname[0] === '/' ? pathname : '/' + pathname; - pathname = new URL(pathname, `http://${host}:${port}`).pathname; - const stream = send(req, encodeURI(decodeURI(pathname)), { + const encodedURI = parsePathname(pathname, host, port); + + if (!encodedURI) { + res.writeHead(400); + res.end('Bad request.'); + return res; + } + + const stream = send(req, encodedURI, { root: fileURLToPath(client), dotfiles: pathname.startsWith('/.well-known/') ? 'allow' : 'deny', }); |