Debian Codegraph @

hugofs/nosymlinks_fs.go

raw
1 // Copyright 2026 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package hugofs
15
16 import (
17 "os"
18 "path/filepath"
19
20 "github.com/gohugoio/hugo/common/herrors"
21 "github.com/spf13/afero"
22 )
23
24 // hasSymlinkParent reports whether any directory between name and base is a symlink.
25 // Lstat only refrains from following the last element of a path, so callers checking
26 // name itself need this to keep a symlinked parent from escaping base.
27 // base itself is not checked; an empty base walks to the root of fs.
28 func hasSymlinkParent(fs afero.Fs, base, name string) (bool, error) {
29 for name != base {
30 parent := filepath.Dir(name)
31 if parent == name || parent == base || parent == "." {
32 return false, nil
33 }
34 fi, err := LstatIfPossible(fs, parent)
35 if err != nil {
36 if herrors.IsNotExist(err) {
37 return false, nil
38 }
39 return false, err
40 }
41 if fi.Mode()&os.ModeSymlink != 0 {
42 return true, nil
43 }
44 name = parent
45 }
46 return false, nil
47 }
48
49 // isSymlinkOrHasSymlinkParent reports whether name is a symlink or has a symlinked parent below base.
50 // Parents are only checked when base is set; absolute mount sources may legitimately
51 // live below symlinked directories (e.g. /tmp on macOS).
52 func isSymlinkOrHasSymlinkParent(fs afero.Fs, base, name string) (bool, error) {
53 fi, err := LstatIfPossible(fs, name)
54 if err != nil {
55 if herrors.IsNotExist(err) {
56 return false, nil
57 }
58 return false, err
59 }
60 if fi.Mode()&os.ModeSymlink != 0 {
61 return true, nil
62 }
63 if base == "" {
64 return false, nil
65 }
66 return hasSymlinkParent(fs, filepath.Clean(base), name)
67 }
68
69 // NewDropSymlinksFs returns an afero.Fs wrapper that treats symlinks as non-existing files.
70 func NewDropSymlinksFs(base afero.Fs) *DropSymlinksFs {
71 return &DropSymlinksFs{base}
72 }
73
74 // DropSymlinksFs is an afero.Fs wrapper that treats symlinks as non-existing files.
75 type DropSymlinksFs struct {
76 afero.Fs
77 }
78
79 func (fs *DropSymlinksFs) Open(name string) (afero.File, error) {
80 if _, err := fs.Stat(name); err != nil {
81 return nil, err
82 }
83 f, err := fs.Fs.Open(name)
84 if err != nil {
85 return nil, err
86 }
87 return f, nil
88 }
89
90 func (fs *DropSymlinksFs) Stat(name string) (os.FileInfo, error) {
91 fi, err := LstatIfPossible(fs.Fs, name)
92 if err != nil {
93 return nil, err
94 }
95 if fi.Mode()&os.ModeSymlink != 0 {
96 return nil, os.ErrNotExist
97 }
98 symlinkParent, err := hasSymlinkParent(fs.Fs, "", name)
99 if err != nil {
100 return nil, err
101 }
102 if symlinkParent {
103 return nil, os.ErrNotExist
104 }
105 return fi, nil
106 }