package provider import ( "context" "errors" "os" "path/filepath" "syscall" "testing" "golang.org/x/sys/unix" ) func TestAccountCodexRuntimeState(t *testing.T) { 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"} { t.Run(name, func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) path := filepath.Join(l.runtime, name) if name == ".tmp" || name == "tmp" { if err := os.MkdirAll(filepath.Join(path, "nested"), 0700); err != nil { t.Fatal(err) } writeFake(t, filepath.Join(path, "nested", "lock"), "fixture") } else { writeFake(t, path, "fixture") } if err := l.ValidatePolicy(context.Background()); err != nil { t.Fatal(err) } }) } } func TestAccountCodexRuntimeStateGuards(t *testing.T) { 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"} { t.Run(mode, func(t *testing.T) { b, r := accountFixture(t) if mode == "other-version" { b.Expected.ProviderVersion = "other" writeMetadata(t, b.Home, b.Expected) } l := openFixture(t, b, r) preflightFixture(t, l) path := filepath.Join(l.runtime, "tmp") if mode == "source-not-staged" { if err := os.Mkdir(filepath.Join(b.Home, "tmp"), 0700); err != nil { t.Fatal(err) } writeFake(t, filepath.Join(b.Home, "installation_id"), "source sentinel") preflightFixture(t, l) for _, name := range []string{"tmp", "installation_id"} { if _, err := os.Lstat(filepath.Join(l.runtime, name)); !os.IsNotExist(err) { t.Fatalf("source artifact staged: %s: %v", name, err) } } return } if mode == "scratch-file" { writeFake(t, path, "fixture") } else { if err := os.Mkdir(path, 0700); err != nil { t.Fatal(err) } } must := func(err error) { t.Helper() if err != nil { t.Fatal(err) } } switch mode { case "state-directory": must(os.Mkdir(filepath.Join(l.runtime, "installation_id"), 0700)) case "nested-setuid": nested := filepath.Join(path, "nested") must(os.Mkdir(nested, 0700)) must(os.Chmod(nested, 0700|os.ModeSetuid)) case "setgid": must(os.Chmod(path, 0700|os.ModeSetgid)) case "symlink": must(os.Symlink(b.Home, filepath.Join(path, "escape"))) case "fifo": must(unix.Mkfifo(filepath.Join(path, "pipe"), 0600)) case "hardlink": must(os.Link(filepath.Join(b.Home, "auth.json"), filepath.Join(path, "linked"))) case "skills": must(os.MkdirAll(filepath.Join(l.runtime, "skills", ".system"), 0700)) case "nested-policy": writeFake(t, filepath.Join(path, "config.toml"), "fixture") case "other-state": writeFake(t, filepath.Join(l.runtime, "memories_2.sqlite"), "fixture") case "other-provider": l.source.Provider = "claude" must(os.Remove(filepath.Join(l.runtime, "auth.json"))) } if err := l.validateRuntimeEntries(); !errors.Is(err, ErrContainment) { t.Fatalf("unsafe runtime accepted: %v", err) } }) } } func TestAccountCodexRuntimeOwner(t *testing.T) { path := filepath.Join(t.TempDir(), "state") writeFake(t, path, "fixture") info, err := os.Lstat(path) if err != nil { t.Fatal(err) } if !runtimeEntryMetadata(info) { t.Fatal("runner-owned regular file rejected") } info.Sys().(*syscall.Stat_t).Uid++ if runtimeEntryMetadata(info) { t.Fatal("foreign owner accepted") } }