Skip to content

Commit

Permalink
Try to fix failing tests on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
mholt authored and francislavoie committed Apr 23, 2024
1 parent 64418de commit a57a6d8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
8 changes: 4 additions & 4 deletions modules/caddyhttp/caddyhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ func StatusCodeMatches(actual, configured int) bool {
// be a trusted path, but reqPath is not; and the output will
// never be outside of root. The resulting path can be used
// with the local file system. If root is empty, the current
// directory is assumed. If the cleaned request path is not
// lexically local, it will be rejected as unsafe and only
// the root will be returned.
// directory is assumed. If the cleaned request path is deemed
// not local according to lexical processing (i.e. ignoring links),
// it will be rejected as unsafe and only the root will be returned.
func SanitizedPathJoin(root, reqPath string) string {
if root == "" {
root = "."
Expand All @@ -241,7 +241,7 @@ func SanitizedPathJoin(root, reqPath string) string {
return root
}

path := filepath.Join(root, relPath)
path := filepath.Join(root, filepath.FromSlash(relPath))

// filepath.Join also cleans the path, and cleaning strips
// the trailing slash, so we need to re-add it afterwards.
Expand Down
18 changes: 12 additions & 6 deletions modules/caddyhttp/caddyhttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package caddyhttp
import (
"net/url"
"path/filepath"
"runtime"
"testing"
)

Expand All @@ -12,9 +13,10 @@ func TestSanitizedPathJoin(t *testing.T) {
// %2f = /
// %5c = \
for i, tc := range []struct {
inputRoot string
inputPath string
expect string
inputRoot string
inputPath string
expect string
expectWindows string
}{
{
inputPath: "",
Expand Down Expand Up @@ -81,9 +83,10 @@ func TestSanitizedPathJoin(t *testing.T) {
expect: filepath.Join("C:\\www", "foo", "bar"),
},
{
inputRoot: "C:\\www",
inputPath: "/D:\\foo\\bar",
expect: filepath.Join("C:\\www", "D:\\foo\\bar"),
inputRoot: "C:\\www",
inputPath: "/D:\\foo\\bar",
expect: filepath.Join("C:\\www", "D:\\foo\\bar"),
expectWindows: filepath.Join("C:\\www"),
},
{
// https://github.com/golang/go/issues/56336#issuecomment-1416214885
Expand All @@ -102,6 +105,9 @@ func TestSanitizedPathJoin(t *testing.T) {
t.Fatalf("Test %d: invalid URL: %v", i, err)
}
actual := SanitizedPathJoin(tc.inputRoot, u.Path)
if runtime.GOOS == "windows" && tc.expectWindows != "" {
tc.expect = tc.expectWindows
}
if actual != tc.expect {
t.Errorf("Test %d: SanitizedPathJoin('%s', '%s') => '%s' (expected '%s')",
i, tc.inputRoot, tc.inputPath, actual, tc.expect)
Expand Down

0 comments on commit a57a6d8

Please sign in to comment.