@sssb/cdk 0.8.0 → 0.10.0
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 +89 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sssb/cdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
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.10.0"
|
|
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.10.0"
|
|
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
|
@@ -50,6 +50,13 @@ export function toolsVersionProblem(tools, own = OWN_VERSION) {
|
|
|
50
50
|
|
|
51
51
|
const TRANSPORTS = /** @type {const} */ (['pg', 'data-api'])
|
|
52
52
|
|
|
53
|
+
/**
|
|
54
|
+
* The Amazon CA bundle in the Lambda Node.js runtime (AWS Lambda's guide, "Using AWS Lambda with Amazon RDS": from
|
|
55
|
+
* Node.js 20 the runtime keeps the Amazon CA certificates, RDS's among them, in this file and no longer loads them).
|
|
56
|
+
* Both Lambdas are zip functions on that runtime (NODEJS_22_X), not container images, which need not have the file.
|
|
57
|
+
*/
|
|
58
|
+
const LAMBDA_CA_BUNDLE = '/var/runtime/ca-cert.pem'
|
|
59
|
+
|
|
53
60
|
/**
|
|
54
61
|
* The migrator entry, a published subpath of `@sssb/kit` (its `exports` map), resolved through that map from the
|
|
55
62
|
* app; and the framework's migration registry, which is not published, so it is named by its path inside the
|
|
@@ -201,14 +208,54 @@ function resolveMigrator(appDir) {
|
|
|
201
208
|
* @returns {{ Environment?: { Variables?: Record<string, unknown> }, Code?: { S3Bucket?: unknown, S3Key?: unknown } }}
|
|
202
209
|
*/
|
|
203
210
|
function renderedProperties(fn) {
|
|
204
|
-
|
|
211
|
+
return renderedCfnProperties(/** @type {any} */ (fn.node.defaultChild), fn.node.path)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* @param {any} cfn an L1 resource
|
|
216
|
+
* @param {string} [path] what to name it by in an error
|
|
217
|
+
* @returns {Record<string, any>}
|
|
218
|
+
*/
|
|
219
|
+
function renderedCfnProperties(cfn, path = cfn?.node?.path) {
|
|
205
220
|
if (typeof cfn?._toCloudFormation !== 'function') {
|
|
206
|
-
throw new Error(`SssbApp: cannot render ${
|
|
221
|
+
throw new Error(`SssbApp: cannot render ${path}'s CloudFormation to check it`)
|
|
207
222
|
}
|
|
208
|
-
const rendered = cdk.Stack.of(
|
|
223
|
+
const rendered = cdk.Stack.of(cfn).resolve(cfn._toCloudFormation())
|
|
209
224
|
return Object.values(rendered.Resources ?? {})[0]?.Properties ?? {}
|
|
210
225
|
}
|
|
211
226
|
|
|
227
|
+
/**
|
|
228
|
+
* The behaviors of a rendered distribution that reach the server without the forwarded-host function on the viewer
|
|
229
|
+
* request: the server believes the headers that function writes (`SSSB_EDGE`), so a behavior without it lets a viewer
|
|
230
|
+
* write the host and address it believes. A behavior reaches the server when its origin's domain is the Function URL's,
|
|
231
|
+
* or it targets an origin group with such an origin among its members.
|
|
232
|
+
* @param {any} config the rendered `DistributionConfig`
|
|
233
|
+
* @param {string} functionUrlRef the Function URL's resolved reference, as JSON
|
|
234
|
+
* @param {string} forwardedHostArn the function's resolved ARN, as JSON
|
|
235
|
+
* @returns {string[]}
|
|
236
|
+
*/
|
|
237
|
+
function unguardedServerBehaviors(config, functionUrlRef, forwardedHostArn) {
|
|
238
|
+
const serverOrigins = new Set(
|
|
239
|
+
(config?.Origins ?? [])
|
|
240
|
+
.filter((/** @type {any} */ o) => JSON.stringify(o?.DomainName ?? null).includes(functionUrlRef))
|
|
241
|
+
.map((/** @type {any} */ o) => o.Id),
|
|
242
|
+
)
|
|
243
|
+
for (const group of config?.OriginGroups?.Items ?? []) {
|
|
244
|
+
const members = (group?.Members?.Items ?? []).map((/** @type {any} */ m) => m?.OriginId)
|
|
245
|
+
if (members.some((/** @type {any} */ id) => serverOrigins.has(id))) serverOrigins.add(group.Id)
|
|
246
|
+
}
|
|
247
|
+
const behaviors = [config?.DefaultCacheBehavior, ...(config?.CacheBehaviors ?? [])]
|
|
248
|
+
return behaviors
|
|
249
|
+
.filter((b) => serverOrigins.has(b?.TargetOriginId))
|
|
250
|
+
.filter((b) => {
|
|
251
|
+
const viewerRequest = (b?.FunctionAssociations ?? []).filter(
|
|
252
|
+
(/** @type {any} */ a) => a?.EventType === 'viewer-request',
|
|
253
|
+
)
|
|
254
|
+
return viewerRequest.length !== 1 || JSON.stringify(viewerRequest[0].FunctionARN) !== forwardedHostArn
|
|
255
|
+
})
|
|
256
|
+
.map((b) => b?.PathPattern ?? 'the default behavior')
|
|
257
|
+
}
|
|
258
|
+
|
|
212
259
|
/**
|
|
213
260
|
* The code esbuild staged for a NodejsFunction during this synth, and problems with what it deploys: a `Code`
|
|
214
261
|
* other than the asset this synth staged (an escape-hatch override would deploy something the bundle check never
|
|
@@ -480,6 +527,14 @@ export class SssbApp extends Construct {
|
|
|
480
527
|
})
|
|
481
528
|
: undefined
|
|
482
529
|
|
|
530
|
+
// How both Lambdas protect their `pg` connection (the kit's `databaseSsl`): TLS that checks the certificate
|
|
531
|
+
// chains to Amazon RDS's roots and names the cluster endpoint. Those roots are in the Amazon CA bundle every
|
|
532
|
+
// Lambda Node.js runtime from 20 on carries at /var/runtime/ca-cert.pem (and loads no longer by default), so the
|
|
533
|
+
// adapter trusts that file for this one connection. Not a parameter: Aurora always offers TLS, so there is no
|
|
534
|
+
// deployment of this construct that should turn it off. On `data-api` neither applies: the SDK speaks HTTPS.
|
|
535
|
+
/** @type {Record<string, string>} */
|
|
536
|
+
const pgTlsEnv = { DATABASE_SSL_MODE: 'verify-full', DATABASE_SSL_ROOT_CERT: LAMBDA_CA_BUNDLE }
|
|
537
|
+
|
|
483
538
|
// The database half of each Lambda's environment (the framework's real adapters read it at boot).
|
|
484
539
|
// data-api builds no DATABASE_URL at all: the credential stays in Secrets Manager, where the Data
|
|
485
540
|
// API reads it, so the `db-credential-not-in-env` concern does not arise on that path.
|
|
@@ -493,6 +548,7 @@ export class SssbApp extends Construct {
|
|
|
493
548
|
}
|
|
494
549
|
: {
|
|
495
550
|
DATABASE_TRANSPORT: 'pg',
|
|
551
|
+
...pgTlsEnv,
|
|
496
552
|
// DRAFT: built via CFN dynamic references so the server
|
|
497
553
|
// can actually boot; revisit with the shared secrets constructs at cutover
|
|
498
554
|
// (env-var exposure vs runtime secret fetch).
|
|
@@ -545,6 +601,10 @@ export class SssbApp extends Construct {
|
|
|
545
601
|
// it is in no VPC. A run.sh that exports HOSTNAME overrides this; the deploy pipeline's must not.
|
|
546
602
|
HOSTNAME: '0.0.0.0',
|
|
547
603
|
APP_MODE: 'real',
|
|
604
|
+
// Every request reaches the server through the distribution, whose viewer-request function writes
|
|
605
|
+
// x-forwarded-host and x-sssb-viewer-address over what a viewer sent (src/forwarded-host.js): so
|
|
606
|
+
// @sssb/kit's publicOrigin and clientAddress may believe them. Nowhere else does anything say so.
|
|
607
|
+
SSSB_EDGE: 'cloudfront',
|
|
548
608
|
NEXT_TELEMETRY_DISABLED: '1',
|
|
549
609
|
...databaseEnv,
|
|
550
610
|
...(storageBucket && { STORAGE_BUCKET: storageBucket.bucketName }),
|
|
@@ -581,7 +641,12 @@ export class SssbApp extends Construct {
|
|
|
581
641
|
// which the synthesized secret does not carry (only the cluster's secret attachment would add it, at deploy).
|
|
582
642
|
environment: dataApi
|
|
583
643
|
? databaseEnv
|
|
584
|
-
: {
|
|
644
|
+
: {
|
|
645
|
+
DATABASE_TRANSPORT: 'pg',
|
|
646
|
+
...pgTlsEnv,
|
|
647
|
+
DB_SECRET_ARN: secret.secretArn,
|
|
648
|
+
DATABASE_NAME: databaseName,
|
|
649
|
+
},
|
|
585
650
|
bundling: {
|
|
586
651
|
format: aws_lambda_nodejs.OutputFormat.ESM,
|
|
587
652
|
// An ESM bundle has no `require`, and esbuild's stand-in for the CommonJS packages it inlines (`pg`,
|
|
@@ -681,7 +746,7 @@ export class SssbApp extends Construct {
|
|
|
681
746
|
functionName: accountUniqueName(),
|
|
682
747
|
runtime: aws_cloudfront.FunctionRuntime.JS_2_0,
|
|
683
748
|
code: aws_cloudfront.FunctionCode.fromInline(FORWARDED_HOST_SOURCE),
|
|
684
|
-
comment: `${slug}:
|
|
749
|
+
comment: `${slug}: forwarded host and port, and viewer address, set at the edge`,
|
|
685
750
|
})
|
|
686
751
|
|
|
687
752
|
// ---- the response's security headers (response headers policy) -----------------------
|
|
@@ -829,6 +894,25 @@ export class SssbApp extends Construct {
|
|
|
829
894
|
for (const name of ['DEMO_MODE', 'E2E_BUILD']) {
|
|
830
895
|
if (name in migratorEnvironment) problems.push(`the migrator's environment sets ${name}`)
|
|
831
896
|
}
|
|
897
|
+
// A behavior of the adopter's own that reaches the server must run the forwarded-host function too, on
|
|
898
|
+
// this distribution or on another of the stack's that has been given the Function URL (an origin with
|
|
899
|
+
// access control of its own). One in another stack names it by an import, which this does not follow.
|
|
900
|
+
const thisStack = cdk.Stack.of(this)
|
|
901
|
+
const functionUrlRef = JSON.stringify(
|
|
902
|
+
thisStack.resolve(/** @type {cdk.CfnResource} */ (functionUrl.node.defaultChild).logicalId),
|
|
903
|
+
)
|
|
904
|
+
const forwardedHostArn = JSON.stringify(thisStack.resolve(forwardedHost.functionArn))
|
|
905
|
+
for (const cfn of thisStack.node.findAll()) {
|
|
906
|
+
if (!(cfn instanceof aws_cloudfront.CfnDistribution)) continue
|
|
907
|
+
const config = renderedCfnProperties(cfn)['DistributionConfig']
|
|
908
|
+
const which = cfn === distribution.node.defaultChild ? '' : ` (on ${cfn.node.path})`
|
|
909
|
+
for (const path of unguardedServerBehaviors(config, functionUrlRef, forwardedHostArn)) {
|
|
910
|
+
problems.push(
|
|
911
|
+
`${path}${which} reaches the server without sssbApp.forwardedHost on viewer-request: a ` +
|
|
912
|
+
'viewer would write the host and address the server believes (SSSB_EDGE)',
|
|
913
|
+
)
|
|
914
|
+
}
|
|
915
|
+
}
|
|
832
916
|
if (cdk.Stack.of(this).bundlingRequired) {
|
|
833
917
|
const bundle = stagedBundle(migrator)
|
|
834
918
|
problems.push(
|