Skip to content

Commit 7926560

Browse files
authored
feat(cloudfront): add denyList to OriginRequestPolicy behaviors (#25767)
Added static method `denyList` for * `OriginRequestCookieBehavior` * `OriginRequestHeaderBehavior` * `OriginRequestQueryStringBehavior` Due to recent CloudFormation update: the `allExcept` allowed behavior was added to * [`CookieBehavior`](awsdocs/aws-cloudformation-user-guide@a38f273#diff-630d84276f15d7dbe9836107b0c289d8692c9279ae10adacf34344273f28fcecR33) * [`HeaderBehavior`](awsdocs/aws-cloudformation-user-guide@a38f273#diff-83c67e21c489d688c4da6943452187182e96e8974f447bd3479044da752fe43bR34) * [`QueryStringBehavior`](awsdocs/aws-cloudformation-user-guide@a38f273#diff-96b632ead034b3554fb62969ffa46e799f53a1edfb3cfed5deba5df4d769aab1R34) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 9bcc6d5 commit 7926560

File tree

7 files changed

+71
-11
lines changed

7 files changed

+71
-11
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/integ-distribution-policies.assets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"version": "32.0.0",
33
"files": {
4-
"01042f10dd3272da413b201384cdf825a7467030c0db8a2d5bcfe10b45a30ced": {
4+
"17ab10021b77a5b225de54b10a02740bc6d638269786a643726871c66180f980": {
55
"source": {
66
"path": "integ-distribution-policies.template.json",
77
"packaging": "file"
88
},
99
"destinations": {
1010
"current_account-current_region": {
1111
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12-
"objectKey": "01042f10dd3272da413b201384cdf825a7467030c0db8a2d5bcfe10b45a30ced.json",
12+
"objectKey": "17ab10021b77a5b225de54b10a02740bc6d638269786a643726871c66180f980.json",
1313
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
1414
}
1515
}

packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/integ-distribution-policies.template.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@
6969
"Properties": {
7070
"OriginRequestPolicyConfig": {
7171
"CookiesConfig": {
72-
"CookieBehavior": "none"
72+
"CookieBehavior": "whitelist",
73+
"Cookies": [
74+
"cookie1"
75+
]
7376
},
7477
"HeadersConfig": {
7578
"HeaderBehavior": "allViewerAndWhitelistCloudFront",
@@ -79,7 +82,10 @@
7982
},
8083
"Name": "ACustomOriginRequestPolicy",
8184
"QueryStringsConfig": {
82-
"QueryStringBehavior": "none"
85+
"QueryStringBehavior": "allExcept",
86+
"QueryStrings": [
87+
"querystringparam"
88+
]
8389
}
8490
}
8591
}

packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"validateOnSynth": false,
1818
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
1919
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
20-
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/01042f10dd3272da413b201384cdf825a7467030c0db8a2d5bcfe10b45a30ced.json",
20+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/17ab10021b77a5b225de54b10a02740bc6d638269786a643726871c66180f980.json",
2121
"requiresBootstrapStackVersion": 6,
2222
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
2323
"additionalDependencies": [

packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/tree.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@
145145
"originRequestPolicyConfig": {
146146
"name": "ACustomOriginRequestPolicy",
147147
"cookiesConfig": {
148-
"cookieBehavior": "none"
148+
"cookieBehavior": "whitelist",
149+
"cookies": [
150+
"cookie1"
151+
]
149152
},
150153
"headersConfig": {
151154
"headerBehavior": "allViewerAndWhitelistCloudFront",
@@ -154,7 +157,10 @@
154157
]
155158
},
156159
"queryStringsConfig": {
157-
"queryStringBehavior": "none"
160+
"queryStringBehavior": "allExcept",
161+
"queryStrings": [
162+
"querystringparam"
163+
]
158164
}
159165
}
160166
}
@@ -244,7 +250,7 @@
244250
"path": "integ-distribution-policies/Dist/Origin1",
245251
"constructInfo": {
246252
"fqn": "constructs.Construct",
247-
"version": "10.2.26"
253+
"version": "10.2.52"
248254
}
249255
},
250256
"Resource": {
@@ -304,7 +310,7 @@
304310
"path": "integ-distribution-policies/Dist-2/Origin1",
305311
"constructInfo": {
306312
"fqn": "constructs.Construct",
307-
"version": "10.2.26"
313+
"version": "10.2.52"
308314
}
309315
},
310316
"Resource": {
@@ -388,7 +394,7 @@
388394
"path": "DistributionPolicies/DefaultTest/Default",
389395
"constructInfo": {
390396
"fqn": "constructs.Construct",
391-
"version": "10.2.26"
397+
"version": "10.2.52"
392398
}
393399
},
394400
"DeployAssert": {
@@ -434,7 +440,7 @@
434440
"path": "Tree",
435441
"constructInfo": {
436442
"fqn": "constructs.Construct",
437-
"version": "10.2.26"
443+
"version": "10.2.52"
438444
}
439445
}
440446
},

packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ const cachePolicyWithRef = new cloudfront.CachePolicy(stack, 'CachePolicyWithRef
3131

3232
const originRequestPolicy = new cloudfront.OriginRequestPolicy(stack, 'OriginRequestPolicy', {
3333
originRequestPolicyName: 'ACustomOriginRequestPolicy',
34+
cookieBehavior: cloudfront.OriginRequestCookieBehavior.allowList('cookie1'),
3435
headerBehavior: cloudfront.OriginRequestHeaderBehavior.all('CloudFront-Forwarded-Proto'),
36+
queryStringBehavior: cloudfront.OriginRequestQueryStringBehavior.denyList('querystringparam'),
3537
});
3638

3739
const responseHeadersPolicy = new cloudfront.ResponseHeadersPolicy(stack, 'ResponseHeadersPolicy', {

packages/aws-cdk-lib/aws-cloudfront/lib/origin-request-policy.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ export class OriginRequestCookieBehavior {
138138
/** All cookies in viewer requests are included in requests that CloudFront sends to the origin. */
139139
public static all() { return new OriginRequestCookieBehavior('all'); }
140140

141+
/** All cookies except the provided `cookies` are included in requests that CloudFront sends to the origin. */
142+
public static denyList(...cookies: string[]) {
143+
if (cookies.length === 0) {
144+
throw new Error('At least one cookie to deny must be provided');
145+
}
146+
return new OriginRequestCookieBehavior('allExcept', cookies);
147+
}
148+
141149
/** Only the provided `cookies` are included in requests that CloudFront sends to the origin. */
142150
public static allowList(...cookies: string[]) {
143151
if (cookies.length === 0) {
@@ -194,6 +202,14 @@ export class OriginRequestHeaderBehavior {
194202
return new OriginRequestHeaderBehavior('whitelist', headers);
195203
}
196204

205+
/** All headers except the provided `headers` are included in requests that CloudFront sends to the origin. */
206+
public static denyList(...headers: string[]) {
207+
if (headers.length === 0) {
208+
throw new Error('At least one header to deny must be provided');
209+
}
210+
return new OriginRequestHeaderBehavior('allExcept', headers);
211+
}
212+
197213
/** The behavior of headers: allow all, none or an allow list. */
198214
public readonly behavior: string;
199215
/** The headers for the allow list or the included CloudFront headers, if applicable. */
@@ -227,6 +243,14 @@ export class OriginRequestQueryStringBehavior {
227243
return new OriginRequestQueryStringBehavior('whitelist', queryStrings);
228244
}
229245

246+
/** All query strings except the provided `queryStrings` are included in requests that CloudFront sends to the origin. */
247+
public static denyList(...queryStrings: string[]) {
248+
if (queryStrings.length === 0) {
249+
throw new Error('At least one query string to deny must be provided');
250+
}
251+
return new OriginRequestQueryStringBehavior('allExcept', queryStrings);
252+
}
253+
230254
/** The behavior of query strings -- allow all, none, or only an allow list. */
231255
public readonly behavior: string;
232256
/** The query strings to allow, if the behavior is an allow list. */

packages/aws-cdk-lib/aws-cloudfront/test/origin-request-policy.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,17 @@ describe.each([
141141
test('allowList() throws if list is empty', () => {
142142
expect(() => clazz.allowList()).toThrow(new RegExp(`At least one ${type} to allow must be provided`));
143143
});
144+
145+
test('denyList()', () => {
146+
const behavior = clazz.denyList('SESSION_ID', 'secrets');
147+
148+
expect(behavior.behavior).toEqual('allExcept');
149+
expect(items(behavior)).toEqual(['SESSION_ID', 'secrets']);
150+
});
151+
152+
test('denyList() throws if list is empty', () => {
153+
expect(() => clazz.denyList()).toThrow(new RegExp(`At least one ${type} to deny must be provided`));
154+
});
144155
});
145156

146157
describe('HeaderBehavior', () => {
@@ -182,4 +193,15 @@ describe('HeaderBehavior', () => {
182193
expect(() => { OriginRequestHeaderBehavior.all('X-MyCustomHeader'); }).toThrow(errorMessage);
183194
});
184195
});
196+
197+
test('denyList()', () => {
198+
const headers = OriginRequestHeaderBehavior.denyList('SESSION_ID', 'secrets');
199+
200+
expect(headers.behavior).toEqual('allExcept');
201+
expect(headers.headers).toEqual(['SESSION_ID', 'secrets']);
202+
});
203+
204+
test('denyList() throws if list is empty', () => {
205+
expect(() => OriginRequestHeaderBehavior.denyList()).toThrow(/At least one header to deny must be provided/);
206+
});
185207
});

0 commit comments

Comments
 (0)