Skip to content

Commit 2814011

Browse files
authored
fix(s3-assets): throw if path property is empty (#29425)
### Issue # (if applicable) Closes #29410. ### Reason for this change It was reported that a `Code.fromAsset('')` was creating an infinite loop by including itself through `cdk.out`. This is caused by the following line: https://github.com/aws/aws-cdk/blob/730fe63efc461c14f6e2b4aa9206c10f9b0f4cd9/packages/aws-cdk-lib/aws-s3-assets/lib/asset.ts#L145 If an empty string is given to `path.resolve()`, the current working directory is returned. ### Description of changes I've added a check that verifies that the given `path` property is not empty. ### Description of how you validated changes I've added a test for both the `aws-lambda` package, where the issue was originally reported, and `aws-s3-assets`, where the fix was implemented ### 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 069013e commit 2814011

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

packages/aws-cdk-lib/aws-lambda/test/code.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ describe('code', () => {
1616
});
1717

1818
describe('lambda.Code.fromAsset', () => {
19+
test('fails if path is empty', () => {
20+
// GIVEN
21+
const fileAsset = lambda.Code.fromAsset('');
22+
23+
// THEN
24+
expect(() => defineFunction(fileAsset)).toThrow(/Asset path cannot be empty/);
25+
});
26+
test('fails if path does not exist', () => {
27+
// GIVEN
28+
const fileAsset = lambda.Code.fromAsset('/path/not/found/' + Math.random() * 999999);
29+
30+
// THEN
31+
expect(() => defineFunction(fileAsset)).toThrow(/Cannot find asset/);
32+
});
1933
test('fails if a non-zip asset is used', () => {
2034
// GIVEN
2135
const fileAsset = lambda.Code.fromAsset(path.join(__dirname, 'my-lambda-handler', 'index.py'));

packages/aws-cdk-lib/aws-s3-assets/lib/asset.ts

+4
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ export class Asset extends Construct implements cdk.IAsset {
137137
constructor(scope: Construct, id: string, props: AssetProps) {
138138
super(scope, id);
139139

140+
if (!props.path) {
141+
throw new Error('Asset path cannot be empty');
142+
}
143+
140144
this.isBundled = props.bundling != null;
141145

142146
// stage the asset source (conditionally).

packages/aws-cdk-lib/aws-s3-assets/test/asset.test.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,18 @@ test('"readers" or "grantRead" can be used to grant read permissions on the asse
143143
});
144144
});
145145

146+
test('fails if path is empty', () => {
147+
const stack = new cdk.Stack();
148+
expect(() => new Asset(stack, 'MyDirectory', {
149+
path: '',
150+
})).toThrow(/Asset path cannot be empty/);
151+
});
152+
146153
test('fails if directory not found', () => {
147154
const stack = new cdk.Stack();
148155
expect(() => new Asset(stack, 'MyDirectory', {
149156
path: '/path/not/found/' + Math.random() * 999999,
150-
})).toThrow();
157+
})).toThrow(/Cannot find asset/);
151158
});
152159

153160
test('multiple assets under the same parent', () => {

0 commit comments

Comments
 (0)