| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "io/fs" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | ) |
| 13 | |
| 14 | // What the sandbox runs on, pinned in this binary (Mac runners review |
| 15 | // round 2): the Node and ripgrep programs as the pinned archives unpack |
| 16 | // them, and the srt package tree `npm ci` makes from the shipped |
| 17 | // lockfile. A gate that once ran outside the sandbox runs as this user |
| 18 | // and could replace any of them; a probe or a sandboxed check trusts |
| 19 | // them only while they match these, checked each time. |
| 20 | const ( |
| 21 | NodeBinarySHA256 = "2cc28a1880d0774616959f2210ee739bffa6ac695c887e8988a496a21271f6c0" |
| 22 | RGBinarySHA256 = "a326a1fb48074202e9ad41e4cd1e389eeea372c8c6f7d7e80da81176d5d9430e" |
| 23 | SRTTreeSHA256 = "6ef1352dcfa9c7c52a0545a2f40683ee9941e1ef93c01aa487f5deb19cc6b210" |
| 24 | ) |
| 25 | |
| 26 | // srtLink is the target npm gives node_modules/.bin/srt. |
| 27 | const srtLink = "../@anthropic-ai/sandbox-runtime/dist/cli.js" |
| 28 | |
| 29 | // ErrUnpinned: a sandbox tool is not where the installer put it or does |
| 30 | // not match its pin. |
| 31 | var ErrUnpinned = errors.New("mac: sandbox tools do not match their pins") |
| 32 | |
| 33 | // PinnedTools are where the installer puts srt, Node and ripgrep under |
| 34 | // installDir. |
| 35 | func PinnedTools(installDir string) Tools { |
| 36 | return Tools{ |
| 37 | SRT: filepath.Join(installDir, "srt", "node_modules", ".bin", "srt"), |
| 38 | Node: filepath.Join(installDir, "tools", Node.Root, filepath.FromSlash(Node.Binary)), |
| 39 | RG: filepath.Join(installDir, "tools", Ripgrep.Root, filepath.FromSlash(Ripgrep.Binary)), |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // VerifyTools checks t against the pins: each at its installed place, |
| 44 | // the srt link to its fixed target, every file of the srt package tree, |
| 45 | // and the Node and ripgrep programs. |
| 46 | func VerifyTools(installDir string, t Tools) error { |
| 47 | want := PinnedTools(installDir) |
| 48 | if t.SRT != want.SRT || t.Node != want.Node || t.RG != want.RG { |
| 49 | return ErrUnpinned |
| 50 | } |
| 51 | if target, err := os.Readlink(t.SRT); err != nil || target != srtLink { |
| 52 | return ErrUnpinned |
| 53 | } |
| 54 | for path, pin := range map[string]string{t.Node: NodeBinarySHA256, t.RG: RGBinarySHA256} { |
| 55 | info, err := os.Lstat(path) |
| 56 | if err != nil || !info.Mode().IsRegular() { |
| 57 | return ErrUnpinned |
| 58 | } |
| 59 | if sum, err := fileSHA256(path); err != nil || sum != pin { |
| 60 | return ErrUnpinned |
| 61 | } |
| 62 | } |
| 63 | if sum, err := TreeSHA256(filepath.Join(installDir, "srt", "node_modules")); err != nil || sum != SRTTreeSHA256 { |
| 64 | return ErrUnpinned |
| 65 | } |
| 66 | return nil |
| 67 | } |
| 68 | |
| 69 | // TreeSHA256 names a node_modules tree by every regular file's path and |
| 70 | // contents, in walk order, leaving out npm's own .bin links and |
| 71 | // .package-lock.json. Any other link or special file is refused. |
| 72 | func TreeSHA256(dir string) (string, error) { |
| 73 | h := sha256.New() |
| 74 | err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | rel, _ := filepath.Rel(dir, path) |
| 79 | switch { |
| 80 | case rel == ".bin" && d.IsDir(), rel == ".package-lock.json": |
| 81 | if d.IsDir() { |
| 82 | return fs.SkipDir |
| 83 | } |
| 84 | return nil |
| 85 | case d.IsDir(): |
| 86 | return nil |
| 87 | case !d.Type().IsRegular(): |
| 88 | return ErrUnpinned |
| 89 | } |
| 90 | sum, err := fileSHA256(path) |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | fmt.Fprintf(h, "%s\x00%s\n", filepath.ToSlash(rel), sum) |
| 95 | return nil |
| 96 | }) |
| 97 | if err != nil { |
| 98 | return "", err |
| 99 | } |
| 100 | return hex.EncodeToString(h.Sum(nil)), nil |
| 101 | } |
| 102 | |
| 103 | func fileSHA256(path string) (string, error) { |
| 104 | f, err := os.Open(path) |
| 105 | if err != nil { |
| 106 | return "", err |
| 107 | } |
| 108 | defer f.Close() |
| 109 | h := sha256.New() |
| 110 | if _, err := io.Copy(h, f); err != nil { |
| 111 | return "", err |
| 112 | } |
| 113 | return hex.EncodeToString(h.Sum(nil)), nil |
| 114 | } |
| 115 | |