package mac import ( "archive/tar" "bytes" "compress/gzip" "context" "crypto/sha256" "encoding/hex" "errors" "io" "os" "path" "path/filepath" "slices" "strings" ) // Tool is a program the Mac worker brings with it rather than asking a // Standard user to install with Homebrew (which needs an administrator): // an official release archive pinned by its SHA-256, checked before // anything is unpacked, unpacked into the runner's own directory. type Tool struct { Name, Version string // URL is the official release archive (.tar.gz); SHA256 its digest // as the project publishes it. URL, SHA256 string // Root is the archive's top directory, unpacked whole; Binary the // program inside it. Root, Binary string // No symbolic or hard link in an archive is ever created. The links // a tool needs are made afterwards with fixed targets (Links: name to // relative target, under Root); the archive's own links at those // names, and at SkipLinks, are left out; any other is refused. Links map[string]string SkipLinks []string } // What srt 0.0.81 needs on macOS (its README, "Platform-Specific // Dependencies"): ripgrep, and Node.js 22.12 or later to run at all // (package.json engines). Nothing else: macOS's sandbox-exec is built in. var ( Ripgrep = Tool{Name: "rg", Version: "15.2.0", URL: "https://github.com/BurntSushi/ripgrep/releases/download/15.2.0/ripgrep-15.2.0-aarch64-apple-darwin.tar.gz", SHA256: "3750b2e93f37e0c692657da574d7019a101c0084da05a790c83fd335bad973e4", Root: "ripgrep-15.2.0-aarch64-apple-darwin", Binary: "rg"} Node = Tool{Name: "node", Version: "v22.23.4", URL: "https://nodejs.org/dist/v22.23.4/node-v22.23.4-darwin-arm64.tar.gz", SHA256: "c9e1dbc748b33ed2d14f42445191ecaf14c56259c7a9a26d0042c49ec2b66428", Root: "node-v22.23.4-darwin-arm64", Binary: "bin/node", // npm runs `npm ci` for srt; corepack is not needed. Links: map[string]string{"bin/npm": "../lib/node_modules/npm/bin/npm-cli.js", "bin/npx": "../lib/node_modules/npm/bin/npx-cli.js"}, SkipLinks: []string{"bin/corepack"}} // SRTTools are fetched by the installer in this order. SRTTools = []Tool{Node, Ripgrep} ) // ErrTool refuses an archive: wrong digest, too large, or an entry that // would land outside its root. var ErrTool = errors.New("mac: tool archive refused") // maxToolArchive bounds a download (Node's is about 50 MB). const maxToolArchive = 128 << 20 // Fetch installs t under dir (dir/) unless it is there already and // returns the program's path. The archive's digest is checked before // anything is unpacked; entries are unpacked into a temporary directory // beside the destination and moved into place whole. func Fetch(ctx context.Context, t Tool, dir string, get func(ctx context.Context, url string, limit int64) ([]byte, error)) (string, error) { dest := filepath.Join(dir, t.Root) bin := filepath.Join(dest, filepath.FromSlash(t.Binary)) if info, err := os.Stat(bin); err == nil && info.Mode().IsRegular() { return bin, nil } raw, err := get(ctx, t.URL, maxToolArchive) if err != nil { return "", err } sum := sha256.Sum256(raw) if hex.EncodeToString(sum[:]) != t.SHA256 { return "", ErrTool } if err := os.MkdirAll(dir, 0700); err != nil { return "", err } tmp, err := os.MkdirTemp(dir, ".fetch-") if err != nil { return "", err } defer os.RemoveAll(tmp) if err := untar(raw, tmp, t); err != nil { return "", err } if info, err := os.Stat(filepath.Join(tmp, t.Root, filepath.FromSlash(t.Binary))); err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0100 == 0 { return "", ErrTool } _ = os.RemoveAll(dest) if err := os.Rename(filepath.Join(tmp, t.Root), dest); err != nil { return "", err } return bin, nil } // untar unpacks a .tar.gz whose every entry is under t.Root: regular // files and directories only, created through an os.Root, which refuses // any path that would leave the staging directory, links included. The // archive's links at t.Links or t.SkipLinks are left out; any other link // is refused. t.Links are then made with their fixed targets. func untar(raw []byte, into string, t Tool) error { z, err := gzip.NewReader(bytes.NewReader(raw)) if err != nil { return ErrTool } root, err := os.OpenRoot(into) if err != nil { return err } defer root.Close() tr := tar.NewReader(z) for { h, err := tr.Next() if err == io.EOF { break } if err != nil { return ErrTool } name := path.Clean(h.Name) if path.IsAbs(name) || strings.HasPrefix(name, "../") || (name != t.Root && !strings.HasPrefix(name, t.Root+"/")) { return ErrTool } switch h.Typeflag { case tar.TypeDir: if err := root.MkdirAll(name, 0700); err != nil { return ErrTool } case tar.TypeReg: if err := root.MkdirAll(path.Dir(name), 0700); err != nil { return ErrTool } f, err := root.OpenFile(name, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.FileMode(h.Mode)&0700|0400) if err != nil { return ErrTool } _, err = io.Copy(f, io.LimitReader(tr, maxToolArchive*4)) if cerr := f.Close(); err == nil { err = cerr } if err != nil { return err } case tar.TypeSymlink: rel := strings.TrimPrefix(name, t.Root+"/") if _, fixed := t.Links[rel]; !fixed && !slices.Contains(t.SkipLinks, rel) { return ErrTool } default: // Hard links, devices, fifos: never. return ErrTool } } for name, target := range t.Links { if err := root.Symlink(target, path.Join(t.Root, name)); err != nil { return ErrTool } } return nil }