| 1 | package guard |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | type asker struct { |
| 12 | answers []bool |
| 13 | asked []string |
| 14 | told []string |
| 15 | } |
| 16 | |
| 17 | func (a *asker) guard(t *testing.T, list string) *Guard { |
| 18 | t.Helper() |
| 19 | dir := t.TempDir() |
| 20 | if list != "" { |
| 21 | os.WriteFile(filepath.Join(dir, FileName), []byte(list), 0o644) |
| 22 | } |
| 23 | g, err := Load(dir, "proj-1a2b3c4d") |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | g.Ask = func(_ context.Context, title, msg string) (bool, bool) { |
| 28 | a.asked = append(a.asked, msg) |
| 29 | if a.answers == nil { |
| 30 | return false, false |
| 31 | } |
| 32 | ans := a.answers[0] |
| 33 | a.answers = a.answers[1:] |
| 34 | return ans, true |
| 35 | } |
| 36 | g.Tell = func(title, msg string) { a.told = append(a.told, msg) } |
| 37 | return g |
| 38 | } |
| 39 | |
| 40 | |
| 41 | |
| 42 | func TestAllow(t *testing.T) { |
| 43 | a := &asker{answers: []bool{false, true}} |
| 44 | g := a.guard(t, "") |
| 45 | for _, rel := range []string{"src/main.go", "Makefile", "package.json", ".envrc.bak", "x.vscode/tasks.json"} { |
| 46 | if !g.Allow("/p", rel, false) { |
| 47 | t.Errorf("%s refused", rel) |
| 48 | } |
| 49 | } |
| 50 | if len(a.asked) != 0 { |
| 51 | t.Fatalf("asked for unguarded paths: %q", a.asked) |
| 52 | } |
| 53 | if g.Allow("/p", "sub/.ENVRC", false) { |
| 54 | t.Error("no was not a no") |
| 55 | } |
| 56 | if !strings.Contains(a.asked[0], "change .envrc in /p") || !strings.Contains(a.asked[0], "Path: sub/.ENVRC") || !strings.Contains(a.asked[0], "direnv runs it") { |
| 57 | t.Errorf("question: %q", a.asked[0]) |
| 58 | } |
| 59 | if g.Allow("/p", "sub/.ENVRC", false) || len(a.asked) != 1 { |
| 60 | t.Error("asked again right after a no") |
| 61 | } |
| 62 | if !g.Allow("/p", ".vscode/tasks.json", false) || !g.Allow("/p", ".vscode/tasks.json", false) || len(a.asked) != 2 { |
| 63 | t.Errorf("yes, then asked again: %d", len(a.asked)) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | |
| 68 | func TestList(t *testing.T) { |
| 69 | a := &asker{answers: []bool{false}} |
| 70 | g := a.guard(t, "# mine\nMakefile\n!.envrc\n /scripts/deploy.sh \n") |
| 71 | if !g.Allow("/p", ".envrc", false) { |
| 72 | t.Error("removed default still guarded") |
| 73 | } |
| 74 | if g.Allow("/p", "Makefile", false) { |
| 75 | t.Error("added path not guarded") |
| 76 | } |
| 77 | if !strings.Contains(a.asked[0], "You asked agent-vm to guard it") { |
| 78 | t.Errorf("question: %q", a.asked[0]) |
| 79 | } |
| 80 | if _, ok := g.match("app/scripts/deploy.sh", false); !ok { |
| 81 | t.Error("a path with folders") |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | |
| 86 | func TestDenyMode(t *testing.T) { |
| 87 | t.Setenv("AGENT_VM_GUARDED_WRITES", "deny") |
| 88 | g, err := Load(t.TempDir(), "p") |
| 89 | if err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | g.Tell = func(string, string) { t.Error("told") } |
| 93 | if g.Allow("/p", ".envrc", false) || !g.Allow("/p", "main.go", false) { |
| 94 | t.Error("deny mode") |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |
| 99 | func TestNoOneToAsk(t *testing.T) { |
| 100 | a := &asker{} |
| 101 | g := a.guard(t, "") |
| 102 | if g.Allow("/p", ".envrc", false) { |
| 103 | t.Error("allowed with no one to ask") |
| 104 | } |
| 105 | if len(a.told) != 1 || !strings.Contains(a.told[0], "no dialog could ask you") { |
| 106 | t.Errorf("told: %q", a.told) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | func TestWindowsHostGuards(t *testing.T) { |
| 114 | old := windowsHost |
| 115 | t.Cleanup(func() { windowsHost = old }) |
| 116 | for _, rel := range []string{"a.lnk", "docs/B.URL", "x.library-ms", "x.searchConnector-ms", "z.scf", "sub/desktop.ini", |
| 117 | "a.lnk.", "a.lnk ", "a.lnk:Zone.Identifier", "a.lnk::$DATA", "A.LnK. . "} { |
| 118 | windowsHost = true |
| 119 | a := &asker{answers: []bool{false}} |
| 120 | g := a.guard(t, "") |
| 121 | if g.Allow("/p", rel, false) || len(a.asked) != 1 { |
| 122 | t.Errorf("%q: let through (asked %d)", rel, len(a.asked)) |
| 123 | } |
| 124 | windowsHost = false |
| 125 | a = &asker{} |
| 126 | if g = a.guard(t, ""); !g.Allow("/p", rel, false) || len(a.asked) != 0 { |
| 127 | t.Errorf("%q: asked on a host that is not Windows", rel) |
| 128 | } |
| 129 | } |
| 130 | windowsHost = true |
| 131 | a := &asker{} |
| 132 | g := a.guard(t, "") |
| 133 | for _, rel := range []string{"a.txt", "lnk", "a.lnkx", "notes/url", "src/main.go"} { |
| 134 | if !g.Allow("/p", rel, false) { |
| 135 | t.Errorf("%q: refused", rel) |
| 136 | } |
| 137 | } |
| 138 | if len(a.asked) != 0 { |
| 139 | t.Errorf("asked: %q", a.asked) |
| 140 | } |
| 141 | |
| 142 | g = a.guard(t, "!*.LNK\n") |
| 143 | if !g.Allow("/p", "a.lnk", false) || g.Allow("/p", "a.url", false) { |
| 144 | t.Error("!*.LNK") |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | func TestEntryTooBig(t *testing.T) { |
| 151 | root := t.TempDir() |
| 152 | os.MkdirAll(filepath.Join(root, "d", "z"), 0o755) |
| 153 | for _, f := range []string{"d/a", "d/b", "d/c", "d/z/.envrc"} { |
| 154 | os.WriteFile(filepath.Join(root, f), nil, 0o644) |
| 155 | } |
| 156 | old := maxScan |
| 157 | t.Cleanup(func() { maxScan = old }) |
| 158 | maxScan = 2 |
| 159 | a := &asker{answers: []bool{false}} |
| 160 | g := a.guard(t, "") |
| 161 | if g.Allow(root, "d", true) { |
| 162 | t.Error("a folder too big to look into was let through") |
| 163 | } |
| 164 | if len(a.asked) != 1 || !strings.Contains(a.asked[0], "more than 2 entries") { |
| 165 | t.Errorf("asked: %q", a.asked) |
| 166 | } |
| 167 | maxScan = old |
| 168 | a = &asker{answers: []bool{false}} |
| 169 | g = a.guard(t, "") |
| 170 | if g.Allow(root, "d", true) || len(a.asked) != 1 || !strings.Contains(a.asked[0], "holds .envrc") { |
| 171 | t.Errorf("a folder holding .envrc: %q", a.asked) |
| 172 | } |
| 173 | } |