Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"

"github.com/chainguard-dev/clog"
Expand Down Expand Up @@ -118,10 +119,27 @@ func cli() *cobra.Command {

// If custom mappings file is provided, load it as ExtraMappings
if mappingsFile != "" {
log.Info("Loading custom mappings file", "file", mappingsFile)
mappingsBytes, err := os.ReadFile(mappingsFile)
cleanedPath := filepath.Clean(mappingsFile)

// Reject empty path
if cleanedPath == "" || cleanedPath == "." {
return fmt.Errorf("mappings file path is empty or invalid: %q", cleanedPath)
}

// Prevents directory traversal
if strings.Contains(cleanedPath, "..") {
return fmt.Errorf("invalid mappings file path: %q", cleanedPath)
}

// Require .yaml or .yml extension
ext := filepath.Ext(cleanedPath)
if ext != ".yaml" && ext != ".yml" {
return fmt.Errorf("mappings file must be .yaml or .yml, got %q", ext)
}
log.Info("Loading custom mappings file", "file", cleanedPath)
mappingsBytes, err := os.ReadFile(cleanedPath)
if err != nil {
return fmt.Errorf("reading mappings file %s: %w", mappingsFile, err)
return fmt.Errorf("reading mappings file %s: %w", cleanedPath, err)
}

var extraMappings dfc.MappingsConfig
Expand Down