| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strconv" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestPiShellPolicyGuards(t *testing.T) { |
| 11 | for _, name := range []string{"wrapper bytes", "shell setting", "go scope", "cache symlink", "go pin", "closed lease"} { |
| 12 | t.Run(name, func(t *testing.T) { |
| 13 | in := piFixture(t, "implement", []string{"bash", "read"}) |
| 14 | in.GoLease = &GoLease{} |
| 15 | t.Cleanup(in.GoLease.Close) |
| 16 | in.GoRoot = t.TempDir() |
| 17 | in.ModuleCache = t.TempDir() |
| 18 | if err := os.WriteFile(filepath.Join(in.GoRoot, "VERSION"), []byte("fixture"), 0600); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | switch name { |
| 22 | case "wrapper bytes": |
| 23 | os.WriteFile(in.Shell, []byte("#!/bin/sh\nexec /bin/bash \"$@\"\n"), 0700) |
| 24 | case "shell setting": |
| 25 | in.Settings = []byte(`{"defaultProjectTrust":"never","shellPath":"/bin/bash"}`) |
| 26 | if err := WritePiConfig(in); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | case "go scope": |
| 30 | in.Action = "inspect" |
| 31 | in.Tools = reviewTools |
| 32 | case "cache symlink": |
| 33 | alias := filepath.Join(t.TempDir(), "alias") |
| 34 | os.Symlink(in.ModuleCache, alias) |
| 35 | in.ModuleCache = alias |
| 36 | case "closed lease": |
| 37 | in.GoLease.Close() |
| 38 | } |
| 39 | p, err := BuildPiPolicy(in) |
| 40 | switch name { |
| 41 | case "go scope": |
| 42 | if err != nil || p.GoToolchain() { |
| 43 | t.Fatal("read-only role received Go") |
| 44 | } |
| 45 | case "go pin": |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | mount := Mount{Source: in.GoRoot, Target: "/opt/go", ReadOnly: true} |
| 50 | if err := os.Rename(in.GoRoot, in.GoRoot+"-old"); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | t.Cleanup(func() { os.RemoveAll(in.GoRoot + "-old") }) |
| 54 | os.Mkdir(in.GoRoot, 0700) |
| 55 | held, err := p.OpenGoTree(Binding{}, mount) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | defer held.Close() |
| 60 | if _, err := os.Stat(filepath.Join("/proc/self/fd", strconv.Itoa(int(held.Fd())), "VERSION")); err != nil { |
| 61 | t.Fatal("source path replaced retained tree", err) |
| 62 | } |
| 63 | default: |
| 64 | if err == nil { |
| 65 | t.Fatal("unsafe policy admitted") |
| 66 | } |
| 67 | } |
| 68 | }) |
| 69 | } |
| 70 | } |
| 71 | |