| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/control" |
| 9 | "reasonix/internal/provider" |
| 10 | ) |
| 11 | |
| 12 | func TestPresentedFileDeclaredRequiresExactTrustedMetadata(t *testing.T) { |
| 13 | result := &control.ToolResultData{Name: "present", PresentedFiles: []provider.PresentedFile{ |
| 14 | {Path: "dist/game.html", Description: "Game"}, |
| 15 | {Path: "/tmp/report.pdf"}, |
| 16 | }} |
| 17 | for _, test := range []struct { |
| 18 | path string |
| 19 | want bool |
| 20 | }{ |
| 21 | {"dist/game.html", true}, |
| 22 | {"/tmp/report.pdf", true}, |
| 23 | {"game.html", false}, |
| 24 | {"dist/../dist/game.html", false}, |
| 25 | {"/tmp/other.pdf", false}, |
| 26 | {"", false}, |
| 27 | } { |
| 28 | if got := presentedFileDeclared(result, test.path); got != test.want { |
| 29 | t.Errorf("presentedFileDeclared(%q) = %v, want %v", test.path, got, test.want) |
| 30 | } |
| 31 | } |
| 32 | if presentedFileDeclared(nil, "dist/game.html") { |
| 33 | t.Fatal("nil result authorized a presented file") |
| 34 | } |
| 35 | result.Name = "plugin.present" |
| 36 | if presentedFileDeclared(result, "dist/game.html") { |
| 37 | t.Fatal("same-named plugin metadata authorized a presented file") |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestPresentedReadPolicyRechecksCurrentForbidRead(t *testing.T) { |
| 42 | isolateDesktopUserDirs(t) |
| 43 | root := t.TempDir() |
| 44 | secretDir := filepath.Join(root, "secret") |
| 45 | if err := os.MkdirAll(secretDir, 0o755); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | secret := filepath.Join(secretDir, "token.txt") |
| 49 | if err := os.WriteFile(secret, []byte("secret"), 0o600); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | if err := os.WriteFile(filepath.Join(root, "reasonix.toml"), []byte("[sandbox]\nforbid_read = [\"secret\"]\n"), 0o600); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | if readPolicyAllowsPath(root, secret) { |
| 56 | t.Fatal("a current forbid_read entry must revoke an older presentation") |
| 57 | } |
| 58 | public := filepath.Join(root, "public.txt") |
| 59 | if err := os.WriteFile(public, []byte("public"), 0o600); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | if !readPolicyAllowsPath(root, public) { |
| 63 | t.Fatal("ordinary readable file was rejected") |
| 64 | } |
| 65 | } |
| 66 |