Skip to content

Commit 7803e06

Browse files
authored
chore(cloudfront): add validation for length of comment in cache policy (#31251)
### Issue # (if applicable) Closes #31248 . ### Reason for this change CDK doesn't validate the comment's length in the cache policy now. ### Description of changes Add validation for the length. ### Description of how you validated changes unit tests. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 04d73ac commit 7803e06

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

packages/aws-cdk-lib/aws-cloudfront/lib/cache-policy.ts

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export interface CachePolicyProps {
2626

2727
/**
2828
* A comment to describe the cache policy.
29+
*
30+
* The comment cannot be longer than 128 characters.
31+
*
2932
* @default - no comment
3033
*/
3134
readonly comment?: string;
@@ -149,6 +152,10 @@ export class CachePolicy extends Resource implements ICachePolicy {
149152
throw new Error(`'cachePolicyName' cannot be longer than 128 characters, got: '${cachePolicyName.length}'`);
150153
}
151154

155+
if (props.comment && !Token.isUnresolved(props.comment) && props.comment.length > 128) {
156+
throw new Error(`'comment' cannot be longer than 128 characters, got: ${props.comment.length}`);
157+
}
158+
152159
const minTtl = (props.minTtl ?? Duration.seconds(0)).toSeconds();
153160
let defaultTtl = (props.defaultTtl ?? Duration.days(1)).toSeconds();
154161
let maxTtl = (props.maxTtl ?? Duration.days(365)).toSeconds();

packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ describe('CachePolicy', () => {
108108
})).not.toThrow();
109109
});
110110

111+
test('throws on long comment over 128 characters', () => {
112+
const errorMessage = /'comment' cannot be longer than 128 characters, got: 129/;
113+
expect(() => new CachePolicy(stack, 'CachePolicy1', { comment: 'a'.repeat(129) })).toThrow(errorMessage);
114+
});
115+
111116
describe('TTLs', () => {
112117
test('default TTLs', () => {
113118
new CachePolicy(stack, 'CachePolicy', { cachePolicyName: 'MyPolicy' });

0 commit comments

Comments
 (0)