package mac import ( "crypto/sha256" "encoding/hex" "errors" "fmt" "io" "io/fs" "os" "path/filepath" ) // What the sandbox runs on, pinned in this binary (Mac runners review // round 2): the Node and ripgrep programs as the pinned archives unpack // them, and the srt package tree `npm ci` makes from the shipped // lockfile. A gate that once ran outside the sandbox runs as this user // and could replace any of them; a probe or a sandboxed check trusts // them only while they match these, checked each time. const ( NodeBinarySHA256 = "2cc28a1880d0774616959f2210ee739bffa6ac695c887e8988a496a21271f6c0" RGBinarySHA256 = "a326a1fb48074202e9ad41e4cd1e389eeea372c8c6f7d7e80da81176d5d9430e" SRTTreeSHA256 = "6ef1352dcfa9c7c52a0545a2f40683ee9941e1ef93c01aa487f5deb19cc6b210" ) // srtLink is the target npm gives node_modules/.bin/srt. const srtLink = "../@anthropic-ai/sandbox-runtime/dist/cli.js" // ErrUnpinned: a sandbox tool is not where the installer put it or does // not match its pin. var ErrUnpinned = errors.New("mac: sandbox tools do not match their pins") // PinnedTools are where the installer puts srt, Node and ripgrep under // installDir. func PinnedTools(installDir string) Tools { return Tools{ SRT: filepath.Join(installDir, "srt", "node_modules", ".bin", "srt"), Node: filepath.Join(installDir, "tools", Node.Root, filepath.FromSlash(Node.Binary)), RG: filepath.Join(installDir, "tools", Ripgrep.Root, filepath.FromSlash(Ripgrep.Binary)), } } // VerifyTools checks t against the pins: each at its installed place, // the srt link to its fixed target, every file of the srt package tree, // and the Node and ripgrep programs. func VerifyTools(installDir string, t Tools) error { want := PinnedTools(installDir) if t.SRT != want.SRT || t.Node != want.Node || t.RG != want.RG { return ErrUnpinned } if target, err := os.Readlink(t.SRT); err != nil || target != srtLink { return ErrUnpinned } for path, pin := range map[string]string{t.Node: NodeBinarySHA256, t.RG: RGBinarySHA256} { info, err := os.Lstat(path) if err != nil || !info.Mode().IsRegular() { return ErrUnpinned } if sum, err := fileSHA256(path); err != nil || sum != pin { return ErrUnpinned } } if sum, err := TreeSHA256(filepath.Join(installDir, "srt", "node_modules")); err != nil || sum != SRTTreeSHA256 { return ErrUnpinned } return nil } // TreeSHA256 names a node_modules tree by every regular file's path and // contents, in walk order, leaving out npm's own .bin links and // .package-lock.json. Any other link or special file is refused. func TreeSHA256(dir string) (string, error) { h := sha256.New() err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { if err != nil { return err } rel, _ := filepath.Rel(dir, path) switch { case rel == ".bin" && d.IsDir(), rel == ".package-lock.json": if d.IsDir() { return fs.SkipDir } return nil case d.IsDir(): return nil case !d.Type().IsRegular(): return ErrUnpinned } sum, err := fileSHA256(path) if err != nil { return err } fmt.Fprintf(h, "%s\x00%s\n", filepath.ToSlash(rel), sum) return nil }) if err != nil { return "", err } return hex.EncodeToString(h.Sum(nil)), nil } func fileSHA256(path string) (string, error) { f, err := os.Open(path) if err != nil { return "", err } defer f.Close() h := sha256.New() if _, err := io.Copy(h, f); err != nil { return "", err } return hex.EncodeToString(h.Sum(nil)), nil }