Skip to content

Find, verify, and analyze leaked credentials

Fix/issue 4578 path normalization for unix and windows (#4614)

Rusted2361f946748dd9df
diff --git a/pkg/sources/git/git.go b/pkg/sources/git/git.go
index 3ee18e21a..2aada8b02 100644
--- a/pkg/sources/git/git.go
+++ b/pkg/sources/git/git.go
@@ -434,9 +434,12 @@ func normalizeFileURI(uri *url.URL) (*url.URL, error) {
return nil, fmt.Errorf("failed to resolve absolute path for %q: %w", rawPath, err)
}
+ // Convert to forward slashes (for Windows compatibility)
+ normalizedPath := filepath.ToSlash(absPath)
+
normalizedURI := &url.URL{
Scheme: "file",
- Path: absPath,
+ Path: normalizedPath,
}
return normalizedURI, nil
@@ -1341,7 +1344,10 @@ func PrepareRepo(ctx context.Context, uriString, clonePath string, trustLocalGit
if !isRepoBare(path) {
// Only copy index file for non-bare clones from working directory repos. This is used to see staged changes.
// Note: To scan **un**staged changes in the future, we'd need to set core.worktree to the original path.
- originalIndexPath := filepath.Join(strings.TrimPrefix(normalizedURI.String(), "file://"), gitDirName, "index")
+ uriPath := normalizedURI.Path
+
+ originalIndexPath := filepath.Join(uriPath, gitDirName, "index")
+
clonedIndexPath := filepath.Join(path, gitDirName, "index")
indexData, err := os.ReadFile(originalIndexPath)
diff --git a/pkg/sources/git/git_test.go b/pkg/sources/git/git_test.go
index f20ec503f..744218b83 100644
--- a/pkg/sources/git/git_test.go
+++ b/pkg/sources/git/git_test.go
@@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path/filepath"
+ "runtime"
"strings"
"testing"
"time"
@@ -861,7 +862,7 @@ func TestNormalizeFileURI(t *testing.T) {
{
name: "absolute file URI unchanged",
input: "file:///absolute/path",
- expected: "file:///absolute/path",
+ expected: "",
},
{
name: "relative file URI with current directory",
@@ -908,6 +909,15 @@ func TestNormalizeFileURI(t *testing.T) {
var expected string
switch tt.name {
+ case "absolute file URI unchanged":
+ // On Windows, absolute paths get drive letter prepended
+ // On Unix, they remain as-is
+ if runtime.GOOS == "windows" {
+ expectedPath, _ := filepath.Abs("/absolute/path")
+ expected = "file://" + expectedPath
+ } else {
+ expected = "file:///absolute/path"
+ }
case "relative file URI with current directory":
expected = "file://" + cwd
case "relative file URI with subdirectory":
@@ -920,7 +930,10 @@ func TestNormalizeFileURI(t *testing.T) {
default:
expected = tt.expected
}
-
+ // Normalize slashes for Windows comparison
+ if runtime.GOOS == "windows" {
+ expected = filepath.ToSlash(expected)
+ }
assert.Equal(t, expected, result.String())
})
}