Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
e68fb87
fix(sandbox): agree on the runtime root across the Windows setup marker
Vasanthdev2004 Aug 13, 2026
920e26e
fix(sandbox): create the Windows runtime roots the ACL plan grants
Vasanthdev2004 Aug 13, 2026
4791a0a
test(sandbox): keep the provisioning tests inside test-owned directories
Vasanthdev2004 Aug 13, 2026
2a71caf
fix(sandbox): pin the Windows runtime root instead of deriving it twice
Vasanthdev2004 Aug 13, 2026
6fcd522
test(sandbox): compare canonical spellings in the ownership guard
Vasanthdev2004 Aug 14, 2026
1b4f880
test(sandbox): decide the alias skip by filesystem identity, not by t…
Vasanthdev2004 Aug 14, 2026
d64cc67
fix(sandbox): name both plan hashes when the setup marker mismatches
Vasanthdev2004 Aug 14, 2026
b57d3e4
test(sandbox): assert the case-folding contract only where it exists
Vasanthdev2004 Aug 14, 2026
3d5cdef
fix(sandbox): decide runtime-root containment on physical paths, not …
Vasanthdev2004 Aug 14, 2026
851bf7b
test(sandbox): alias the cache by its real spelling, not a stacked ca…
Vasanthdev2004 Aug 14, 2026
1811e7b
fix(sandbox): retry the final-path buffer whenever the size is not a …
Vasanthdev2004 Aug 19, 2026
5fc455b
fix(sandbox): roll back runtime roots a failed setup created, and kee…
Vasanthdev2004 Aug 20, 2026
e3a65bc
fix(sandbox): make the selected runtime root a setup-to-command contr…
Vasanthdev2004 Aug 20, 2026
62a835c
fix(sandbox): record the runtime root, open it by handle, roll back c…
Vasanthdev2004 Aug 21, 2026
725967f
fix(sandbox): stop building the runtime tree through a path other use…
Vasanthdev2004 Aug 21, 2026
b116603
fix(sandbox): stamp the runtime tree through the handle that carries …
Vasanthdev2004 Aug 22, 2026
63322d0
fix(sandbox): stop the sandbox rewriting the attestation about itself
Vasanthdev2004 Aug 22, 2026
e280f0e
fix(sandbox): make the ACL and its stamp one transaction, and fail se…
Vasanthdev2004 Aug 24, 2026
bff135a
test(sandbox): compare the DACL's entries, not the descriptor's SDDL
Vasanthdev2004 Aug 24, 2026
b45b38d
fix(sandbox): put the user boundary above every private fallback comp…
Vasanthdev2004 Aug 26, 2026
072d3fe
fix(sandbox): attest the object before trusting the applied-plan marker
Vasanthdev2004 Aug 26, 2026
29795b4
fix(sandbox): make rollback prove it holds the object it changed
Vasanthdev2004 Aug 26, 2026
c73e47a
fix(sandbox): recognise the user-scoped fallback as an owned runtime …
Vasanthdev2004 Aug 27, 2026
fb96d06
fix(sandbox): attest the effective grant, not the presence of the SID
Vasanthdev2004 Aug 27, 2026
c965339
fix(sandbox): attest the grant on both tiers, and roll back through o…
Vasanthdev2004 Aug 27, 2026
9f34f4a
fix(sandbox): put the grant attestation on the launch gate, not in th…
Vasanthdev2004 Aug 27, 2026
25075d0
fix(sandbox): one sandbox-home authority, and keep the tests out of t…
Vasanthdev2004 Aug 27, 2026
ddd236f
fix(sandbox): hold the runtime lease through setup, and bind compensa…
Vasanthdev2004 Aug 27, 2026
4b2e10a
test(sandbox): give the lease tests the runtime root shape production…
Vasanthdev2004 Aug 27, 2026
5737cb3
fix(sandbox): grant the stamp to the identity that validates it
Vasanthdev2004 Aug 28, 2026
da85c67
fix(sandbox): refuse compensation it cannot bind to this run's object
Vasanthdev2004 Aug 28, 2026
8b63915
fix(sandbox): mutate compensation targets through the verified handle
Vasanthdev2004 Aug 28, 2026
fbd6b47
test(sandbox): fail the rollback fixture on identity, not on a stale …
Vasanthdev2004 Aug 28, 2026
c879333
fix(sandbox): keep repair write when the reader is a repair identity
Vasanthdev2004 Aug 28, 2026
e7c23b3
fix(sandbox): treat only proven absence as a verified removal
Vasanthdev2004 Aug 29, 2026
7fb33c3
fix(sandbox): carry the stamp reader across the elevation boundary
Vasanthdev2004 Aug 29, 2026
b3c0ac9
fix(sandbox): assemble rollback records from one object, not one name
Vasanthdev2004 Aug 29, 2026
9a289b9
fix(sandbox): prove the runtime create and stamp records before mutating
Vasanthdev2004 Aug 31, 2026
427e716
fix(sandbox): create the owned runtime tail from retained handles
Vasanthdev2004 Sep 2, 2026
fcedabf
fix(doctor): ask whether a command would still select the recorded root
Vasanthdev2004 Sep 2, 2026
bcbdd65
fix(sandbox,doctor): fix the boundary each carried fact crosses
Vasanthdev2004 Sep 3, 2026
0057381
fix(sandbox): acquire the setup lease through the handle boundary too
Vasanthdev2004 Sep 4, 2026
ea56072
test(sandbox): reproduce the lease junction race, not a pre-planted j…
Vasanthdev2004 Sep 4, 2026
4315bec
fix(sandbox): bind the stamp snapshot and the ACL apply to one object
Vasanthdev2004 Sep 4, 2026
b314301
fix(sandbox): keep a recorded fallback authoritative across a TEMP ch…
Vasanthdev2004 Sep 4, 2026
aaf681d
fix(sandbox): make both lease holders verify the same ordinary file
Vasanthdev2004 Sep 5, 2026
bd031f0
test(sandbox): record what the unprivileged reparse case cannot show
Vasanthdev2004 Sep 5, 2026
ec86c77
fix(sandbox): create the POSIX runtime tail through a no-follow boundary
Vasanthdev2004 Sep 5, 2026
0504fa7
fix(sandbox,cli): carry the lease creation ledger into setup compensa…
Vasanthdev2004 Sep 5, 2026
37d08e3
test(sandbox): pin that a pre-existing lease file survives rollback
Vasanthdev2004 Sep 5, 2026
ec85bd5
fix(sandbox): hold cleanup exclusivity across every mutation that nee…
Vasanthdev2004 Sep 7, 2026
3da7325
fix(sandbox): publish creation ownership before the step that can fail
Vasanthdev2004 Sep 7, 2026
d7f047d
test(sandbox): drive the lease-creation failure through the real builder
Vasanthdev2004 Sep 7, 2026
8bebff7
Merge origin/main into fix/windows-setup-marker-runtime-root
Vasanthdev2004 Sep 12, 2026
72515e0
fix(sandbox): a lease is only a lease while its name still resolves t…
Vasanthdev2004 Sep 12, 2026
3eda0c1
fix(sandbox): reject a capability grant that stops at the immediate c…
Vasanthdev2004 Sep 13, 2026
b98cdde
Merge remote-tracking branch 'origin/main' into HEAD
Vasanthdev2004 Sep 13, 2026
6235e85
Merge remote-tracking branch 'origin/main' into fix/windows-setup-mar…
Vasanthdev2004 Sep 18, 2026
5e574c7
fix(sandbox): scope the runtime attestation to its plan and order set…
Vasanthdev2004 Sep 18, 2026
0a600a8
test(sandbox): a setup parked mid-transaction still excludes eviction
Vasanthdev2004 Sep 18, 2026
1d75e35
test(sandbox): a fixture that owns its inputs fails instead of skipping
Vasanthdev2004 Sep 18, 2026
a4a248c
fix(sandbox): one resolver for selection and its diagnostic, and admi…
Vasanthdev2004 Sep 21, 2026
87124fb
test(sandbox): pin the ACE type rule on its own, beside the size rule
Vasanthdev2004 Sep 21, 2026
ed355ca
fix(sandbox): answer the open review threads on the runtime root
Vasanthdev2004 Sep 24, 2026
c84640c
test(sandbox): derive the fallback root from the canonical workspace,…
Vasanthdev2004 Sep 24, 2026
1e0120c
fix(sandbox): replace the runtime stamp in one step
Vasanthdev2004 Sep 26, 2026
34dea45
fix(sandbox): report a failed setup lock release instead of succeeding
Vasanthdev2004 Sep 26, 2026
7f9ca08
fix(sandbox): prepare the runtime tree's children through a handle on…
Vasanthdev2004 Sep 26, 2026
4c94548
chore(sandbox): ignore the read-only closes explicitly, as lint asks
Vasanthdev2004 Sep 26, 2026
158d5ee
Merge remote-tracking branch 'origin/main' into fix/windows-setup-mar…
Vasanthdev2004 Oct 17, 2026
0121fc4
chore: follow the Twigpine module path
Vasanthdev2004 Oct 17, 2026
d6d0d0c
Merge remote-tracking branch 'origin/main' into fix/windows-setup-mar…
Vasanthdev2004 Oct 17, 2026
ae30755
refactor(doctor): fingerprint the recorded runtime root that was just…
Vasanthdev2004 Oct 17, 2026
de4dfbe
fix(sandbox): say when an ACL rollback target was never identified
Vasanthdev2004 Oct 17, 2026
b46e943
Merge remote-tracking branch 'origin/main' into fix/windows-setup-mar…
Vasanthdev2004 Oct 25, 2026
fc8999b
Merge remote-tracking branch 'origin/main' into fix/windows-setup-mar…
Vasanthdev2004 Nov 17, 2026
d06baec
fix(sandbox): fail closed on command env home resolution
jatmn Nov 17, 2026
8a520e9
test(sandbox): require directory compensation to remove the swapped o…
jatmn Nov 17, 2026
5f0e89c
test(sandbox): fail closed on sandbox-home errors on every OS
jatmn Nov 17, 2026
1ed2723
fix(sandbox): preserve published Windows setup state on helper error
jatmn Nov 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion internal/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ func defaultAppDeps() appDeps {
cmd := exec.Command(path, args...)
cmd.Stdout = stdout
cmd.Stderr = stderr
return cmd.Run()
if err := cmd.Start(); err != nil {
return fmt.Errorf("%w: %w", errSandboxSetupHelperNotStarted, err)
}
return cmd.Wait()
},
registerMCPTools: func(ctx context.Context, registry *tools.Registry, cfg config.MCPConfig, options mcp.RegisterOptions) (mcpToolRuntime, error) {
return mcp.RegisterTools(ctx, registry, cfg, options)
Expand Down
21 changes: 18 additions & 3 deletions internal/cli/sandbox.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cli

import (
"errors"
"fmt"
"io"
"os/exec"
"path/filepath"
"strings"

Expand All @@ -20,6 +22,8 @@ type sandboxCommandOptions struct {

const permissionProfileScopeNote = "permissionProfile is derived from this process's environment and working directory; a command run with its own environment can add further credential deny-read entries."

var errSandboxSetupHelperNotStarted = errors.New("sandbox setup helper did not start")

func runSandbox(args []string, stdout io.Writer, stderr io.Writer, deps appDeps) int {
if len(args) == 0 {
return writeExecUsageError(stderr, "sandbox subcommand required. Use `zero sandbox policy` or `zero sandbox grants list`.")
Expand Down Expand Up @@ -174,17 +178,28 @@ func runSandboxSetup(args []string, stdout io.Writer, stderr io.Writer, deps app
if !setupHelper.Available() {
return writeAppError(stderr, "Windows sandbox setup helper is not available", exitProvider)
}
setupArgs, err := zeroSandbox.BuildWindowsSandboxSetupArgs(zeroSandbox.WindowsSandboxSetupArgsOptions{
setupPlan, err := zeroSandbox.BuildWindowsSandboxSetupArgs(zeroSandbox.WindowsSandboxSetupArgsOptions{
CommandCWD: workspaceRoot,
WorkspaceRoots: []string{workspaceRoot},
PermissionProfile: profile,
})
if err != nil {
return writeAppError(stderr, err.Error(), exitCrash)
}
setupArgs = append(append([]string{}, setupHelper.ArgsPrefix...), setupArgs...)
setupArgs := append(append([]string{}, setupHelper.ArgsPrefix...), setupPlan.Args...)
if err := deps.runSandboxSetupHelper(setupHelper.Name, setupArgs, stdout, stderr); err != nil {
return writeAppError(stderr, "Windows sandbox setup failed: "+err.Error(), exitProvider)
message := "Windows sandbox setup failed: " + err.Error()
// Argument preparation may have created the runtime lease and its parent
// directories. Undo those writes only if this helper could not have
// published a marker. A generic process error leaves that outcome unknown.
var exitErr *exec.ExitError
if errors.Is(err, errSandboxSetupHelperNotStarted) ||
(errors.As(err, &exitErr) && exitErr.ExitCode() == zeroSandbox.WindowsSandboxSetupUnpublishedExitCode) {
if undo := setupPlan.Rollback(); undo != nil {
message += "\nleftover sandbox runtime state could not be removed: " + undo.Error()
}
}
return writeAppError(stderr, message, exitProvider)
}
return writeSandboxSetupResult(stdout, options.json, sandboxSetupResult{
Platform: "windows",
Expand Down
83 changes: 83 additions & 0 deletions internal/cli/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"reflect"
"runtime"
"sort"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -439,6 +440,88 @@ func TestRunSandboxSetupRunsWindowsHelper(t *testing.T) {
}
}

func TestRunSandboxSetupRollback(t *testing.T) {
const helperExitEnv = "ZERO_TEST_SANDBOX_SETUP_HELPER_EXIT"
if value, ok := os.LookupEnv(helperExitEnv); ok {
code, err := strconv.Atoi(value)
if err != nil {
t.Fatal(err)
}
os.Exit(code)
}
testBinary, err := os.Executable()
if err != nil {
t.Fatal(err)
}
launch := defaultAppDeps().runSandboxSetupHelper
for _, tc := range []struct {
name string
helper string
exit int
rollback bool
}{
{name: "failed to start", rollback: true},
{name: "failed before publication", helper: testBinary, exit: sandbox.WindowsSandboxSetupUnpublishedExitCode, rollback: true},
{name: "failed after publication", helper: testBinary, exit: 1},
{name: "unknown outcome", helper: testBinary, exit: 2},
} {
t.Run(tc.name, func(t *testing.T) {
workspace, home, tempDir := t.TempDir(), t.TempDir(), t.TempDir()
t.Setenv("ZERO_WINDOWS_SANDBOX_HOME", t.TempDir())
t.Setenv("HOME", home)
t.Setenv("LOCALAPPDATA", home)
t.Setenv("XDG_CACHE_HOME", home)
t.Setenv("TMPDIR", tempDir)
t.Setenv("TEMP", tempDir)
t.Setenv("TMP", tempDir)
cache, err := os.UserCacheDir()
if err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(cache, 0o700); err != nil {
t.Fatal(err)
}
t.Setenv(helperExitEnv, strconv.Itoa(tc.exit))
var leasePath string
deps := appDeps{
getwd: func() (string, error) { return workspace, nil },
resolveConfig: func(string, config.Overrides) (config.ResolvedConfig, error) {
return config.ResolvedConfig{}, nil
},
selectSandboxBackend: func(sandbox.BackendOptions) sandbox.Backend {
return sandbox.Backend{
Platform: "windows", Name: sandbox.BackendWindowsRestrictedToken,
Available: true, Executable: testBinary,
}
},
runSandboxSetupHelper: func(_ string, _ []string, stdout, stderr io.Writer) error {
leases, err := filepath.Glob(filepath.Join(cache, "zero", "runtime", "v1", "*.lease"))
if err != nil || len(leases) != 1 {
t.Fatalf("setup did not create one lease before starting its helper: %v, %v", leases, err)
}
leasePath = leases[0]
path := tc.helper
if path == "" {
path = filepath.Join(workspace, "missing-helper.exe")
}
return launch(path, []string{"-test.run=^TestRunSandboxSetupRollback$"}, stdout, stderr)
},
}
var stdout, stderr bytes.Buffer
if code := runWithDeps([]string{"sandbox", "setup"}, &stdout, &stderr, deps); code != exitProvider || leasePath == "" {
t.Fatalf("setup exit = %d, lease = %q, stdout = %q, stderr = %q", code, leasePath, &stdout, &stderr)
}
_, err = os.Stat(leasePath)
if tc.rollback && !errors.Is(err, os.ErrNotExist) {
t.Errorf("rollback did not remove the lease: %v", err)
}
if !tc.rollback && err != nil {
t.Errorf("setup removed its runtime lease without a confirmed pre-publication failure: %v", err)
}
})
}
}

func TestRunSandboxSetupNoopsForNonWindowsBackend(t *testing.T) {
deps := appDeps{
getwd: func() (string, error) { return t.TempDir(), nil },
Expand Down
74 changes: 70 additions & 4 deletions internal/doctor/hardening.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,77 @@ func windowsSandboxSetupCheck(goos string, backend sandbox.Backend, workspaceRoo
return &result
}
profile := sandbox.PermissionProfileFromPolicy(workspaceRoot, doctorSandboxPolicy(sandboxConfig), scope)
// A RECORDED ROOT IS HISTORY, NOT PROOF. Doctor pins the marker's runtime
// root so the stamp check can run without taking a lease, but a command does
// not pin blindly: it derives the current cache and fallback candidates and
// honours the marker only when its root is one of them. Run setup with the
// cache at A, relocate the cache so commands derive B, and the stamped A tree
// remains: pinning A here reported a healthy sandbox immediately before every
// real command rejected A and failed on the out-of-date marker.
//
// So ask the command's own question first, through the command's own
// function. Only a root a command would still select is used to validate
// the marker; anything else is reported as out of date, with the remedy.
//
// THREE STATES, NOT TWO. The helper answers current, stale, or "I could not
// tell", and the last one used to fall through to the pinned branch: a valid
// old marker then made doctor report healthy while selectSandboxRuntimeRoot,
// reached by BuildCommandPlan, propagated the same resolution error and stopped
// every command from launching. Historical state is not a substitute for a
// failed current selection, so an error is surfaced as its own warning with the
// cause, and the pinned validation profile is built only once currentness has
// actually been established.
recorded, current, rootErr := sandbox.WindowsSandboxRecordedRuntimeRootIsCurrent(sandboxHome, workspaceRoot)
if rootErr != nil {
result := check("sandbox.backend", "Sandbox backend", StatusWarn, fmt.Sprintf("Native sandbox backend %s is installed, but the runtime root a command would select cannot be resolved (%s), so the recorded setup cannot be trusted and commands are likely to fail to launch.", backend.Name, rootErr.Error()), map[string]any{
"backend": string(backend.Name),
"platform": goos,
"supportLevel": string(backend.SupportLevel()),
"setupStatus": "runtime-root-unresolved",
"error": rootErr.Error(),
"remedy": "run `zero sandbox setup` to prepare the Windows native sandbox for the current cache location",
})
return &result
}
if recorded != "" && !current {
result := check("sandbox.backend", "Sandbox backend", StatusWarn, fmt.Sprintf("Native sandbox backend %s is installed, but the runtime root setup recorded (%s) is not one a command would select now, so the setup is out of date.", backend.Name, recorded), map[string]any{
"backend": string(backend.Name),
"platform": goos,
"supportLevel": string(backend.SupportLevel()),
"setupStatus": "runtime-root-stale",
"runtimeRoot": recorded,
"remedy": "run `zero sandbox setup` to prepare the Windows native sandbox for the current cache location",
})
return &result
}
setupConfig := sandbox.WindowsSandboxSetupConfig{
SandboxHome: sandboxHome,
CommandCWD: workspaceRoot,
WorkspaceRoots: []string{workspaceRoot},
PermissionProfile: profile,
SandboxHome: sandboxHome,
CommandCWD: workspaceRoot,
WorkspaceRoots: []string{workspaceRoot},
// The same augmentation setup and the command plan apply, so doctor
// fingerprints what a real command fingerprints. Checking the bare profile
// made doctor call a correctly prepared machine "out of date", which is the
// mismatch this pairing exists to close. Safe to resolve in this process:
// doctor runs in the operator's shell, not behind the sandbox TEMP
// redirection that stops the runner deriving these for itself.
//
// The RECORDED runtime root goes on the profile as well, because that is
// what makes the stamp check run at all. Without it profile.Runtime is nil,
// validateWindowsSandboxRuntimeStamp returns nil early, and doctor reported
// a healthy sandbox on a machine whose runtime tree had been evicted and
// silently recreated without the capability ACE -- precisely the state the
// stamp exists to catch, and the state where every sandboxed command then
// fails with nothing explaining why.
//
// Read from the marker rather than selected, so doctor takes no lease and
// creates nothing, and read ONCE: this is the root whose currentness was
// checked above, not a second read that a rewritten marker could change. A
// marker from an older schema records no root, which leaves this exactly as
// it was.
PermissionProfile: sandbox.WindowsSandboxProfileWithRuntimeRoots(
sandbox.PermissionProfileWithRuntimeRoot(profile, recorded),
[]string{workspaceRoot},
),
}
if err := sandbox.ValidateWindowsSandboxSetupMarker(setupConfig); err != nil {
result := check("sandbox.backend", "Sandbox backend", StatusWarn, fmt.Sprintf("Native sandbox backend %s is installed, but Windows sandbox setup is missing or out of date: %v.", backend.Name, err), map[string]any{
Expand Down
Loading
Loading