Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions modules/caddyhttp/fileserver/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,7 @@ func (fsrv *FileServer) loadDirectoryContents(dir *os.File, root, urlPath string
// user can presumably browse "up" to parent folder if path is longer than "/"
canGoUp := len(urlPath) > 1

l, err := fsrv.directoryListing(files, canGoUp, root, urlPath, repl)
if err != nil {
return browseTemplateContext{}, err
}

return l, nil
return fsrv.directoryListing(files, canGoUp, root, urlPath, repl), nil
}

// browseApplyQueryParams applies query parameters to the listing.
Expand Down
19 changes: 12 additions & 7 deletions modules/caddyhttp/fileserver/browsetplcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import (
"time"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/dustin/go-humanize"
)

func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root, urlPath string, repl *caddy.Replacer) (browseTemplateContext, error) {
func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root, urlPath string, repl *caddy.Replacer) browseTemplateContext {
filesToHide := fsrv.transformHidePaths(repl)

var dirCount, fileCount int
Expand All @@ -52,14 +53,18 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root
fileCount++
}

fileIsSymlink := isSymlink(f)
size := f.Size()
fileIsSymlink := isSymlink(f)
if fileIsSymlink {
info, err := os.Stat(name)
if err != nil {
return browseTemplateContext{}, err
path := caddyhttp.SanitizedPathJoin(root, path.Join(urlPath, f.Name()))
fileInfo, err := os.Stat(path)
if err == nil {
size = fileInfo.Size()
}
size = info.Size()
// An error most likely means the symlink target doesn't exist,
// which isn't entirely unusual and shouldn't fail the listing.
// In this case, just use the size of the symlink itself, which
// was already set above.
}

fileInfos = append(fileInfos, fileInfo{
Expand All @@ -80,7 +85,7 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root
Items: fileInfos,
NumDirs: dirCount,
NumFiles: fileCount,
}, nil
}
}

// browseTemplateContext provides the template context for directory listings.
Expand Down