aports/community/caddy/0001-fix-symlinks.patch
2022-04-16 15:10:27 +00:00

82 lines
3.0 KiB
Diff

Patch-Source: https://github.com/caddyserver/caddy/pull/4415
From 89a32af0b97ecb85c92bba8c90a3f31a9bd2e65f Mon Sep 17 00:00:00 2001
From: Jeremy Lin <jeremy.lin@gmail.com>
Date: Fri, 12 Nov 2021 02:25:48 -0800
Subject: [PATCH] fileserver: Fix handling of symlink sizes in directory
listings
---
modules/caddyhttp/fileserver/browse.go | 7 +------
.../caddyhttp/fileserver/browsetplcontext.go | 19 ++++++++++++-------
2 files changed, 13 insertions(+), 13 deletions(-)
diff --git a/modules/caddyhttp/fileserver/browse.go b/modules/caddyhttp/fileserver/browse.go
index 92a1e6ec64..cd9bcbcd7c 100644
--- a/modules/caddyhttp/fileserver/browse.go
+++ b/modules/caddyhttp/fileserver/browse.go
@@ -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.
diff --git a/modules/caddyhttp/fileserver/browsetplcontext.go b/modules/caddyhttp/fileserver/browsetplcontext.go
index 7dc4f080d8..2c57e5295a 100644
--- a/modules/caddyhttp/fileserver/browsetplcontext.go
+++ b/modules/caddyhttp/fileserver/browsetplcontext.go
@@ -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
@@ -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{
@@ -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.