package opencode import ( "archive/tar" "bytes" "compress/gzip" "context" "crypto/sha256" "crypto/sha512" "encoding/base64" "encoding/hex" "encoding/json" "errors" "os" "path/filepath" "testing" "amadan.net/oficina/equipo/internal/runner/provider" ) // The committed package.json asks for @opencode/cli alone; the lockfile // pins it at Version with the integrity npm published, and every // platform package it lists (the supported ones among them) carries an // integrity and the registry's URL. func TestLockfilePinsOpenCode(t *testing.T) { pkg, _ := Package() var p struct { Dependencies map[string]string `json:"dependencies"` } if err := json.Unmarshal(pkg, &p); err != nil || p.Dependencies[PackageName] != Version || len(p.Dependencies) != 1 { t.Fatalf("package.json: %v %v", p.Dependencies, err) } if !Pinned() { t.Fatal("the lockfile does not pin OpenCode") } for name, l := range lockfile() { if name == "" { continue } if len(l.Integrity) <= len("sha512-") || l.Resolved == "" || l.Version != Version { t.Errorf("%s is not pinned: %+v", name, l) } } for _, goarch := range []string{"amd64", "arm64"} { tree, platform, ok := Pins(goarch) if !ok || len(tree) != 64 || len(platform.BinarySHA256) != 64 { t.Fatalf("%s pins: %q %+v", goarch, tree, platform) } if _, ok := Lock(platform.Package); !ok { t.Fatalf("%s's package %s is not in the lockfile", goarch, platform.Package) } if _, ok := CommittedPins(goarch); !ok { t.Fatalf("%s: no committed pins", goarch) } } if _, _, ok := Pins("riscv64"); ok { t.Fatal("an unsupported platform has pins") } } type entry struct { name, body string mode int64 kind byte link string } func tarball(t *testing.T, entries []entry) []byte { t.Helper() var buf bytes.Buffer gz := gzip.NewWriter(&buf) tw := tar.NewWriter(gz) for _, e := range entries { h := &tar.Header{Name: e.name, Mode: e.mode, Size: int64(len(e.body)), Typeflag: e.kind, Linkname: e.link} if e.kind == 0 { h.Typeflag = tar.TypeReg } if h.Typeflag != tar.TypeReg { h.Size = 0 } if err := tw.WriteHeader(h); err != nil { t.Fatal(err) } if h.Typeflag == tar.TypeReg { tw.Write([]byte(e.body)) } } tw.Close() gz.Close() return buf.Bytes() } func integrity(raw []byte) string { s := sha512.Sum512(raw) return "sha512-" + base64.StdEncoding.EncodeToString(s[:]) } // fakePins are pins for two synthetic packages, served by fetch. func fakePins(t *testing.T, cli, bin []byte) (InstallPins, Fetch, *int) { t.Helper() calls := 0 served := map[string][]byte{"https://registry.example/cli.tgz": cli, "https://registry.example/bin.tgz": bin} fetch := func(_ context.Context, url string, limit int64) ([]byte, error) { calls++ raw, ok := served[url] if !ok || int64(len(raw)) > limit { return nil, errors.New("no such file") } return raw, nil } sum := sha256.Sum256([]byte("a binary")) pins := InstallPins{Platform: Platform{Package: "@opencode/cli-test", BinarySHA256: hex.EncodeToString(sum[:])}, Packages: map[string]Locked{ PackageName: {Version: Version, Resolved: "https://registry.example/cli.tgz", Integrity: integrity(cli)}, "@opencode/cli-test": {Version: Version, Resolved: "https://registry.example/bin.tgz", Integrity: integrity(bin)}, }} return pins, fetch, &calls } var goodCLI = []entry{{name: "package/package.json", body: `{"name":"@opencode/cli"}`, mode: 0644}, {name: "package/bin/opencode.exe", body: "#!/bin/sh\nexit 1\n", mode: 0755}} var goodBin = []entry{{name: "package/package.json", body: `{"name":"@opencode/cli-test"}`, mode: 0644}, {name: "package/bin/", kind: tar.TypeDir, mode: 0755}, {name: "package/bin/opencode", body: "a binary", mode: 0755}} // The installer fetches exactly the lockfile's two packages, refuses one // whose bytes are not the lockfile's, unpacks regular files and // directories only, never a link or a path out of the package, seals the // tree and holds it and the binary to their digests, keeps a verified // install, and removes one that does not verify. func TestInstallFromArchives(t *testing.T) { ctx := context.Background() cli, bin := tarball(t, goodCLI), tarball(t, goodBin) pins, fetch, calls := fakePins(t, cli, bin) // The digest of what the install makes, then the install held to it. probe := filepath.Join(t.TempDir(), "probe") if err := InstallWith(ctx, probe, pins, fetch); !errors.Is(err, ErrUnpinned) { t.Fatalf("an install with no tree pin verified: %v", err) } if _, err := os.Stat(filepath.Join(probe, "opencode", "node_modules")); !os.IsNotExist(err) { t.Fatalf("an install that did not verify was left: %v", err) } scratch := filepath.Join(t.TempDir(), "node_modules") for name, raw := range map[string][]byte{PackageName: cli, "@opencode/cli-test": bin} { if err := unpack(raw, filepath.Join(scratch, filepath.FromSlash(name))); err != nil { t.Fatal(err) } } t.Cleanup(func() { removeTree(scratch) }) if err := provider.SealTree(scratch); err != nil { t.Fatal(err) } pins.Tree, _ = provider.TreeSHA256(scratch) root := filepath.Join(t.TempDir(), "root") t.Cleanup(func() { removeTree(root) }) *calls = 0 if err := InstallWith(ctx, root, pins, fetch); err != nil { t.Fatal(err) } if *calls != 2 { t.Fatalf("%d fetches", *calls) } tree, binary := Paths(root, pins.Platform.Package) if info, err := os.Stat(binary); err != nil || info.Mode().Perm()&0222 != 0 || info.Mode().Perm()&0111 == 0 { t.Fatalf("binary %v %v", info, err) } if VerifyWith(root, pins) != nil { t.Fatal("the install does not verify") } // Installed and verified: kept, nothing fetched. *calls = 0 if err := InstallWith(ctx, root, pins, fetch); err != nil || *calls != 0 { t.Fatalf("reinstall: %v, %d fetches", err, *calls) } // Another binary digest, or another tree, does not verify. other := pins other.Platform.BinarySHA256 = pins.Tree if VerifyWith(root, other) == nil { t.Fatal("another binary pin verified") } other = pins other.Tree = pins.Platform.BinarySHA256 if VerifyWith(root, other) == nil { t.Fatal("another tree pin verified") } // A file changed in the tree after the install. os.Chmod(filepath.Dir(binary), 0755) os.Chmod(binary, 0755) os.WriteFile(filepath.Join(tree, "@opencode", "cli", "package.json"), []byte("{}"), 0644) if VerifyWith(root, pins) == nil { t.Fatal("a changed tree verified") } for name, bad := range map[string]struct { cli, bin []byte tamper bool }{ "bytes that are not the lockfile's": {cli, bin, true}, "a symlink": {cli, tarball(t, append(goodBin, entry{name: "package/bin/link", kind: tar.TypeSymlink, link: "/etc/passwd"})), false}, "a hard link": {cli, tarball(t, append(goodBin, entry{name: "package/bin/hard", kind: tar.TypeLink, link: "package/bin/opencode"})), false}, "a path out of the package": {cli, tarball(t, append(goodBin, entry{name: "package/../../evil", body: "x", mode: 0644})), false}, "an absolute path": {cli, tarball(t, append(goodBin, entry{name: "/etc/evil", body: "x", mode: 0644})), false}, "a path outside package/": {cli, tarball(t, append(goodBin, entry{name: "other/evil", body: "x", mode: 0644})), false}, "a device": {cli, tarball(t, append(goodBin, entry{name: "package/dev", kind: tar.TypeChar})), false}, } { p, f, _ := fakePins(t, bad.cli, bad.bin) p.Tree = pins.Tree if bad.tamper { l := p.Packages["@opencode/cli-test"] l.Integrity = integrity([]byte("other bytes")) p.Packages["@opencode/cli-test"] = l } dir := filepath.Join(t.TempDir(), "root") if err := InstallWith(ctx, dir, p, f); err == nil { t.Errorf("%s: installed", name) } if _, err := os.Stat(filepath.Join(dir, "evil")); err == nil { t.Errorf("%s: wrote outside the package", name) } if tr, _ := Paths(dir, p.Platform.Package); func() bool { _, err := os.Stat(tr); return err == nil }() { t.Errorf("%s: a tree was left", name) } removeTree(dir) } } // The real pinned install, where a runner (or CI's box) has one: it // verifies against this platform's committed pins. func TestPinnedInstallVerifies(t *testing.T) { root := os.Getenv("EQUIPO_OPENCODE_ROOT") if root == "" { t.Skip("no pinned OpenCode install (EQUIPO_OPENCODE_ROOT)") } if err := Verify(root); err != nil { t.Fatalf("the pinned install does not verify: %v", err) } }