| 1 | package opencode |
| 2 | |
| 3 | import ( |
| 4 | "archive/tar" |
| 5 | "bytes" |
| 6 | "compress/gzip" |
| 7 | "context" |
| 8 | "crypto/sha512" |
| 9 | "encoding/base64" |
| 10 | "errors" |
| 11 | "io" |
| 12 | "os" |
| 13 | "path" |
| 14 | "path/filepath" |
| 15 | "runtime" |
| 16 | "strings" |
| 17 | |
| 18 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 19 | ) |
| 20 | |
| 21 | // Paths are an install root's tree and the binary in it, as the |
| 22 | // installer lays them out: <root>/opencode/node_modules, with the binary |
| 23 | // at <platform package>/bin/opencode. |
| 24 | func Paths(root, platformPackage string) (tree, binary string) { |
| 25 | tree = filepath.Join(root, "opencode", "node_modules") |
| 26 | return tree, filepath.Join(tree, filepath.FromSlash(platformPackage), "bin", "opencode") |
| 27 | } |
| 28 | |
| 29 | // InstallPins are what an install is held to: the tree's digest, the |
| 30 | // platform's binary package and the lockfile entries of the packages it |
| 31 | // fetches. The committed ones come from Pins and the lockfile; a test |
| 32 | // passes its own. |
| 33 | type InstallPins struct { |
| 34 | Tree string |
| 35 | Platform Platform |
| 36 | Packages map[string]Locked |
| 37 | } |
| 38 | |
| 39 | // CommittedPins are this repository's pins for goarch. |
| 40 | func CommittedPins(goarch string) (InstallPins, bool) { |
| 41 | tree, platform, ok := Pins(goarch) |
| 42 | if !ok { |
| 43 | return InstallPins{}, false |
| 44 | } |
| 45 | pins := InstallPins{Tree: tree, Platform: platform, Packages: map[string]Locked{}} |
| 46 | for _, name := range []string{PackageName, platform.Package} { |
| 47 | l, ok := Lock(name) |
| 48 | if !ok { |
| 49 | return InstallPins{}, false |
| 50 | } |
| 51 | pins.Packages[name] = l |
| 52 | } |
| 53 | return pins, Pinned() |
| 54 | } |
| 55 | |
| 56 | // Fetch reads a URL's body, at most limit bytes. |
| 57 | type Fetch func(ctx context.Context, url string, limit int64) ([]byte, error) |
| 58 | |
| 59 | // maxTarball bounds one package's download; maxFile one file in it. |
| 60 | const ( |
| 61 | maxTarball = 256 << 20 |
| 62 | maxFile = 320 << 20 |
| 63 | ) |
| 64 | |
| 65 | // InstallPinned installs the pinned OpenCode under root for goarch: the two |
| 66 | // packages the lockfile names (@opencode/cli and this platform's binary |
| 67 | // package), each checked against its integrity before it is unpacked, |
| 68 | // no script run, the tree sealed read-only, then the tree and the binary |
| 69 | // held to the committed digests. An install that does not verify is |
| 70 | // removed. An install already in place and verified is kept. |
| 71 | func InstallPinned(ctx context.Context, root, goarch string, fetch Fetch) error { |
| 72 | pins, ok := CommittedPins(goarch) |
| 73 | if !ok { |
| 74 | return ErrUnpinned |
| 75 | } |
| 76 | return InstallWith(ctx, root, pins, fetch) |
| 77 | } |
| 78 | |
| 79 | // InstallWith is InstallPinned held to pins. |
| 80 | func InstallWith(ctx context.Context, root string, pins InstallPins, fetch Fetch) error { |
| 81 | if VerifyWith(root, pins) == nil { |
| 82 | return nil |
| 83 | } |
| 84 | if !filepath.IsAbs(root) || filepath.Clean(root) != root { |
| 85 | return ErrUnpinned |
| 86 | } |
| 87 | if err := os.MkdirAll(root, 0700); err != nil { |
| 88 | return err |
| 89 | } |
| 90 | scratch, err := os.MkdirTemp(root, ".install-") |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | defer removeTree(scratch) |
| 95 | tree := filepath.Join(scratch, "node_modules") |
| 96 | for _, name := range []string{PackageName, pins.Platform.Package} { |
| 97 | l, ok := pins.Packages[name] |
| 98 | if !ok || !strings.HasPrefix(l.Resolved, "https://") { |
| 99 | return ErrUnpinned |
| 100 | } |
| 101 | raw, err := fetch(ctx, l.Resolved, maxTarball) |
| 102 | if err != nil { |
| 103 | return err |
| 104 | } |
| 105 | if !integrityMatches(raw, l.Integrity) { |
| 106 | return ErrUnpinned |
| 107 | } |
| 108 | if err := unpack(raw, filepath.Join(tree, filepath.FromSlash(name))); err != nil { |
| 109 | return err |
| 110 | } |
| 111 | } |
| 112 | if err := provider.SealTree(tree); err != nil { |
| 113 | return err |
| 114 | } |
| 115 | dst, _ := Paths(root, pins.Platform.Package) |
| 116 | removeTree(filepath.Dir(dst)) |
| 117 | if err := os.MkdirAll(filepath.Dir(dst), 0700); err != nil { |
| 118 | return err |
| 119 | } |
| 120 | // A directory moves to another parent only while it is writable. |
| 121 | if err := os.Chmod(tree, 0755); err != nil { |
| 122 | return err |
| 123 | } |
| 124 | if err := os.Rename(tree, dst); err != nil { |
| 125 | return err |
| 126 | } |
| 127 | if err := os.Chmod(dst, 0555); err != nil { |
| 128 | return err |
| 129 | } |
| 130 | if err := VerifyWith(root, pins); err != nil { |
| 131 | removeTree(filepath.Dir(dst)) |
| 132 | return err |
| 133 | } |
| 134 | return nil |
| 135 | } |
| 136 | |
| 137 | // integrityMatches compares an npm sha512 integrity with raw's. |
| 138 | func integrityMatches(raw []byte, integrity string) bool { |
| 139 | sum := sha512.Sum512(raw) |
| 140 | return integrity == "sha512-"+base64.StdEncoding.EncodeToString(sum[:]) |
| 141 | } |
| 142 | |
| 143 | // errArchive: a package archive holds something an install never |
| 144 | // unpacks (a link, a device, a path out of its package, a file too |
| 145 | // large). |
| 146 | var errArchive = errors.New("opencode: the package archive is not one this unpacks") |
| 147 | |
| 148 | // unpack writes an npm package tarball's package/ files into dir: regular |
| 149 | // files and directories only, each path inside dir. |
| 150 | func unpack(raw []byte, dir string) error { |
| 151 | gz, err := gzip.NewReader(bytes.NewReader(raw)) |
| 152 | if err != nil { |
| 153 | return errArchive |
| 154 | } |
| 155 | tr := tar.NewReader(gz) |
| 156 | if err := os.MkdirAll(dir, 0755); err != nil { |
| 157 | return err |
| 158 | } |
| 159 | for { |
| 160 | h, err := tr.Next() |
| 161 | if err == io.EOF { |
| 162 | return nil |
| 163 | } |
| 164 | if err != nil { |
| 165 | return errArchive |
| 166 | } |
| 167 | name := path.Clean(h.Name) |
| 168 | rest, ok := strings.CutPrefix(name, "package/") |
| 169 | if !ok || rest == "" || path.IsAbs(rest) || rest == ".." || strings.HasPrefix(rest, "../") || strings.Contains(rest, "/../") { |
| 170 | return errArchive |
| 171 | } |
| 172 | target := filepath.Join(dir, filepath.FromSlash(rest)) |
| 173 | if !strings.HasPrefix(target, dir+string(filepath.Separator)) { |
| 174 | return errArchive |
| 175 | } |
| 176 | switch h.Typeflag { |
| 177 | case tar.TypeDir: |
| 178 | if err := os.MkdirAll(target, 0755); err != nil { |
| 179 | return err |
| 180 | } |
| 181 | case tar.TypeReg: |
| 182 | if h.Size < 0 || h.Size > maxFile { |
| 183 | return errArchive |
| 184 | } |
| 185 | if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil { |
| 186 | return err |
| 187 | } |
| 188 | mode := os.FileMode(0644) |
| 189 | if h.Mode&0111 != 0 { |
| 190 | mode = 0755 |
| 191 | } |
| 192 | f, err := os.OpenFile(target, os.O_CREATE|os.O_EXCL|os.O_WRONLY, mode) |
| 193 | if err != nil { |
| 194 | return err |
| 195 | } |
| 196 | n, err := io.Copy(f, io.LimitReader(tr, maxFile+1)) |
| 197 | if cerr := f.Close(); err == nil { |
| 198 | err = cerr |
| 199 | } |
| 200 | if err != nil { |
| 201 | return err |
| 202 | } |
| 203 | if n != h.Size { |
| 204 | return errArchive |
| 205 | } |
| 206 | default: |
| 207 | return errArchive |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // Verify holds an install root to this platform's committed pins. |
| 213 | func Verify(root string) error { |
| 214 | pins, ok := CommittedPins(runtime.GOARCH) |
| 215 | if !ok { |
| 216 | return ErrUnpinned |
| 217 | } |
| 218 | return VerifyWith(root, pins) |
| 219 | } |
| 220 | |
| 221 | // VerifyWith holds an install root to pins: the tree sealed and safe, |
| 222 | // its digest (every file, mode and link) and the binary's. |
| 223 | func VerifyWith(root string, pins InstallPins) error { |
| 224 | tree, binary := Paths(root, pins.Platform.Package) |
| 225 | if provider.TreeRootSafe(tree) != nil { |
| 226 | return ErrUnpinned |
| 227 | } |
| 228 | if got, err := provider.TreeSHA256(tree); err != nil || got != pins.Tree { |
| 229 | return ErrUnpinned |
| 230 | } |
| 231 | if got, err := provider.FileSHA256(binary); err != nil || got != pins.Platform.BinarySHA256 { |
| 232 | return ErrUnpinned |
| 233 | } |
| 234 | return nil |
| 235 | } |
| 236 | |
| 237 | // removeTree removes a tree a seal made read-only. |
| 238 | func removeTree(dir string) { |
| 239 | filepath.Walk(dir, func(p string, info os.FileInfo, err error) error { |
| 240 | if err == nil && info.IsDir() { |
| 241 | os.Chmod(p, 0700) |
| 242 | } |
| 243 | return nil |
| 244 | }) |
| 245 | os.RemoveAll(dir) |
| 246 | } |
| 247 | |