package mac import ( "archive/tar" "bytes" "compress/gzip" "context" "crypto/sha256" "encoding/hex" "errors" "os" "path/filepath" "strings" "testing" ) // The pins: exactly the official darwin/arm64 archives, by the digests // their projects publish (ripgrep's .sha256 asset, Node's SHASUMS256.txt; // both re-checked by downloading the archive on 2026-11-09). func TestToolPins(t *testing.T) { for _, c := range []struct { tool Tool version, url, sha, root, bin string }{ {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"}, {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"}, } { 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 { t.Fatalf("%s pin changed: %+v", c.tool.Name, c.tool) } } if !NodeSupported(Node.Version) { t.Fatal("the pinned Node is older than srt needs") } if len(SRTTools) != 2 || SRTTools[0].Name != Node.Name || SRTTools[1].Name != Ripgrep.Name { t.Fatalf("srt's macOS needs: %+v", SRTTools) } } type entry struct { name, body, link string dir bool } func archive(t *testing.T, entries ...entry) []byte { t.Helper() var buf bytes.Buffer z := gzip.NewWriter(&buf) tw := tar.NewWriter(z) for _, e := range entries { h := &tar.Header{Name: e.name, Mode: 0755, Size: int64(len(e.body)), Typeflag: tar.TypeReg} switch { case e.dir: h.Typeflag, h.Size = tar.TypeDir, 0 case e.link != "": h.Typeflag, h.Linkname, h.Size = tar.TypeSymlink, e.link, 0 } if err := tw.WriteHeader(h); err != nil { t.Fatal(err) } if h.Size > 0 { _, _ = tw.Write([]byte(e.body)) } } _ = tw.Close() _ = z.Close() return buf.Bytes() } func pinned(raw []byte) Tool { sum := sha256.Sum256(raw) return Tool{Name: "rg", Version: "1", URL: "https://example.invalid/rg.tar.gz", SHA256: hex.EncodeToString(sum[:]), Root: "rg-1", Binary: "rg"} } // The digest is checked before anything is unpacked; an archive that does // not match leaves nothing behind. A matching one is unpacked whole, its // program executable, and a second install does not download again. func TestFetchChecksTheDigest(t *testing.T) { 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"}) tool := pinned(good) dir := t.TempDir() served := 0 get := func(body []byte) func(context.Context, string, int64) ([]byte, error) { return func(_ context.Context, url string, _ int64) ([]byte, error) { served++ if url != tool.URL { return nil, errors.New("url " + url) } return body, nil } } tampered := append([]byte{}, good...) tampered[len(tampered)/2] ^= 1 if _, err := Fetch(context.Background(), tool, dir, get(tampered)); !errors.Is(err, ErrTool) { t.Fatalf("tampered archive: %v", err) } if left, _ := os.ReadDir(dir); len(left) != 0 { t.Fatalf("a refused archive left %v", left) } bin, err := Fetch(context.Background(), tool, dir, get(good)) if err != nil || bin != filepath.Join(dir, "rg-1", "rg") { t.Fatalf("%s %v", bin, err) } if info, err := os.Stat(bin); err != nil || info.Mode().Perm()&0100 == 0 { t.Fatal("program not executable") } if _, err := Fetch(context.Background(), tool, dir, get(nil)); err != nil || served != 2 { t.Fatalf("reinstalled: %v served %d", err, served) } } // Nothing lands outside the archive's root: no absolute or climbing // names, no symlink out, no other root. func TestFetchKeepsToTheRoot(t *testing.T) { for name, a := range map[string][]entry{ "climbing": {{name: "rg-1/rg", body: "x"}, {name: "rg-1/../../evil", body: "x"}}, "absolute": {{name: "rg-1/rg", body: "x"}, {name: "/etc/evil", body: "x"}}, "other root": {{name: "rg-1/rg", body: "x"}, {name: "other/rg", body: "x"}}, "link out": {{name: "rg-1/rg", body: "x"}, {name: "rg-1/up", link: "../.."}}, "absolute link": {{name: "rg-1/rg", body: "x"}, {name: "rg-1/up", link: "/Users/paul"}}, "no program": {{name: "rg-1/README", body: "x"}}, } { raw := archive(t, a...) dir := t.TempDir() if _, err := Fetch(context.Background(), pinned(raw), dir, func(context.Context, string, int64) ([]byte, error) { return raw, nil }); !errors.Is(err, ErrTool) { t.Fatalf("%s: %v", name, err) } if left, _ := os.ReadDir(dir); len(left) != 0 { t.Fatalf("%s left %v", name, left) } } } // No link in an archive is ever made. A chain of links that each look // harmless on paper (d/x to its parent, then d/x/y to its parent, which // on disk is outside the root) and a file written through them are // refused, and nothing lands outside. The links a tool needs are made // afterwards with fixed targets; the archive's own at those names are // left out. func TestFetchMakesNoLinksFromTheArchive(t *testing.T) { outside := t.TempDir() 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"}) dir := filepath.Join(filepath.Dir(outside), "staging-"+filepath.Base(outside)) if _, err := Fetch(context.Background(), pinned(chain), dir, func(context.Context, string, int64) ([]byte, error) { return chain, nil }); !errors.Is(err, ErrTool) { t.Fatalf("chained links: %v", err) } defer os.RemoveAll(dir) if _, err := os.Stat(filepath.Join(outside, "evil")); !os.IsNotExist(err) { t.Fatal("a file landed outside the root") } var hard bytes.Buffer z := gzip.NewWriter(&hard) tw := tar.NewWriter(z) _ = tw.WriteHeader(&tar.Header{Name: "rg-1/rg", Mode: 0755, Size: 1, Typeflag: tar.TypeReg}) _, _ = tw.Write([]byte("x")) _ = tw.WriteHeader(&tar.Header{Name: "rg-1/also", Typeflag: tar.TypeLink, Linkname: "rg-1/rg"}) _ = tw.Close() _ = z.Close() 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) { t.Fatalf("hard link: %v", err) } 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: "../../.."}) tool := pinned(node) tool.Root, tool.Binary, tool.Links, tool.SkipLinks = "node-1", "bin/node", map[string]string{"bin/npm": "../lib/npm-cli.js"}, []string{"bin/corepack"} dir = t.TempDir() if _, err := Fetch(context.Background(), tool, dir, func(context.Context, string, int64) ([]byte, error) { return node, nil }); err != nil { t.Fatal(err) } if target, err := os.Readlink(filepath.Join(dir, "node-1/bin/npm")); err != nil || target != "../lib/npm-cli.js" { t.Fatalf("npm link %q %v", target, err) } if _, err := os.Lstat(filepath.Join(dir, "node-1/bin/corepack")); !os.IsNotExist(err) { t.Fatal("a skipped link was made") } } // With the real archives at hand (EQUIPO_TOOL_ARCHIVES: a directory // holding them under their release names), the pins match and both // unpack. Skipped otherwise: CI does not download them. func TestFetchRealArchives(t *testing.T) { src := os.Getenv("EQUIPO_TOOL_ARCHIVES") if src == "" { t.Skip("EQUIPO_TOOL_ARCHIVES not set") } dir := t.TempDir() for _, tool := range SRTTools { bin, err := Fetch(context.Background(), tool, dir, func(_ context.Context, url string, _ int64) ([]byte, error) { return os.ReadFile(filepath.Join(src, url[strings.LastIndex(url, "/")+1:])) }) if err != nil { t.Fatalf("%s: %v", tool.Name, err) } t.Log(bin) } if _, err := os.Lstat(filepath.Join(dir, Node.Root, "bin", "npm")); err != nil { t.Fatal("npm missing from Node") } }