| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "archive/tar" |
| 5 | "bytes" |
| 6 | "compress/gzip" |
| 7 | "context" |
| 8 | "crypto/sha256" |
| 9 | "encoding/hex" |
| 10 | "errors" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | ) |
| 16 | |
| 17 | // The pins: exactly the official darwin/arm64 archives, by the digests |
| 18 | // their projects publish (ripgrep's .sha256 asset, Node's SHASUMS256.txt; |
| 19 | // both re-checked by downloading the archive on 2026-11-09). |
| 20 | func TestToolPins(t *testing.T) { |
| 21 | for _, c := range []struct { |
| 22 | tool Tool |
| 23 | version, url, sha, root, bin string |
| 24 | }{ |
| 25 | {Ripgrep, "15.2.0", "https://github.com/BurntSushi/ripgrep/releases/download/15.2.0/ripgrep-15.2.0-aarch64-apple-darwin.tar.gz", "3750b2e93f37e0c692657da574d7019a101c0084da05a790c83fd335bad973e4", "ripgrep-15.2.0-aarch64-apple-darwin", "rg"}, |
| 26 | {Node, "v22.23.4", "https://nodejs.org/dist/v22.23.4/node-v22.23.4-darwin-arm64.tar.gz", "c9e1dbc748b33ed2d14f42445191ecaf14c56259c7a9a26d0042c49ec2b66428", "node-v22.23.4-darwin-arm64", "bin/node"}, |
| 27 | } { |
| 28 | if c.tool.Version != c.version || c.tool.URL != c.url || c.tool.SHA256 != c.sha || c.tool.Root != c.root || c.tool.Binary != c.bin { |
| 29 | t.Fatalf("%s pin changed: %+v", c.tool.Name, c.tool) |
| 30 | } |
| 31 | } |
| 32 | if !NodeSupported(Node.Version) { |
| 33 | t.Fatal("the pinned Node is older than srt needs") |
| 34 | } |
| 35 | if len(SRTTools) != 2 || SRTTools[0].Name != Node.Name || SRTTools[1].Name != Ripgrep.Name { |
| 36 | t.Fatalf("srt's macOS needs: %+v", SRTTools) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | type entry struct { |
| 41 | name, body, link string |
| 42 | dir bool |
| 43 | } |
| 44 | |
| 45 | func archive(t *testing.T, entries ...entry) []byte { |
| 46 | t.Helper() |
| 47 | var buf bytes.Buffer |
| 48 | z := gzip.NewWriter(&buf) |
| 49 | tw := tar.NewWriter(z) |
| 50 | for _, e := range entries { |
| 51 | h := &tar.Header{Name: e.name, Mode: 0755, Size: int64(len(e.body)), Typeflag: tar.TypeReg} |
| 52 | switch { |
| 53 | case e.dir: |
| 54 | h.Typeflag, h.Size = tar.TypeDir, 0 |
| 55 | case e.link != "": |
| 56 | h.Typeflag, h.Linkname, h.Size = tar.TypeSymlink, e.link, 0 |
| 57 | } |
| 58 | if err := tw.WriteHeader(h); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | if h.Size > 0 { |
| 62 | _, _ = tw.Write([]byte(e.body)) |
| 63 | } |
| 64 | } |
| 65 | _ = tw.Close() |
| 66 | _ = z.Close() |
| 67 | return buf.Bytes() |
| 68 | } |
| 69 | |
| 70 | func pinned(raw []byte) Tool { |
| 71 | sum := sha256.Sum256(raw) |
| 72 | return Tool{Name: "rg", Version: "1", URL: "https://example.invalid/rg.tar.gz", SHA256: hex.EncodeToString(sum[:]), Root: "rg-1", Binary: "rg"} |
| 73 | } |
| 74 | |
| 75 | // The digest is checked before anything is unpacked; an archive that does |
| 76 | // not match leaves nothing behind. A matching one is unpacked whole, its |
| 77 | // program executable, and a second install does not download again. |
| 78 | func TestFetchChecksTheDigest(t *testing.T) { |
| 79 | good := archive(t, entry{name: "rg-1/", dir: true}, entry{name: "rg-1/rg", body: "#!/bin/sh\n"}, entry{name: "rg-1/doc/README.md", body: "x"}) |
| 80 | tool := pinned(good) |
| 81 | dir := t.TempDir() |
| 82 | served := 0 |
| 83 | get := func(body []byte) func(context.Context, string, int64) ([]byte, error) { |
| 84 | return func(_ context.Context, url string, _ int64) ([]byte, error) { |
| 85 | served++ |
| 86 | if url != tool.URL { |
| 87 | return nil, errors.New("url " + url) |
| 88 | } |
| 89 | return body, nil |
| 90 | } |
| 91 | } |
| 92 | tampered := append([]byte{}, good...) |
| 93 | tampered[len(tampered)/2] ^= 1 |
| 94 | if _, err := Fetch(context.Background(), tool, dir, get(tampered)); !errors.Is(err, ErrTool) { |
| 95 | t.Fatalf("tampered archive: %v", err) |
| 96 | } |
| 97 | if left, _ := os.ReadDir(dir); len(left) != 0 { |
| 98 | t.Fatalf("a refused archive left %v", left) |
| 99 | } |
| 100 | bin, err := Fetch(context.Background(), tool, dir, get(good)) |
| 101 | if err != nil || bin != filepath.Join(dir, "rg-1", "rg") { |
| 102 | t.Fatalf("%s %v", bin, err) |
| 103 | } |
| 104 | if info, err := os.Stat(bin); err != nil || info.Mode().Perm()&0100 == 0 { |
| 105 | t.Fatal("program not executable") |
| 106 | } |
| 107 | if _, err := Fetch(context.Background(), tool, dir, get(nil)); err != nil || served != 2 { |
| 108 | t.Fatalf("reinstalled: %v served %d", err, served) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Nothing lands outside the archive's root: no absolute or climbing |
| 113 | // names, no symlink out, no other root. |
| 114 | func TestFetchKeepsToTheRoot(t *testing.T) { |
| 115 | for name, a := range map[string][]entry{ |
| 116 | "climbing": {{name: "rg-1/rg", body: "x"}, {name: "rg-1/../../evil", body: "x"}}, |
| 117 | "absolute": {{name: "rg-1/rg", body: "x"}, {name: "/etc/evil", body: "x"}}, |
| 118 | "other root": {{name: "rg-1/rg", body: "x"}, {name: "other/rg", body: "x"}}, |
| 119 | "link out": {{name: "rg-1/rg", body: "x"}, {name: "rg-1/up", link: "../.."}}, |
| 120 | "absolute link": {{name: "rg-1/rg", body: "x"}, {name: "rg-1/up", link: "/Users/paul"}}, |
| 121 | "no program": {{name: "rg-1/README", body: "x"}}, |
| 122 | } { |
| 123 | raw := archive(t, a...) |
| 124 | dir := t.TempDir() |
| 125 | if _, err := Fetch(context.Background(), pinned(raw), dir, func(context.Context, string, int64) ([]byte, error) { return raw, nil }); !errors.Is(err, ErrTool) { |
| 126 | t.Fatalf("%s: %v", name, err) |
| 127 | } |
| 128 | if left, _ := os.ReadDir(dir); len(left) != 0 { |
| 129 | t.Fatalf("%s left %v", name, left) |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // No link in an archive is ever made. A chain of links that each look |
| 135 | // harmless on paper (d/x to its parent, then d/x/y to its parent, which |
| 136 | // on disk is outside the root) and a file written through them are |
| 137 | // refused, and nothing lands outside. The links a tool needs are made |
| 138 | // afterwards with fixed targets; the archive's own at those names are |
| 139 | // left out. |
| 140 | func TestFetchMakesNoLinksFromTheArchive(t *testing.T) { |
| 141 | outside := t.TempDir() |
| 142 | chain := archive(t, entry{name: "rg-1/rg", body: "x"}, entry{name: "rg-1/d/", dir: true}, entry{name: "rg-1/d/x", link: ".."}, entry{name: "rg-1/d/x/y", link: ".."}, entry{name: "rg-1/d/x/y/" + filepath.Base(outside) + "/evil", body: "x"}) |
| 143 | dir := filepath.Join(filepath.Dir(outside), "staging-"+filepath.Base(outside)) |
| 144 | if _, err := Fetch(context.Background(), pinned(chain), dir, func(context.Context, string, int64) ([]byte, error) { return chain, nil }); !errors.Is(err, ErrTool) { |
| 145 | t.Fatalf("chained links: %v", err) |
| 146 | } |
| 147 | defer os.RemoveAll(dir) |
| 148 | if _, err := os.Stat(filepath.Join(outside, "evil")); !os.IsNotExist(err) { |
| 149 | t.Fatal("a file landed outside the root") |
| 150 | } |
| 151 | var hard bytes.Buffer |
| 152 | z := gzip.NewWriter(&hard) |
| 153 | tw := tar.NewWriter(z) |
| 154 | _ = tw.WriteHeader(&tar.Header{Name: "rg-1/rg", Mode: 0755, Size: 1, Typeflag: tar.TypeReg}) |
| 155 | _, _ = tw.Write([]byte("x")) |
| 156 | _ = tw.WriteHeader(&tar.Header{Name: "rg-1/also", Typeflag: tar.TypeLink, Linkname: "rg-1/rg"}) |
| 157 | _ = tw.Close() |
| 158 | _ = z.Close() |
| 159 | if _, err := Fetch(context.Background(), pinned(hard.Bytes()), t.TempDir(), func(context.Context, string, int64) ([]byte, error) { return hard.Bytes(), nil }); !errors.Is(err, ErrTool) { |
| 160 | t.Fatalf("hard link: %v", err) |
| 161 | } |
| 162 | node := archive(t, entry{name: "node-1/bin/node", body: "#!/bin/sh\n"}, entry{name: "node-1/lib/npm-cli.js", body: "x"}, entry{name: "node-1/bin/npm", link: "/etc/passwd"}, entry{name: "node-1/bin/corepack", link: "../../.."}) |
| 163 | tool := pinned(node) |
| 164 | tool.Root, tool.Binary, tool.Links, tool.SkipLinks = "node-1", "bin/node", map[string]string{"bin/npm": "../lib/npm-cli.js"}, []string{"bin/corepack"} |
| 165 | dir = t.TempDir() |
| 166 | if _, err := Fetch(context.Background(), tool, dir, func(context.Context, string, int64) ([]byte, error) { return node, nil }); err != nil { |
| 167 | t.Fatal(err) |
| 168 | } |
| 169 | if target, err := os.Readlink(filepath.Join(dir, "node-1/bin/npm")); err != nil || target != "../lib/npm-cli.js" { |
| 170 | t.Fatalf("npm link %q %v", target, err) |
| 171 | } |
| 172 | if _, err := os.Lstat(filepath.Join(dir, "node-1/bin/corepack")); !os.IsNotExist(err) { |
| 173 | t.Fatal("a skipped link was made") |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // With the real archives at hand (EQUIPO_TOOL_ARCHIVES: a directory |
| 178 | // holding them under their release names), the pins match and both |
| 179 | // unpack. Skipped otherwise: CI does not download them. |
| 180 | func TestFetchRealArchives(t *testing.T) { |
| 181 | src := os.Getenv("EQUIPO_TOOL_ARCHIVES") |
| 182 | if src == "" { |
| 183 | t.Skip("EQUIPO_TOOL_ARCHIVES not set") |
| 184 | } |
| 185 | dir := t.TempDir() |
| 186 | for _, tool := range SRTTools { |
| 187 | bin, err := Fetch(context.Background(), tool, dir, func(_ context.Context, url string, _ int64) ([]byte, error) { |
| 188 | return os.ReadFile(filepath.Join(src, url[strings.LastIndex(url, "/")+1:])) |
| 189 | }) |
| 190 | if err != nil { |
| 191 | t.Fatalf("%s: %v", tool.Name, err) |
| 192 | } |
| 193 | t.Log(bin) |
| 194 | } |
| 195 | if _, err := os.Lstat(filepath.Join(dir, Node.Root, "bin", "npm")); err != nil { |
| 196 | t.Fatal("npm missing from Node") |
| 197 | } |
| 198 | } |
| 199 | |