package provider import ( "os" "path/filepath" "strconv" "testing" ) func TestPiShellPolicyGuards(t *testing.T) { for _, name := range []string{"wrapper bytes", "shell setting", "go scope", "cache symlink", "go pin", "closed lease"} { t.Run(name, func(t *testing.T) { in := piFixture(t, "implement", []string{"bash", "read"}) in.GoLease = &GoLease{} t.Cleanup(in.GoLease.Close) in.GoRoot = t.TempDir() in.ModuleCache = t.TempDir() if err := os.WriteFile(filepath.Join(in.GoRoot, "VERSION"), []byte("fixture"), 0600); err != nil { t.Fatal(err) } switch name { case "wrapper bytes": os.WriteFile(in.Shell, []byte("#!/bin/sh\nexec /bin/bash \"$@\"\n"), 0700) case "shell setting": in.Settings = []byte(`{"defaultProjectTrust":"never","shellPath":"/bin/bash"}`) if err := WritePiConfig(in); err != nil { t.Fatal(err) } case "go scope": in.Action = "inspect" in.Tools = reviewTools case "cache symlink": alias := filepath.Join(t.TempDir(), "alias") os.Symlink(in.ModuleCache, alias) in.ModuleCache = alias case "closed lease": in.GoLease.Close() } p, err := BuildPiPolicy(in) switch name { case "go scope": if err != nil || p.GoToolchain() { t.Fatal("read-only role received Go") } case "go pin": if err != nil { t.Fatal(err) } mount := Mount{Source: in.GoRoot, Target: "/opt/go", ReadOnly: true} if err := os.Rename(in.GoRoot, in.GoRoot+"-old"); err != nil { t.Fatal(err) } t.Cleanup(func() { os.RemoveAll(in.GoRoot + "-old") }) os.Mkdir(in.GoRoot, 0700) held, err := p.OpenGoTree(Binding{}, mount) if err != nil { t.Fatal(err) } defer held.Close() if _, err := os.Stat(filepath.Join("/proc/self/fd", strconv.Itoa(int(held.Fd())), "VERSION")); err != nil { t.Fatal("source path replaced retained tree", err) } default: if err == nil { t.Fatal("unsafe policy admitted") } } }) } }