Repository navigation
Confine .thvignore overlays to the mounted directory - #7196
Conversation
A .thvignore is read from the root of the directory being mounted, so whoever wrote that directory controls its patterns. A pattern with ".." segments, or one that goes through a symlink pointing out of the directory, matched host paths outside the mount, and the overlay was then bind-mounted outside the mount target in the container. That let a mounted repository blank arbitrary paths such as /etc/ssl in the MCP server container. Resolve symlinks on the match and the mount root, skip matches that fall outside the mount with a warning, and build the container path from the resolved relative path so the runtime never follows a symlink out of the mount target. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7196 +/- ##
==========================================
- Coverage 81.60% 81.58% -0.02%
==========================================
Files 896 898 +2
Lines 91441 91538 +97
==========================================
+ Hits 74619 74680 +61
- Misses 16817 16853 +36
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The container overlay path is joined from a relative path that was already confined, so the second guard and the extra filepath.Rel could never fail. Fold the containment check into one helper and cover the reachable error paths (missing mount root, dangling symlink match) with direct tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks for the quick fix! If it's not too much trouble, a |
|
Thanks again for the report, added |
peppescg
left a comment
There was a problem hiding this comment.
Approving. The fix closes both reported escapes, .. segments and symlinks that leave the mount, and the table test covers them well.
None of the inline comments block the merge. Three are worth a look here or in a follow-up:
- the race between this check and container start
- the misleading WARN for broken symlinks inside the mount
- escaping patterns being walked on the host before they are rejected
The rest are nits.
|
|
||
| // relWithin returns the path of target relative to dir and reports whether | ||
| // target is dir or a path below it. | ||
| func relWithin(dir, target string) (string, bool) { |
There was a problem hiding this comment.
nit: this is the same function as relWithin in pkg/skills/refcheck.go. It could move to pkg/fileutils so a future fix reaches both.
There was a problem hiding this comment.
Tried it, but pkg/ignore can't import pkg/fileutils: that creates a cycle (fileutils → workloads/types → container/runtime → ignore). Sharing the helper needs a new standalone package, so I left it local here and noted it in the PR body as a separate refactor. Leaving this thread open in case you'd rather do it in this PR.
🤖 Addressed by Claude Code
Reject patterns that escape the mount lexically before globbing them, so a line like "../../*/*" no longer walks the host filesystem and logs one warning per pattern instead of one per match. Resolve the mount root once per mount, skip matches that resolve to the mount root itself, and stat the resolved match. A match that cannot be resolved, such as a dangling symlink inside the mount, is logged at debug again instead of being reported as an escape. Co-Authored-By: Claude Opus 5.5 <[email protected]>
peppescg
left a comment
There was a problem hiding this comment.
Re-approving on 38860d6. All points from the earlier review are addressed:
- The lexical check in
processPatternnow rejects escaping patterns beforegetMatchingPaths, so they no longer glob the host, and it logs one WARN per pattern. - Only
errOutsideMountreaches the WARN. Broken symlinks inside the mount are back at DEBUG. - Matches that resolve to the mount root (
.,sub/..) are skipped. - The root is resolved once,
os.Statruns on the resolved match, and the comment is accurate. - The TOCTOU window and nested mounts are listed as known limitations in the PR body.
On relWithin: I confirmed the cycle (pkg/fileutils already depends on pkg/ignore through workloads/types and container/runtime), so keeping it local here and sharing it in a separate refactor is fine.
Summary
thv runreads a.thvignorefrom the root of every directory it bind-mounts and overlays each match with an empty directory or file. That file sits inside the mounted directory, so whoever wrote the directory, for example the author of a cloned repository, controls the patterns. Patterns were not confined to the mount:..segments (../../../../../../etc/ssl) matched a host path outside the mount, and the overlay target was built asfilepath.Join(containerPath, "../../.."), which lands outside/workspacein the container.link/sslwithlink -> /etc) passed a lexical check, and the container runtime then followed/workspace/linkinside the container when it set up the mount.Either way a mounted directory could blank arbitrary paths in the MCP server container, such as the CA store,
/etc/hostsor binaries. The overlay source is always empty, so no host data is read or written...) before globbing it, with one WARN per pattern, so a line such as../../*/*never walks the host filesystem..,sub/..), which would otherwise overlay the whole mount target.Reported-by: @mansur-akhmadi
Type of change
Test plan
task test)task test-e2e)pkg/ignore; localtask lint-fixhits the Go export-data skew oncrypto/subtle)The new table test covers
.., sibling, directory-pattern, glob, deep-traversal and symlink escapes, plus in-mount paths with and without a symlink. Every escape case fails againstmainand passes with this change.confinedRelPathandrelWithinalso have direct tests for a missing mount root, a dangling symlink, and relative or sibling paths.Manually, with Docker 29.9.3, I ran
thv run --volume "$PWD/proj:/workspace" alpine:3.20 -- sleep 600whereproj/.thvignoreheld../…/etc/ssl,../…/etc/hosts,link/ssl(withproj/link -> /etc) andsub/secret. Onmain,link/sslalone left/etc/sslempty in the container. With this change, the three escaping patterns are skipped with a WARN,/etc/ssland/etc/hostsare intact, and/workspace/sub/secretis still masked.Does this introduce a user-facing change?
Before: a
.thvignorepattern could match paths outside the mounted directory, through..or a symlink, and ToolHive masked the matching path in the container even though it was outside the mount target. After: such patterns are skipped and logged at WARN ("Skipping ignore pattern that reaches outside the mounted directory" for.., "Skipping ignore pattern match outside the mounted directory" for a symlink). A pattern that matches the mount root itself is skipped with a WARN. A pattern that goes through a symlink staying inside the mount now masks the symlink's target path in the container. Patterns that stay inside the mount behave as before.Special notes for reviewers
Known limitations, both outside what this change tries to cover:
-v a:/workspace -v b:/workspace/sub, an overlay computed forabelow/workspace/subis resolved throughbin the container, where a symlink inbcan redirect it.relWithinduplicates the helper inpkg/skills/refcheck.go. Moving it topkg/fileutilswould create an import cycle (fileutils → workloads/types → container/runtime → ignore), so sharing it needs a new standalone package and is left for a separate refactor.The global
~/.config/toolhive/thvignorepatterns go through the same check. They are joined onto each mount root, so a global pattern that reaches outside a mount is now skipped too. That is intended: an overlay should only ever hide content of the mount it was computed for.🤖 Generated with Claude Code