Skip to content

Commit 765f441

Browse files
authored
fix(amplify): custom headers break with tokens (#20395)
`YAML.stringify` generates YAML with a fixed line length, splitting long strings. This can split the token string value on multiple lines making it unresolvable: `${Token[AWS.URLSuf\\\n fix.2]}` This can be the case with `Content-Security-Policy` headers with lots of directives and referencing API endpoints in the `connect-src` for example. Get rid of `YAML.stringify` and generate this "simple" YAML string "manually". ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 33b983c commit 765f441

File tree

11 files changed

+61
-45
lines changed

11 files changed

+61
-45
lines changed

Diff for: package.json

-4
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,8 @@
8080
"@aws-cdk/assertions-alpha/fs-extra/**",
8181
"@aws-cdk/assertions/fs-extra",
8282
"@aws-cdk/assertions/fs-extra/**",
83-
"@aws-cdk/aws-amplify-alpha/yaml",
84-
"@aws-cdk/aws-amplify-alpha/yaml/**",
8583
"@aws-cdk/aws-iot-actions-alpha/case",
8684
"@aws-cdk/aws-iot-actions-alpha/case/**",
87-
"@aws-cdk/aws-amplify/yaml",
88-
"@aws-cdk/aws-amplify/yaml/**",
8985
"@aws-cdk/aws-codebuild/yaml",
9086
"@aws-cdk/aws-codebuild/yaml/**",
9187
"@aws-cdk/aws-codepipeline-actions/case",

Diff for: packages/@aws-cdk/aws-amplify/NOTICE

-21
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,2 @@
11
AWS Cloud Development Kit (AWS CDK)
22
Copyright 2018-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
4-
-------------------------------------------------------------------------------
5-
6-
The AWS CDK includes the following third-party software/licensing:
7-
8-
** yaml - https://www.npmjs.com/package/yaml
9-
Copyright 2018 Eemeli Aro <[email protected]>
10-
11-
Permission to use, copy, modify, and/or distribute this software for any purpose
12-
with or without fee is hereby granted, provided that the above copyright notice
13-
and this permission notice appear in all copies.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
16-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17-
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
18-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
19-
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20-
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
21-
THIS SOFTWARE.
22-
23-
----------------

Diff for: packages/@aws-cdk/aws-amplify/lib/app.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as codebuild from '@aws-cdk/aws-codebuild';
22
import * as iam from '@aws-cdk/aws-iam';
33
import { IResource, Lazy, Resource, SecretValue } from '@aws-cdk/core';
44
import { Construct } from 'constructs';
5-
import * as YAML from 'yaml';
65
import { CfnApp } from './amplify.generated';
76
import { BasicAuth } from './basic-auth';
87
import { Branch, BranchOptions } from './branch';
@@ -515,11 +514,18 @@ export interface CustomResponseHeader {
515514
}
516515

517516
function renderCustomResponseHeaders(customHeaders: CustomResponseHeader[]): string {
518-
const modifiedHeaders = customHeaders.map(customHeader => ({
519-
...customHeader,
520-
headers: Object.entries(customHeader.headers).map(([key, value]) => ({ key, value })),
521-
}));
517+
const yaml = [
518+
'customHeaders:',
519+
];
520+
521+
for (const customHeader of customHeaders) {
522+
yaml.push(` - pattern: "${customHeader.pattern}"`);
523+
yaml.push(' headers:');
524+
for (const [key, value] of Object.entries(customHeader.headers)) {
525+
yaml.push(` - key: "${key}"`);
526+
yaml.push(` value: "${value}"`);
527+
}
528+
}
522529

523-
const customHeadersObject = { customHeaders: modifiedHeaders };
524-
return YAML.stringify(customHeadersObject);
530+
return `${yaml.join('\n')}\n`;
525531
}

Diff for: packages/@aws-cdk/aws-amplify/package.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
"@aws-cdk/cfn2ts": "0.0.0",
8888
"@aws-cdk/pkglint": "0.0.0",
8989
"@types/jest": "^27.5.0",
90-
"@types/yaml": "1.9.6",
9190
"aws-sdk": "^2.848.0"
9291
},
9392
"dependencies": {
@@ -101,12 +100,8 @@
101100
"@aws-cdk/aws-secretsmanager": "0.0.0",
102101
"@aws-cdk/core": "0.0.0",
103102
"@aws-cdk/custom-resources": "0.0.0",
104-
"constructs": "^3.3.69",
105-
"yaml": "1.10.2"
103+
"constructs": "^3.3.69"
106104
},
107-
"bundledDependencies": [
108-
"yaml"
109-
],
110105
"peerDependencies": {
111106
"@aws-cdk/aws-codebuild": "0.0.0",
112107
"@aws-cdk/aws-codecommit": "0.0.0",

Diff for: packages/@aws-cdk/aws-amplify/test/app.integ.snapshot/cdk-amplify-app.template.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,18 @@
5656
},
5757
"Username": "aws"
5858
},
59-
"CustomHeaders": "customHeaders:\n - pattern: \"*.json\"\n headers:\n - key: custom-header-name-1\n value: custom-header-value-1\n - key: custom-header-name-2\n value: custom-header-value-2\n - pattern: /path/*\n headers:\n - key: custom-header-name-1\n value: custom-header-value-2\n",
59+
"CustomHeaders": {
60+
"Fn::Join": [
61+
"",
62+
[
63+
"customHeaders:\n - pattern: \"*.json\"\n headers:\n - key: \"custom-header-name-1\"\n value: \"custom-header-value-1\"\n - key: \"custom-header-name-2\"\n value: \"custom-header-value-2\"\n - pattern: \"/path/*\"\n headers:\n - key: \"custom-header-name-1\"\n value: \"custom-header-value-2\"\n - key: \"x-aws-url-suffix\"\n value: \"this-is-the-suffix-",
64+
{
65+
"Ref": "AWS::URLSuffix"
66+
},
67+
"\"\n"
68+
]
69+
]
70+
},
6071
"CustomRules": [
6172
{
6273
"Source": "/source",
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"17.0.0"}
1+
{"version":"19.0.0"}

Diff for: packages/@aws-cdk/aws-amplify/test/app.integ.snapshot/integ.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"version": "18.0.0",
2+
"version": "19.0.0",
33
"testCases": {
4-
"aws-amplify/test/integ.app": {
4+
"integ.app": {
55
"stacks": [
66
"cdk-amplify-app"
77
],

Diff for: packages/@aws-cdk/aws-amplify/test/app.integ.snapshot/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "17.0.0",
2+
"version": "19.0.0",
33
"artifacts": {
44
"Tree": {
55
"type": "cdk:tree",

Diff for: packages/@aws-cdk/aws-amplify/test/app.integ.snapshot/tree.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,18 @@
113113
]
114114
}
115115
},
116-
"customHeaders": "customHeaders:\n - pattern: \"*.json\"\n headers:\n - key: custom-header-name-1\n value: custom-header-value-1\n - key: custom-header-name-2\n value: custom-header-value-2\n - pattern: /path/*\n headers:\n - key: custom-header-name-1\n value: custom-header-value-2\n",
116+
"customHeaders": {
117+
"Fn::Join": [
118+
"",
119+
[
120+
"customHeaders:\n - pattern: \"*.json\"\n headers:\n - key: \"custom-header-name-1\"\n value: \"custom-header-value-1\"\n - key: \"custom-header-name-2\"\n value: \"custom-header-value-2\"\n - pattern: \"/path/*\"\n headers:\n - key: \"custom-header-name-1\"\n value: \"custom-header-value-2\"\n - key: \"x-aws-url-suffix\"\n value: \"this-is-the-suffix-",
121+
{
122+
"Ref": "AWS::URLSuffix"
123+
},
124+
"\"\n"
125+
]
126+
]
127+
},
117128
"customRules": [
118129
{
119130
"source": "/source",

Diff for: packages/@aws-cdk/aws-amplify/test/app.test.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,28 @@ test('with custom headers', () => {
417417
'custom-header-name-1': 'custom-header-value-2',
418418
},
419419
},
420+
{
421+
pattern: '/with-tokens/*',
422+
headers: {
423+
'x-custom': `${'hello'.repeat(10)}${Stack.of(stack).urlSuffix} `,
424+
},
425+
},
420426
],
421427
});
422428

423429
// THEN
424430
Template.fromStack(stack).hasResourceProperties('AWS::Amplify::App', {
425-
CustomHeaders: 'customHeaders:\n - pattern: "*.json"\n headers:\n - key: custom-header-name-1\n value: custom-header-value-1\n - key: custom-header-name-2\n value: custom-header-value-2\n - pattern: /path/*\n headers:\n - key: custom-header-name-1\n value: custom-header-value-2\n',
431+
CustomHeaders: {
432+
'Fn::Join': [
433+
'',
434+
[
435+
'customHeaders:\n - pattern: "*.json"\n headers:\n - key: "custom-header-name-1"\n value: "custom-header-value-1"\n - key: "custom-header-name-2"\n value: "custom-header-value-2"\n - pattern: "/path/*"\n headers:\n - key: "custom-header-name-1"\n value: "custom-header-value-2"\n - pattern: "/with-tokens/*"\n headers:\n - key: "x-custom"\n value: "hellohellohellohellohellohellohellohellohellohello',
436+
{
437+
Ref: 'AWS::URLSuffix',
438+
},
439+
' "\n',
440+
],
441+
],
442+
},
426443
});
427444
});

Diff for: packages/@aws-cdk/aws-amplify/test/integ.app.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class TestStack extends Stack {
2121
pattern: '/path/*',
2222
headers: {
2323
'custom-header-name-1': 'custom-header-value-2',
24+
'x-aws-url-suffix': `this-is-the-suffix-${Stack.of(this).urlSuffix}`,
2425
},
2526
},
2627
],

0 commit comments

Comments
 (0)