gortex icon indicating copy to clipboard operation
gortex copied to clipboard

Windows directory junctions bypass repository confinement (pathguard, guardSymlinkWithinRepo, batch destination guard)

Open zzet opened this issue 3 months ago • 1 comments

Summary

Repository confinement treats Windows directory junctions as ordinary directories. Under Go ≥ 1.23 (winsymlink=1, the default) a junction reparse point (IO_REPARSE_TAG_MOUNT_POINT) is reported by os.Lstat with ModeIrregular, not ModeSymlink, and filepath.EvalSymlinks no longer evaluates mount points ($GOROOT/src/os/types_windows.go, "winsymlink" in internal/godebugs/table.go). Every confinement guard keys on one of those two primitives:

  • internal/pathguard — isSymlink checks ModeSymlink only; linkTargetEscapes uses EvalSymlinks.
  • internal/mcp/tools_fileops.go — guardSymlinkWithinRepo / resolveNearestExistingAncestor use EvalSymlinks.
  • internal/mcp/batch_transaction.go — guardBatchLifecycleDestination walks destination components and refuses only ModeSymlink.

So a junction inside an indexed repository that points outside it (<repo>\link → C:\elsewhere) should pass all three guards on Windows: the lexical path is in-repo, EvalSymlinks returns it unchanged, and the component walk sees ModeIrregular. The consequences would be the same class the symlink guards exist for — indexing/serving out-of-repo bytes on the read side, and edit_file / write_file / batch_edit (move_file destination, delete_file source) landing outside the repository on the write side.

Status

Not reproduced — derived from the Go runtime source and the guard code while validating #544 (which explicitly asks for symlink and junction traversal to be refused). It needs a Windows host to confirm; CI's build-windows leg runs ./internal/pathguard, so a junction case there (cmd /c mklink /J) would settle it.

Suggested fix

Treat ModeIrregular reparse points as links in pathguard.isSymlink and in the batch destination walk, and resolve junction targets on Windows before the containment check (either os.Readlink on the reparse point, or opting the process into winsymlink=0 semantics for the resolution step). Add a junction test to internal/pathguard guarded by runtime.GOOS == "windows".

zzet avatar Sep 03 '26 12:09 zzet

Confirmed on Windows 11 with Go 1.27.0, using internal/pathguard and resolveNearestExistingAncestor copied unchanged from 1003759, not a full daemon run. With <repo>\link created by mklink /J and pointing outside the repo:

  • os.Lstat(link) reports ModeIrregular and EvalSymlinks(link) returns it unchanged, as you predicted.
  • EvalSymlinks(link\secret.txt) fails with "The system cannot find the path specified". guardSymlinkWithinRepo then falls back to resolveNearestExistingAncestor, which stops at link and keeps the lexical path, so the guard allows both an existing file and a new one. A plain os.WriteFile through that path lands outside the repo.
  • With GODEBUG=winsymlink=0, the same logic refuses both and still allows a junction that stays inside the repo.

I'd like to take this. //go:debug winsymlink=0 would be one line, but it's process-wide and doesn't reach pkg/gortex users, so I'd suggest a small junction-aware resolver in pathguard that all three guards share, plus a mklink /J test on the build-windows leg. #1036 lists junctions as hardening to pull in only if the release gate fires, so is now a good time, or would you rather wait?

NishilRathod avatar Nov 05 '26 14:11 NishilRathod