Skip to content

Commit c4fff2b

Browse files
test(sandbox): give the cross-platform anchor tests native paths
Moving these beside the other BuildWindowsACLPlan tests made them run on Linux and macOS, where their Windows path literals stop meaning what they say: pathWithinRoot is filepath.Rel underneath, a backslash is an ordinary character off Windows, and C:\workspace\.git\hooks is then one component that is not under C:\workspace, so every anchor came back empty. The neighbouring tests get away with such literals because they only compare strings they built the same way and never ask whether one contains another. Paths are built with filepath.Join from temp roots now, so the containment question is asked in the separator the running platform actually uses.
1 parent 38c1e33 commit c4fff2b

1 file changed

Lines changed: 33 additions & 19 deletions

File tree

‎internal/sandbox/windows_acl_test.go‎

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -243,25 +243,36 @@ func TestDedupeWindowsACLEntriesKeepsInheritanceVariants(t *testing.T) {
243243

244244
// THE PLAN HAS TO CARRY THE ANCHOR, OR THE APPLY HAS NOTHING TO ENFORCE.
245245
//
246-
// Every test above hands applyWindowsACLPathGroup an anchor directly, so they
246+
// The apply-side tests hand applyWindowsACLPathGroup an anchor directly, so they
247247
// pass whether or not anything ever sets one. This drives the real builder and
248248
// checks which entries come out anchored: the carveouts derived from a write
249249
// root, and not the paths the operator named, which have no owned tail and whose
250250
// intermediates are the operator's own business.
251+
//
252+
// NATIVE PATHS, NOT WINDOWS LITERALS. The builder is cross-platform and so is
253+
// this test, and the anchoring decision runs through pathWithinRoot, which is
254+
// filepath.Rel underneath. On Linux a backslash is an ordinary character, so
255+
// `C:\workspace\.git\hooks` is one component and is not under `C:\workspace`;
256+
// the neighbouring tests get away with such literals because they only compare
257+
// strings they built the same way and never ask whether one contains another.
251258
func TestBuildWindowsACLPlanAnchorsDerivedCarveouts(t *testing.T) {
252259
home := t.TempDir()
260+
workspace := t.TempDir()
261+
carveout := filepath.Join(workspace, ".git", "hooks")
262+
metadata := filepath.Join(workspace, ".zero")
263+
named := filepath.Join(t.TempDir(), "named-by-the-operator")
253264
config := WindowsSandboxCommandConfig{
254265
SandboxHome: home,
255-
WorkspaceRoots: []string{`C:\workspace`},
266+
WorkspaceRoots: []string{workspace},
256267
PermissionProfile: PermissionProfile{
257268
FileSystem: FileSystemPolicy{
258269
Kind: FileSystemRestricted,
259270
WriteRoots: []WritableRoot{{
260-
Root: `C:\workspace`,
261-
ReadOnlySubpaths: []string{`C:\workspace\.git\hooks`},
271+
Root: workspace,
272+
ReadOnlySubpaths: []string{carveout},
262273
ProtectedMetadataNames: []string{".zero"},
263274
}},
264-
DenyWrite: []string{`C:\elsewhere\named-by-the-operator`},
275+
DenyWrite: []string{named},
265276
},
266277
Network: NetworkPolicy{Mode: NetworkDeny},
267278
},
@@ -276,21 +287,21 @@ func TestBuildWindowsACLPlanAnchorsDerivedCarveouts(t *testing.T) {
276287
for _, entry := range plan.Entries {
277288
anchors[strings.ToLower(entry.Path)] = entry.Anchor
278289
}
279-
for _, derived := range []string{`C:\workspace\.git\hooks`, `C:\workspace\.zero`} {
290+
for _, derived := range []string{carveout, metadata} {
280291
anchor, present := anchors[strings.ToLower(derived)]
281292
if !present {
282293
t.Fatalf("SETUP INVALID: the plan has no entry for the derived carveout %s", derived)
283294
}
284-
if anchor != `C:\workspace` {
295+
if anchor != workspace {
285296
t.Errorf("derived carveout %s carries anchor %q, want the write root it came from", derived, anchor)
286297
}
287298
}
288-
named, present := anchors[strings.ToLower(`C:\elsewhere\named-by-the-operator`)]
299+
operatorAnchor, present := anchors[strings.ToLower(named)]
289300
if !present {
290301
t.Fatal("SETUP INVALID: the plan has no entry for the operator-named deny path")
291302
}
292-
if named != "" {
293-
t.Errorf("operator-named path carries anchor %q, want none: its intermediates are not the sandbox's to police", named)
303+
if operatorAnchor != "" {
304+
t.Errorf("operator-named path carries anchor %q, want none: its intermediates are not the sandbox's to police", operatorAnchor)
294305
}
295306
}
296307

@@ -303,15 +314,18 @@ func TestBuildWindowsACLPlanAnchorsDerivedCarveouts(t *testing.T) {
303314
// under it are held to it.
304315
func TestBuildWindowsACLPlanLeavesAnOutOfRootSubpathUnanchored(t *testing.T) {
305316
home := t.TempDir()
317+
workspace := t.TempDir()
318+
inside := filepath.Join(workspace, "vendor")
319+
outside := filepath.Join(t.TempDir(), "shared")
306320
plan, err := BuildWindowsACLPlan(WindowsSandboxCommandConfig{
307321
SandboxHome: home,
308-
WorkspaceRoots: []string{`C:\workspace`},
322+
WorkspaceRoots: []string{workspace},
309323
PermissionProfile: PermissionProfile{
310324
FileSystem: FileSystemPolicy{
311325
Kind: FileSystemRestricted,
312326
WriteRoots: []WritableRoot{{
313-
Root: `C:\workspace`,
314-
ReadOnlySubpaths: []string{`C:\workspace\vendor`, `D:\somewhere-else\shared`},
327+
Root: workspace,
328+
ReadOnlySubpaths: []string{inside, outside},
315329
}},
316330
},
317331
Network: NetworkPolicy{Mode: NetworkDeny},
@@ -325,18 +339,18 @@ func TestBuildWindowsACLPlanLeavesAnOutOfRootSubpathUnanchored(t *testing.T) {
325339
for _, entry := range plan.Entries {
326340
seen[strings.ToLower(entry.Path)] = entry.Anchor
327341
}
328-
inside, present := seen[strings.ToLower(`C:\workspace\vendor`)]
342+
insideAnchor, present := seen[strings.ToLower(inside)]
329343
if !present {
330344
t.Fatal("SETUP INVALID: the plan has no entry for the in-root subpath")
331345
}
332-
if inside != `C:\workspace` {
333-
t.Errorf("in-root subpath carries anchor %q, want the write root", inside)
346+
if insideAnchor != workspace {
347+
t.Errorf("in-root subpath carries anchor %q, want the write root", insideAnchor)
334348
}
335-
outside, present := seen[strings.ToLower(`D:\somewhere-else\shared`)]
349+
outsideAnchor, present := seen[strings.ToLower(outside)]
336350
if !present {
337351
t.Fatal("SETUP INVALID: the plan has no entry for the out-of-root subpath")
338352
}
339-
if outside != "" {
340-
t.Errorf("out-of-root subpath carries anchor %q, want none: the apply would refuse a config that works today", outside)
353+
if outsideAnchor != "" {
354+
t.Errorf("out-of-root subpath carries anchor %q, want none: the apply would refuse a config that works today", outsideAnchor)
341355
}
342356
}

0 commit comments

Comments
 (0)