Repository navigation
fix(sandbox): protect worktree git pointers #805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kevincodex1
merged 11 commits into
Twigpine:main
from
jaivial:fix/sandbox-worktree-git-carveout
Sep 7, 2026
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3303dc0
fix(sandbox): resolve git metadata carveouts for worktrees
jaivial 84446e1
fix(sandbox): bound worktree git metadata carveouts to the write root
jatmn 1b72ceb
fix(sandbox): evaluate gitdir/commondir through symlinks
jatmn 7134ddd
fix(sandbox): fail closed on gitdir eval and protect the pointer file
jatmn 5e717da
fix(sandbox): deny worktree-local gitdir metadata and parse first git…
jatmn a8af70c
fix(sandbox): skip special git metadata files and re-check carveouts …
jatmn e25d7e5
fix(sandbox): follow directory .git symlinks and unix-tag FIFO tests
jatmn 014c32e
fix(sandbox): protect worktree git pointer directly
jatmn 02b3ebc
test(sandbox): skip unavailable symlink fixture
jatmn 79419ef
fix(sandbox): preserve git symlink semantics
jatmn 2fdff70
fix(sandbox): limit git pointer carveout to regular files
jatmn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| package sandbox | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func writeGitFile(t *testing.T, path, contents string) { | ||
| t.Helper() | ||
| if err := os.WriteFile(path, []byte(contents), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| func mustEqualCarveouts(t *testing.T, got, want []string) { | ||
| t.Helper() | ||
| if len(got) != len(want) { | ||
| t.Fatalf("carveouts = %v, want %v", got, want) | ||
| } | ||
| for i := range want { | ||
| if got[i] != want[i] { | ||
| t.Fatalf("carveout[%d] = %q, want %q", i, got[i], want[i]) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Worktree/submodule checkouts store .git as a *file* ("gitdir: <path>"). The | ||
| // hooks/config carveout must resolve the real (common) git dir instead of the | ||
| // bogus <root>/.git/hooks under that file — a --tmpfs there makes bwrap fail | ||
| // ("Can't mkdir parents ... Not a directory") and blocks every sandboxed tool. | ||
| func TestGitMetadataWriteCarveoutsResolvesWorktree(t *testing.T) { | ||
| root := t.TempDir() | ||
| commonGit := filepath.Join(root, ".git-common") | ||
| worktreeGit := filepath.Join(root, ".git-wt") | ||
| if err := os.MkdirAll(worktreeGit, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.MkdirAll(commonGit, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| writeGitFile(t, filepath.Join(worktreeGit, "commondir"), filepath.Join("..", ".git-common")+"\n") | ||
| writeGitFile(t, filepath.Join(root, ".git"), "gitdir: "+worktreeGit+"\n") | ||
|
|
||
| got := gitMetadataWriteCarveouts(root) | ||
| want := []string{ | ||
| filepath.Join(root, ".git"), | ||
| filepath.Join(commonGit, "hooks"), | ||
| filepath.Join(commonGit, "config"), | ||
| filepath.Join(worktreeGit, "hooks"), | ||
| filepath.Join(worktreeGit, "config"), | ||
| } | ||
| mustEqualCarveouts(t, got, want) | ||
| bogus := filepath.Join(root, ".git", "hooks") | ||
| for _, c := range got { | ||
| if c == bogus { | ||
| t.Fatalf("carveout points under .git file: %q", c) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestGitMetadataWriteCarveoutsOutsideWorktreeGitdirReturnsNone(t *testing.T) { | ||
| main := t.TempDir() | ||
| commonGit := filepath.Join(main, ".git") | ||
| worktreeGit := filepath.Join(commonGit, "worktrees", "wt") | ||
| if err := os.MkdirAll(worktreeGit, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| writeGitFile(t, filepath.Join(worktreeGit, "commondir"), "../..\n") | ||
| worktree := t.TempDir() | ||
| writeGitFile(t, filepath.Join(worktree, ".git"), "gitdir: "+worktreeGit+"\n") | ||
| got := gitMetadataWriteCarveouts(worktree) | ||
| want := []string{filepath.Join(worktree, ".git")} | ||
| mustEqualCarveouts(t, got, want) | ||
| } | ||
|
|
||
| func TestGitMetadataWriteCarveoutsPlainCheckout(t *testing.T) { | ||
| root := t.TempDir() | ||
| want := []string{ | ||
| filepath.Join(root, ".git", "hooks"), | ||
| filepath.Join(root, ".git", "config"), | ||
| } | ||
| got := gitMetadataWriteCarveouts(root) | ||
| mustEqualCarveouts(t, got, want) | ||
|
|
||
| if err := os.MkdirAll(filepath.Join(root, ".git"), 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| got = gitMetadataWriteCarveouts(root) | ||
| mustEqualCarveouts(t, got, want) | ||
| } | ||
|
|
||
| func TestGitMetadataWriteCarveoutsRejectsEscapingGitdir(t *testing.T) { | ||
| outside := t.TempDir() | ||
| if err := os.MkdirAll(filepath.Join(outside, "git"), 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| root := t.TempDir() | ||
| writeGitFile(t, filepath.Join(root, ".git"), "gitdir: "+filepath.Join(outside, "git")+"\n") | ||
| got := gitMetadataWriteCarveouts(root) | ||
| mustEqualCarveouts(t, got, []string{filepath.Join(root, ".git")}) | ||
| } | ||
|
|
||
| func TestGitMetadataWriteCarveoutsRejectsRelativeEscapingGitdir(t *testing.T) { | ||
| parent := t.TempDir() | ||
| outside := filepath.Join(parent, "elsewhere") | ||
| if err := os.MkdirAll(outside, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| root := filepath.Join(parent, "ws") | ||
| if err := os.MkdirAll(root, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| writeGitFile(t, filepath.Join(root, ".git"), "gitdir: ../elsewhere\n") | ||
| got := gitMetadataWriteCarveouts(root) | ||
| mustEqualCarveouts(t, got, []string{filepath.Join(root, ".git")}) | ||
| } | ||
|
|
||
| func TestGitMetadataWriteCarveoutsRejectsEscapingCommondir(t *testing.T) { | ||
| root := t.TempDir() | ||
| gitDir := filepath.Join(root, ".git-real") | ||
| if err := os.MkdirAll(gitDir, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| outside := t.TempDir() | ||
| writeGitFile(t, filepath.Join(gitDir, "commondir"), outside+"\n") | ||
| writeGitFile(t, filepath.Join(root, ".git"), "gitdir: "+gitDir+"\n") | ||
| got := gitMetadataWriteCarveouts(root) | ||
| mustEqualCarveouts(t, got, []string{filepath.Join(root, ".git")}) | ||
| } | ||
|
|
||
| func TestGitMetadataWriteCarveoutsCommondirReadFailureKeepsGitDir(t *testing.T) { | ||
| root := t.TempDir() | ||
| gitDir := filepath.Join(root, ".git-real") | ||
| if err := os.MkdirAll(gitDir, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| writeGitFile(t, filepath.Join(root, ".git"), "gitdir: "+gitDir+"\n") | ||
| got := gitMetadataWriteCarveouts(root) | ||
| want := []string{ | ||
| filepath.Join(root, ".git"), | ||
| filepath.Join(gitDir, "hooks"), | ||
| filepath.Join(gitDir, "config"), | ||
| } | ||
| mustEqualCarveouts(t, got, want) | ||
| } | ||
|
|
||
| func TestGitMetadataWriteCarveoutsStaleGitdirReturnsNone(t *testing.T) { | ||
| root := t.TempDir() | ||
| writeGitFile(t, filepath.Join(root, ".git"), "gitdir: missing-target\n") | ||
| got := gitMetadataWriteCarveouts(root) | ||
| mustEqualCarveouts(t, got, []string{filepath.Join(root, ".git")}) | ||
| } | ||
|
|
||
| func TestGitMetadataWriteCarveoutsRejectsNonDirectoryTarget(t *testing.T) { | ||
| root := t.TempDir() | ||
| target := filepath.Join(root, "not-a-dir") | ||
| writeGitFile(t, target, "nope\n") | ||
| writeGitFile(t, filepath.Join(root, ".git"), "gitdir: "+target+"\n") | ||
| got := gitMetadataWriteCarveouts(root) | ||
| mustEqualCarveouts(t, got, []string{filepath.Join(root, ".git")}) | ||
| } | ||
|
|
||
| func TestGitMetadataWriteCarveoutsRejectsSymlinkGitdir(t *testing.T) { | ||
| outside := t.TempDir() | ||
| root := t.TempDir() | ||
| alias := filepath.Join(root, "alias") | ||
| if err := os.Symlink(outside, alias); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| writeGitFile(t, filepath.Join(root, ".git"), "gitdir: "+alias+"\n") | ||
| got := gitMetadataWriteCarveouts(root) | ||
| mustEqualCarveouts(t, got, []string{filepath.Join(root, ".git")}) | ||
| } | ||
|
|
||
| func TestGitMetadataWriteCarveoutsResolvesWorktreeUnderSymlinkRoot(t *testing.T) { | ||
| realRoot := t.TempDir() | ||
| commonGit := filepath.Join(realRoot, ".git-common") | ||
| worktreeGit := filepath.Join(realRoot, ".git-wt") | ||
| if err := os.MkdirAll(worktreeGit, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.MkdirAll(commonGit, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| writeGitFile(t, filepath.Join(worktreeGit, "commondir"), filepath.Join("..", ".git-common")+"\n") | ||
| writeGitFile(t, filepath.Join(realRoot, ".git"), "gitdir: "+worktreeGit+"\n") | ||
| parent := t.TempDir() | ||
| alias := filepath.Join(parent, "ws") | ||
| if err := os.Symlink(realRoot, alias); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| got := gitMetadataWriteCarveouts(alias) | ||
| want := []string{ | ||
| filepath.Join(alias, ".git"), | ||
| filepath.Join(alias, ".git-common", "hooks"), | ||
| filepath.Join(alias, ".git-common", "config"), | ||
| filepath.Join(alias, ".git-wt", "hooks"), | ||
| filepath.Join(alias, ".git-wt", "config"), | ||
| } | ||
| mustEqualCarveouts(t, got, want) | ||
| } | ||
|
|
||
| func TestGitMetadataWriteCarveoutsParsesFirstGitdirLine(t *testing.T) { | ||
| root := t.TempDir() | ||
| gitDir := filepath.Join(root, ".git-real") | ||
| if err := os.MkdirAll(gitDir, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| writeGitFile(t, filepath.Join(root, ".git"), "gitdir: "+gitDir+"\n# comment\n") | ||
| got := gitMetadataWriteCarveouts(root) | ||
| want := []string{ | ||
| filepath.Join(root, ".git"), | ||
| filepath.Join(gitDir, "hooks"), | ||
| filepath.Join(gitDir, "config"), | ||
| } | ||
| mustEqualCarveouts(t, got, want) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.