Backtestshugorun-20261024-162305#15367
Allow symlinks for mount source for the main project
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches make the same core change: skip the symlinked-mount-root rejection in NewRootMappingFs when rm.IsProject is set. That directly resolves the reported scenario (a project mount whose source directory is a symlink), and all three independent tests pass on both. The difference is completeness and test coverage. Patch B only lifts the check at mount-construction time; a project mount whose source is a single symlinked file still dies in statRoot's per-file symlink rejection, so the 'main project is trusted' rule B states in its comment is only half-implemented. Patch A carries the exemption through consistently: statRoot follows the symlink only when the mapping is a project single-file mount (root.fiSingleFile != nil, after rename/inclusion filtering has pinned filename to the mounted file), and componentFsDir keeps the corresponding entry under the same narrow condition. The extra code is small and guarded, so it adds little risk, and it closes an inconsistency a user hitting this issue would plausibly trip over next. Patch A's testscript is also stronger: it reproduces the issue's exact shape (an explicit 'content' mount plus a symlinked 'projectcontentroot' mounted at content/project-a, checked via site.GetPage), covers the symlinked-parent and single-file cases, and keeps the negative assertions that theme symlinked mounts, in-tree symlinked assets, and the symlinked page are still dropped, which guards against the exemption leaking to directory contents. Patch B's test is adequate but narrower, covering only asset directory mounts. No flaws found in B beyond the gap above; A is the more complete fix with better tests.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
1h
Wall-clock time in the container
Full test suite
No new failures
1,931 passed · 5 skipped · 1,989 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
71vs 31 merged
Changed lines, ours vs merged
The issue
View on GitHubHugo 0.166.0 introduced commit https://github.com/gohugoio/hugo/commit/938c820626bd0a524dc1482cbe83b6c2e1171d20 (hugofs: Drop symlinked mount roots) that breaks a legit use-case of symlinks.
In my case, I mount a directory in my hugo.yaml:
module:
mounts:
- source: "content"
target: "content"
# Mount content generated in another repository
- source: "_build/includes/project-a"
target: "content/project-a"
_build/includes/project-a itself can be a symlink to another local directory, e.g. ../../../project-a, which allows quick local debugging. In production, a CI workflow may clone the Git repo of project-a to this destination which does not trigger the problem.
I am not familiar with the internals of hugofs and the security concept, but would appreciate if users could trust certain mounts, and therefore bypass the newly introduced hard drop of symlinks.
The two patches
hugofs/component_fs.goindex d5c8ca676..cddbb3117 100644@@ -103,6 +103,10 @@ func (f *componentFsDir) ReadDirWithContext(ctx context.Context, count int) ([]i } if info.Mode()&os.ModeSymlink == 0 { keep = true+ } else if meta := fi.(FileMetaInfo).Meta(); meta.IsProject && meta.Rename != nil {+ // A single file mount in the main project may itself be a symlink;+ // the rename and inclusion filter guarantee that this is the mounted file.+ keep = true } } if keep {hugofs/rootmapping_fs.goindex 4b123bf21..b5f246f9b 100644@@ -63,12 +63,16 @@ func NewRootMappingFs(fs afero.Fs, rms ...*RootMapping) (*RootMappingFs, error) // Don't allow a symlinked mount root (or any directory between it and // the module root) to escape the module.- symlink, err := isSymlinkOrHasSymlinkParent(fs, rm.ToBase, rm.To)- if err != nil {- return nil, err- }- if symlink {- continue+ // The main project is trusted: its mounts can already point anywhere+ // using absolute paths, so symlinked mount roots are allowed there.+ if !rm.IsProject {+ symlink, err := isSymlinkOrHasSymlinkParent(fs, rm.ToBase, rm.To)+ if err != nil {+ return nil, err+ }+ if symlink {+ continue+ } } fi, err := fs.Stat(rm.To)@@ -742,7 +746,16 @@ func (fs *RootMappingFs) statRoot(root *RootMapping, filename string) (FileMetaI // Don't allow symlinks to escape the mount, neither as the file itself // nor as any directory between it and the mount root. if fi.Mode()&os.ModeSymlink != 0 {- return nil, os.ErrNotExist+ // A single file mount in the main project may itself be a symlink;+ // the rename and inclusion filter above guarantee that filename+ // is the mounted file.+ if !root.IsProject || root.fiSingleFile == nil {+ return nil, os.ErrNotExist+ }+ fi, err = fs.Fs.Stat(filename)+ if err != nil {+ return nil, err+ } } symlinkParent, err := hasSymlinkParent(fs.Fs, root.To, filename) if err != nil {testscripts/commands/no_symlinks.txtindex f62e608d9..4a67f9391 100644@@ -5,6 +5,9 @@ ln ./rootfile.txt ./themes/mytheme/static/modstaticsymlink.txt ln ./README.md ./content/pagesymlink.md ln ./rootdir ./assets/myassets/symlinkdir ln ./rootdir ./projectmountroot+ln ./rootdir ./projectmountparent+ln ./rootfile.txt ./projectmountfile.txt+ln ./rootcontentdir ./projectcontentroot ln ./rootdir ./themes/mytheme/thememountroot ln ./rootdir ./themes/mytheme/thememountparent @@ -21,7 +24,15 @@ stdout 'modstatictok' ! stdout 'modstaticsymlink' stdout pageok ! stdout pagesymlink-! stdout rootdirfile+stdout 'projectmountroot/'+stdout 'projectmountparent/'+stdout 'projectmountfile.txt'+stdout 'rootdirfile1.txt'+stdout 'rootdirfile3.txt'+stdout 'projectpage/'+! stdout 'thememount'+! stdout 'symlinkdir'+! stdout 'rootfile.txt' -- hugo.toml -- disableKinds = ["taxonomy", "term", "rss"]@@ -33,6 +44,18 @@ target = 'assets' [[module.mounts]] source = 'projectmountroot' target = 'assets/projectmountroot'+[[module.mounts]]+source = 'projectmountparent/subdir'+target = 'assets/projectmountparent'+[[module.mounts]]+source = 'projectmountfile.txt'+target = 'assets/projectmountfile.txt'+[[module.mounts]]+source = 'content'+target = 'content'+[[module.mounts]]+source = 'projectcontentroot'+target = 'content/project-a' -- themes/mytheme/hugo.toml -- [[module.mounts]] source = 'assets'@@ -54,10 +77,17 @@ Read me. {{ with resources.GetMatch "modassetsymlink.txt"}}FAIL {{ .Publish }}{{ else }}OK{{ end }} {{ with resources.GetMatch "myassets/symlinkdir/**"}}FAIL {{ .Publish }}{{ else }}OK{{ end }} {{ with resources.Get "myassets/symlinkdir/rootdirfile1.txt"}}FAIL {{ .Publish }}{{ else }}OK{{ end }}-{{ with resources.Get "projectmountroot/rootdirfile1.txt"}}FAIL {{ .Publish }}{{ else }}OK{{ end }}+{{/* Symlinked mount roots are allowed in the main project. */}}+{{ with resources.Get "projectmountroot/rootdirfile1.txt"}}OK {{ .Publish }}{{ else }}FAIL{{ end }}+{{ with resources.Get "projectmountroot/subdir/rootdirfile3.txt"}}OK {{ .Publish }}{{ else }}FAIL{{ end }}+{{ with resources.Get "projectmountparent/rootdirfile3.txt"}}OK {{ .Publish }}{{ else }}FAIL{{ end }}+{{ with resources.Get "projectmountfile.txt"}}OK {{ .Publish }}{{ else }}FAIL{{ end }}+{{ with resources.GetMatch "projectmountfile.txt"}}OK{{ else }}FAIL{{ end }}+{{ with resources.GetMatch "projectmountroot/**"}}OK{{ else }}FAIL{{ end }}+{{ with site.GetPage "/project-a/projectpage" }}OK{{ else }}FAIL{{ end }} {{ with resources.Get "thememountroot/rootdirfile1.txt"}}FAIL {{ .Publish }}{{ else }}OK{{ end }} {{ with resources.Get "thememountparent/rootdirfile3.txt"}}FAIL {{ .Publish }}{{ else }}OK{{ end }}-{{ with resources.GetMatch "*mountroot/**"}}FAIL {{ .Publish }}{{ else }}OK{{ end }}+{{ with resources.GetMatch "thememountroot/**"}}FAIL {{ .Publish }}{{ else }}OK{{ end }} Page: {{ .RelPermalink }}|{{ .Content }}| {{/* os template package. */}}@@ -77,6 +107,10 @@ Page: {{ .RelPermalink }}|{{ .Content }}| {{ end }} -- content/pageok.md --+-- rootcontentdir/projectpage.md --+---+title: Project page+--- -- themes/mytheme/assets/modassetok.txt -- Content. -- themes/mytheme/static/modstatictok.txt --
hugofs/rootmapping_fs.goindex 4b123bf21..e8c8e2804 100644@@ -62,13 +62,16 @@ func NewRootMappingFs(fs afero.Fs, rms ...*RootMapping) (*RootMappingFs, error) } // Don't allow a symlinked mount root (or any directory between it and- // the module root) to escape the module.- symlink, err := isSymlinkOrHasSymlinkParent(fs, rm.ToBase, rm.To)- if err != nil {- return nil, err- }- if symlink {- continue+ // the module root) to escape the module. The main project is exempt;+ // its mount sources may be absolute, so a symlink gains nothing.+ if !rm.IsProject {+ symlink, err := isSymlinkOrHasSymlinkParent(fs, rm.ToBase, rm.To)+ if err != nil {+ return nil, err+ }+ if symlink {+ continue+ } } fi, err := fs.Stat(rm.To)testscripts/commands/no_symlinks.txtindex f62e608d9..cf8904d3e 100644@@ -5,6 +5,7 @@ ln ./rootfile.txt ./themes/mytheme/static/modstaticsymlink.txt ln ./README.md ./content/pagesymlink.md ln ./rootdir ./assets/myassets/symlinkdir ln ./rootdir ./projectmountroot+ln ./rootdir ./projectmountparent ln ./rootdir ./themes/mytheme/thememountroot ln ./rootdir ./themes/mytheme/thememountparent @@ -19,9 +20,11 @@ stdout modassetok ! stdout modassetsymlink stdout 'modstatictok' ! stdout 'modstaticsymlink'+stdout projectmountroot+stdout projectmountparent stdout pageok ! stdout pagesymlink-! stdout rootdirfile+! stdout thememount -- hugo.toml -- disableKinds = ["taxonomy", "term", "rss"]@@ -33,6 +36,9 @@ target = 'assets' [[module.mounts]] source = 'projectmountroot' target = 'assets/projectmountroot'+[[module.mounts]]+source = 'projectmountparent/subdir'+target = 'assets/projectmountparent' -- themes/mytheme/hugo.toml -- [[module.mounts]] source = 'assets'@@ -54,10 +60,12 @@ Read me. {{ with resources.GetMatch "modassetsymlink.txt"}}FAIL {{ .Publish }}{{ else }}OK{{ end }} {{ with resources.GetMatch "myassets/symlinkdir/**"}}FAIL {{ .Publish }}{{ else }}OK{{ end }} {{ with resources.Get "myassets/symlinkdir/rootdirfile1.txt"}}FAIL {{ .Publish }}{{ else }}OK{{ end }}-{{ with resources.Get "projectmountroot/rootdirfile1.txt"}}FAIL {{ .Publish }}{{ else }}OK{{ end }}+{{ with resources.Get "projectmountroot/rootdirfile1.txt"}}OK {{ .Publish }}{{ else }}FAIL{{ end }}+{{ with resources.Get "projectmountparent/rootdirfile3.txt"}}OK {{ .Publish }}{{ else }}FAIL{{ end }}+{{ with resources.Get "projectmountroot/subdir/rootdirfile3.txt"}}OK {{ .Publish }}{{ else }}FAIL{{ end }} {{ with resources.Get "thememountroot/rootdirfile1.txt"}}FAIL {{ .Publish }}{{ else }}OK{{ end }} {{ with resources.Get "thememountparent/rootdirfile3.txt"}}FAIL {{ .Publish }}{{ else }}OK{{ end }}-{{ with resources.GetMatch "*mountroot/**"}}FAIL {{ .Publish }}{{ else }}OK{{ end }}+{{ with resources.GetMatch "thememount*/**"}}FAIL {{ .Publish }}{{ else }}OK{{ end }} Page: {{ .RelPermalink }}|{{ .Content }}| {{/* os template package. */}}