Skip to content

Commit 91b8c67

Browse files
committed
feat: implement x-forwarded-host header
The SSR server now uses the x-forwarded-host header to determine the URL request origin (if set). It is still overridden by the ORIGIN environment variable (if set). The router will add the x-forwarded-host header before modifying the host header when forwarding onto the lambda URL origins.
1 parent 28f8ec3 commit 91b8c67

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ handler and lambda@edge router. They are defined as follows:
5858
### buildServer(builder, artifactPath, esbuildOptions) ⇒ [<code>Promise.&lt;SiteProps&gt;</code>](#SiteProps)
5959

6060
<p>Prepare SvelteKit SSR server files for deployment to AWS services.</p>
61-
<p>Note that the ORIGIN environment variable can be set to rewrite the URL
62-
request origin prior to rendering with sveltekit. This prevents CORS
63-
errors caused by redirects.</p>
61+
<p>To determine the URL request origin the server uses the following hierarchy:</p>
62+
<ul>
63+
<li>The ORIGIN environment variable</li>
64+
<li>The value of the 'X-Forwarded-Host' header</li>
65+
<li>The domain name within the request event</li>
66+
</ul>
67+
<p>The origin value is important to prevent CORS errors.</p>
6468

6569
**Kind**: global function
6670

@@ -89,6 +93,8 @@ errors caused by redirects.</p>
8993
### buildRouter(builder, static_directory, prerendered_directory, serverURL, optionsURL, artifactPath) ⇒ <code>Promise.&lt;string&gt;</code>
9094

9195
<p>Prepare lambda@edge origin router for deployment to AWS services</p>
96+
<p>Note that this function will forward the original Host header as
97+
'X-Forwarded-Host' to the lambda URLs.</p>
9298

9399
**Kind**: global function
94100
**Returns**: <code>Promise.&lt;string&gt;</code> - Location of files for the origin router

index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ type SiteProps = {
2727
/**
2828
* Prepare SvelteKit SSR server files for deployment to AWS services.
2929
*
30-
* Note that the ORIGIN environment variable can be set to rewrite the URL
31-
* request origin prior to rendering with sveltekit. This prevents CORS
32-
* errors caused by redirects.
30+
* To determine the URL request origin the server uses the following hierarchy:
31+
* + The ORIGIN environment variable
32+
* + The value of the 'X-Forwarded-Host' header
33+
* + The domain name within the request event
34+
*
35+
* The origin value is important to prevent CORS errors.
3336
*
3437
* @param {any} builder The SvelteKit provided [Builder]{@link https://kit.svelte.dev/docs/types#public-types-builder} object
3538
* @param {string} artifactPath The path where to place to SvelteKit files
@@ -134,6 +137,10 @@ export async function buildOptions(
134137

135138
/**
136139
* Prepare lambda@edge origin router for deployment to AWS services
140+
*
141+
* Note that this function will forward the original Host header as
142+
* 'X-Forwarded-Host' to the lambda URLs.
143+
*
137144
* @param {any} builder The SvelteKit provided [Builder]{@link https://kit.svelte.dev/docs/types#public-types-builder} object
138145
* @param {string} static_directory location of static page files
139146
* @param {string} prerendered_directory location of prerendered page files

lambda/router.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ async function performReWrite(uri, request, target) {
7676
customHeaders: {},
7777
},
7878
}
79+
request.headers['x-forwarded-host'] = [
80+
{
81+
key: 'X-Forwarded-Host',
82+
value: request.headers['host'][0].value,
83+
},
84+
]
7985
request.headers['host'] = [{ key: 'host', value: domainName }]
8086

8187
const searchParams = new URLSearchParams(request.querystring)

lambda/serverless.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ export async function handler(event, context) {
2323
headers['cookie'] = cookies.join('; ')
2424
}
2525

26+
const domainName =
27+
'x-forwarded-host' in headers
28+
? headers['x-forwarded-host']
29+
: requestContext.domainName
30+
2631
const origin =
27-
'ORIGIN' in process.env
28-
? process.env['ORIGIN']
29-
: `https://${requestContext.domainName}`
32+
'ORIGIN' in process.env ? process.env['ORIGIN'] : `https://${domainName}`
3033

3134
let rawURL = `${origin}${rawPath}${
3235
rawQueryString ? `?${rawQueryString}` : ''

0 commit comments

Comments
 (0)