Skip to content

Commit 0f40880

Browse files
authored
fix(lambda): validation for FunctionUrlCorsOptions.maxAge (#25495)
AWS::Lambda::Url's Cors.MaxAge has a maximum value of 86400 (secs) see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-url-cors.html#cfn-lambda-url-cors-maxage This PR adds validation for it. Note: No maximum value for S3, CloudFront (ResponseHeadersPolicy), and API Gateway. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent d60bf6f commit 0f40880

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

packages/aws-cdk-lib/aws-lambda/lib/function-url.ts

+4
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ export class FunctionUrl extends Resource implements IFunctionUrl {
229229
}
230230

231231
private renderCors(cors: FunctionUrlCorsOptions): CfnUrl.CorsProperty {
232+
if (cors.maxAge && !cors.maxAge.isUnresolved() && cors.maxAge.toSeconds() > 86400) {
233+
throw new Error(`FunctionUrl CORS maxAge should be less than or equal to 86400 secs (got ${cors.maxAge.toSeconds()})`);
234+
}
235+
232236
return {
233237
allowCredentials: cors.allowCredentials,
234238
allowHeaders: cors.allowedHeaders,

packages/aws-cdk-lib/aws-lambda/test/function-url.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,26 @@ describe('FunctionUrl', () => {
140140
}).toThrow(/FunctionUrl cannot be used with a Version/);
141141
});
142142

143+
test('throws when CORS maxAge is greater than 86400 secs', () => {
144+
// GIVEN
145+
const stack = new cdk.Stack();
146+
const fn = new lambda.Function(stack, 'MyLambda', {
147+
code: new lambda.InlineCode('hello()'),
148+
handler: 'index.hello',
149+
runtime: lambda.Runtime.NODEJS_14_X,
150+
});
151+
152+
// WHEN
153+
expect(() => {
154+
new lambda.FunctionUrl(stack, 'FunctionUrl', {
155+
function: fn,
156+
cors: {
157+
maxAge: cdk.Duration.seconds(86401),
158+
},
159+
});
160+
}).toThrow(/FunctionUrl CORS maxAge should be less than or equal to 86400 secs/);
161+
});
162+
143163
test('grantInvokeUrl: adds appropriate permissions', () => {
144164
// GIVEN
145165
const stack = new cdk.Stack();

0 commit comments

Comments
 (0)