mirror of
https://github.com/NeuralNomadsAI/CodeNomad.git
synced 2026-11-17 05:49:28 +00:00
## Mechanism first, no npm release change Introduce `.npm-version` at the npm release already supplied by the pinned Node archive. Both desktop hosts package that exact registry release. This PR does not adopt npm 12: after it merges, maintenance will be dispatched to test creation of an independent candidate PR. ## Implementation - Download the exact npm registry tarball and verify its canonical metadata, package/version, sha512 integrity, bundled dependencies and engines against each host's Node. Reuse the platform tar extractor; no handwritten archive parser. The registry supplies both metadata and archive, so this checks registry content consistency, not an independent publisher signature. - Preserve Tauri's Node binary and existing per-platform npm layout, and Electron's npm-only resources. Smoke and verification check the npm pin. - Scope lifecycle script permission to the exact `@opencode/cli` release for direct installs and native upgrade. No blanket script permission or user npmrc edits. - Daily maintenance audits the pinned npm and candidate and proposes `.npm-version` changes only when declared engines accept both runtimes. A major is explicitly unqualified and needs maintainer review; engines acceptance is not behavioral validation. No automatic merge or release. - Partial proposals remain visible errors naming the existing branch, with reconciliation instructions; no forced overwrite/deletion. A candidate-audit failure blocks only the npm proposal; pinned audit and metadata failures remain failures. - Documentation references pins rather than copied versions. GitHub token-created proposal validation requires approving the workflow runs, or closing/reopening with a maintainer account; no new credentials added. ## Validation - Unit/packaging/maintenance/Tauri script suites: 47 pass. Server update tests: 70 pass, 1 platform skip. Typecheck passes. - Windows Electron45 package and Tauri release binary build/smoke pass. - Real install, upgrade and legacy-replacement fixtures pass on both Windows layouts with current npm11 and a temporary, uncommitted npm12 candidate. - Ubuntu: both layouts prepare/verify; first-install fixture passes with both npm releases. - The candidate removes the two high audit entries, but their applicability to CodeNomad's prebuilt OpenCode installation is low; 32 moderate entries remain. The proposal requires release-note review and native-fixture qualification rather than equating a lower audit count with safety. macOS candidate behavior still needs CI qualification. No pin to npm12, application release, registry publishing or daemon restart is part of this PR.
97 lines
4.8 KiB
JavaScript
97 lines
4.8 KiB
JavaScript
// Both desktop hosts bundle this exact npm, pinned in .npm-version separately
|
|
// from the Node archive. The registry tarball is accepted only when its sha512
|
|
// matches the integrity the same registry publishes for that version: this
|
|
// proves the bytes are the registry's published package (and HTTPS protects the
|
|
// transfer), not an independent publisher signature.
|
|
const crypto = require("crypto")
|
|
const fs = require("fs")
|
|
const path = require("path")
|
|
|
|
const NPM_REGISTRY = "https://registry.npmjs.org"
|
|
|
|
function readExactVersion(file) {
|
|
const value = fs.readFileSync(path.join(__dirname, "..", file), "utf8").trim()
|
|
if (!/^\d+\.\d+\.\d+$/.test(value)) throw new Error(`${file} must pin an exact release`)
|
|
return value
|
|
}
|
|
|
|
const NPM_VERSION = readExactVersion(".npm-version")
|
|
|
|
function npmTarballUrl(version) {
|
|
return `${NPM_REGISTRY}/npm/-/npm-${version}.tgz`
|
|
}
|
|
|
|
/** Fails unless one sha512 entry of the SRI string matches; weaker algorithms are ignored. */
|
|
function verifyIntegrity(buffer, integrity) {
|
|
const expected = String(integrity ?? "").split(/\s+/).filter(entry => entry.startsWith("sha512-")).map(entry => entry.slice(7))
|
|
if (!expected.length) throw new Error("npm registry metadata has no sha512 integrity")
|
|
const actual = crypto.createHash("sha512").update(buffer).digest("base64")
|
|
if (!expected.includes(actual)) throw new Error("Integrity mismatch for the npm package")
|
|
}
|
|
|
|
/** The system tar refuses `..` member paths and strips a leading `/`; this also
|
|
* refuses any other root than npm's `package/`, and any link or device it wrote.
|
|
* The verified archive authorizes only its own bytes: a `package` link (POSIX
|
|
* symlink, Windows junction) would make the walk and the copy read host content
|
|
* instead, and a hard link shares its data with a file outside the archive's
|
|
* control. Every entry, the root first, is checked with lstat, never followed. */
|
|
function extractedPackage(extractRoot) {
|
|
const roots = fs.readdirSync(extractRoot)
|
|
if (roots.length !== 1 || roots[0] !== "package") throw new Error(`Unexpected npm package contents: ${roots.join(", ")}`)
|
|
const npmRoot = path.join(extractRoot, "package")
|
|
const pending = [npmRoot]
|
|
while (pending.length) {
|
|
const directory = pending.pop()
|
|
if (!fs.lstatSync(directory).isDirectory()) throw new Error(`Unsupported npm package entry: ${path.relative(extractRoot, directory)}`)
|
|
for (const name of fs.readdirSync(directory)) {
|
|
const file = path.join(directory, name)
|
|
const stat = fs.lstatSync(file)
|
|
if (stat.isDirectory()) pending.push(file)
|
|
else if (!stat.isFile() || stat.nlink !== 1) throw new Error(`Unsupported npm package entry: ${path.relative(extractRoot, file)}`)
|
|
}
|
|
}
|
|
return npmRoot
|
|
}
|
|
|
|
function readPackage(npmRoot) {
|
|
return JSON.parse(fs.readFileSync(path.join(npmRoot, "package.json"), "utf8"))
|
|
}
|
|
|
|
/** npm itself only warns about an unsupported engine; packaging must refuse it. */
|
|
function checkNpmEngine(npmRoot, nodeVersion, runtimeLabel) {
|
|
const engine = readPackage(npmRoot).engines?.node
|
|
const semver = require(path.join(npmRoot, "node_modules", "semver"))
|
|
if (!engine || !semver.satisfies(nodeVersion, engine)) {
|
|
throw new Error(`npm ${readPackage(npmRoot).version} requires Node ${engine ?? "(undeclared)"}, but ${runtimeLabel} runs Node ${nodeVersion}`)
|
|
}
|
|
return engine
|
|
}
|
|
|
|
/** Downloads, verifies and extracts the pinned npm under `workRoot` (an empty
|
|
* directory) with `extract(archive, destination)`; returns the package root. */
|
|
async function downloadPinnedNpm({ request, extract, workRoot, version = NPM_VERSION }) {
|
|
const manifest = JSON.parse((await request(`${NPM_REGISTRY}/npm/${version}`)).toString("utf8"))
|
|
if (manifest.name !== "npm" || manifest.version !== version || manifest.dist?.tarball !== npmTarballUrl(version)) {
|
|
throw new Error(`Unexpected registry metadata for npm ${version}`)
|
|
}
|
|
const tarball = await request(manifest.dist.tarball)
|
|
verifyIntegrity(tarball, manifest.dist.integrity)
|
|
const archive = path.join(workRoot, `npm-${version}.tgz`)
|
|
const extractRoot = path.join(workRoot, "npm-package")
|
|
fs.writeFileSync(archive, tarball)
|
|
fs.mkdirSync(extractRoot)
|
|
extract(archive, extractRoot)
|
|
const npmRoot = extractedPackage(extractRoot)
|
|
const extracted = readPackage(npmRoot)
|
|
if (extracted.name !== "npm" || extracted.version !== version || !fs.existsSync(path.join(npmRoot, "bin", "npm-cli.js"))) {
|
|
throw new Error(`The npm ${version} package is incomplete`)
|
|
}
|
|
for (const dependency of extracted.bundleDependencies ?? []) {
|
|
if (!fs.existsSync(path.join(npmRoot, "node_modules", ...dependency.split("/"), "package.json"))) {
|
|
throw new Error(`The npm ${version} package is missing its bundled dependency ${dependency}`)
|
|
}
|
|
}
|
|
return npmRoot
|
|
}
|
|
|
|
module.exports = { NPM_VERSION, checkNpmEngine, downloadPinnedNpm, extractedPackage, npmTarballUrl, readExactVersion, verifyIntegrity }
|