Skip to content

Commit 1e124f3

Browse files
Checkmarx Automationcx-anurag-dalke
authored andcommitted
Fix oci-dir validation to allow directories without tags
- Allow oci-dir: prefix to reference directories without requiring tags - Allow file:, docker-archive:, oci-archive: prefixes without tags - Add comprehensive test coverage for oci-dir validation - Fixes issue where skopeo-generated OCI directories were incorrectly rejected - Test cases cover: oci-dir without tag, with tag, with tar files, missing directories The OCI directory layout stores tag information internally, so requiring a tag in the CLI input is incorrect. This fix allows commands like: cx scan create --container-images "oci-dir:my-alpine-image" ... to work correctly with skopeo-generated OCI directories.
1 parent 93fbfd5 commit 1e124f3

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

internal/commands/scan.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3593,7 +3593,7 @@ func validateContainerImageFormat(containerImage string) error {
35933593
return nil // Valid image:tag format
35943594
}
35953595

3596-
// Step 3: No colon found - check if it's a tar file
3596+
// Step 3: No colon found - check if it's a tar file or special prefix that doesn't require tags
35973597
lowerInput := strings.ToLower(sanitizedInput)
35983598
if strings.HasSuffix(lowerInput, ".tar") {
35993599
// It's a tar file - check if it exists locally
@@ -3618,7 +3618,20 @@ func validateContainerImageFormat(containerImage string) error {
36183618
return errors.Errorf("--container-images flag error: image does not have a tag. Did you try to scan a tar file?")
36193619
}
36203620

3621-
// Step 4: Not a tar file and no colon - assume user tries to use image with tag (error)
3621+
// Step 4: Special handling for prefixes that don't require tags (e.g., oci-dir:)
3622+
if hasKnownSource {
3623+
prefix := getPrefixFromInput(containerImage, knownSources)
3624+
// oci-dir can reference directories without tags, validate it
3625+
if prefix == "oci-dir:" {
3626+
return validatePrefixedContainerImage(containerImage, prefix)
3627+
}
3628+
// Archive prefixes (file:, docker-archive:, oci-archive:) can reference files without tags
3629+
if prefix == "file:" || prefix == "docker-archive:" || prefix == "oci-archive:" {
3630+
return validatePrefixedContainerImage(containerImage, prefix)
3631+
}
3632+
}
3633+
3634+
// Step 5: Not a tar file, no special prefix, and no colon - assume user forgot to add tag (error)
36223635
return errors.Errorf("--container-images flag error: image does not have a tag")
36233636
}
36243637

internal/commands/scan_test.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,7 @@ func TestValidateContainerImageFormat_Comprehensive(t *testing.T) {
21952195
containerImage string
21962196
expectedError string
21972197
setupFiles []string
2198+
setupDirs []string
21982199
}{
21992200
// ==================== Basic Format Tests ====================
22002201
{
@@ -2409,6 +2410,37 @@ func TestValidateContainerImageFormat_Comprehensive(t *testing.T) {
24092410
expectedError: "image does not have a tag",
24102411
},
24112412

2413+
// ==================== OCI-Dir Tests ====================
2414+
{
2415+
name: "Valid oci-dir without tag",
2416+
containerImage: "oci-dir:my-alpine-image",
2417+
expectedError: "",
2418+
setupDirs: []string{"my-alpine-image"},
2419+
},
2420+
{
2421+
name: "Valid oci-dir with tag",
2422+
containerImage: "oci-dir:my-image:latest",
2423+
expectedError: "",
2424+
setupDirs: []string{"my-image"},
2425+
},
2426+
{
2427+
name: "Valid oci-dir with directory name",
2428+
containerImage: "oci-dir:oci-image-dir",
2429+
expectedError: "",
2430+
setupDirs: []string{"oci-image-dir"},
2431+
},
2432+
{
2433+
name: "Invalid oci-dir - directory does not exist",
2434+
containerImage: "oci-dir:nonexistent-dir",
2435+
expectedError: "--container-images flag error: path nonexistent-dir does not exist",
2436+
},
2437+
{
2438+
name: "Valid oci-dir with tar file",
2439+
containerImage: "oci-dir:image.tar",
2440+
expectedError: "",
2441+
setupFiles: []string{"image.tar"},
2442+
},
2443+
24122444
// ==================== Dir Prefix (Forbidden) ====================
24132445
{
24142446
name: "Invalid - dir prefix not supported",
@@ -2438,8 +2470,8 @@ func TestValidateContainerImageFormat_Comprehensive(t *testing.T) {
24382470
for _, tc := range testCases {
24392471
tc := tc
24402472
t.Run(tc.name, func(t *testing.T) {
2441-
// Setup test files if needed
2442-
cleanupFuncs := setupTestFilesAndDirs(t, tc.setupFiles, nil)
2473+
// Setup test files and directories if needed
2474+
cleanupFuncs := setupTestFilesAndDirs(t, tc.setupFiles, tc.setupDirs)
24432475
defer func() {
24442476
for _, cleanup := range cleanupFuncs {
24452477
cleanup()

0 commit comments

Comments
 (0)