Skip to content

Commit 5d707e2

Browse files
author
Checkmarx Automation
committed
Improve container-images validation error messages
- Add helpful error message for .tar files with paths suggesting file: prefix - Add specific filenames to file existence error messages - Detect when users input file paths without proper prefix format - Prevent customer confusion about format requirements Examples: - 'empty/alpine.tar' → suggests 'file:empty/alpine.tar' - 'file:missing.tar' → shows 'file missing.tar does not exist' (not just 'file does not exist') This addresses customer usability issues and makes error messages more actionable.
1 parent 7b44fb2 commit 5d707e2

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

internal/commands/scan.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3363,7 +3363,7 @@ func validatePrefixedContainerImage(containerImage, prefix string) error {
33633363
return errors.Errorf("--container-images flag error: %v", err)
33643364
}
33653365
if !exists {
3366-
return errors.Errorf("--container-images flag error: file does not exist")
3366+
return errors.Errorf("--container-images flag error: file '%s' does not exist", imageRef)
33673367
}
33683368
return nil
33693369
}
@@ -3401,7 +3401,7 @@ func validatePrefixedContainerImage(containerImage, prefix string) error {
34013401
return errors.Errorf("--container-images flag error: %v", err)
34023402
}
34033403
if !exists {
3404-
return errors.Errorf("--container-images flag error: file does not exist")
3404+
return errors.Errorf("--container-images flag error: file '%s' does not exist", imageRef)
34053405
}
34063406
return nil
34073407
}
@@ -3445,12 +3445,17 @@ func validatePrefixedContainerImage(containerImage, prefix string) error {
34453445
func validateTraditionalContainerImage(containerImage string) error {
34463446
// Handle legacy .tar file format
34473447
if strings.HasSuffix(containerImage, ".tar") {
3448+
// Check if this looks like a file path that should use a prefix
3449+
if strings.Contains(containerImage, "/") || strings.Contains(containerImage, "\\") {
3450+
return errors.Errorf("Invalid value for --container-images flag. The value '%s' appears to be a file path. For file-based scanning, use the 'file:' prefix: 'file:%s'", containerImage, containerImage)
3451+
}
3452+
34483453
exists, err := osinstaller.FileExists(containerImage)
34493454
if err != nil {
34503455
return errors.Errorf("--container-images flag error: %v", err)
34513456
}
34523457
if !exists {
3453-
return errors.Errorf("--container-images flag error: file does not exist")
3458+
return errors.Errorf("--container-images flag error: file '%s' does not exist", containerImage)
34543459
}
34553460
return nil
34563461
}

internal/commands/scan_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,6 +2199,16 @@ func TestValidateContainerImageFormat(t *testing.T) {
21992199
expectedError: nil,
22002200
setupFiles: []string{"nginx.tar"},
22012201
},
2202+
{
2203+
name: "Invalid tar file with path - suggests file prefix",
2204+
containerImage: "empty/alpine.tar",
2205+
expectedError: errors.New("Invalid value for --container-images flag. The value 'empty/alpine.tar' appears to be a file path. For file-based scanning, use the 'file:' prefix: 'file:empty/alpine.tar'"),
2206+
},
2207+
{
2208+
name: "Invalid tar file with absolute path - suggests file prefix",
2209+
containerImage: "/path/to/image.tar",
2210+
expectedError: errors.New("Invalid value for --container-images flag. The value '/path/to/image.tar' appears to be a file path. For file-based scanning, use the 'file:' prefix: 'file:/path/to/image.tar'"),
2211+
},
22022212
{
22032213
name: "Missing image name",
22042214
containerImage: ":latest",
@@ -2284,7 +2294,7 @@ func TestValidateContainerImageFormat(t *testing.T) {
22842294
{
22852295
name: "Invalid docker archive format - non-existent file",
22862296
containerImage: "docker-archive:nonexistent.tar",
2287-
expectedError: errors.New("--container-images flag error: file does not exist"),
2297+
expectedError: errors.New("--container-images flag error: file 'nonexistent.tar' does not exist"),
22882298
},
22892299

22902300
// OCI archive prefix tests
@@ -2303,7 +2313,7 @@ func TestValidateContainerImageFormat(t *testing.T) {
23032313
{
23042314
name: "Invalid oci archive format - non-existent file",
23052315
containerImage: "oci-archive:nonexistent.tar",
2306-
expectedError: errors.New("--container-images flag error: file does not exist"),
2316+
expectedError: errors.New("--container-images flag error: file 'nonexistent.tar' does not exist"),
23072317
},
23082318

23092319
// OCI directory prefix tests (matches Syft behavior)
@@ -2366,7 +2376,7 @@ func TestValidateContainerImageFormat(t *testing.T) {
23662376
{
23672377
name: "Invalid file format - non-existent file",
23682378
containerImage: "file:nonexistent.file",
2369-
expectedError: errors.New("--container-images flag error: file does not exist"),
2379+
expectedError: errors.New("--container-images flag error: file 'nonexistent.file' does not exist"),
23702380
},
23712381

23722382
// Registry prefix tests (restricted to single images only)

0 commit comments

Comments
 (0)