package provider import ( "context" "encoding/json" "os" "path/filepath" "strings" "testing" ) func policyFixture(t *testing.T) (Binding, PolicyInputs) { t.Helper() b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) binary := filepath.Join(t.TempDir(), "codex") writeFake(t, binary, "fake binary") companion := filepath.Join(filepath.Dir(binary), "helper") writeFake(t, companion, "fake helper") work, scratch := filepath.Join(t.TempDir(), "work"), filepath.Join(t.TempDir(), "scratch") os.Mkdir(work, 0700) os.Mkdir(scratch, 0700) return l.Binding(), PolicyInputs{RuntimeFiles: []Mount{{Source: binary, Target: "/opt/codex", ReadOnly: true}, {Source: companion, Target: "/opt/helper", ReadOnly: true}}, DeniedPaths: []string{b.Home}, EgressHosts: []string{"chatgpt.com"}, ProviderVersion: "0.165.0", LauncherVersion: "2", BwrapVersion: "0.11.0", Settings: json.RawMessage(`{"sandbox":"read-only"}`), MCP: json.RawMessage(`{"servers":{}}`), ThreadPolicy: json.RawMessage(`{"start":{"permissions":"probe"},"resume":{"permissions":"probe"}}`), TurnPolicy: json.RawMessage(`{}`), Tools: []string{"read", "shell"}, Argv: []string{"/opt/codex", "app-server"}, Workspace: work, Scratch: scratch} } func mustPolicy(t *testing.T, b Binding, in PolicyInputs) Policy { t.Helper() p, e := BuildPolicy(b, in) if e != nil { t.Fatal(e) } return p } func TestEffectivePolicyHash(t *testing.T) { for _, field := range []string{"binary", "settings", "routing", "tools", "mcp", "managed", "env", "mount", "thread-start", "thread-resume", "turn", "provider-version", "launcher-version", "bwrap-version", "argv", "denied"} { t.Run(field, func(t *testing.T) { b, in := policyFixture(t) before := mustPolicy(t, b, in) switch field { case "binary": writeFake(t, in.RuntimeFiles[0].Source, "different binary") case "settings": in.Settings = json.RawMessage(`{"sandbox":"workspace-write"}`) case "routing": in.EgressHosts = append(in.EgressHosts, "refresh.example") case "tools": in.Tools = append(in.Tools, "image") case "mcp": in.MCP = json.RawMessage(`{"servers":{"equipo":{"command":"/opt/mcp"}}}`) case "managed": file := filepath.Join(t.TempDir(), "managed.json") writeFake(t, file, `{}`) in.RuntimeFiles = append(in.RuntimeFiles, Mount{Source: file, Target: "/etc/codex/managed.json", ReadOnly: true}) case "env": in.Env = map[string]string{"LANG": "en_IE.UTF-8"} case "mount": in.RuntimeFiles[1].Target = "/opt/other" case "thread-start": in.ThreadPolicy = json.RawMessage(`{"start":{"permissions":"other"},"resume":{"permissions":"probe"}}`) case "thread-resume": in.ThreadPolicy = json.RawMessage(`{"start":{"permissions":"probe"},"resume":{"permissions":"other"}}`) case "turn": in.TurnPolicy = json.RawMessage(`{"effort":"high"}`) case "provider-version": in.ProviderVersion = "new" case "launcher-version": in.LauncherVersion = "new" case "bwrap-version": in.BwrapVersion = "new" case "argv": in.Argv = append(in.Argv, "--flag") case "denied": in.DeniedPaths = append(in.DeniedPaths, "/protected") } after := mustPolicy(t, b, in) if before.Hash == after.Hash || before.ManifestHash == after.ManifestHash { t.Fatal("manifest change not hashed") } }) } t.Run("canonical and concrete", func(t *testing.T) { b, in := policyFixture(t) p := mustPolicy(t, b, in) in.Settings = json.RawMessage("{ \"sandbox\" : \"read-only\" }") q := mustPolicy(t, b, in) if p.Hash != q.Hash { t.Fatal("JSON not canonical") } in.Workspace = filepath.Join(t.TempDir(), "next") os.Mkdir(in.Workspace, 0700) q = mustPolicy(t, b, in) if p.Hash != q.Hash || p.ManifestHash == q.ManifestHash { t.Fatal("workspace role or concrete hash wrong") } if strings.Contains(string(p.Manifest), "fake-one") { t.Fatal("credential leaked") } }) t.Run("account exact", func(t *testing.T) { b, in := policyFixture(t) p := mustPolicy(t, b, in) other, _ := policyFixture(t) q := mustPolicy(t, other, in) if p.Hash == q.Hash { t.Fatal("different account home reused certificate") } }) t.Run("instructions separate", func(t *testing.T) { b, in := policyFixture(t) path := filepath.Join(t.TempDir(), "instructions") writeFake(t, path, "one") in.InstructionFiles = []Mount{{Source: path, Target: "/config/instructions", ReadOnly: true}} p := mustPolicy(t, b, in) writeFake(t, path, "two") q := mustPolicy(t, b, in) if p.Hash != q.Hash || p.InstructionHash == q.InstructionHash || p.ManifestHash == q.ManifestHash { t.Fatal("instructions not separately hashed") } }) } func TestUnexpectedConfigLayer(t *testing.T) { b, in := policyFixture(t) p := mustPolicy(t, b, in) good := ConfigReadback{Settings: in.Settings, MCP: in.MCP, ThreadPolicy: in.ThreadPolicy, TurnPolicy: in.TurnPolicy, Layers: []string{"equipo"}, HooksDisabled: true, PluginsDisabled: true} if e := p.ValidateReadback(good); e != nil { t.Fatal(e) } for _, kind := range []string{"user", "project", "managed", "remote", "ignored", "hooks", "plugins", "turn override"} { t.Run(kind, func(t *testing.T) { bad := good switch kind { case "remote": bad.RemoteControl = true case "ignored": bad.Settings = json.RawMessage(`{}`) case "hooks": bad.HooksDisabled = false case "plugins": bad.PluginsDisabled = false case "turn override": bad.TurnPolicy = json.RawMessage(`{"sandboxPolicy":{}}`) default: bad.Layers = append([]string{"equipo"}, kind) } if p.ValidateReadback(bad) == nil { t.Fatal("unexpected config accepted") } }) } for _, key := range []string{"ANTHROPIC_API_KEY", "OPENAI_API_KEY", "HTTPS_PROXY", "SSH_AUTH_SOCK", "DBUS_SESSION_BUS_ADDRESS", "PASEO_HOME", "CLAUDE_CODE_OAUTH_TOKEN"} { t.Run(key, func(t *testing.T) { t.Setenv(key, "parent-secret") p := mustPolicy(t, b, in) if strings.Contains(string(p.Manifest), "parent-secret") { t.Fatal("inherited environment") } bad := in bad.Env = map[string]string{key: "injected"} if _, e := BuildPolicy(b, bad); e == nil { t.Fatal("injected environment accepted") } }) } for _, key := range []string{"sandboxPolicy", "approvalPolicy", "permissions", "permissionProfile", "cwd", "model"} { bad := in bad.TurnPolicy = json.RawMessage(`{"` + key + `":"override"}`) if _, e := BuildPolicy(b, bad); e == nil { t.Fatalf("turn override %s accepted", key) } } forged := b forged.provenance = nil if _, e := BuildPolicy(forged, in); e == nil { t.Fatal("unverified binding accepted") } } func TestEffectivePolicyLaunchAttestation(t *testing.T) { b, in := policyFixture(t) p := mustPolicy(t, b, in) l := Launch{AttemptKey: "attempt", Workspace: in.Workspace, Binding: b, Policy: p, Selection: Selection{Provider: b.Provider, AccountID: b.AccountID, Model: "model", Family: "gpt", ConnectionID: "person-connection"}} hash, e := AttestLaunch(l, in.Argv) if e != nil || hash == "" || hash == p.Hash { t.Fatal("missing concrete attempt hash", e) } for _, kind := range []string{"argv", "workspace", "env", "mount", "binary", "account", "manifest", "manifesthash", "policyhash"} { t.Run(kind, func(t *testing.T) { bad := l argv := append([]string(nil), in.Argv...) switch kind { case "argv": argv = append(argv, "--override") case "workspace": bad.Workspace = "/different" case "env": bad.Policy.Env = map[string]string{"LANG": "bad"} case "mount": bad.Policy.Mounts = nil case "binary": writeFake(t, in.RuntimeFiles[0].Source, "changed") defer writeFake(t, in.RuntimeFiles[0].Source, "fake binary") case "account": bad.Selection.AccountID = "other" case "manifest": bad.Policy.Manifest = json.RawMessage(`{}`) case "manifesthash": bad.Policy.ManifestHash = "changed" case "policyhash": bad.Policy.Hash = "changed" } if _, e := AttestLaunch(bad, argv); e == nil { t.Fatal("changed launch accepted") } }) } if e := p.ValidateNetwork(b.Provider, []string{"other.example"}); e == nil { t.Fatal("egress drift accepted") } } func TestUnexpectedConfigInputs(t *testing.T) { for _, kind := range []string{"duplicate JSON", "secret JSON", "missing binary", "writable mount", "unprotected role", "duplicate tool", "symlink binary", "network number"} { t.Run(kind, func(t *testing.T) { b, in := policyFixture(t) switch kind { case "duplicate JSON": in.Settings = json.RawMessage(`{"a":1,"a":2}`) case "secret JSON": in.MCP = json.RawMessage(`{"apiKey":"secret"}`) case "missing binary": in.RuntimeFiles = nil case "writable mount": in.RuntimeFiles[0].ReadOnly = false case "unprotected role": in.Workspace = b.CredentialHome case "duplicate tool": in.Tools = append(in.Tools, in.Tools[0]) case "symlink binary": alias := filepath.Join(t.TempDir(), "alias") os.Symlink(in.RuntimeFiles[0].Source, alias) in.RuntimeFiles[0].Source = alias case "network number": in.EgressHosts = []string{"127.0.0.1"} } if _, e := BuildPolicy(b, in); e == nil { t.Fatal("unsafe policy input accepted") } }) } } func TestEffectivePolicyPreflightLaunch(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) _, in := policyFixture(t) e := l.Preflight(context.Background(), func(ctx context.Context, b Binding) (Identity, error) { p, e := BuildPolicy(b, in) if e != nil { return Identity{}, e } _, e = AttestLaunch(Launch{AttemptKey: "auth-check", Binding: b, Policy: p, Workspace: in.Workspace, Selection: Selection{Provider: b.Provider, AccountID: b.AccountID}}, in.Argv) return b.Expected, e }) if e != nil { t.Fatal("preflight could not launch status check", e) } } func TestEffectivePolicyHostBinary(t *testing.T) { b, in := policyFixture(t) p := mustPolicy(t, b, in) if p.ValidateRuntimeBinaries(in.RuntimeFiles[0].Source) != nil { t.Fatal("hashed binary rejected") } if p.ValidateRuntimeBinaries("/missing/binary") == nil { t.Fatal("unhashed host binary accepted") } } func TestEffectivePolicyManifestBoundary(t *testing.T) { b, in := policyFixture(t) p := mustPolicy(t, b, in) var value map[string]any json.Unmarshal(p.Manifest, &value) if value["OuterMounts"] == nil || value["NamespaceArgs"] == nil || value["GeneratedMounts"] == nil { t.Fatal("outer mount policy missing from manifest") } if p.ValidateFileDigest(in.RuntimeFiles[0].Source, "changed") == nil { t.Fatal("changed pinned file digest accepted") } } func TestPrivateHomeRejectsCredentialMount(t *testing.T) { b, in := policyFixture(t) fake := filepath.Join(t.TempDir(), "other-home") if e := os.Mkdir(fake, 0700); e != nil { t.Fatal(e) } secret := filepath.Join(fake, "auth.json") writeFake(t, secret, `{"token":"other-fake-login"}`) in.RuntimeFiles = append(in.RuntimeFiles, Mount{Source: secret, Target: "/config/credential", ReadOnly: true}) if _, e := BuildPolicy(b, in); e == nil { t.Fatal("credential source entered public manifest") } alias := filepath.Join(t.TempDir(), "aliased-runtime") if e := os.Link(secret, alias); e != nil { t.Fatal(e) } in.RuntimeFiles[len(in.RuntimeFiles)-1].Source = alias if _, e := BuildPolicy(b, in); e == nil { t.Fatal("credential inode alias entered public manifest") } } func TestEffectivePolicyTurnSources(t *testing.T) { for _, kind := range []string{"settings", "mcp", "managed"} { t.Run(kind, func(t *testing.T) { b, in := policyFixture(t) managed := filepath.Join(t.TempDir(), "managed.json") writeFake(t, managed, `{}`) in.RuntimeFiles = append(in.RuntimeFiles, Mount{Source: managed, Target: "/etc/codex/managed.json", ReadOnly: true}) p := mustPolicy(t, b, in) ctx := context.Background() if e := b.provenance.BeginTurn(ctx); e != nil { t.Fatal(e) } path := managed for _, m := range p.Mounts { if (kind == "settings" && m.Target == SettingsPath) || (kind == "mcp" && m.Target == MCPPath) { path = m.Source } } writeFake(t, path, `{"changed":true}`) _ = b.provenance.EndTurn(ctx) if e := b.provenance.BeginTurn(ctx); e == nil { t.Fatal("changed policy admitted next turn") } }) } } func TestPrivateHomeConfigOutsideWritableMounts(t *testing.T) { b, in := policyFixture(t) p := mustPolicy(t, b, in) for _, m := range p.Mounts { if m.Target == SettingsPath || m.Target == MCPPath { for _, w := range []string{b.Home, in.Workspace, in.Scratch} { if withinPath(m.Source, w) { t.Fatal("config source has writable namespace alias") } } } } } func TestEffectivePolicyRejectsWritableSource(t *testing.T) { for _, kind := range []string{"workspace", "scratch"} { t.Run(kind, func(t *testing.T) { b, in := policyFixture(t) root := in.Workspace if kind == "scratch" { root = in.Scratch } path := filepath.Join(root, "managed.json") writeFake(t, path, `{}`) in.RuntimeFiles = append(in.RuntimeFiles, Mount{Source: path, Target: "/etc/codex/managed.json", ReadOnly: true}) if _, e := BuildPolicy(b, in); e == nil { t.Fatal("writable managed alias accepted") } }) } } func TestEffectivePolicyProviderRequiresManifest(t *testing.T) { if _, e := AttestLaunch(Launch{Selection: Selection{Provider: "codex"}, Policy: Policy{Hash: "legacy"}}, []string{"/opt/codex"}); e == nil { t.Fatal("direct provider accepted legacy attestation") } } func TestEffectivePolicyConfigDirectory(t *testing.T) { for _, kind := range []string{"replacement", "mode", "cleanup", "resume"} { t.Run(kind, func(t *testing.T) { b, in := policyFixture(t) p := mustPolicy(t, b, in) l := b.provenance switch kind { case "replacement": if e := os.Rename(l.config, l.config+"-old"); e != nil { t.Fatal(e) } if e := os.Mkdir(l.config, 0700); e != nil { t.Fatal(e) } writeFake(t, filepath.Join(l.config, "equipo-settings.json"), string(p.Settings)) writeFake(t, filepath.Join(l.config, "equipo-mcp.json"), string(p.MCP)) defer func() { os.RemoveAll(l.config); os.Rename(l.config+"-old", l.config) }() case "mode": if e := os.Chmod(l.config, 0755); e != nil { t.Fatal(e) } case "cleanup": if e := l.Close(); e != nil { t.Fatal(e) } if _, e := os.Stat(l.config); !os.IsNotExist(e) { t.Fatal("config directory survived lease close") } return case "resume": writeFake(t, filepath.Join(l.config, "equipo-settings.json"), `{"changed":true}`) } if e := l.ValidatePolicy(context.Background()); e == nil { t.Fatal("changed config admitted resume") } }) } }