Skip to content

Commit b6c790e

Browse files
done for booth bugs
1 parent a4bf451 commit b6c790e

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

internal/commands/scan.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,7 +2268,7 @@ func definePathForZipFileOrDirectory(cmd *cobra.Command) (zipFile, sourceDir str
22682268
return zipFile, sourceDir, err
22692269
}
22702270

2271-
// enforceLocalResolutionForTarFiles checks if any container image is a tar file
2271+
// enforceLocalResolutionForTarFiles checks if any container image is a tar file or oci-dir
22722272
// and enforces local resolution by setting the --containers-local-resolution flag.
22732273
// Container-security scan-type related function.
22742274
func enforceLocalResolutionForTarFiles(cmd *cobra.Command) error {
@@ -2289,7 +2289,7 @@ func enforceLocalResolutionForTarFiles(cmd *cobra.Command) error {
22892289

22902290
// Parse container images list
22912291
containerImagesList := strings.Split(strings.TrimSpace(containerImagesFlag), ",")
2292-
hasTarFile := false
2292+
needsLocalResolution := false
22932293

22942294
for _, containerImageName := range containerImagesList {
22952295
// Normalize input: trim spaces and quotes
@@ -2303,15 +2303,21 @@ func enforceLocalResolutionForTarFiles(cmd *cobra.Command) error {
23032303

23042304
// Check if this is a tar file by checking if it contains a tar file reference
23052305
if isTarFileReference(containerImageName) {
2306-
hasTarFile = true
2306+
needsLocalResolution = true
2307+
break
2308+
}
2309+
2310+
// Check if this is an oci-dir reference - these also require local resolution
2311+
if strings.HasPrefix(containerImageName, ociDirPrefix) {
2312+
needsLocalResolution = true
23072313
break
23082314
}
23092315
}
23102316

2311-
// If at least one tar file is found, enforce local resolution
2312-
if hasTarFile {
2313-
logger.PrintIfVerbose("Detected tar file(s) in --container-images flag")
2314-
fmt.Println("Warning: Tar file(s) detected in --container-images. Automatically enabling --containers-local-resolution flag.")
2317+
// If at least one tar file or oci-dir is found, enforce local resolution
2318+
if needsLocalResolution {
2319+
logger.PrintIfVerbose("Detected tar file(s) or oci-dir in --container-images flag")
2320+
fmt.Println("Warning: Tar file(s) or oci-dir detected in --container-images. Automatically enabling --containers-local-resolution flag.")
23152321

23162322
// Set the flag to true
23172323
err := cmd.Flags().Set(commonParams.ContainerResolveLocallyFlag, "true")
@@ -3828,13 +3834,19 @@ func validateOCIDirPrefix(imageRef string) error {
38283834
// 3. Can have optional :tag suffix
38293835

38303836
pathToCheck := imageRef
3831-
if strings.Contains(imageRef, ":") {
3837+
3838+
// Handle Windows absolute paths (e.g., C:\path\to\dir) before splitting on colons
3839+
// Windows paths have a drive letter followed by colon and path separator
3840+
if !isWindowsAbsolutePath(imageRef) && strings.Contains(imageRef, ":") {
38323841
// Handle case like "oci-dir:/path/to/dir:tag" or "oci-dir:name.tar:tag"
3842+
// For Unix paths, we can safely split on colon to extract the tag
38333843
pathParts := strings.Split(imageRef, ":")
38343844
if len(pathParts) > 0 && pathParts[0] != "" {
38353845
pathToCheck = pathParts[0]
38363846
}
38373847
}
3848+
// For Windows absolute paths, use the entire imageRef as pathToCheck
3849+
// since the colon is part of the drive letter (e.g., C:\path\to\dir)
38383850

38393851
exists, err := osinstaller.FileExists(pathToCheck)
38403852
if err != nil {

internal/commands/scan_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4515,7 +4515,7 @@ func TestIsTarFileReference(t *testing.T) {
45154515
}
45164516
}
45174517

4518-
// TestEnforceLocalResolutionForTarFiles tests the automatic enforcement of local resolution when tar files are detected.
4518+
// TestEnforceLocalResolutionForTarFiles tests the automatic enforcement of local resolution when tar files or oci-dir are detected.
45194519
// Container-security scan-type related test function.
45204520
func TestEnforceLocalResolutionForTarFiles(t *testing.T) {
45214521
testCases := []struct {
@@ -4530,15 +4530,20 @@ func TestEnforceLocalResolutionForTarFiles(t *testing.T) {
45304530
{"Already enabled", "alpine.tar", true, true, false},
45314531
{"Only image:tag", "nginx:latest,alpine:3.18", false, false, false},
45324532
{"Non-tar prefixes", "docker:nginx:latest,registry:ubuntu:22.04", false, false, false},
4533-
{"Invalid tar:tag format", "oci-dir:file.tar:latest", false, false, false},
45344533

4535-
// Should enable local resolution
4534+
// Should enable local resolution - tar files
45364535
{"Single tar", "alpine.tar", false, true, true},
45374536
{"Mixed tar+image", "nginx:latest,alpine.tar", false, true, true},
45384537
{"Tar with spaces/quotes", " 'alpine.tar' ,nginx:latest", false, true, true},
45394538
{"Prefixed tar", "docker-archive:alpine.tar", false, true, true},
45404539
{"oci-dir tar", "oci-dir:image.tar", false, true, true},
45414540
{"Tar at end", "nginx:latest,ubuntu.tar", false, true, true},
4541+
4542+
// Should enable local resolution - oci-dir directories
4543+
{"oci-dir directory", "oci-dir:my-alpine-image", false, true, true},
4544+
{"oci-dir with path", "oci-dir:/path/to/oci-layout", false, true, true},
4545+
{"oci-dir with tag suffix", "oci-dir:file.tar:latest", false, true, true},
4546+
{"Mixed oci-dir+image", "nginx:latest,oci-dir:my-image", false, true, true},
45424547
}
45434548

45444549
for _, tc := range testCases {
@@ -4578,7 +4583,7 @@ func TestEnforceLocalResolutionForTarFiles(t *testing.T) {
45784583
t.Errorf("Expected local resolution=%v, got=%v", tc.expectedLocalResolution, actualLocalResolution)
45794584
}
45804585

4581-
hasWarning := strings.Contains(output, "Warning:") && strings.Contains(output, "Tar file")
4586+
hasWarning := strings.Contains(output, "Warning:") && (strings.Contains(output, "Tar file") || strings.Contains(output, "oci-dir"))
45824587
if tc.expectWarning && !hasWarning {
45834588
t.Errorf("Expected warning but got: %s", output)
45844589
} else if !tc.expectWarning && hasWarning {

0 commit comments

Comments
 (0)