Skip to content

Commit a416a2d

Browse files
authored
fix(lambda): Validate Lambda "functionName" parameter (#17970)
Closes #13264 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7e6c70e commit a416a2d

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as kms from '@aws-cdk/aws-kms';
66
import * as logs from '@aws-cdk/aws-logs';
77
import * as sns from '@aws-cdk/aws-sns';
88
import * as sqs from '@aws-cdk/aws-sqs';
9-
import { Annotations, ArnFormat, CfnResource, Duration, Fn, Lazy, Names, Stack } from '@aws-cdk/core';
9+
import { Annotations, ArnFormat, CfnResource, Duration, Fn, Lazy, Names, Stack, Token } from '@aws-cdk/core';
1010
import { Construct } from 'constructs';
1111
import { Architecture } from './architecture';
1212
import { Code, CodeConfig } from './code';
@@ -627,6 +627,15 @@ export class Function extends FunctionBase {
627627
physicalName: props.functionName,
628628
});
629629

630+
if (props.functionName && !Token.isUnresolved(props.functionName)) {
631+
if (props.functionName.length > 64) {
632+
throw new Error(`Function name can not be longer than 64 characters but has ${props.functionName.length} characters.`);
633+
}
634+
if (!/^[a-zA-Z0-9-_]+$/.test(props.functionName)) {
635+
throw new Error(`Function name ${props.functionName} can contain only letters, numbers, hyphens, or underscores with no spaces.`);
636+
}
637+
}
638+
630639
const managedPolicies = new Array<iam.IManagedPolicy>();
631640

632641
// the arn is in the form of - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

packages/@aws-cdk/aws-lambda/test/function.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as sns from '@aws-cdk/aws-sns';
1212
import * as sqs from '@aws-cdk/aws-sqs';
1313
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
1414
import * as cdk from '@aws-cdk/core';
15+
import { Intrinsic, Token } from '@aws-cdk/core';
1516
import * as constructs from 'constructs';
1617
import * as _ from 'lodash';
1718
import * as lambda from '../lib';
@@ -2404,6 +2405,45 @@ describe('function', () => {
24042405
});
24052406
expect(fn.architecture?.name).toEqual('arm64');
24062407
});
2408+
2409+
test('Error when function name is longer than 64 chars', () => {
2410+
const stack = new cdk.Stack();
2411+
expect(() => new lambda.Function(stack, 'MyFunction', {
2412+
code: lambda.Code.fromInline('foo'),
2413+
runtime: lambda.Runtime.NODEJS_14_X,
2414+
handler: 'index.handler',
2415+
functionName: 'a'.repeat(65),
2416+
})).toThrow(/Function name can not be longer than 64 characters/);
2417+
});
2418+
2419+
test('Error when function name contains invalid characters', () => {
2420+
const stack = new cdk.Stack();
2421+
[' ', '\n', '\r', '[', ']', '<', '>', '$'].forEach(invalidChar => {
2422+
expect(() => {
2423+
new lambda.Function(stack, `foo${invalidChar}`, {
2424+
code: new lambda.InlineCode('foo'),
2425+
handler: 'index.handler',
2426+
runtime: lambda.Runtime.NODEJS_14_X,
2427+
functionName: `foo${invalidChar}`,
2428+
});
2429+
}).toThrow(/can contain only letters, numbers, hyphens, or underscores with no spaces./);
2430+
});
2431+
});
2432+
2433+
test('No error when function name is Tokenized and Unresolved', () => {
2434+
const stack = new cdk.Stack();
2435+
expect(() => {
2436+
const realFunctionName = 'a'.repeat(141);
2437+
const tokenizedFunctionName = Token.asString(new Intrinsic(realFunctionName));
2438+
2439+
new lambda.Function(stack, 'foo', {
2440+
code: new lambda.InlineCode('foo'),
2441+
handler: 'index.handler',
2442+
runtime: lambda.Runtime.NODEJS_14_X,
2443+
functionName: tokenizedFunctionName,
2444+
});
2445+
}).not.toThrow();
2446+
});
24072447
});
24082448

24092449
function newTestLambda(scope: constructs.Construct) {

0 commit comments

Comments
 (0)