diff options
author | Matthew Holt <[email protected]> | 2024-04-23 15:31:13 -0600 |
---|---|---|
committer | Francis Lavoie <[email protected]> | 2024-04-23 19:55:18 -0400 |
commit | a57a6d876c826ea9f7ef28de35b2de53cde9fa38 (patch) | |
tree | a9baf0f6f620a3d5ee8141ab2bb7052680a46408 | |
parent | 64418de0adb669936351b4d7002fd736e81a5110 (diff) | |
download | caddy-a57a6d876c826ea9f7ef28de35b2de53cde9fa38.tar.gz caddy-a57a6d876c826ea9f7ef28de35b2de53cde9fa38.zip |
Try to fix failing tests on Windows
-rw-r--r-- | modules/caddyhttp/caddyhttp.go | 8 | ||||
-rw-r--r-- | modules/caddyhttp/caddyhttp_test.go | 18 |
2 files changed, 16 insertions, 10 deletions
diff --git a/modules/caddyhttp/caddyhttp.go b/modules/caddyhttp/caddyhttp.go index f4dc5f37a..26b21b541 100644 --- a/modules/caddyhttp/caddyhttp.go +++ b/modules/caddyhttp/caddyhttp.go @@ -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 = "." @@ -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. diff --git a/modules/caddyhttp/caddyhttp_test.go b/modules/caddyhttp/caddyhttp_test.go index 8c55fcb7a..e457d9542 100644 --- a/modules/caddyhttp/caddyhttp_test.go +++ b/modules/caddyhttp/caddyhttp_test.go @@ -3,6 +3,7 @@ package caddyhttp import ( "net/url" "path/filepath" + "runtime" "testing" ) @@ -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: "", @@ -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 @@ -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) |