From d5277df5a4d1e9a8a7b6c8d7b87912e13a163f7f Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Fri, 1 Mar 2024 11:26:28 +0100 Subject: [PATCH] fix(node): Safely create requests (#10285) * fix(node): Wrap request creation in try catch * chore: changeset --- .changeset/perfect-poets-teach.md | 5 +++++ packages/integrations/node/src/serve-app.ts | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 .changeset/perfect-poets-teach.md diff --git a/.changeset/perfect-poets-teach.md b/.changeset/perfect-poets-teach.md new file mode 100644 index 000000000000..2e91fb67852a --- /dev/null +++ b/.changeset/perfect-poets-teach.md @@ -0,0 +1,5 @@ +--- +"@astrojs/node": patch +--- + +Fixes an issue where malformed requests could cause the server to error in certain cases. diff --git a/packages/integrations/node/src/serve-app.ts b/packages/integrations/node/src/serve-app.ts index f2fc61f010d4..a9840b72148c 100644 --- a/packages/integrations/node/src/serve-app.ts +++ b/packages/integrations/node/src/serve-app.ts @@ -8,7 +8,15 @@ import type { RequestHandler } from './types.js'; */ export function createAppHandler(app: NodeApp): RequestHandler { return async (req, res, next, locals) => { - const request = NodeApp.createRequest(req); + let request; + try { + request = NodeApp.createRequest(req); + } catch (err) { + res.statusCode = 500; + res.end('Internal Server Error'); + return; + } + const routeData = app.match(request); if (routeData) { const response = await app.render(request, {