Backtestshugorun-20261007-020926#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.
The production code change is identical in both patches. Each wraps the `isSymlinkOrHasSymlinkParent` check in `if !rm.IsProject`, so symlinked mount roots and parents work for the main project but are still dropped for themes and modules. That fixes the issue at its root, and correctness is a tie. The difference is in the tests. Both update `no_symlinks.txt` the same way: they flip the project-mount-root assertion to expect success, add a symlinked-parent project mount, and narrow the glob so it still covers theme mounts only. Patch B checks this through `.Publish` and `tree` stdout assertions. Patch A checks it through `.Content` greps, and it also keeps the existing `! stdout rootdirfile` tree assertion valid by not publishing those files. Patch A then adds `symlinked_project_mount_roots.txt`, which reproduces the reported scenario directly. It symlinks `_build/includes/project-a` to an external directory, mounts its `content` and `static` subdirectories into `content/project-a` and `static/static-a`, and covers a symlinked parent directory (`linkedparent/project-b`). It also confirms that a theme's symlinked content mount is still excluded while the theme's normal static files still publish. That test would fail before the fix and pass after it, and it exercises content-page rendering, which is the issue's actual use case. Patch B only tests asset resources. With identical code, Patch A's more targeted test coverage makes it the better fix.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
12m 14s
Wall-clock time in the container
Full test suite
No new failures
3,775 passed · 101 skipped · 3,878 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
97vs 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/rootmapping_fs.goindex 4b123bf21..8e635dca4 100644@@ -63,12 +63,15 @@ 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, so its mount roots may be symlinks.+ 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..5a3668752 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 @@ -12,6 +13,8 @@ hugo grep 'OK' public/index.html ! grep 'FAIL' public/index.html+grep 'OK Rootdirfile1 content.' public/index.html+grep 'OK Rootdirfile3 content.' public/index.html tree public @@ -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,13 @@ 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 {{ .Content }}{{ else }}FAIL{{ end }}+{{ with resources.Get "projectmountparent/rootdirfile3.txt"}}OK {{ .Content }}{{ else }}FAIL{{ end }}+{{ with resources.GetMatch "projectmountroot/**"}}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. */}}testscripts/commands/symlinked_project_mount_roots.txtnew file mode 100644index 000000000..2da57ba86@@ -0,0 +1,69 @@+# Symlinked mount roots (and symlinked directories between the mount root and+# the project root) are allowed in the main project, but not in themes/modules.++[windows] skip++mkdir _build/includes+ln ./external/project-a ./_build/includes/project-a+ln ./external ./linkedparent+ln ./external/project-a ./themes/mytheme/themecontent++hugo++grep 'Title: Project A Home\|' public/project-a/index.html+grep 'Title: Project A Page 1\|Content: <p>Project A content.</p>' public/project-a/p1/index.html+grep 'Title: Project B Page 1\|' public/project-b/p1/index.html+grep 'Project A static.' public/static-a/s1.txt+grep 'Theme static.' public/theme.txt+! exists public/theme-content/p1/index.html++-- hugo.toml --+baseURL = "https://example.org/"+disableKinds = ["taxonomy", "term", "rss", "sitemap", "404"]+[[module.imports]]+path = 'mytheme'+[[module.mounts]]+source = 'content'+target = 'content'+[[module.mounts]]+source = '_build/includes/project-a/content'+target = 'content/project-a'+[[module.mounts]]+source = 'linkedparent/project-b'+target = 'content/project-b'+[[module.mounts]]+source = '_build/includes/project-a/static'+target = 'static/static-a'+[[module.mounts]]+source = 'layouts'+target = 'layouts'+-- themes/mytheme/hugo.toml --+[[module.mounts]]+source = 'themecontent/content'+target = 'content/theme-content'+[[module.mounts]]+source = 'static'+target = 'static'+-- content/_index.md --+---+title: Home+---+-- layouts/all.html --+Title: {{ .Title }}|Content: {{ .Content }}+-- themes/mytheme/static/theme.txt --+Theme static.+-- external/project-a/content/_index.md --+---+title: Project A Home+---+-- external/project-a/content/p1.md --+---+title: Project A Page 1+---+Project A content.+-- external/project-a/static/s1.txt --+Project A static.+-- external/project-b/p1.md --+---+title: Project B Page 1+---
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. */}}