| 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 | "io" |
| 12 | "os" |
| 13 | "path" |
| 14 | "path/filepath" |
| 15 | "slices" |
| 16 | "strings" |
| 17 | ) |
| 18 | |
| 19 | // Tool is a program the Mac worker brings with it rather than asking a |
| 20 | // Standard user to install with Homebrew (which needs an administrator): |
| 21 | // an official release archive pinned by its SHA-256, checked before |
| 22 | // anything is unpacked, unpacked into the runner's own directory. |
| 23 | type Tool struct { |
| 24 | Name, Version string |
| 25 | // URL is the official release archive (.tar.gz); SHA256 its digest |
| 26 | // as the project publishes it. |
| 27 | URL, SHA256 string |
| 28 | // Root is the archive's top directory, unpacked whole; Binary the |
| 29 | // program inside it. |
| 30 | Root, Binary string |
| 31 | // No symbolic or hard link in an archive is ever created. The links |
| 32 | // a tool needs are made afterwards with fixed targets (Links: name to |
| 33 | // relative target, under Root); the archive's own links at those |
| 34 | // names, and at SkipLinks, are left out; any other is refused. |
| 35 | Links map[string]string |
| 36 | SkipLinks []string |
| 37 | } |
| 38 | |
| 39 | // What srt 0.0.81 needs on macOS (its README, "Platform-Specific |
| 40 | // Dependencies"): ripgrep, and Node.js 22.12 or later to run at all |
| 41 | // (package.json engines). Nothing else: macOS's sandbox-exec is built in. |
| 42 | var ( |
| 43 | Ripgrep = Tool{Name: "rg", Version: "15.2.0", |
| 44 | URL: "https://github.com/BurntSushi/ripgrep/releases/download/15.2.0/ripgrep-15.2.0-aarch64-apple-darwin.tar.gz", |
| 45 | SHA256: "3750b2e93f37e0c692657da574d7019a101c0084da05a790c83fd335bad973e4", |
| 46 | Root: "ripgrep-15.2.0-aarch64-apple-darwin", Binary: "rg"} |
| 47 | Node = Tool{Name: "node", Version: "v22.23.4", |
| 48 | URL: "https://nodejs.org/dist/v22.23.4/node-v22.23.4-darwin-arm64.tar.gz", |
| 49 | SHA256: "c9e1dbc748b33ed2d14f42445191ecaf14c56259c7a9a26d0042c49ec2b66428", |
| 50 | Root: "node-v22.23.4-darwin-arm64", Binary: "bin/node", |
| 51 | // npm runs `npm ci` for srt; corepack is not needed. |
| 52 | Links: map[string]string{"bin/npm": "../lib/node_modules/npm/bin/npm-cli.js", "bin/npx": "../lib/node_modules/npm/bin/npx-cli.js"}, |
| 53 | SkipLinks: []string{"bin/corepack"}} |
| 54 | // SRTTools are fetched by the installer in this order. |
| 55 | SRTTools = []Tool{Node, Ripgrep} |
| 56 | ) |
| 57 | |
| 58 | // ErrTool refuses an archive: wrong digest, too large, or an entry that |
| 59 | // would land outside its root. |
| 60 | var ErrTool = errors.New("mac: tool archive refused") |
| 61 | |
| 62 | // maxToolArchive bounds a download (Node's is about 50 MB). |
| 63 | const maxToolArchive = 128 << 20 |
| 64 | |
| 65 | // Fetch installs t under dir (dir/<root>) unless it is there already and |
| 66 | // returns the program's path. The archive's digest is checked before |
| 67 | // anything is unpacked; entries are unpacked into a temporary directory |
| 68 | // beside the destination and moved into place whole. |
| 69 | func Fetch(ctx context.Context, t Tool, dir string, get func(ctx context.Context, url string, limit int64) ([]byte, error)) (string, error) { |
| 70 | dest := filepath.Join(dir, t.Root) |
| 71 | bin := filepath.Join(dest, filepath.FromSlash(t.Binary)) |
| 72 | if info, err := os.Stat(bin); err == nil && info.Mode().IsRegular() { |
| 73 | return bin, nil |
| 74 | } |
| 75 | raw, err := get(ctx, t.URL, maxToolArchive) |
| 76 | if err != nil { |
| 77 | return "", err |
| 78 | } |
| 79 | sum := sha256.Sum256(raw) |
| 80 | if hex.EncodeToString(sum[:]) != t.SHA256 { |
| 81 | return "", ErrTool |
| 82 | } |
| 83 | if err := os.MkdirAll(dir, 0700); err != nil { |
| 84 | return "", err |
| 85 | } |
| 86 | tmp, err := os.MkdirTemp(dir, ".fetch-") |
| 87 | if err != nil { |
| 88 | return "", err |
| 89 | } |
| 90 | defer os.RemoveAll(tmp) |
| 91 | if err := untar(raw, tmp, t); err != nil { |
| 92 | return "", err |
| 93 | } |
| 94 | if info, err := os.Stat(filepath.Join(tmp, t.Root, filepath.FromSlash(t.Binary))); err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0100 == 0 { |
| 95 | return "", ErrTool |
| 96 | } |
| 97 | _ = os.RemoveAll(dest) |
| 98 | if err := os.Rename(filepath.Join(tmp, t.Root), dest); err != nil { |
| 99 | return "", err |
| 100 | } |
| 101 | return bin, nil |
| 102 | } |
| 103 | |
| 104 | // untar unpacks a .tar.gz whose every entry is under t.Root: regular |
| 105 | // files and directories only, created through an os.Root, which refuses |
| 106 | // any path that would leave the staging directory, links included. The |
| 107 | // archive's links at t.Links or t.SkipLinks are left out; any other link |
| 108 | // is refused. t.Links are then made with their fixed targets. |
| 109 | func untar(raw []byte, into string, t Tool) error { |
| 110 | z, err := gzip.NewReader(bytes.NewReader(raw)) |
| 111 | if err != nil { |
| 112 | return ErrTool |
| 113 | } |
| 114 | root, err := os.OpenRoot(into) |
| 115 | if err != nil { |
| 116 | return err |
| 117 | } |
| 118 | defer root.Close() |
| 119 | tr := tar.NewReader(z) |
| 120 | for { |
| 121 | h, err := tr.Next() |
| 122 | if err == io.EOF { |
| 123 | break |
| 124 | } |
| 125 | if err != nil { |
| 126 | return ErrTool |
| 127 | } |
| 128 | name := path.Clean(h.Name) |
| 129 | if path.IsAbs(name) || strings.HasPrefix(name, "../") || (name != t.Root && !strings.HasPrefix(name, t.Root+"/")) { |
| 130 | return ErrTool |
| 131 | } |
| 132 | switch h.Typeflag { |
| 133 | case tar.TypeDir: |
| 134 | if err := root.MkdirAll(name, 0700); err != nil { |
| 135 | return ErrTool |
| 136 | } |
| 137 | case tar.TypeReg: |
| 138 | if err := root.MkdirAll(path.Dir(name), 0700); err != nil { |
| 139 | return ErrTool |
| 140 | } |
| 141 | f, err := root.OpenFile(name, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.FileMode(h.Mode)&0700|0400) |
| 142 | if err != nil { |
| 143 | return ErrTool |
| 144 | } |
| 145 | _, err = io.Copy(f, io.LimitReader(tr, maxToolArchive*4)) |
| 146 | if cerr := f.Close(); err == nil { |
| 147 | err = cerr |
| 148 | } |
| 149 | if err != nil { |
| 150 | return err |
| 151 | } |
| 152 | case tar.TypeSymlink: |
| 153 | rel := strings.TrimPrefix(name, t.Root+"/") |
| 154 | if _, fixed := t.Links[rel]; !fixed && !slices.Contains(t.SkipLinks, rel) { |
| 155 | return ErrTool |
| 156 | } |
| 157 | default: |
| 158 | // Hard links, devices, fifos: never. |
| 159 | return ErrTool |
| 160 | } |
| 161 | } |
| 162 | for name, target := range t.Links { |
| 163 | if err := root.Symlink(target, path.Join(t.Root, name)); err != nil { |
| 164 | return ErrTool |
| 165 | } |
| 166 | } |
| 167 | return nil |
| 168 | } |
| 169 | |