Skip to content

Commit

Permalink
fix: do not crash server if specified ADDRESS_HEADER is missing fro…
Browse files Browse the repository at this point in the history
…m a request (#10285)

This fixes #10284 by moving the check for the presence of the header specified in ADDRESS_HEADER inside the getClientAddress function. The old behavior is that non-prerendered requests without the appropriate header will instantly crash the server. The new behavior is that getClientAddress() will throw an exception, like with the other checks.
  • Loading branch information
Conduitry authored Jun 30, 2023
1 parent 9a83074 commit 33d8f7b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-brooms-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': patch
---

fix: do not crash server if specified `ADDRESS_HEADER` is missing from a request
16 changes: 8 additions & 8 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,20 @@ const ssr = async (req, res) => {
return;
}

if (address_header && !(address_header in req.headers)) {
throw new Error(
`Address header was specified with ${
ENV_PREFIX + 'ADDRESS_HEADER'
}=${address_header} but is absent from request`
);
}

setResponse(
res,
await server.respond(request, {
platform: { req },
getClientAddress: () => {
if (address_header) {
if (!(address_header in req.headers)) {
throw new Error(
`Address header was specified with ${
ENV_PREFIX + 'ADDRESS_HEADER'
}=${address_header} but is absent from request`
);
}

const value = /** @type {string} */ (req.headers[address_header]) || '';

if (address_header === 'x-forwarded-for') {
Expand Down

0 comments on commit 33d8f7b

Please sign in to comment.