Skip to content

Commit 534794c

Browse files
authored
feat(cognito): validate oidc provider name (#28802)
Closes #28667. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 9e21803 commit 534794c

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

packages/aws-cdk-lib/aws-cognito/lib/user-pool-idps/oidc.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ export class UserPoolIdentityProviderOidc extends UserPoolIdentityProviderBase {
107107
constructor(scope: Construct, id: string, props: UserPoolIdentityProviderOidcProps) {
108108
super(scope, id, props);
109109

110-
if (props.name && !Token.isUnresolved(props.name) && (props.name.length < 3 || props.name.length > 32)) {
111-
throw new Error(`Expected provider name to be between 3 and 32 characters, received ${props.name} (${props.name.length} characters)`);
112-
}
113-
114110
const scopes = props.scopes ?? ['openid'];
115111

116112
const resource = new CfnUserPoolIdentityProvider(this, 'Resource', {
@@ -140,6 +136,11 @@ export class UserPoolIdentityProviderOidc extends UserPoolIdentityProviderBase {
140136
if (!Token.isUnresolved(name) && (name.length < 3 || name.length > 32)) {
141137
throw new Error(`Expected provider name to be between 3 and 32 characters, received ${name} (${name.length} characters)`);
142138
}
139+
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpoolidentityprovider.html#cfn-cognito-userpoolidentityprovider-providername
140+
// u is for unicode
141+
if (!name.match(/^[^_\p{Z}][\p{L}\p{M}\p{S}\p{N}\p{P}][^_\p{Z}]+$/u)) {
142+
throw new Error(`Expected provider name must match [^_\p{Z}][\p{L}\p{M}\p{S}\p{N}\p{P}][^_\p{Z}]+, received ${name}`);
143+
}
143144
return name;
144145
}
145146

packages/aws-cdk-lib/aws-cognito/test/user-pool-idps/oidc.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,22 @@ describe('UserPoolIdentityProvider', () => {
173173
})).toThrow(/Expected provider name to be between 3 and 32 characters/);
174174
});
175175

176+
test('throws with provider name that doesn\'t match pattern', () => {
177+
// GIVEN
178+
const stack = new Stack();
179+
const pool = new UserPool(stack, 'userpool');
180+
const name = ' thisisabadname';
181+
182+
// THEN
183+
expect(() => new UserPoolIdentityProviderOidc(stack, 'userpoolidp', {
184+
userPool: pool,
185+
name,
186+
clientId: 'client-id',
187+
clientSecret: 'client-secret',
188+
issuerUrl: 'https://my-issuer-url.com',
189+
})).toThrow(`Expected provider name must match [^_\p{Z}][\p{L}\p{M}\p{S}\p{N}\p{P}][^_\p{Z}]+, received ${name}`);
190+
});
191+
176192
test('generates a valid name when unique id is too short', () => {
177193
// GIVEN
178194
const stack = new Stack();

0 commit comments

Comments
 (0)