Skip to content

Commit ff448aa

Browse files
author
Mina Asham
committed
Add an optional parameter to enable wildcard in bucket name validation
1 parent 9951e4d commit ff448aa

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

packages/@aws-cdk/aws-s3/lib/bucket.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@ export class Bucket extends BucketBase {
15401540
if (!bucketName) {
15411541
throw new Error('Bucket name is required');
15421542
}
1543-
Bucket.validateBucketName(bucketName);
1543+
Bucket.validateBucketName(bucketName, true);
15441544

15451545
const newUrlFormat = attrs.bucketWebsiteNewUrlFormat === undefined
15461546
? false
@@ -1585,7 +1585,7 @@ export class Bucket extends BucketBase {
15851585
*
15861586
* @param physicalName name of the bucket.
15871587
*/
1588-
public static validateBucketName(physicalName: string): void {
1588+
public static validateBucketName(physicalName: string, allowWildcard: boolean = false): void {
15891589
const bucketName = physicalName;
15901590
if (!bucketName || Token.isUnresolved(bucketName)) {
15911591
// the name is a late-bound value, not a defined string,
@@ -1596,19 +1596,20 @@ export class Bucket extends BucketBase {
15961596
const errors: string[] = [];
15971597

15981598
// Rules codified from https://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html
1599-
if (bucketName.indexOf('*') == -1 && (bucketName.length < 3 || bucketName.length > 63)) {
1599+
if ((!allowWildcard || bucketName.indexOf('*') === -1) && (bucketName.length < 3 || bucketName.length > 63)) {
16001600
errors.push('Bucket name must be at least 3 and no more than 63 characters');
16011601
}
16021602
const charsetMatch = bucketName.match(/[^*a-z0-9.-]/);
16031603
if (charsetMatch) {
16041604
errors.push('Bucket name must only contain lowercase characters and the symbols, period (.) and dash (-) '
16051605
+ `(offset: ${charsetMatch.index})`);
16061606
}
1607-
if (!/[*a-z0-9]/.test(bucketName.charAt(0))) {
1607+
const allowedCharsStartEnd = allowWildcard ? /[*a-z0-9]/ : /[a-z0-9]/;
1608+
if (!allowedCharsStartEnd.test(bucketName.charAt(0))) {
16081609
errors.push('Bucket name must start and end with a lowercase character or number '
16091610
+ '(offset: 0)');
16101611
}
1611-
if (!/[*a-z0-9]/.test(bucketName.charAt(bucketName.length - 1))) {
1612+
if (!allowedCharsStartEnd.test(bucketName.charAt(bucketName.length - 1))) {
16121613
errors.push('Bucket name must start and end with a lowercase character or number '
16131614
+ `(offset: ${bucketName.length - 1})`);
16141615
}

0 commit comments

Comments
 (0)