fix(opencode): verify Bun runtime zip checksum in release builds (#59360)

This commit is contained in:
Ryan Vogel
2026-10-26 16:09:06 -03:00
committed by GitHub
Unverified
parent 0ec4d0f862
commit 0f3a0c206f
+20 -4
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env bun
import { $ } from "bun"
import { createHash } from "node:crypto"
import { mkdir, rm } from "fs/promises"
import path from "path"
import { Script } from "@opencode/script"
@@ -252,16 +253,31 @@ async function compileExecutable(item: (typeof allTargets)[number]) {
const assets = await compileReleaseAssets(release)
const url = assets.get(`${name}.zip`)
if (!url) throw new Error(`Bun release ${release} does not include ${name}.zip`)
const sumsUrl = assets.get("SHASUMS256.txt")
if (!sumsUrl) throw new Error(`Bun release ${release} does not include SHASUMS256.txt`)
const token = process.env.GH_TOKEN ?? process.env.GITHUB_TOKEN
const response = await fetch(url, {
headers: { Accept: "application/octet-stream", ...(token ? { Authorization: `Bearer ${token}` } : {}) },
})
const headers = { Accept: "application/octet-stream", ...(token ? { Authorization: `Bearer ${token}` } : {}) }
const [response, sums] = await Promise.all([fetch(url, { headers }), fetch(sumsUrl, { headers })])
if (!response.ok) throw new Error(`Failed to download ${name} from Bun release ${release}: ${response.status}`)
if (!sums.ok) throw new Error(`Failed to download Bun release ${release} checksums: ${sums.status}`)
const expected = (await sums.text())
.split("\n")
.find((line) => line.trimEnd().endsWith(` ${name}.zip`))
?.split(/\s+/)[0]
if (!expected) throw new Error(`Missing checksum for ${name}.zip in Bun release ${release}`)
// Stream to disk instead of `Bun.write(archive, response)`: passing the Response object
// hangs forever if it gets GC'd mid-download (https://github.com/oven-sh/bun/issues/40278).
const hash = createHash("sha256")
const sink = Bun.file(archive).writer()
for await (const chunk of response.body!) await sink.write(chunk)
for await (const chunk of response.body!) {
hash.update(chunk)
await sink.write(chunk)
}
await sink.end()
if (hash.digest("hex") !== expected) {
await rm(archive)
throw new Error(`Checksum mismatch for ${name}.zip in Bun release ${release}`)
}
await $`unzip -oq ${archive} -d ${cache}`
await rm(archive)
return executable