From 8d038ca515ffaa4d37dca05a95181203b2db64e7 Mon Sep 17 00:00:00 2001 From: Matt Holt Date: Mon, 2 Nov 2020 14:20:12 -0700 Subject: fileserver: Improve and clarify file hiding logic (#3844) * fileserver: Improve and clarify file hiding logic * Oops, forgot to run integration tests * Make this one integration test OS-agnostic * See if this appeases the Windows gods * D'oh --- modules/caddyhttp/fileserver/staticfiles_test.go | 50 ++++++++++++++++++++---- 1 file changed, 42 insertions(+), 8 deletions(-) (limited to 'modules/caddyhttp/fileserver/staticfiles_test.go') diff --git a/modules/caddyhttp/fileserver/staticfiles_test.go b/modules/caddyhttp/fileserver/staticfiles_test.go index 1bbd77b..690f46a 100644 --- a/modules/caddyhttp/fileserver/staticfiles_test.go +++ b/modules/caddyhttp/fileserver/staticfiles_test.go @@ -17,6 +17,8 @@ package fileserver import ( "net/url" "path/filepath" + "runtime" + "strings" "testing" ) @@ -44,7 +46,7 @@ func TestSanitizedPathJoin(t *testing.T) { }, { inputPath: "/foo/", - expect: "foo" + string(filepath.Separator), + expect: "foo" + separator, }, { inputPath: "/foo/bar", @@ -77,7 +79,7 @@ func TestSanitizedPathJoin(t *testing.T) { { inputRoot: "/a/b", inputPath: "/%2e%2e%2f%2e%2e%2f", - expect: filepath.Join("/", "a", "b") + string(filepath.Separator), + expect: filepath.Join("/", "a", "b") + separator, }, { inputRoot: "C:\\www", @@ -154,6 +156,26 @@ func TestFileHidden(t *testing.T) { inputPath: "/foo/asdf/bar", expect: true, }, + { + inputHide: []string{"*.txt"}, + inputPath: "/foo/bar.txt", + expect: true, + }, + { + inputHide: []string{"/foo/bar/*.txt"}, + inputPath: "/foo/bar/baz.txt", + expect: true, + }, + { + inputHide: []string{"/foo/bar/*.txt"}, + inputPath: "/foo/bar.txt", + expect: false, + }, + { + inputHide: []string{"/foo/bar/*.txt"}, + inputPath: "/foo/bar/index.html", + expect: false, + }, { inputHide: []string{"/foo"}, inputPath: "/foo", @@ -164,17 +186,29 @@ func TestFileHidden(t *testing.T) { inputPath: "/foobar", expect: false, }, + { + inputHide: []string{"first", "second"}, + inputPath: "/second", + expect: true, + }, } { - // for Windows' sake - tc.inputPath = filepath.FromSlash(tc.inputPath) - for i := range tc.inputHide { - tc.inputHide[i] = filepath.FromSlash(tc.inputHide[i]) + if runtime.GOOS == "windows" { + if strings.HasPrefix(tc.inputPath, "/") { + tc.inputPath, _ = filepath.Abs(tc.inputPath) + } + tc.inputPath = filepath.FromSlash(tc.inputPath) + for i := range tc.inputHide { + if strings.HasPrefix(tc.inputHide[i], "/") { + tc.inputHide[i], _ = filepath.Abs(tc.inputHide[i]) + } + tc.inputHide[i] = filepath.FromSlash(tc.inputHide[i]) + } } actual := fileHidden(tc.inputPath, tc.inputHide) if actual != tc.expect { - t.Errorf("Test %d: Is %s hidden in %v? Got %t but expected %t", - i, tc.inputPath, tc.inputHide, actual, tc.expect) + t.Errorf("Test %d: Does %v hide %s? Got %t but expected %t", + i, tc.inputHide, tc.inputPath, actual, tc.expect) } } } -- cgit v1.2.3