Drop symlinks in os.ReadDir, os.ReadFile, os.Stat and os.FileExists

Fixes #15019
This commit is contained in:
Bjørn Erik Pedersen
2026-06-09 11:20:01 +02:00
parent 11f09f59fc
commit cf9c8f93ca
7 changed files with 115 additions and 8 deletions
+13 -6
View File
@@ -24,24 +24,25 @@ import (
"github.com/bep/overlayfs"
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/hugofs"
"github.com/spf13/afero"
"github.com/spf13/cast"
)
// New returns a new instance of the os-namespaced template functions.
func New(d *deps.Deps) *Namespace {
var readFileFs, workFs afero.Fs
var readFileFs, workFs *hugofs.DropSymlinksFs
// The docshelper script does not have or need all the dependencies set up.
if d.PathSpec != nil {
readFileFs = overlayfs.New(overlayfs.Options{
readFileFs = hugofs.NewDropSymlinksFs(overlayfs.New(overlayfs.Options{
Fss: []afero.Fs{
d.PathSpec.BaseFs.Work,
d.PathSpec.BaseFs.Content.Fs,
},
})
}))
// See #9599
workFs = d.PathSpec.BaseFs.WorkDir
workFs = hugofs.NewDropSymlinksFs(d.PathSpec.BaseFs.WorkDir)
}
return &Namespace{
@@ -53,8 +54,8 @@ func New(d *deps.Deps) *Namespace {
// Namespace provides template functions for the "os" namespace.
type Namespace struct {
readFileFs afero.Fs
workFs afero.Fs
readFileFs *hugofs.DropSymlinksFs
workFs *hugofs.DropSymlinksFs
deps *deps.Deps
}
@@ -118,6 +119,9 @@ func (ns *Namespace) ReadDir(i any) ([]_os.FileInfo, error) {
list, err := afero.ReadDir(ns.workFs, path)
if err != nil {
if herrors.IsNotExist(err) {
return nil, nil
}
return nil, fmt.Errorf("failed to read directory %q: %s", path, err)
}
@@ -156,6 +160,9 @@ func (ns *Namespace) Stat(i any) (_os.FileInfo, error) {
r, err := ns.readFileFs.Stat(path)
if err != nil {
if herrors.IsNotExist(err) {
return nil, nil
}
return nil, err
}