@marcel91200 / caddy Mirror · read-only / commits / c8b5a816
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 14, 01:30 UTC

fileserver: Fix handling of symlink sizes in directory listings (#4415)

Jeremy Lin committed Nov 22, 2021 at 13:59 UTC c8b5a81607c9c2799f46f42154ee311884723aa4
2 files changed +13 -13
modules/caddyhttp/fileserver/browse.go
+1 -6
@@ -141,12 +141,7 @@ func (fsrv *FileServer) loadDirectoryContents(dir *os.File, root, urlPath string
141 // user can presumably browse "up" to parent folder if path is longer than "/"
142 canGoUp := len(urlPath) > 1
143
144 - l, err := fsrv.directoryListing(files, canGoUp, root, urlPath, repl)
145 - if err != nil {
146 - return browseTemplateContext{}, err
147 - }
148 -
149 - return l, nil
144 + return fsrv.directoryListing(files, canGoUp, root, urlPath, repl), nil
145 }
146
147 // browseApplyQueryParams applies query parameters to the listing.
modules/caddyhttp/fileserver/browsetplcontext.go
+12 -7
@@ -24,10 +24,11 @@ import (
24 "time"
25
26 "github.com/caddyserver/caddy/v2"
27 + "github.com/caddyserver/caddy/v2/modules/caddyhttp"
28 "github.com/dustin/go-humanize"
29 )
30
30 -func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root, urlPath string, repl *caddy.Replacer) (browseTemplateContext, error) {
31 +func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root, urlPath string, repl *caddy.Replacer) browseTemplateContext {
32 filesToHide := fsrv.transformHidePaths(repl)
33
34 var dirCount, fileCount int
@@ -52,14 +53,18 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root
53 fileCount++
54 }
55
55 - fileIsSymlink := isSymlink(f)
56 size := f.Size()
57 + fileIsSymlink := isSymlink(f)
58 if fileIsSymlink {
58 - info, err := os.Stat(name)
59 - if err != nil {
60 - return browseTemplateContext{}, err
59 + path := caddyhttp.SanitizedPathJoin(root, path.Join(urlPath, f.Name()))
60 + fileInfo, err := os.Stat(path)
61 + if err == nil {
62 + size = fileInfo.Size()
63 }
62 - size = info.Size()
64 + // An error most likely means the symlink target doesn't exist,
65 + // which isn't entirely unusual and shouldn't fail the listing.
66 + // In this case, just use the size of the symlink itself, which
67 + // was already set above.
68 }
69
70 fileInfos = append(fileInfos, fileInfo{
@@ -80,7 +85,7 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root
85 Items: fileInfos,
86 NumDirs: dirCount,
87 NumFiles: fileCount,
83 - }, nil
88 + }
89 }
90
91 // browseTemplateContext provides the template context for directory listings.