Skip to content

Commit aa0ceda

Browse files
bfangerBob FangerbenmccannRich-Harris
authored
fix: fallback to Host header when the custom HOST_HEADER is not in the request (#11154)
Co-authored-by: Bob Fanger <[email protected]> Co-authored-by: Ben McCann <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent 3bbf4cb commit aa0ceda

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

.changeset/spotty-yaks-lie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-node': patch
3+
---
4+
5+
fix: fallback to `host` header if header specified by `HOST_HEADER` is not in request headers

packages/adapter-node/src/handler.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const origin = env('ORIGIN', undefined);
1919
const xff_depth = parseInt(env('XFF_DEPTH', '1'));
2020
const address_header = env('ADDRESS_HEADER', '').toLowerCase();
2121
const protocol_header = env('PROTOCOL_HEADER', '').toLowerCase();
22-
const host_header = env('HOST_HEADER', 'host').toLowerCase();
22+
const host_header = env('HOST_HEADER', '').toLowerCase();
2323
const port_header = env('PORT_HEADER', '').toLowerCase();
2424

2525
const body_size_limit = parse_as_bytes(env('BODY_SIZE_LIMIT', '512K'));
@@ -182,14 +182,11 @@ function sequence(handlers) {
182182
* @returns
183183
*/
184184
function get_origin(headers) {
185-
const protocol = (protocol_header && headers[protocol_header]) || 'https';
186-
const host = headers[host_header];
185+
const protocol = (protocol_header && headers[protocol_header]) ?? 'https';
186+
const host = (host_header && headers[host_header]) ?? headers['host'];
187187
const port = port_header && headers[port_header];
188-
if (port) {
189-
return `${protocol}://${host}:${port}`;
190-
} else {
191-
return `${protocol}://${host}`;
192-
}
188+
189+
return port ? `${protocol}://${host}:${port}` : `${protocol}://${host}`;
193190
}
194191

195192
export const handler = sequence(

0 commit comments

Comments
 (0)