Skip to content

Commit

Permalink
chore(cloudfront): check size of the Cache Policy headers (aws#13425)
Browse files Browse the repository at this point in the history
Similar to aws#13410

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
robertd committed Mar 9, 2021
1 parent 960189c commit e08213f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ export class CacheHeaderBehavior {
if (headers.length === 0) {
throw new Error('At least one header to allow must be provided');
}
if (headers.length > 10) {
throw new Error(`Maximum allowed headers in Cache Policy is 10; got ${headers.length}.`);
}
return new CacheHeaderBehavior('whitelist', headers);
}

Expand Down
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-cloudfront/test/cache-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ describe('CachePolicy', () => {
expect(() => new CachePolicy(stack, 'CachePolicy6', { cachePolicyName: 'My_Policy' })).not.toThrow();
});

test('throws if more than 10 CacheHeaderBehavior headers are being passed', () => {
const errorMessage = /Maximum allowed headers in Cache Policy is 10; got (.*?)/;
expect(() => new CachePolicy(stack, 'CachePolicy1', {
headerBehavior: CacheHeaderBehavior.allowList('Lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit', 'sed', 'do', 'eiusmod'),
})).toThrow(errorMessage);

expect(() => new CachePolicy(stack, 'CachePolicy2', {
headerBehavior: CacheHeaderBehavior.allowList('Lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit', 'sed', 'do'),
})).not.toThrow();
});

test('does not throw if cachePolicyName is a token', () => {
expect(() => new CachePolicy(stack, 'CachePolicy', {
cachePolicyName: Aws.STACK_NAME,
Expand Down

0 comments on commit e08213f

Please sign in to comment.