package provider import ( "errors" "os" "path/filepath" "strings" "syscall" "testing" ) func TestAccountCodexRuntimeLinks(t *testing.T) { const dir = "tmp/arg0-linux/codex-arg0aB12z9/" for _, tc := range []struct { name, path, target string accept bool }{ {"apply_patch", dir + "apply_patch", "/opt/codex", true}, {"applypatch", dir + "applypatch", "/opt/codex", true}, {"sandbox", dir + "codex-linux-sandbox", "/opt/codex", true}, {"regular", dir + "apply_patch", "", false}, {"directory", dir + "applypatch", "", false}, {"other-name", dir + "codex", "/opt/codex", false}, {"short", "tmp/arg0-linux/codex-arg0abcde/apply_patch", "/opt/codex", false}, {"long", "tmp/arg0-linux/codex-arg0abcdefg/apply_patch", "/opt/codex", false}, {"suffix-punctuation", "tmp/arg0-linux/codex-arg0abc_de/apply_patch", "/opt/codex", false}, {"other-parent", "tmp/arg0/codex-arg0abc123/apply_patch", "/opt/codex", false}, {"other-top", ".tmp/arg0-linux/codex-arg0abc123/apply_patch", "/opt/codex", false}, {"nested", dir + "nested/apply_patch", "/opt/codex", false}, {"relative", dir + "apply_patch", "../../../opt/codex", false}, {"normalized-target", dir + "apply_patch", "/opt/./codex", false}, {"other-target", dir + "apply_patch", "/opt/evil", false}, {"other-version", dir + "apply_patch", "/opt/codex", false}, {"other-provider", dir + "apply_patch", "/opt/codex", false}, {"source-not-staged", dir + "apply_patch", "/opt/codex", true}, } { t.Run(tc.name, func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) root := l.runtime if tc.name == "source-not-staged" { root = b.Home } path := filepath.Join(root, tc.path) if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { t.Fatal(err) } var createErr error if tc.name == "regular" { createErr = os.WriteFile(path, []byte("fixture"), 0600) } else if tc.name == "directory" { createErr = os.Mkdir(path, 0700) } else { createErr = os.Symlink(tc.target, path) } if err := createErr; err != nil { t.Fatal(err) } if tc.name == "other-version" { l.source.Expected.ProviderVersion = "other" } if tc.name == "other-provider" { l.source.Provider = "claude" } err := l.validateRuntimeEntries() if tc.accept { if err != nil { t.Fatal(err) } } else if !errors.Is(err, ErrContainment) || (tc.name != "other-version" && tc.name != "other-provider" && !strings.Contains(err.Error(), "runtime_special_entry") && !strings.Contains(err.Error(), "runtime_codex_")) { t.Fatalf("unsafe link accepted or wrong reason: %v", err) } if tc.name == "source-not-staged" { preflightFixture(t, l) if _, err = os.Lstat(filepath.Join(l.runtime, tc.path)); !os.IsNotExist(err) { t.Fatal("source link staged") } } }) } } func TestAccountCodexLinkOwner(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) rel := "tmp/arg0-linux/codex-arg0aB12z9/apply_patch" path := filepath.Join(l.runtime, rel) if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { t.Fatal(err) } if err := os.Symlink("/opt/codex", path); err != nil { t.Fatal(err) } info, err := os.Lstat(path) if err != nil { t.Fatal(err) } if !l.codexRuntimeLink(path, rel, info) { t.Fatal("runner link rejected") } info.Sys().(*syscall.Stat_t).Uid++ if l.codexRuntimeLink(path, rel, info) { t.Fatal("foreign link accepted") } } func TestAccountCodexLinkScope(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) rel := "tmp/arg0-linux/codex-arg0aB12z9/apply_patch" path := filepath.Join(l.runtime, rel) if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { t.Fatal(err) } if err := os.Symlink("/opt/codex", path); err != nil { t.Fatal(err) } info, err := os.Lstat(path) if err != nil { t.Fatal(err) } for _, scope := range []string{"provider", "version"} { t.Run(scope, func(t *testing.T) { l.source.Provider = "codex" l.source.Expected.ProviderVersion = "0.165.0" if scope == "provider" { l.source.Provider = "claude" } else { l.source.Expected.ProviderVersion = "other" } if l.codexRuntimeLink(path, rel, info) { t.Fatal("link accepted outside pinned Codex runtime") } }) } }