Skip to content

Commit d9468c5

Browse files
fix: stack account id throws error if not a string (#25134)
If the account id passed in as stack environment is not a string, then that leads to errors in some lookup functions. This PR is adding a type check for stack's account id and region. These are only allowed to be `string` datatype. Closes #21364 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 4eb5e99 commit d9468c5

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

packages/aws-cdk-lib/core/lib/stack.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,15 @@ export class Stack extends Construct implements ITaggable {
13581358
// in which Export/Fn::ImportValue would work is if the value are the same
13591359
// between producer and consumer anyway, so we can just assume that they are).
13601360
const containingAssembly = Stage.of(this);
1361+
1362+
if (env.account && typeof(env.account) !== 'string') {
1363+
throw new Error(`Account id of stack environment must be a 'string' but received '${typeof(env.account)}'`);
1364+
}
1365+
1366+
if (env.region && typeof(env.region) !== 'string') {
1367+
throw new Error(`Region of stack environment must be a 'string' but received '${typeof(env.region)}'`);
1368+
}
1369+
13611370
const account = env.account ?? containingAssembly?.account ?? Aws.ACCOUNT_ID;
13621371
const region = env.region ?? containingAssembly?.region ?? Aws.REGION;
13631372

packages/aws-cdk-lib/core/test/stack.test.ts

+36-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
2-
import * as cxapi from '../../cx-api';
3-
import { Fact, RegionInfo } from '../../region-info';
42
import { Construct, Node } from 'constructs';
53
import { toCloudFormation } from './util';
4+
import * as cxapi from '../../cx-api';
5+
import { Fact, RegionInfo } from '../../region-info';
66
import {
77
App, CfnCondition, CfnInclude, CfnOutput, CfnParameter,
88
CfnResource, Lazy, ScopedAws, Stack, validateString,
@@ -2071,6 +2071,40 @@ describe('stack', () => {
20712071
stack.node.setContext(cxapi.BUNDLING_STACKS, []);
20722072
expect(stack.bundlingRequired).toBe(false);
20732073
});
2074+
2075+
test('account id passed in stack environment must be a string', () => {
2076+
// GIVEN
2077+
const envConfig: any = {
2078+
account: 11111111111,
2079+
};
2080+
2081+
// WHEN
2082+
const app = new App();
2083+
2084+
// THEN
2085+
expect(() => {
2086+
new Stack(app, 'Stack', {
2087+
env: envConfig,
2088+
});
2089+
}).toThrowError('Account id of stack environment must be a \'string\' but received \'number\'');
2090+
});
2091+
2092+
test('region passed in stack environment must be a string', () => {
2093+
// GIVEN
2094+
const envConfig: any = {
2095+
region: 2,
2096+
};
2097+
2098+
// WHEN
2099+
const app = new App();
2100+
2101+
// THEN
2102+
expect(() => {
2103+
new Stack(app, 'Stack', {
2104+
env: envConfig,
2105+
});
2106+
}).toThrowError('Region of stack environment must be a \'string\' but received \'number\'');
2107+
});
20742108
});
20752109

20762110
describe('permissions boundary', () => {

0 commit comments

Comments
 (0)