Skip to content

Commit e54b745

Browse files
committed
fix: enhance how we identify ECR images
instead of just checking for ".ecr.", adding a more complicated regular expression testing all the constant parts, as documented by AWS: https://docs.aws.amazon.com/AmazonECR/latest/userguide/ECR_on_EKS.html
1 parent 174e430 commit e54b745

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/scanner/images/credentials.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ export async function getSourceCredentials(imageSource: string): Promise<string
1111
}
1212

1313
export function isEcrSource(imageSource: string): boolean {
14-
// TODO is this the best way we can determine the image's source?
15-
return imageSource.indexOf('.ecr.') !== -1;
14+
// this regex tests the image source against the template:
15+
// <SOMETHING>.dkr.ecr.<SOMETHING>.amazonaws.com/<SOMETHING>
16+
const ecrImageRegex = new RegExp('\.dkr\.ecr\..*\.amazonaws\.com\/', 'i');
17+
return ecrImageRegex.test(imageSource);
1618
}
1719

1820
function getEcrCredentials(region: string): Promise<string> {

test/unit/scanner/image-registry-credentials.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@ tap.test('isEcrSource()', async (t) => {
2222
t.equals(sourceCredentialsForRandomImageName, false, 'unidentified image source is not ECR');
2323

2424
const sourceCredentialsForInvalidEcrImage = credentials.isEcrSource('derka.ecr.derka');
25-
t.equals(sourceCredentialsForInvalidEcrImage, true, 'image with .ecr. is considered ECR');
25+
t.equals(sourceCredentialsForInvalidEcrImage, false, 'image just with .ecr. is not considered from ECR');
2626

2727
const sourceCredentialsForEcrImage = credentials.isEcrSource('aws_account_id.dkr.ecr.region.amazonaws.com/my-web-app:latest');
28-
t.equals(sourceCredentialsForEcrImage, true, 'image with .ecr. is considered ECR');
28+
t.equals(sourceCredentialsForEcrImage, true, 'correct ECR template');
29+
30+
const sourceCredentialsForEcrImageWithRepo = credentials.isEcrSource('a291964488713.dkr.ecr.us-east-2.amazonaws.com/snyk/debian:10');
31+
t.equals(sourceCredentialsForEcrImageWithRepo, true, 'correct ECR template');
32+
33+
const sourceCredentialsForEcrImageMixedCase = credentials.isEcrSource('aws_account_id.dKr.ecR.region.amazonAWS.cOm/my-web-app:latest');
34+
t.equals(sourceCredentialsForEcrImageMixedCase, true, 'correct ECR template');
2935
});

0 commit comments

Comments
 (0)