Files
hermes-agent/tests-js/allow-scripts-sync.test.ts
Maciej Lisiewski 7062229090 chore(js): switch the workspaces from npm to pnpm
pnpm 12.15.0 (native executable) installs every JS workspace from one
pnpm-lock.yaml, imported from package-lock.json with the same resolved
versions. website/, the WhatsApp bridge and the photon sidecar stay separate
npm projects.

Configuration moves from .npmrc and the root manifest into
pnpm-workspace.yaml: the 14-day minimumReleaseAge and its excludes,
overrides, allowBuilds (the old allowScripts set, with strictDepBuilds
failing any unlisted build script), engineStrict, sideEffectsCache: false
(in-place rebuilds must not write into the store) and
verifyDepsBeforeRun: false (a `pnpm run` never installs).

The pnpm version is pinned in pm/lock.json and in root devEngines.
pmOnFail: ignore keeps that pin out of pnpm-lock.yaml: a leading pin
document makes osv-scanner read zero packages. scripts/build/node-deps.mjs
enforces the exact version instead, and every lock reader refuses a
multi-document lock. A root preinstall refuses `npm install`; devEngines
onFail is warn so npx (MCP servers, the tsc linter) keeps working inside
the checkout.

User-machine installs run --frozen-lockfile --trust-lockfile (release age is
checked when locking, in CI, not per user). node-deps removes npm-era
node_modules trees before the first pnpm install. Nix builds the store with
fetchPnpmDeps and a hash file that CI checks.
2026-11-06 04:08:24 +01:00

214 lines
7.4 KiB
TypeScript

/**
* Invariants tying the dependency build allowlists to the lockfiles they gate:
* pnpm's ``allowBuilds`` (root ``pnpm-workspace.yaml``) and npm's
* ``allowScripts`` (``website/``, still an npm project).
*
* Both allowlists are keyed by exact ``name@version``, so an entry silently
* stops matching the moment that dependency is bumped. npm downgrades the
* blocked script to a warning buried in install output, and the failure only
* surfaces much later as a missing native artifact.
*
* That has now bitten twice on Windows. ``get-windows`` was added to
* ``apps/desktop`` without an allow entry, so its node-pre-gyp install script
* never downloaded the win32 binding and ``hermes desktop`` died in
* ``stage-native-deps``. In the same window, a CVE sweep moved Electron to
* 40.10.6 and left the ``electron@40.10.2`` pin behind, blocking Electron's
* own postinstall on any clean install.
*
* Two contracts keep an allowlist honest:
*
* - Every versioned pin names a version the lockfile actually resolves, so a
* dependency bump that orphans its pin fails here instead of in a user's
* build.
* - Every package with an install script is covered by a decision — allowed
* at its exact version, or denied by name. npm's lockfile marks those
* packages, so it is checked here; pnpm's does not, and pnpm enforces it
* itself when ``strictDepBuilds`` fails the install on an unlisted build.
* That check only sees packages the installing host builds: a macOS- or
* Windows-only dependency (``fsevents``) is caught by those OS's install
* lanes, never by a Linux install.
*
* A bare-name key (no ``@version``) is a deliberate standing decision that
* survives version bumps, which is how ``unicode-animations: false`` stays a
* permanent denial.
*/
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import { load } from 'js-yaml'
import { describe, test } from 'vitest'
const REPO_ROOT = path.resolve(__dirname, '..')
const MANIFESTS = [
{ name: 'website', dir: 'website' }
]
function manifestLabel(dir: string): string {
return path.join(dir, 'package.json')
}
interface LockPackage {
name?: string
version?: string
hasInstallScript?: boolean
}
function readJson(filePath: string): Record<string, unknown> {
return JSON.parse(fs.readFileSync(filePath, 'utf-8'))
}
function packageNameFor(lockPath: string, entry: LockPackage): string {
return entry.name ?? lockPath.split('node_modules/').pop() ?? lockPath
}
/** Every version of every package the lockfile installs, keyed by name. */
function installedVersions(lock: Record<string, unknown>): Map<string, Set<string>> {
const versions = new Map<string, Set<string>>()
for (const [lockPath, entry] of Object.entries(
(lock.packages ?? {}) as Record<string, LockPackage>
)) {
if (!lockPath || !entry.version) {
continue
}
const name = packageNameFor(lockPath, entry)
const seen = versions.get(name) ?? new Set<string>()
seen.add(entry.version)
versions.set(name, seen)
}
return versions
}
function splitPin(key: string): { name: string; version: string } | null {
// Scoped packages carry a leading @, so match the LAST @ as the separator.
const match = key.match(/^(.+)@([^@]+)$/)
return match ? { name: match[1], version: match[2] } : null
}
describe('root allowBuilds', () => {
// SAFETY: pnpm writes both files; a missing section fails the assertions below.
const workspace = load(fs.readFileSync(path.join(REPO_ROOT, 'pnpm-workspace.yaml'), 'utf-8')) as {
allowBuilds?: Record<string, boolean>
strictDepBuilds?: boolean
}
test('an unlisted dependency build fails the install', () => {
assert.equal(workspace.strictDepBuilds, true, 'pnpm-workspace.yaml must set strictDepBuilds: true')
})
test('every versioned pin matches a version in the lockfile', () => {
const lock = load(fs.readFileSync(path.join(REPO_ROOT, 'pnpm-lock.yaml'), 'utf-8')) as { packages?: Record<string, unknown> }
const versions = new Map<string, Set<string>>()
for (const key of Object.keys(lock.packages ?? {})) {
const pin = splitPin(key)
assert.ok(pin, `unreadable pnpm-lock.yaml package key ${key}`)
versions.set(pin.name, (versions.get(pin.name) ?? new Set<string>()).add(pin.version))
}
const allow = workspace.allowBuilds ?? {}
const stale: string[] = []
assert.ok(Object.keys(allow).length > 0, 'pnpm-workspace.yaml has no allowBuilds decisions')
for (const key of Object.keys(allow)) {
const pin = splitPin(key)
if (!pin) {
continue
}
const installed = versions.get(pin.name)
if (!installed?.has(pin.version)) {
stale.push(` "${key}" — lockfile resolves ${pin.name} to ${installed ? [...installed].join(', ') : '<nothing>'}`)
}
}
assert.deepEqual(
stale,
[],
`Stale allowBuilds entries in pnpm-workspace.yaml:\n${stale.join('\n')}\n` +
"pnpm matches these by exact version, so each package's build fails the install " +
'under strictDepBuilds. Update the pin to the installed version, or drop the ' +
'entry if the dependency is gone.'
)
})
})
describe.each(MANIFESTS)('$name allowScripts', ({ dir }) => {
const manifestPath = path.join(REPO_ROOT, dir, 'package.json')
const lockPath = path.join(REPO_ROOT, dir, 'package-lock.json')
const label = manifestLabel(dir)
test('every versioned pin matches a version in the lockfile', () => {
assert.ok(fs.existsSync(lockPath), `${label} has no lockfile beside it to check allowScripts against`)
const allow = (readJson(manifestPath).allowScripts ?? {}) as Record<string, boolean>
const versions = installedVersions(readJson(lockPath))
const stale: string[] = []
for (const key of Object.keys(allow)) {
const pin = splitPin(key)
if (!pin) {
continue
}
const installed = versions.get(pin.name)
if (!installed?.has(pin.version)) {
stale.push(` "${key}" — lockfile resolves ${pin.name} to ${installed ? [...installed].join(', ') : '<nothing>'}`)
}
}
assert.deepEqual(
stale,
[],
`Stale allowScripts entries in ${label}:\n${stale.join('\n')}\n` +
"npm matches these by exact version, so each package's install script is " +
'silently blocked. Update the pin to the installed version, or drop the ' +
'entry if the dependency is gone.'
)
})
test('every package with an install script has an allowScripts decision', () => {
assert.ok(fs.existsSync(lockPath), `${label} has no lockfile beside it to check allowScripts against`)
const allow = (readJson(manifestPath).allowScripts ?? {}) as Record<string, boolean>
const lock = readJson(lockPath)
const uncovered: string[] = []
for (const [entryPath, entry] of Object.entries(
(lock.packages ?? {}) as Record<string, LockPackage>
)) {
if (!entryPath || !entry.hasInstallScript) {
continue
}
const name = packageNameFor(entryPath, entry)
if (!(`${name}@${entry.version}` in allow) && !(name in allow)) {
uncovered.push(` ${name}@${entry.version}`)
}
}
assert.deepEqual(
uncovered,
[],
`Packages with install scripts and no allowScripts decision in ${label}:\n` +
`${uncovered.join('\n')}\n` +
'npm blocks these. Add "<name>@<version>": true to allow, or "<name>": false ' +
'to deny permanently.'
)
})
})