Skip to content

fix(windows): exclude the sandbox runtime root from the setup marker fingerprint - #1251

Open
the-dreamsmith wants to merge 2 commits into
Twigpine:mainfrom
the-dreamsmith:fix/windows-sandbox-marker-runtime-root
Open

the-dreamsmith wants to merge 2 commits into
Twigpine:mainfrom
the-dreamsmith:fix/windows-sandbox-marker-runtime-root

Conversation

@the-dreamsmith

@the-dreamsmith the-dreamsmith commented Dec 3, 2026 •

Copy link
Copy Markdown

Summary

zero sandbox setup writes the Windows sandbox setup marker from a permission
profile that never contains the sandbox runtime root, while every real command
validates the marker against a profile to which the runner appended one
(permissionProfileWithRuntime). The two sides therefore never compute the same
ACL plan: the stored marker has 10 entries, the validated plan has 15, so every
restricted-token spawn fails with:

windows sandbox setup is out of date: permission roots or deny lists changed

No number of zero sandbox setup runs can close the gap — the setup rewrites the
same 10-entry marker every time.

It gets worse for workspaces that contain the user cache directory
(%LOCALAPPDATA%): there the deterministic runtime root falls under the
workspace, so prepareSandboxRuntime falls back to a fresh random temp directory
per process (zero-runtime-*), and the validated plan carries a different root
(with a freshly minted capability SID) on every run. A marker that includes the
runtime root could never validate there either.

Fix

BuildWindowsSandboxSetupMarker now fingerprints the marker from
markerCommandConfig(config): a copy of the command config whose permission
profile no longer contains the runtime root appended by the runner. The setup path
is a no-op (it never carries a runtime root), so the marker it writes is
unchanged, and the validation path now computes the same plan the setup wrote.

Explicitly untouched: the real execution ACL plan. BuildWindowsACLPlan for
enforcement and the restricted token still receive the full profile including the
runtime root — its ACLs are applied and the token still carries its capability
SID. Only the marker fingerprint excludes the runtime root. As a side effect,
validation no longer mints a capability SID for the runtime root.

Also included: the ACL mismatch error now reports the numbers already computed
(acl entries %d, want %d; acl plan hash %s, want %s), so the next person
debugging this sees immediately which side diverged instead of needing a source
dive.

Tests

  • New regression test TestWindowsSandboxSetupMarkerIgnoresRuntimeRoot: writes a
    marker as zero sandbox setup would (no runtime root), then validates a real
    command profile carrying the appended runtime root, asserts the marker is
    identical with and without the runtime root, and asserts the caller's profile is
    not mutated. It fails without this patch with the exact production error above,
    and passes with it.
  • gofmt clean, go vet ./internal/sandbox/ clean, go test ./internal/sandbox/
    passes.

Verified end-to-end on Windows 11 (zero 0.14.0): after the fix, a restricted-token
spawn with the real exec profile passes marker validation, and
zero sandbox setup still writes the identical marker. This fixes marker
validation only; it does not change what the restricted token itself permits the
spawned process to do.

Summary by CodeRabbit

  • Bug Fixes
    • Windows sandbox setup markers now validate consistently when an appended runtime root is also the final write root, unless it matches a workspace root.
    • Existing explicit runtime grants remain represented in setup markers.
    • Stale-setup errors now include expected and actual ACL entry counts and hashes.
    • Marker creation no longer changes the original permission profile.

@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Windows sandbox setup marker now uses a normalized command configuration for ACL and network plans. Runtime state records whether permission augmentation added the runtime root. ACL mismatch errors include actual and expected entry counts and plan hashes.

Changes

Windows setup marker

Layer / File(s) Summary
Track runtime write-root augmentation
internal/sandbox/runtime_state.go
SandboxRuntime records whether permission augmentation appended the runtime root. If the root already exists, the runtime processing path does not set the flag.
Normalize marker plans and validate results
internal/sandbox/windows_setup.go, internal/sandbox/windows_setup_test.go
The marker builder removes an appended runtime root from a copied configuration and uses the result for ACL and network plans. ACL mismatch errors report actual and expected entry counts and hashes. Tests check marker validation, equality, explicit grants, and preservation of the original profile.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related issues

Suggested reviewers: anandh8x, vasanthdev2004

Merge Risk: Minimal · up to 8105f

This change fixes Windows sandbox setup-marker validation, which previously rejected every restricted-token spawn as out of date. The change is narrow and covered by targeted tests. No merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the Windows sandbox setup marker fingerprint now excludes the sandbox runtime root. The raw summary confirms markerCommandConfig strips the appended runtime root for t…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the Windows sandbox setup marker fingerprint now excludes the sandbox runtime root. The raw summary confirms markerCommandConfig strips the appended runtime root for the marker, and the PR objectives describe the same fix for the setup-vs-validation fingerprint mismatch. The title is specific, one sentence, and free of noise, so it passes.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Twigpine/zero
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: 2c509ce
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 90a2d99e-0ea6-4274-86a7-fae2d48e7151
Commits

Reviewing files that changed from the base of the PR and between f015c7e and 8105fd6.

Files selected for processing: 3

  • internal/sandbox/runtime_state.go
  • internal/sandbox/windows_setup.go
  • internal/sandbox/windows_setup_test.go

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

coderabbitai[bot]
coderabbitai Bot previously approved these changes Dec 3, 2026

@gnanam1990 gnanam1990 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One marker-validation regression reproduced on the exact head; details and before/after CI are inline.

return command
}
roots := command.PermissionProfile.FileSystem.WriteRoots
if n := len(roots); n > 0 && windowsCapabilityPathKey(roots[n-1].Root) == root && !windowsRootMatchesWorkspace(runtime.Root, config.WorkspaceRoots) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve an existing non-workspace grant that matches the runtime root

When the profile already grants the runtime directory as its last non-workspace write root, permissionProfileWithRuntime returns without appending anything. This condition still removes that existing grant. The setup marker includes it, but command validation now computes a different ACL plan and refuses the command as out of date. Re-running setup repeats the mismatch. The workspace-root check protects only one kind of pre-existing grant.

The fixture uses the actual producer, asserts that it did not append a root, writes the setup marker, then validates the command profile. In Windows, Linux and macOS CI, main 2c509ce9 passes; head f015c7ed fails with acl entries 2, want 1.

Keep explicit grants in the marker and remove only an entry known to have been introduced by runtime augmentation. Please cover the existing-grant case outside the workspace as well as the newly appended case.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this — you were right, v1 stripped a pre-existing grant whenever it happened to be the last write root. Fixed by recording the augmentation itself: permissionProfileWithRuntime now sets SandboxRuntime.Appended only when it actually appends the runtime root, and markerCommandConfig strips the last entry only in that case. An explicit grant for the same root stays in the marker. Added TestWindowsSandboxSetupMarkerPreservesExistingRuntimeGrant covering your case (it fails on v1 with acl entries 2, want 1 and passes now); the existing appended-root test now goes through the real producer. Full internal/sandbox suite passes, including -race.

@gnanam1990 gnanam1990 Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rechecked 8105fd63 with the original reproduction. The existing non-workspace runtime grant is retained: the producer leaves Appended false, and marker calculation keeps that grant.

The same fixture passes on main and this head, and still fails on f015c7ed, under the race detector on Windows and Linux. The supplemental macOS job was cancelled without running its steps. This resolves my original finding. This fixture checks marker calculation and grant provenance; it does not exercise a live restricted-token launch.

markerCommandConfig stripped the last write root whenever it matched the
runtime root, even when that grant pre-existed and permissionProfileWithRuntime
appended nothing. Record the augmentation in SandboxRuntime.Appended and
strip only an entry the runtime actually introduced, so an explicit grant for
the same root stays in the marker. The appended-root test now goes through
the real producer; add a regression test for the pre-existing grant case
(review feedback on Twigpine#1251).

@Vasanthdev2004 Vasanthdev2004 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for digging into this. I have #901 open for the same bug (#881), so weigh this with that in mind, but I think this change moves the failure rather than fixing it.

On the restricted-token tier the marker is the only check before launch. Nothing applies an ACL plan at command time there: runWindowsSandboxCommand validates the marker and starts the process. The capability ACLs are written only by zero sandbox setup, from a profile that never has the runtime root in it. So once the runtime root is left out of the marker, a command validates, its token carries a capability SID for the runtime root, and that SID is granted nowhere. The description says the runtime root's ACLs are still applied; I could not find where on this tier.

I checked it on Windows 11 by calling runWindowsSandboxCommand at WindowsSandboxLevelRestrictedToken. Setup's ACL plan applied and the marker written the way setup does, then a command whose profile went through permissionProfileWithRuntime, with TEMP pointing into the runtime root:

main 2c509ce9 this PR 8105fd63
echo x> <workspace>\a.txt exit 1, setup is out of date exit 0, written
echo x> <runtime root>\tmp\b.txt exit 1, setup is out of date exit 1, not written
the same, after applying the plan that includes the runtime root exit 1, setup is out of date exit 0, written

The last row is the control: the write fails only because the runtime root has no grant.

TEMP, TMP, GOCACHE, GOMODCACHE, CARGO_HOME and the npm, yarn and pip caches all point into the runtime root (sandboxRuntimeEnvironment). So a build or an install would now fail with access denied somewhere inside the tool, where today it fails up front with a message that names setup.

For this to hold, setup has to grant the root that commands will actually use, and the marker has to cover it. That is the part #901 adds: setup selects the runtime root the same way a command does, applies its grant and records it, and a later command uses that record. Happy to compare notes there.

Two smaller things. The PR does not link #881, and CONTRIBUTING asks for the approved parent issue. And the new tests only compare markers, so they pass whether or not anything is writable; a test that runs a command through the runner would have caught the table above.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants