Skip to content

Commit

Permalink
Try to sanitize filename on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
osterman committed Dec 24, 2024
1 parent ac1a504 commit 0ba32ea
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion internal/exec/vendor_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"
"path/filepath"
"runtime"
"sort"
"strings"

Expand Down Expand Up @@ -514,6 +515,34 @@ func determineSourceType(uri *string, vendorConfigFilePath string) (bool, bool,
return useOciScheme, useLocalFileSystem, sourceIsLocalFile
}

// sanitizeFileName replaces invalid characters and query strings with underscores for Windows.
func sanitizeFileName(uri string) string {
// This logic applies only to Windows
if runtime.GOOS != "windows" {
return filepath.Base(uri)
}

// Extract the path part (ignoring query strings)
if idx := strings.Index(uri, "?"); idx != -1 {
uri = strings.ReplaceAll(uri, "?", "_")
}

// Extract the base name (last segment of the path)
base := filepath.Base(uri)

// Replace invalid characters for Windows
base = strings.Map(func(r rune) rune {
switch r {
case '\\', '/', ':', '*', '?', '"', '<', '>', '|':
return '_'
default:
return r
}
}, base)

return base
}

func copyToTarget(atmosConfig schema.AtmosConfiguration, tempDir, targetPath string, s *schema.AtmosVendorSource, sourceIsLocalFile bool, uri string) error {
copyOptions := cp.Options{
Skip: generateSkipFunction(atmosConfig, tempDir, s),
Expand All @@ -524,7 +553,9 @@ func copyToTarget(atmosConfig schema.AtmosConfiguration, tempDir, targetPath str

// Adjust the target path if it's a local file with no extension
if sourceIsLocalFile && filepath.Ext(targetPath) == "" {
targetPath = filepath.Join(targetPath, filepath.Base(uri))
// Sanitize the URI for safe filenames, especially on Windows
sanitizedBase := sanitizeFileName(uri)
targetPath = filepath.Join(targetPath, sanitizedBase)
}

return cp.Copy(tempDir, targetPath, copyOptions)
Expand Down

0 comments on commit 0ba32ea

Please sign in to comment.