@sssb/cdk 0.8.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/forwarded-host.js +11 -2
- package/src/index.d.ts +4 -2
- package/src/sssb-app.js +67 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sssb/cdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -26,11 +26,11 @@
|
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"aws-cdk-lib": "^2.274.0",
|
|
28
28
|
"constructs": "^10.8.1",
|
|
29
|
-
"@sssb/tools": "0.
|
|
29
|
+
"@sssb/tools": "0.9.1"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"aws-cdk-lib": "^2.274.0",
|
|
33
33
|
"constructs": "^10.8.1",
|
|
34
|
-
"@sssb/tools": "0.
|
|
34
|
+
"@sssb/tools": "0.9.1"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/src/forwarded-host.js
CHANGED
|
@@ -7,22 +7,31 @@
|
|
|
7
7
|
* and next-intl's domain redirects read it (Next's own `request.url` names the server's own address, `localhost` and
|
|
8
8
|
* its port, either way). A viewer could otherwise set it, and `x-forwarded-port` (next-intl's domain redirects read
|
|
9
9
|
* that too), to anything. So both are set here from the request CloudFront received, overwriting what the viewer sent:
|
|
10
|
-
* the `Host` it routed by (lowercased, any port dropped), and 443, the only port a viewer reaches the app on once every
|
|
10
|
+
* the `Host` it routed by (lowercased, any port and a fully-qualified name's trailing dot dropped), and 443, the only port a viewer reaches the app on once every
|
|
11
11
|
* behavior redirects HTTP to HTTPS. A viewer's RFC 7239 `Forwarded` is dropped, never being CloudFront's. `X-Forwarded-Proto` is CloudFront's: functions
|
|
12
12
|
* may not touch it, and it removes a viewer's (CloudFront's guide, "Request and response behavior for custom
|
|
13
13
|
* origins").
|
|
14
|
+
* The function also writes `x-sssb-viewer-address`, over any a viewer sent: the address CloudFront took from the
|
|
15
|
+
* connection (`event.viewer.ip`), an address no viewer can have written, unlike the first `X-Forwarded-For` entry. It is
|
|
16
|
+
* the header `@sssb/kit`'s `clientAddress` reads (the server is told to believe it by `SSSB_EDGE=cloudfront`,
|
|
17
|
+
* ./sssb-app.js), and it never reads `X-Forwarded-For`.
|
|
14
18
|
*/
|
|
15
19
|
export const FORWARDED_HOST_SOURCE = `function handler(event) {
|
|
16
20
|
var request = event.request;
|
|
17
21
|
var headers = request.headers;
|
|
18
22
|
delete headers['forwarded'];
|
|
19
|
-
var host = headers.host ? headers.host.value.toLowerCase().replace(/:[0-9]*$/, '') : '';
|
|
23
|
+
var host = headers.host ? headers.host.value.toLowerCase().replace(/:[0-9]*$/, '').replace(/\\.$/, '') : '';
|
|
20
24
|
if (host) {
|
|
21
25
|
headers['x-forwarded-host'] = { value: host };
|
|
22
26
|
} else {
|
|
23
27
|
delete headers['x-forwarded-host'];
|
|
24
28
|
}
|
|
25
29
|
headers['x-forwarded-port'] = { value: '443' };
|
|
30
|
+
if (event.viewer && event.viewer.ip) {
|
|
31
|
+
headers['x-sssb-viewer-address'] = { value: event.viewer.ip };
|
|
32
|
+
} else {
|
|
33
|
+
delete headers['x-sssb-viewer-address'];
|
|
34
|
+
}
|
|
26
35
|
return request;
|
|
27
36
|
}
|
|
28
37
|
`
|
package/src/index.d.ts
CHANGED
|
@@ -124,8 +124,10 @@ export declare class SssbApp extends Construct {
|
|
|
124
124
|
readonly distribution: cdk.aws_cloudfront.Distribution
|
|
125
125
|
/**
|
|
126
126
|
* The viewer-request CloudFront Function that gives the server the host the viewer asked for
|
|
127
|
-
* (`x-forwarded-host`, `x-forwarded-port`)
|
|
128
|
-
*
|
|
127
|
+
* (`x-forwarded-host`, `x-forwarded-port`) and the address they connect from (`x-sssb-viewer-address`),
|
|
128
|
+
* overwriting what the viewer sent. The server is told to believe them (`SSSB_EDGE=cloudfront`, read by
|
|
129
|
+
* `@sssb/kit`'s `publicOrigin` and `clientAddress`), so associate it with every behavior of your own that reaches
|
|
130
|
+
* the server: synth fails a behavior, of any distribution in this stack, that reaches it without the function.
|
|
129
131
|
*/
|
|
130
132
|
readonly forwardedHost: cdk.aws_cloudfront.Function
|
|
131
133
|
/**
|
package/src/sssb-app.js
CHANGED
|
@@ -201,14 +201,54 @@ function resolveMigrator(appDir) {
|
|
|
201
201
|
* @returns {{ Environment?: { Variables?: Record<string, unknown> }, Code?: { S3Bucket?: unknown, S3Key?: unknown } }}
|
|
202
202
|
*/
|
|
203
203
|
function renderedProperties(fn) {
|
|
204
|
-
|
|
204
|
+
return renderedCfnProperties(/** @type {any} */ (fn.node.defaultChild), fn.node.path)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* @param {any} cfn an L1 resource
|
|
209
|
+
* @param {string} [path] what to name it by in an error
|
|
210
|
+
* @returns {Record<string, any>}
|
|
211
|
+
*/
|
|
212
|
+
function renderedCfnProperties(cfn, path = cfn?.node?.path) {
|
|
205
213
|
if (typeof cfn?._toCloudFormation !== 'function') {
|
|
206
|
-
throw new Error(`SssbApp: cannot render ${
|
|
214
|
+
throw new Error(`SssbApp: cannot render ${path}'s CloudFormation to check it`)
|
|
207
215
|
}
|
|
208
|
-
const rendered = cdk.Stack.of(
|
|
216
|
+
const rendered = cdk.Stack.of(cfn).resolve(cfn._toCloudFormation())
|
|
209
217
|
return Object.values(rendered.Resources ?? {})[0]?.Properties ?? {}
|
|
210
218
|
}
|
|
211
219
|
|
|
220
|
+
/**
|
|
221
|
+
* The behaviors of a rendered distribution that reach the server without the forwarded-host function on the viewer
|
|
222
|
+
* request: the server believes the headers that function writes (`SSSB_EDGE`), so a behavior without it lets a viewer
|
|
223
|
+
* write the host and address it believes. A behavior reaches the server when its origin's domain is the Function URL's,
|
|
224
|
+
* or it targets an origin group with such an origin among its members.
|
|
225
|
+
* @param {any} config the rendered `DistributionConfig`
|
|
226
|
+
* @param {string} functionUrlRef the Function URL's resolved reference, as JSON
|
|
227
|
+
* @param {string} forwardedHostArn the function's resolved ARN, as JSON
|
|
228
|
+
* @returns {string[]}
|
|
229
|
+
*/
|
|
230
|
+
function unguardedServerBehaviors(config, functionUrlRef, forwardedHostArn) {
|
|
231
|
+
const serverOrigins = new Set(
|
|
232
|
+
(config?.Origins ?? [])
|
|
233
|
+
.filter((/** @type {any} */ o) => JSON.stringify(o?.DomainName ?? null).includes(functionUrlRef))
|
|
234
|
+
.map((/** @type {any} */ o) => o.Id),
|
|
235
|
+
)
|
|
236
|
+
for (const group of config?.OriginGroups?.Items ?? []) {
|
|
237
|
+
const members = (group?.Members?.Items ?? []).map((/** @type {any} */ m) => m?.OriginId)
|
|
238
|
+
if (members.some((/** @type {any} */ id) => serverOrigins.has(id))) serverOrigins.add(group.Id)
|
|
239
|
+
}
|
|
240
|
+
const behaviors = [config?.DefaultCacheBehavior, ...(config?.CacheBehaviors ?? [])]
|
|
241
|
+
return behaviors
|
|
242
|
+
.filter((b) => serverOrigins.has(b?.TargetOriginId))
|
|
243
|
+
.filter((b) => {
|
|
244
|
+
const viewerRequest = (b?.FunctionAssociations ?? []).filter(
|
|
245
|
+
(/** @type {any} */ a) => a?.EventType === 'viewer-request',
|
|
246
|
+
)
|
|
247
|
+
return viewerRequest.length !== 1 || JSON.stringify(viewerRequest[0].FunctionARN) !== forwardedHostArn
|
|
248
|
+
})
|
|
249
|
+
.map((b) => b?.PathPattern ?? 'the default behavior')
|
|
250
|
+
}
|
|
251
|
+
|
|
212
252
|
/**
|
|
213
253
|
* The code esbuild staged for a NodejsFunction during this synth, and problems with what it deploys: a `Code`
|
|
214
254
|
* other than the asset this synth staged (an escape-hatch override would deploy something the bundle check never
|
|
@@ -545,6 +585,10 @@ export class SssbApp extends Construct {
|
|
|
545
585
|
// it is in no VPC. A run.sh that exports HOSTNAME overrides this; the deploy pipeline's must not.
|
|
546
586
|
HOSTNAME: '0.0.0.0',
|
|
547
587
|
APP_MODE: 'real',
|
|
588
|
+
// Every request reaches the server through the distribution, whose viewer-request function writes
|
|
589
|
+
// x-forwarded-host and x-sssb-viewer-address over what a viewer sent (src/forwarded-host.js): so
|
|
590
|
+
// @sssb/kit's publicOrigin and clientAddress may believe them. Nowhere else does anything say so.
|
|
591
|
+
SSSB_EDGE: 'cloudfront',
|
|
548
592
|
NEXT_TELEMETRY_DISABLED: '1',
|
|
549
593
|
...databaseEnv,
|
|
550
594
|
...(storageBucket && { STORAGE_BUCKET: storageBucket.bucketName }),
|
|
@@ -681,7 +725,7 @@ export class SssbApp extends Construct {
|
|
|
681
725
|
functionName: accountUniqueName(),
|
|
682
726
|
runtime: aws_cloudfront.FunctionRuntime.JS_2_0,
|
|
683
727
|
code: aws_cloudfront.FunctionCode.fromInline(FORWARDED_HOST_SOURCE),
|
|
684
|
-
comment: `${slug}:
|
|
728
|
+
comment: `${slug}: forwarded host and port, and viewer address, set at the edge`,
|
|
685
729
|
})
|
|
686
730
|
|
|
687
731
|
// ---- the response's security headers (response headers policy) -----------------------
|
|
@@ -829,6 +873,25 @@ export class SssbApp extends Construct {
|
|
|
829
873
|
for (const name of ['DEMO_MODE', 'E2E_BUILD']) {
|
|
830
874
|
if (name in migratorEnvironment) problems.push(`the migrator's environment sets ${name}`)
|
|
831
875
|
}
|
|
876
|
+
// A behavior of the adopter's own that reaches the server must run the forwarded-host function too, on
|
|
877
|
+
// this distribution or on another of the stack's that has been given the Function URL (an origin with
|
|
878
|
+
// access control of its own). One in another stack names it by an import, which this does not follow.
|
|
879
|
+
const thisStack = cdk.Stack.of(this)
|
|
880
|
+
const functionUrlRef = JSON.stringify(
|
|
881
|
+
thisStack.resolve(/** @type {cdk.CfnResource} */ (functionUrl.node.defaultChild).logicalId),
|
|
882
|
+
)
|
|
883
|
+
const forwardedHostArn = JSON.stringify(thisStack.resolve(forwardedHost.functionArn))
|
|
884
|
+
for (const cfn of thisStack.node.findAll()) {
|
|
885
|
+
if (!(cfn instanceof aws_cloudfront.CfnDistribution)) continue
|
|
886
|
+
const config = renderedCfnProperties(cfn)['DistributionConfig']
|
|
887
|
+
const which = cfn === distribution.node.defaultChild ? '' : ` (on ${cfn.node.path})`
|
|
888
|
+
for (const path of unguardedServerBehaviors(config, functionUrlRef, forwardedHostArn)) {
|
|
889
|
+
problems.push(
|
|
890
|
+
`${path}${which} reaches the server without sssbApp.forwardedHost on viewer-request: a ` +
|
|
891
|
+
'viewer would write the host and address the server believes (SSSB_EDGE)',
|
|
892
|
+
)
|
|
893
|
+
}
|
|
894
|
+
}
|
|
832
895
|
if (cdk.Stack.of(this).bundlingRequired) {
|
|
833
896
|
const bundle = stagedBundle(migrator)
|
|
834
897
|
problems.push(
|