| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "syscall" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestAccountCodexRuntimeLinks(t *testing.T) { |
| 13 | const dir = "tmp/arg0-linux/codex-arg0aB12z9/" |
| 14 | for _, tc := range []struct { |
| 15 | name, path, target string |
| 16 | accept bool |
| 17 | }{ |
| 18 | {"apply_patch", dir + "apply_patch", "/opt/codex", true}, |
| 19 | {"applypatch", dir + "applypatch", "/opt/codex", true}, |
| 20 | {"sandbox", dir + "codex-linux-sandbox", "/opt/codex", true}, |
| 21 | {"regular", dir + "apply_patch", "", false}, |
| 22 | {"directory", dir + "applypatch", "", false}, |
| 23 | {"other-name", dir + "codex", "/opt/codex", false}, |
| 24 | {"short", "tmp/arg0-linux/codex-arg0abcde/apply_patch", "/opt/codex", false}, |
| 25 | {"long", "tmp/arg0-linux/codex-arg0abcdefg/apply_patch", "/opt/codex", false}, |
| 26 | {"suffix-punctuation", "tmp/arg0-linux/codex-arg0abc_de/apply_patch", "/opt/codex", false}, |
| 27 | {"other-parent", "tmp/arg0/codex-arg0abc123/apply_patch", "/opt/codex", false}, |
| 28 | {"other-top", ".tmp/arg0-linux/codex-arg0abc123/apply_patch", "/opt/codex", false}, |
| 29 | {"nested", dir + "nested/apply_patch", "/opt/codex", false}, |
| 30 | {"relative", dir + "apply_patch", "../../../opt/codex", false}, |
| 31 | {"normalized-target", dir + "apply_patch", "/opt/./codex", false}, |
| 32 | {"other-target", dir + "apply_patch", "/opt/evil", false}, |
| 33 | {"other-version", dir + "apply_patch", "/opt/codex", false}, |
| 34 | {"other-provider", dir + "apply_patch", "/opt/codex", false}, |
| 35 | {"source-not-staged", dir + "apply_patch", "/opt/codex", true}, |
| 36 | } { |
| 37 | t.Run(tc.name, func(t *testing.T) { |
| 38 | b, r := accountFixture(t) |
| 39 | l := openFixture(t, b, r) |
| 40 | root := l.runtime |
| 41 | if tc.name == "source-not-staged" { |
| 42 | root = b.Home |
| 43 | } |
| 44 | path := filepath.Join(root, tc.path) |
| 45 | if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | var createErr error |
| 49 | if tc.name == "regular" { |
| 50 | createErr = os.WriteFile(path, []byte("fixture"), 0600) |
| 51 | } else if tc.name == "directory" { |
| 52 | createErr = os.Mkdir(path, 0700) |
| 53 | } else { |
| 54 | createErr = os.Symlink(tc.target, path) |
| 55 | } |
| 56 | if err := createErr; err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | if tc.name == "other-version" { |
| 60 | l.source.Expected.ProviderVersion = "other" |
| 61 | } |
| 62 | if tc.name == "other-provider" { |
| 63 | l.source.Provider = "claude" |
| 64 | } |
| 65 | err := l.validateRuntimeEntries() |
| 66 | if tc.accept { |
| 67 | if err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | } 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_")) { |
| 71 | t.Fatalf("unsafe link accepted or wrong reason: %v", err) |
| 72 | } |
| 73 | if tc.name == "source-not-staged" { |
| 74 | preflightFixture(t, l) |
| 75 | if _, err = os.Lstat(filepath.Join(l.runtime, tc.path)); !os.IsNotExist(err) { |
| 76 | t.Fatal("source link staged") |
| 77 | } |
| 78 | } |
| 79 | }) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestAccountCodexLinkOwner(t *testing.T) { |
| 84 | b, r := accountFixture(t) |
| 85 | l := openFixture(t, b, r) |
| 86 | rel := "tmp/arg0-linux/codex-arg0aB12z9/apply_patch" |
| 87 | path := filepath.Join(l.runtime, rel) |
| 88 | if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | if err := os.Symlink("/opt/codex", path); err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | info, err := os.Lstat(path) |
| 95 | if err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | if !l.codexRuntimeLink(path, rel, info) { |
| 99 | t.Fatal("runner link rejected") |
| 100 | } |
| 101 | info.Sys().(*syscall.Stat_t).Uid++ |
| 102 | if l.codexRuntimeLink(path, rel, info) { |
| 103 | t.Fatal("foreign link accepted") |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestAccountCodexLinkScope(t *testing.T) { |
| 108 | b, r := accountFixture(t) |
| 109 | l := openFixture(t, b, r) |
| 110 | rel := "tmp/arg0-linux/codex-arg0aB12z9/apply_patch" |
| 111 | path := filepath.Join(l.runtime, rel) |
| 112 | if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | if err := os.Symlink("/opt/codex", path); err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | info, err := os.Lstat(path) |
| 119 | if err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | for _, scope := range []string{"provider", "version"} { |
| 123 | t.Run(scope, func(t *testing.T) { |
| 124 | l.source.Provider = "codex" |
| 125 | l.source.Expected.ProviderVersion = "0.165.0" |
| 126 | if scope == "provider" { |
| 127 | l.source.Provider = "claude" |
| 128 | } else { |
| 129 | l.source.Expected.ProviderVersion = "other" |
| 130 | } |
| 131 | if l.codexRuntimeLink(path, rel, info) { |
| 132 | t.Fatal("link accepted outside pinned Codex runtime") |
| 133 | } |
| 134 | }) |
| 135 | } |
| 136 | } |
| 137 | |