Skip to content

Confine .thvignore overlays to the mounted directory - #7196

Merged
rdimitrov merged 3 commits into
mainfrom
rdimitrov/thvignore-confine
Nov 20, 2026
Merged

rdimitrov merged 3 commits into
mainfrom
rdimitrov/thvignore-confine

Conversation

@rdimitrov

@rdimitrov rdimitrov commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Summary

thv run reads a .thvignore from 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:

  • A pattern with .. segments (../../../../../../etc/ssl) matched a host path outside the mount, and the overlay target was built as filepath.Join(containerPath, "../../.."), which lands outside /workspace in the container.
  • A pattern through a symlink that points out of the mount (link/ssl with link -> /etc) passed a lexical check, and the container runtime then followed /workspace/link inside 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/hosts or binaries. The overlay source is always empty, so no host data is read or written.

  • Reject a pattern that leaves the mount lexically (..) before globbing it, with one WARN per pattern, so a line such as ../../*/* never walks the host filesystem.
  • Resolve the mount root once per mount and the symlinks in each match, and skip a match that resolves outside the mount, with a WARN naming the pattern. A match that cannot be resolved, such as a dangling symlink inside the mount, is logged at DEBUG as before.
  • Skip a pattern that resolves to the mount root itself (., sub/..), which would otherwise overlay the whole mount target.
  • Build the container overlay path from the resolved relative path, so the destination never goes through a symlink inside the mount. A symlink that stays inside the mount now masks its target.

Reported-by: @mansur-akhmadi

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (CI-pinned golangci-lint v2.15.0 on pkg/ignore; local task lint-fix hits the Go export-data skew on crypto/subtle)
  • Manual testing (describe below)

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 against main and passes with this change. confinedRelPath and relWithin also 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 600 where proj/.thvignore held ../…/etc/ssl, ../…/etc/hosts, link/ssl (with proj/link -> /etc) and sub/secret. On main, link/ssl alone left /etc/ssl empty in the container. With this change, the three escaping patterns are skipped with a WARN, /etc/ssl and /etc/hosts are intact, and /workspace/sub/secret is still masked.

Does this introduce a user-facing change?

Before: a .thvignore pattern 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:

  • Time-of-check to time-of-use (TOCTOU). The check runs when the overlays are computed, but the runtime resolves mount destinations at container start. A process with write access to the workspace that swaps a checked directory for a symlink in that window can still redirect an overlay. A cloned repository cannot do this on its own, and the impact is still masking paths in that container.
  • Nested mounts. Confinement holds per mount. With -v a:/workspace -v b:/workspace/sub, an overlay computed for a below /workspace/sub is resolved through b in the container, where a symlink in b can redirect it.

relWithin duplicates the helper in pkg/skills/refcheck.go. Moving it to pkg/fileutils would 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/thvignore patterns 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

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]>
@github-actions github-actions Bot added the size/S Small PR: 100-299 lines changed label Nov 19, 2026
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.87500% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 81.58%. Comparing base (01b1d76) to head (38860d6).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
pkg/ignore/processor.go 96.87% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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]>
@github-actions github-actions Bot added size/S Small PR: 100-299 lines changed and removed size/S Small PR: 100-299 lines changed labels Nov 19, 2026
@mansur-akhmadi

Copy link
Copy Markdown

Thanks for the quick fix! If it's not too much trouble, a Reported-by: @mansur-akhmadi line in the PR or release notes would be appreciated.

@rdimitrov

Copy link
Copy Markdown
Member Author

Thanks again for the report, added Reported-by to the PR and it'll be carried into the merge commit.

@github-actions github-actions Bot added size/S Small PR: 100-299 lines changed and removed size/S Small PR: 100-299 lines changed labels Nov 19, 2026
peppescg
peppescg previously approved these changes Nov 20, 2026

@peppescg peppescg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/ignore/processor.go Resolved
Comment thread pkg/ignore/processor.go Resolved
Comment thread pkg/ignore/processor.go Resolved
Comment thread pkg/ignore/processor.go Resolved
Comment thread pkg/ignore/processor.go Resolved
Comment thread pkg/ignore/processor.go Resolved
Comment thread pkg/ignore/processor.go Resolved
Comment thread pkg/ignore/processor.go

// 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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@github-actions github-actions Bot added size/S Small PR: 100-299 lines changed and removed size/S Small PR: 100-299 lines changed labels Nov 20, 2026

@peppescg peppescg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approving on 38860d6. All points from the earlier review are addressed:

  • The lexical check in processPattern now rejects escaping patterns before getMatchingPaths, so they no longer glob the host, and it logs one WARN per pattern.
  • Only errOutsideMount reaches 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.Stat runs 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Small PR: 100-299 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants