@marcel91200 / caddy Mirror · read-only / commits / 44e5e9e4
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 23:00 UTC

caddyhttp: Fix test when /tmp/etc already exists (#4544)

The TestFileListing test in tplcontext_test has one test that verifies if directory traversal is not happening. The context root is set to '/tmp' and then it tries to open '../../../../../etc', which gets normalized to '/tmp/etc'. The test then expects an error to be returned, assuming that '/tmp/etc' does not exist on the system. When it does exist, it results in a test failure: ``` --- FAIL: TestFileListing (0.00s) tplcontext_test.go:422: Test 4: Expected error but had none FAIL FAIL github.com/caddyserver/caddy/v2/modules/caddyhttp/templates 0.042s ``` Instead of using '/tmp' as root, use a dedicated directory created with `os.MkdirTemp()` instead. That way, we know that the directory is empty.

Kevin Daudt committed Jan 24, 2022 at 22:41 UTC 44e5e9e43f3583f04613bbbb1996e9b5a13a60ac
1 file changed +8 -1
modules/caddyhttp/templates/tplcontext_test.go
+8 -1
@@ -608,6 +608,9 @@ title = "Welcome"
608
609 func getContextOrFail(t *testing.T) TemplateContext {
610 tplContext, err := initTestContext()
611 + t.Cleanup(func() {
612 + os.RemoveAll(string(tplContext.Root.(http.Dir)))
613 + })
614 if err != nil {
615 t.Fatalf("failed to prepare test context: %v", err)
616 }
@@ -620,8 +623,12 @@ func initTestContext() (TemplateContext, error) {
623 if err != nil {
624 return TemplateContext{}, err
625 }
626 + tmpDir, err := os.MkdirTemp(os.TempDir(), "caddy")
627 + if err != nil {
628 + return TemplateContext{}, err
629 + }
630 return TemplateContext{
624 - Root: http.Dir(os.TempDir()),
631 + Root: http.Dir(tmpDir),
632 Req: request,
633 RespHeader: WrappedHeader{make(http.Header)},
634 }, nil