Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): updates logical id validation regex #26251

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/core/lib/private/logical-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class LogicalIDs {
}
}

const VALID_LOGICALID_REGEX = /^[A-Za-z][A-Za-z0-9]{1,254}$/;
const VALID_LOGICALID_REGEX = /^[A-Za-z0-9]{1,255}$/;

/**
* Validate logical ID is valid for CloudFormation
Expand Down
27 changes: 26 additions & 1 deletion packages/aws-cdk-lib/core/test/logical-id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,25 @@ describe('logical id', () => {
expect(() => toCloudFormation(stack)).toThrow(/Logical ID must adhere to the regular expression/);
});

test('any alphaneumeric logical id is allowed', () => {
// GIVEN
const stack = new Stack();

// WHEN
const validLogicalIdAlpha = generateString(200);
const validLogicalIdNumber = generateNumberString(200);
new CfnResource(stack, validLogicalIdAlpha, { type: 'R' });
new CfnResource(stack, validLogicalIdNumber, { type: 'R' } );

// THEN
expect(toCloudFormation(stack)).toEqual({
Resources: {
[validLogicalIdAlpha]: { Type: 'R' },
[validLogicalIdNumber]: { Type: 'R' },
},
});
});

test('too large identifiers are truncated yet still remain unique', () => {
// GIVEN
const stack = new Stack();
Expand Down Expand Up @@ -253,7 +272,13 @@ describe('logical id', () => {
}).toThrow(/section 'Resources' already contains 'C'/);
});
});

function generateNumberString(chars: number) {
let s = '';
for (let i = 0; i < chars; ++i) {
s += Math.floor(Math.random() * 9);
}
return s;
}
function generateString(chars: number) {
let s = '';
for (let i = 0; i < chars; ++i) {
Expand Down