Merge pull request #3158 from danmobot/fix/sandbox-fs-windows-paths

test: cover sandbox fs Windows path handling
This commit is contained in:
Mauro 2026-07-02 21:39:45 +02:00 committed by GitHub
commit 79ae6bf97f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 35 additions and 2 deletions

View file

@ -7,6 +7,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"log"
"maps" "maps"
"net/http" "net/http"
"net/url" "net/url"

View file

@ -1064,8 +1064,8 @@ func (r *sandboxFs) execute(path string, fn func(root *os.Root, relPath string)
return err return err
} }
// os.Root api on windows only accept forward slashes (/) // os.Root API on Windows only accepts forward slashes (/).
relPath = filepath.ToSlash(relPath) relPath = normalizeRootRelPath(relPath)
return fn(root, relPath) return fn(root, relPath)
} }
@ -1230,6 +1230,17 @@ func buildFs(workspace string, restrict bool, patterns []*regexp.Regexp) fileSys
return sandbox return sandbox
} }
func normalizeRootRelPath(relPath string) string {
return normalizeRootRelPathForSeparator(relPath, os.PathSeparator)
}
func normalizeRootRelPathForSeparator(relPath string, sep rune) string {
if sep == '\\' {
return strings.ReplaceAll(relPath, `\`, `/`)
}
return relPath
}
// Helper to get a safe relative path for os.Root usage // Helper to get a safe relative path for os.Root usage
func getSafeRelPath(workspace, path string) (string, error) { func getSafeRelPath(workspace, path string) (string, error) {
if workspace == "" { if workspace == "" {

View file

@ -0,0 +1,21 @@
package fstools
import "testing"
func TestNormalizeRootRelPathForWindowsSeparator(t *testing.T) {
got := normalizeRootRelPathForSeparator(`aaa\bbb\file.txt`, '\\')
want := "aaa/bbb/file.txt"
if got != want {
t.Fatalf("normalizeRootRelPathForSeparator() = %q, want %q", got, want)
}
}
func TestNormalizeRootRelPathForUnixSeparatorLeavesBackslashUnchanged(t *testing.T) {
input := `aaa\bbb\file.txt`
got := normalizeRootRelPathForSeparator(input, '/')
if got != input {
t.Fatalf("normalizeRootRelPathForSeparator() = %q, want %q", got, input)
}
}