Skip to content

Commit 86840f6

Browse files
committed
fix(sandbox): address CodeRabbit review feedback on PR 640
1 parent c41f55b commit 86840f6

5 files changed

Lines changed: 27 additions & 117 deletions

File tree

‎internal/sandbox/runner_windows_integration_test.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ func runWindowsRealSmokeCommandExpectError(t *testing.T, runnerExe string, base
451451
defer cancel()
452452
cmd := exec.CommandContext(ctx, runnerExe, args...)
453453
output, err := cmd.CombinedOutput()
454+
if ctx.Err() != nil {
455+
t.Fatalf("Windows sandbox command timed out: %v\n%s", ctx.Err(), output)
456+
}
454457
if err == nil {
455458
t.Fatalf("Windows sandbox command exit code = 0, want error containing %v\n%s", wantSubstr, output)
456459
}

‎internal/sandbox/windows_acl.go‎

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,9 @@ const (
1616
// WindowsACLRevokeCapability removes any existing ACE (allow or deny) for
1717
// Capability at Path, without itself granting or denying anything (applied
1818
// via SetEntriesInAclW's SET_ACCESS mode with a zero mask, not
19-
// REVOKE_ACCESS — see windowsACLAccess for why). It reconciles stale
20-
// shared/descendant DenyWrite ACEs an earlier setup run applied for the
21-
// stable read-only capability SID (see BuildWindowsACLPlan) that a later
22-
// run no longer intends: if a path previously covered by the
23-
// shared-root/descendant DenyWrite mitigation is later configured as an
24-
// allowed write root, that old deny is otherwise left on disk and wins
25-
// over the new Allow under Windows' deny-before-allow evaluation — see
26-
// jatmn's review. Clearing a SID with no matching ACE is a safe no-op, so
27-
// this can always be emitted unconditionally alongside every write-root
28-
// Allow entry.
19+
// REVOKE_ACCESS — see windowsACLAccess for why). It is consumed by
20+
// applyWindowsACLPlan for migration cleanup and tests; no current
21+
// plan-generation path emits this action, preserving legacy-process confinement.
2922
WindowsACLRevokeCapability WindowsACLAction = "revoke-capability"
3023
)
3124

@@ -44,33 +37,15 @@ type WindowsACLEntry struct {
4437
Materialize bool `json:"materialize,omitempty"`
4538
// ScanDescendants marks a shared-root DenyWrite entry whose EXISTING
4639
// writable descendants must ALSO be denied, one direct (non-inheriting)
47-
// deny per writable descendant, at apply time. A non-inherited deny on the
48-
// root object alone does not cover a pre-existing child that independently
49-
// grants Users/Authenticated Users write, because a Windows access check
50-
// for that child never consults a non-inherited ACE on its parent. This is
51-
// deliberately NOT serialized (json:"-"): the concrete descendant set is
52-
// live-filesystem state that differs between the setup process and a later
53-
// command run, so folding it into the hashed plan would make
54-
// ValidateWindowsSandboxSetupMarker non-deterministic. The flag itself is
55-
// derived deterministically from the same inputs on both sides, and the
56-
// descendant enumeration/denies happen as an apply-time side effect in
57-
// applyWindowsACLPlan (windows-only), never in the cross-platform plan hash.
40+
// deny per writable descendant, at apply time. This flag and its associated
41+
// walker are consumed by applyWindowsACLPlan and tests; no current
42+
// plan-generation path sets this flag since broad shared-root DenyWrite ACEs
43+
// and SID broadening are no longer planned.
5844
ScanDescendants bool `json:"-"`
59-
// RevokeDescendants marks a write-root's WindowsACLRevokeCapability entry
60-
// (see the constant below) as needing the same stale-deny cleanup applied
61-
// recursively to the root's existing descendants, not just the root path
62-
// itself. A tree scanned and denied by an earlier setup run (either
63-
// because it WAS one of the four shared roots, or because it was a
64-
// writable descendant applyWindowsSharedDescendantDenies found and denied
65-
// elsewhere in the tree) can later be promoted to an allowed write root by
66-
// the caller configuring some ANCESTOR of it as a WriteRoot. Revoking only
67-
// at the exact configured root leaves any stale direct, non-inheriting
68-
// deny on that ancestor's descendants in place, and a stale deny wins over
69-
// the newly-added inheritable Allow under Windows' deny-before-allow
70-
// evaluation — see jatmn's review. Like ScanDescendants, this is
71-
// deliberately NOT serialized (json:"-"): the concrete stale-deny set is
72-
// live-filesystem state, and the actual descendant walk/revoke happens as
73-
// an apply-time side effect in applyWindowsACLPlan (windows-only).
45+
// RevokeDescendants marks a WindowsACLRevokeCapability entry as needing
46+
// stale-deny cleanup applied recursively to existing descendants, not just
47+
// the root path itself. This flag is consumed by applyWindowsACLPlan for
48+
// migration cleanup and tests; no current plan-generation path sets this flag.
7449
RevokeDescendants bool `json:"-"`
7550
}
7651

‎internal/sandbox/windows_acl_descendants.go‎

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,3 @@ func windowsPathIsDriveRootPath(path string) bool {
4242
c := trimmed[0]
4343
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
4444
}
45-
46-
// windowsMountPathIsOnlySystemDrive reports whether a volume mount path is the
47-
// system drive root (e.g. `C:\` or `C:`) rather than another letter or a
48-
// folder mount such as `C:\mnt\data`. Used by the volume gate so a second
49-
// fixed volume mounted only as a folder is rejected the same as one mounted
50-
// on a drive letter.
51-
func windowsMountPathIsOnlySystemDrive(mountPath, systemDrive string) bool {
52-
trimmed := strings.TrimSuffix(mountPath, `\`)
53-
return strings.EqualFold(strings.ToUpper(trimmed), strings.ToUpper(systemDrive))
54-
}
55-
56-
// windowsMountPathsAreOnlySystemDrive reports whether mountPaths (a fixed
57-
// volume's DOS/folder mount points from GetVolumePathNamesForVolumeName) name
58-
// the system drive root and nothing else. A fixed volume with NO mount paths
59-
// at all is still directly reachable through its raw "\\?\Volume{GUID}\"
60-
// path even though it has no conventional mount point, so an empty list
61-
// fails closed (false) instead of being read as "unreachable." Pure string
62-
// logic so non-Windows tests can pin the fail-closed cases without Win32.
63-
func windowsMountPathsAreOnlySystemDrive(mountPaths []string, systemDrive string) bool {
64-
if len(mountPaths) == 0 {
65-
return false
66-
}
67-
for _, mountPath := range mountPaths {
68-
if !windowsMountPathIsOnlySystemDrive(mountPath, systemDrive) {
69-
return false
70-
}
71-
}
72-
return true
73-
}

‎internal/sandbox/windows_acl_descendants_test.go‎

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -43,53 +43,3 @@ func TestWindowsPathIsDriveRootPath(t *testing.T) {
4343
}
4444
}
4545
}
46-
47-
func TestWindowsMountPathIsOnlySystemDrive(t *testing.T) {
48-
cases := []struct {
49-
mount, system string
50-
want bool
51-
}{
52-
{`C:\`, `C:`, true},
53-
{`C:`, `C:`, true},
54-
{`c:\`, `C:`, true},
55-
{`D:\`, `C:`, false},
56-
{`C:\mnt\data`, `C:`, false},
57-
{`C:\mnt\data\`, `C:`, false},
58-
{`\\?\Volume{guid}\`, `C:`, false},
59-
}
60-
for _, tc := range cases {
61-
got := windowsMountPathIsOnlySystemDrive(tc.mount, tc.system)
62-
if got != tc.want {
63-
t.Fatalf("windowsMountPathIsOnlySystemDrive(%q, %q) = %v, want %v", tc.mount, tc.system, got, tc.want)
64-
}
65-
}
66-
}
67-
68-
// TestWindowsMountPathsAreOnlySystemDrive pins the volume-gate fail-closed fix
69-
// (jatmn's review): a fixed volume with no mount points at all must not be
70-
// read as "unreachable" (it is still reachable via its raw
71-
// "\\?\Volume{GUID}\" path), and any mount path other than the system drive
72-
// root disqualifies the volume, matching the existing per-path behavior.
73-
func TestWindowsMountPathsAreOnlySystemDrive(t *testing.T) {
74-
cases := []struct {
75-
name string
76-
mountPaths []string
77-
system string
78-
want bool
79-
}{
80-
{"only system drive", []string{`C:\`}, `C:`, true},
81-
{"no mount points at all", nil, `C:`, false},
82-
{"empty mount list", []string{}, `C:`, false},
83-
{"extra drive letter", []string{`C:\`, `D:\`}, `C:`, false},
84-
{"folder mount point", []string{`C:\mnt\data`}, `C:`, false},
85-
{"other drive only", []string{`D:\`}, `C:`, false},
86-
}
87-
for _, tc := range cases {
88-
t.Run(tc.name, func(t *testing.T) {
89-
got := windowsMountPathsAreOnlySystemDrive(tc.mountPaths, tc.system)
90-
if got != tc.want {
91-
t.Fatalf("windowsMountPathsAreOnlySystemDrive(%#v, %q) = %v, want %v", tc.mountPaths, tc.system, got, tc.want)
92-
}
93-
})
94-
}
95-
}

‎internal/sandbox/windows_acl_descendants_windows_test.go‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,12 @@ func TestWindowsAceSIDSkipsUnhandledAceTypes(t *testing.T) {
568568
}
569569

570570
func TestWindowsPathDeniesCapabilitySIDRequiresEssentialWriteMask(t *testing.T) {
571+
caps, err := LoadOrCreateWindowsCapabilitySIDs(t.TempDir())
572+
if err != nil {
573+
t.Fatalf("LoadOrCreateWindowsCapabilitySIDs: %v", err)
574+
}
575+
sid := caps.ReadOnly
571576
dir := t.TempDir()
572-
sid := "S-1-1-0"
573577

574578
group := windowsACLPathGroup{
575579
Path: dir,
@@ -634,7 +638,14 @@ func denyCapabilityMask(t *testing.T, path, capabilitySID string, mask windows.A
634638
// pass through a Users/AuthUsers allow under a partial-deny check that only
635639
// tested non-zero overlap with the probe mask.
636640
func TestWindowsPathDeniesCapabilitySIDRejectsPartialWriteDeny(t *testing.T) {
637-
const sidStr = "S-1-1-0"
641+
// Synthetic capability SID: a full DenyWrite for a group the test process
642+
// belongs to (e.g. Everyone) denies DELETE/WRITE_DAC and blocks t.TempDir
643+
// cleanup.
644+
caps, err := LoadOrCreateWindowsCapabilitySIDs(t.TempDir())
645+
if err != nil {
646+
t.Fatalf("LoadOrCreateWindowsCapabilitySIDs: %v", err)
647+
}
648+
sidStr := caps.ReadOnly
638649
dir := t.TempDir()
639650
denyCapabilityMask(t, dir, sidStr, windows.FILE_WRITE_ATTRIBUTES)
640651

0 commit comments

Comments
 (0)