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 all 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
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,115 @@ | ||
| package sandbox | ||
|
|
||
| import ( | ||
| "errors" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| 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 pointer file. Protect the file | ||
| // itself and never create a bwrap mount target beneath it. | ||
| func TestGitMetadataWriteCarveoutsProtectsWorktreePointer(t *testing.T) { | ||
| root := t.TempDir() | ||
| gitPath := filepath.Join(root, ".git") | ||
| if err := os.WriteFile(gitPath, []byte("gitdir: ../outside\n"), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| got := gitMetadataWriteCarveouts(root) | ||
| mustEqualCarveouts(t, got, []string{gitPath}) | ||
| for _, carveout := range got { | ||
| if carveout == filepath.Join(gitPath, "hooks") || carveout == filepath.Join(gitPath, "config") { | ||
| t.Fatalf("carveout points beneath .git file: %q", carveout) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestLinuxBwrapPlanDoesNotMountBelowWorktreePointer(t *testing.T) { | ||
| root := t.TempDir() | ||
| gitPath := filepath.Join(root, ".git") | ||
| if err := os.WriteFile(gitPath, []byte("gitdir: ../outside\n"), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| profile := PermissionProfile{FileSystem: FileSystemPolicy{ | ||
| Kind: FileSystemRestricted, | ||
| WriteRoots: []WritableRoot{{ | ||
| Root: root, | ||
| ReadOnlySubpaths: gitMetadataWriteCarveouts(root), | ||
| }}, | ||
| }} | ||
| args := linuxBwrapFilesystemArgs(profile) | ||
| assertArgsContainSequence(t, args, "--ro-bind", gitPath, gitPath) | ||
| for _, child := range []string{"hooks", "config"} { | ||
| bogus := filepath.Join(gitPath, child) | ||
| if argsContainSequence(args, "--tmpfs", bogus) || argsContainSequence(args, "--ro-bind", bogus, bogus) { | ||
| t.Fatalf("bwrap args contain mount below .git file %q: %v", bogus, args) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestGitMetadataWriteCarveoutsPlainCheckout(t *testing.T) { | ||
| root := t.TempDir() | ||
| want := []string{ | ||
| filepath.Join(root, ".git", "hooks"), | ||
| filepath.Join(root, ".git", "config"), | ||
| } | ||
| mustEqualCarveouts(t, gitMetadataWriteCarveouts(root), want) | ||
|
|
||
| if err := os.MkdirAll(filepath.Join(root, ".git"), 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| mustEqualCarveouts(t, gitMetadataWriteCarveouts(root), want) | ||
| } | ||
|
|
||
| func TestGitMetadataWriteCarveoutsPreservesSymlinkBehavior(t *testing.T) { | ||
| for _, targetKind := range []string{"directory", "file", "dangling"} { | ||
| t.Run(targetKind, func(t *testing.T) { | ||
| root := t.TempDir() | ||
| target := filepath.Join(root, "real.git") | ||
| switch targetKind { | ||
| case "directory": | ||
| if err := os.MkdirAll(target, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| case "file": | ||
| if err := os.WriteFile(target, []byte("gitdir: ../outside\n"), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| if err := os.Symlink(target, filepath.Join(root, ".git")); err != nil { | ||
| t.Skipf("symlink unavailable: %v", err) | ||
| } | ||
| want := []string{ | ||
| filepath.Join(root, ".git", "hooks"), | ||
| filepath.Join(root, ".git", "config"), | ||
| } | ||
| mustEqualCarveouts(t, gitMetadataWriteCarveouts(root), want) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGitMetadataWriteCarveoutsPreservesInspectionErrorBehavior(t *testing.T) { | ||
| root := t.TempDir() | ||
| gitPath := filepath.Join(root, ".git") | ||
| want := []string{ | ||
| filepath.Join(gitPath, "hooks"), | ||
| filepath.Join(gitPath, "config"), | ||
| } | ||
| got := gitMetadataWriteCarveoutsWithLstat(root, func(string) (os.FileInfo, error) { | ||
| return nil, errors.New("inspection failed") | ||
| }) | ||
| mustEqualCarveouts(t, got, want) | ||
| } |
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,21 @@ | ||
| //go:build unix | ||
|
|
||
| package sandbox | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "syscall" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestGitMetadataWriteCarveoutsPreservesSpecialGitEntryBehavior(t *testing.T) { | ||
| root := t.TempDir() | ||
| gitPath := filepath.Join(root, ".git") | ||
| if err := syscall.Mkfifo(gitPath, 0o644); err != nil { | ||
| t.Skip(err) | ||
| } | ||
| mustEqualCarveouts(t, gitMetadataWriteCarveouts(root), []string{ | ||
| filepath.Join(gitPath, "hooks"), | ||
| filepath.Join(gitPath, "config"), | ||
| }) | ||
| } |
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.