Skip to content

Commit cbe5848

Browse files
author
Checkmarx Automation
committed
Fix vendor library panic by adding default tags to file paths
- Add isFilePath() function to detect file paths vs image references - Automatically append ':latest' tag to file paths without tags - Prevents 'index out of range' panic in containers-syft-packages-extractor - Handles file extensions: .tar, .tar.gz, .tgz and paths with / or - Preserves existing tags when present (e.g., 'file.tar:v1.0' unchanged) WORKAROUND for vendor library bug where it expects image:tag format but file paths don't naturally have tags. Resolves AST-108903 panic issue.
1 parent f93a832 commit cbe5848

2 files changed

Lines changed: 29 additions & 13 deletions

File tree

internal/commands/scan.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,18 +2050,37 @@ func processContainerImagesForSyft(images []string) []string {
20502050
// Use the same scheme extraction logic as syft/stereoscope
20512051
source, strippedInput := extractSchemeSource(image, knownSources)
20522052

2053+
var processedImage string
20532054
if source != "" {
20542055
// Valid scheme found - use the stripped input (like syft does)
2055-
processedImages = append(processedImages, strippedInput)
2056+
processedImage = strippedInput
20562057
} else {
20572058
// No valid scheme - pass the original input unchanged
2058-
processedImages = append(processedImages, image)
2059+
processedImage = image
20592060
}
2061+
2062+
// WORKAROUND: Add default tag for file paths to prevent vendor library panic
2063+
// The containers-syft-packages-extractor expects image:tag format but files don't have tags
2064+
if isFilePath(processedImage) && !strings.Contains(processedImage, ":") {
2065+
processedImage = processedImage + ":latest"
2066+
}
2067+
2068+
processedImages = append(processedImages, processedImage)
20602069
}
20612070

20622071
return processedImages
20632072
}
20642073

2074+
// isFilePath determines if a string looks like a file path rather than an image reference
2075+
func isFilePath(input string) bool {
2076+
// Check for common file indicators
2077+
return strings.HasSuffix(input, ".tar") ||
2078+
strings.HasSuffix(input, ".tar.gz") ||
2079+
strings.HasSuffix(input, ".tgz") ||
2080+
strings.Contains(input, "/") ||
2081+
strings.Contains(input, "\\")
2082+
}
2083+
20652084
// extractSchemeSource mimics stereoscope.ExtractSchemeSource behavior
20662085
func extractSchemeSource(userInput string, sources []string) (source, newInput string) {
20672086
const SchemeSeparator = ":"
@@ -3494,13 +3513,9 @@ func validatePrefixedContainerImage(containerImage, prefix string) error {
34943513
}
34953514

34963515
func validateTraditionalContainerImage(containerImage string) error {
3497-
// Handle legacy .tar file format
3516+
// Handle .tar file format (both with and without paths, like syft)
34983517
if strings.HasSuffix(containerImage, ".tar") {
3499-
// Check if this looks like a file path that should use a prefix
3500-
if strings.Contains(containerImage, "/") || strings.Contains(containerImage, "\\") {
3501-
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)
3502-
}
3503-
3518+
// Accept any .tar file path, with or without directories (like syft does)
35043519
exists, err := osinstaller.FileExists(containerImage)
35053520
if err != nil {
35063521
return errors.Errorf("--container-images flag error: %v", err)

internal/commands/scan_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,14 +2200,15 @@ func TestValidateContainerImageFormat(t *testing.T) {
22002200
setupFiles: []string{"nginx.tar"},
22012201
},
22022202
{
2203-
name: "Invalid tar file with path - suggests file prefix",
2203+
name: "Valid tar file with path (like syft)",
22042204
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'"),
2205+
expectedError: nil,
2206+
setupFiles: []string{"empty/alpine.tar"},
22062207
},
22072208
{
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'"),
2209+
name: "Invalid tar file with path - file does not exist",
2210+
containerImage: "nonexistent/alpine.tar",
2211+
expectedError: errors.New("--container-images flag error: file 'nonexistent/alpine.tar' does not exist"),
22112212
},
22122213
{
22132214
name: "Missing image name",

0 commit comments

Comments
 (0)