| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "syscall" |
| 9 | "testing" |
| 10 | |
| 11 | "golang.org/x/sys/unix" |
| 12 | ) |
| 13 | |
| 14 | func TestAccountCodexRuntimeState(t *testing.T) { |
| 15 | for _, name := range []string{".tmp", "tmp", "installation_id", "goals_1.sqlite", "goals_1.sqlite-shm", "goals_1.sqlite-wal", "memories_1.sqlite", "memories_1.sqlite-shm", "memories_1.sqlite-wal", "queue_1.sqlite", "queue_1.sqlite-shm", "queue_1.sqlite-wal"} { |
| 16 | t.Run(name, func(t *testing.T) { |
| 17 | b, r := accountFixture(t) |
| 18 | l := openFixture(t, b, r) |
| 19 | preflightFixture(t, l) |
| 20 | path := filepath.Join(l.runtime, name) |
| 21 | if name == ".tmp" || name == "tmp" { |
| 22 | if err := os.MkdirAll(filepath.Join(path, "nested"), 0700); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | writeFake(t, filepath.Join(path, "nested", "lock"), "fixture") |
| 26 | } else { |
| 27 | writeFake(t, path, "fixture") |
| 28 | } |
| 29 | if err := l.ValidatePolicy(context.Background()); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | }) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestAccountCodexRuntimeStateGuards(t *testing.T) { |
| 37 | for _, mode := range []string{"scratch-file", "state-directory", "nested-setuid", "setgid", "symlink", "fifo", "hardlink", "skills", "nested-policy", "other-state", "other-version", "other-provider", "source-not-staged"} { |
| 38 | t.Run(mode, func(t *testing.T) { |
| 39 | b, r := accountFixture(t) |
| 40 | if mode == "other-version" { |
| 41 | b.Expected.ProviderVersion = "other" |
| 42 | writeMetadata(t, b.Home, b.Expected) |
| 43 | } |
| 44 | l := openFixture(t, b, r) |
| 45 | preflightFixture(t, l) |
| 46 | path := filepath.Join(l.runtime, "tmp") |
| 47 | if mode == "source-not-staged" { |
| 48 | if err := os.Mkdir(filepath.Join(b.Home, "tmp"), 0700); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | writeFake(t, filepath.Join(b.Home, "installation_id"), "source sentinel") |
| 52 | preflightFixture(t, l) |
| 53 | for _, name := range []string{"tmp", "installation_id"} { |
| 54 | if _, err := os.Lstat(filepath.Join(l.runtime, name)); !os.IsNotExist(err) { |
| 55 | t.Fatalf("source artifact staged: %s: %v", name, err) |
| 56 | } |
| 57 | } |
| 58 | return |
| 59 | } |
| 60 | if mode == "scratch-file" { |
| 61 | writeFake(t, path, "fixture") |
| 62 | } else { |
| 63 | if err := os.Mkdir(path, 0700); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | } |
| 67 | must := func(err error) { |
| 68 | t.Helper() |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | } |
| 73 | switch mode { |
| 74 | case "state-directory": |
| 75 | must(os.Mkdir(filepath.Join(l.runtime, "installation_id"), 0700)) |
| 76 | case "nested-setuid": |
| 77 | nested := filepath.Join(path, "nested") |
| 78 | must(os.Mkdir(nested, 0700)) |
| 79 | must(os.Chmod(nested, 0700|os.ModeSetuid)) |
| 80 | case "setgid": |
| 81 | must(os.Chmod(path, 0700|os.ModeSetgid)) |
| 82 | case "symlink": |
| 83 | must(os.Symlink(b.Home, filepath.Join(path, "escape"))) |
| 84 | case "fifo": |
| 85 | must(unix.Mkfifo(filepath.Join(path, "pipe"), 0600)) |
| 86 | case "hardlink": |
| 87 | must(os.Link(filepath.Join(b.Home, "auth.json"), filepath.Join(path, "linked"))) |
| 88 | case "skills": |
| 89 | must(os.MkdirAll(filepath.Join(l.runtime, "skills", ".system"), 0700)) |
| 90 | case "nested-policy": |
| 91 | writeFake(t, filepath.Join(path, "config.toml"), "fixture") |
| 92 | case "other-state": |
| 93 | writeFake(t, filepath.Join(l.runtime, "memories_2.sqlite"), "fixture") |
| 94 | case "other-provider": |
| 95 | l.source.Provider = "claude" |
| 96 | must(os.Remove(filepath.Join(l.runtime, "auth.json"))) |
| 97 | } |
| 98 | if err := l.validateRuntimeEntries(); !errors.Is(err, ErrContainment) { |
| 99 | t.Fatalf("unsafe runtime accepted: %v", err) |
| 100 | } |
| 101 | }) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestAccountCodexRuntimeOwner(t *testing.T) { |
| 106 | path := filepath.Join(t.TempDir(), "state") |
| 107 | writeFake(t, path, "fixture") |
| 108 | info, err := os.Lstat(path) |
| 109 | if err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | if !runtimeEntryMetadata(info) { |
| 113 | t.Fatal("runner-owned regular file rejected") |
| 114 | } |
| 115 | info.Sys().(*syscall.Stat_t).Uid++ |
| 116 | if runtimeEntryMetadata(info) { |
| 117 | t.Fatal("foreign owner accepted") |
| 118 | } |
| 119 | } |
| 120 | |