-
Notifications
You must be signed in to change notification settings - Fork 0
fix(agent): grant the linked worktree's git dirs to the OS sandbox (P7-SANDBOX-01) #142
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
Open
Reederey87
wants to merge
1
commit into
main
Choose a base branch
from
fix/p7-sandbox-01
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,98 @@ | ||
| package git | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| // TestWorktreeSandboxWriteDirs proves the resolver grants exactly the git | ||
| // storage a linked worktree writes for add/commit — objects, refs, logs, and | ||
| // the per-worktree admin dir — and NEVER the common dir itself or its | ||
| // hooks/config (which would be a sandbox escape, P7-SANDBOX-01). | ||
| func TestWorktreeSandboxWriteDirs(t *testing.T) { | ||
| repo, r := initSquashMergeRepo(t) // real local repo on branch main with one commit | ||
| ctx := context.Background() | ||
|
|
||
| wt := filepath.Join(t.TempDir(), "wt") | ||
| if err := r.WorktreeAdd(ctx, repo, wt, "agent/x", "main"); err != nil { | ||
| t.Fatalf("WorktreeAdd: %v", err) | ||
| } | ||
|
|
||
| dirs, err := r.WorktreeSandboxWriteDirs(ctx, wt) | ||
| if err != nil { | ||
| t.Fatalf("WorktreeSandboxWriteDirs: %v", err) | ||
| } | ||
| if len(dirs) != 4 { | ||
| t.Fatalf("want 4 grant dirs (objects/refs/logs + per-worktree admin), got %d: %v", len(dirs), dirs) | ||
| } | ||
|
|
||
| commonReal := mustEval(t, filepath.Join(repo, ".git")) | ||
| var bases []string | ||
| sawWorktreeAdmin := false | ||
| for _, d := range dirs { | ||
| // Security invariants: never the common dir root, never hooks/, never config. | ||
| if d == commonReal { | ||
| t.Errorf("grant includes the common dir root %q — would expose hooks/config (sandbox escape)", d) | ||
| } | ||
| if strings.Contains(d, string(os.PathSeparator)+"hooks") || strings.HasSuffix(d, string(os.PathSeparator)+"config") { | ||
| t.Errorf("grant includes a hooks/config path %q", d) | ||
| } | ||
| bases = append(bases, filepath.Base(d)) | ||
| if strings.Contains(d, string(os.PathSeparator)+"worktrees"+string(os.PathSeparator)) { | ||
| sawWorktreeAdmin = true | ||
| } | ||
| } | ||
| for _, want := range []string{"objects", "refs", "logs"} { | ||
| if !contains(bases, want) { | ||
| t.Errorf("missing grant for %s; bases=%v", want, bases) | ||
| } | ||
| } | ||
| if !sawWorktreeAdmin { | ||
| t.Errorf("missing the per-worktree admin dir (…/worktrees/<name>); dirs=%v", dirs) | ||
| } | ||
| } | ||
|
|
||
| // TestWorktreeSandboxWriteDirsNonRepo returns (nil, nil) outside a git worktree | ||
| // so the caller grants nothing without special-casing. | ||
| func TestWorktreeSandboxWriteDirsNonRepo(t *testing.T) { | ||
| r := Runner{Bin: gitBinOrSkip(t), Timeout: 5 * time.Second} | ||
| dirs, err := r.WorktreeSandboxWriteDirs(context.Background(), t.TempDir()) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if dirs != nil { | ||
| t.Fatalf("want nil grant outside a git worktree, got %v", dirs) | ||
| } | ||
| } | ||
|
|
||
| func mustEval(t *testing.T, p string) string { | ||
| t.Helper() | ||
| r, err := filepath.EvalSymlinks(p) | ||
| if err != nil { | ||
| return filepath.Clean(p) | ||
| } | ||
| return r | ||
| } | ||
|
|
||
| func contains(s []string, v string) bool { | ||
| for _, x := range s { | ||
| if x == v { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func gitBinOrSkip(t *testing.T) string { | ||
| t.Helper() | ||
| bin, err := exec.LookPath("git") | ||
| if err != nil { | ||
| t.Skip("git not installed") | ||
| } | ||
| return bin | ||
| } | ||
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
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,65 @@ | ||
| package platform | ||
|
|
||
| import ( | ||
| "slices" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| // gitStorageDirs are the kind of paths WorktreeSandboxWriteDirs yields: the | ||
| // linked worktree's git storage, never the common dir root or hooks/config. | ||
| var gitStorageDirs = []string{ | ||
| "/home/dev/clone/.git/objects", | ||
| "/home/dev/clone/.git/refs", | ||
| "/home/dev/clone/.git/logs", | ||
| "/home/dev/clone/.git/worktrees/agent-x", | ||
| } | ||
|
|
||
| // TestSBPLProfileGrantsGitDirs proves the Seatbelt write allow-list includes | ||
| // each git storage dir so an agent `git commit` in a linked worktree is not | ||
| // EPERM'd (P7-SANDBOX-01) — the platform where the default-on sandbox bites. | ||
| func TestSBPLProfileGrantsGitDirs(t *testing.T) { | ||
| spec := SandboxSpec{WorktreeDir: "/wt", TmpDir: "/tmp", GitDirs: gitStorageDirs} | ||
| profile := sbplProfile(spec, nil, nil) | ||
| for _, d := range gitStorageDirs { | ||
| if !strings.Contains(profile, `(subpath "`+d+`")`) { | ||
| t.Errorf("SBPL write allow-list missing git dir %q\n%s", d, profile) | ||
| } | ||
| } | ||
| // The common dir root must never be granted wholesale (hooks/config escape). | ||
| if strings.Contains(profile, `(subpath "/home/dev/clone/.git")`) { | ||
| t.Error("SBPL grants the whole common dir — hooks/config sandbox escape") | ||
| } | ||
| } | ||
|
|
||
| // TestBwrapArgsGrantsGitDirs proves the bubblewrap write binds include each git | ||
| // storage dir (via --bind-try, tolerating an absent reflog). | ||
| func TestBwrapArgsGrantsGitDirs(t *testing.T) { | ||
| spec := SandboxSpec{WorktreeDir: "/wt", TmpDir: "/tmp", GitDirs: gitStorageDirs} | ||
| args := bwrapArgs(spec, nil, nil, bwrapOptions{}) | ||
| for _, d := range gitStorageDirs { | ||
| if indexSequence(args, "--bind-try", d, d) == -1 { | ||
| t.Errorf("bwrap args missing --bind-try %s %s\n%v", d, d, args) | ||
| } | ||
| } | ||
| if slices.Contains(args, "/home/dev/clone/.git") { | ||
| t.Error("bwrap binds the whole common dir — hooks/config sandbox escape") | ||
| } | ||
| } | ||
|
|
||
| // TestReadConfineRootsIncludesGitDirs proves git storage stays readable under | ||
| // --read-confine (readonly policy) so git read ops work; the RW grant already | ||
| // makes them writable elsewhere. | ||
| func TestReadConfineRootsIncludesGitDirs(t *testing.T) { | ||
| roots := readConfineRoots(SandboxSpec{ | ||
| WorktreeDir: "/wt", | ||
| TmpDir: "/tmp", | ||
| ReadConfine: true, | ||
| GitDirs: gitStorageDirs, | ||
| }) | ||
| for _, d := range gitStorageDirs { | ||
| if !slices.Contains(roots, d) { | ||
| t.Errorf("read-confine roots missing git dir %q: %v", d, roots) | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: Reederey87/DevStrap
Length of output: 414
🏁 Script executed:
Repository: Reederey87/DevStrap
Length of output: 1353
🏁 Script executed:
Repository: Reederey87/DevStrap
Length of output: 5566
Cover the plain-checkout branch too Add a case for the
gitDirAbs == commonAbspath in a regular checkout; the current tests only cover linked worktrees and non-repos.🤖 Prompt for AI Agents