Files
orca/config/scripts/resolve-7za-path.mjs
T
39a200d900 fix(release): restore the Windows inner-binary signature gate (#6487) (#10719)
* fix(release): restore the Windows inner-binary signature gate

electron-builder 26.9+ dropped the bundled 7zip-bin package, so the gate's
hardcoded node_modules/7zip-bin path stopped resolving in 1d2cd33c83. The
gate is fail-open, so it swallowed the error and 11 releases shipped with
no signature verification and an evidence artifact that looked clean.

Resolve 7za through app-builder-lib's toolset instead, and always record a
verdict so a degraded gate can't pass for a healthy one.

Refs #6487

* test(release): make the signing-gate structural tests assert executed code, not text

The round-2 harness matched /\bthrow\b/ and /\bcatch\b/ against raw block text, so
the word satisfied the assertion wherever it appeared. Downgrading the resolver
throw to `Write-Host "...would normally throw..."` — the exact silent fail-open
this PR exists to kill — left all 11 tests green.

Every span is now classified once (code / string / comment) by the same walk that
pairs braces, and assertions run against the string-and-comment-blanked view.
Blanking preserves length, so indices still line up across views.

Also re-anchors the catch-ordering test: `blockAfter(step, '} catch {')` picked
the first catch in the step, which stopped being the gate's own once the
persistence helpers grew theirs — moving the policy throw inside the try was
passing again.

Co-authored-by: Orca <help@stably.ai>

* test(release): pin the evidence filename the gate writes to the one the upload collects

The upload step is `if-no-files-found: ignore`, so renaming the evidence file on
one side and not the other ships a green run whose artifact silently omits the
verdict — the same silent-degradation class this PR exists to close.

Co-authored-by: Orca <help@stably.ai>

* fix(release): preserve 7za resolver failures

---------

Co-authored-by: Orca <help@stably.ai>
Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
2026-07-27 15:48:39 -07:00

103 lines
3.5 KiB
JavaScript

#!/usr/bin/env node
// Why: electron-builder 26.9+ dropped the bundled `7zip-bin` package in favour of a
// toolset downloaded at build time, so the hardcoded `node_modules/7zip-bin/...` path
// the release signing gates used silently stopped resolving (#6487).
import { createRequire } from 'node:module'
import { existsSync, statSync } from 'node:fs'
import { resolve } from 'node:path'
const require = createRequire(import.meta.url)
// Why not existsSync: PowerShell's `Test-Path` is true for directories too, so a
// override pointing at a folder would satisfy both checks and only fail later as
// an opaque exec error inside the gate.
function isFile(path) {
try {
return statSync(path).isFile()
} catch {
return false
}
}
// Legacy layout, still valid if a transitive dep reintroduces 7zip-bin. The
// package ships `mac/`, not `darwin/`, and keeps a separate ia32 build.
export function legacy7zaRelativePath(platform = process.platform, arch = process.arch) {
if (platform === 'win32') {
return ['node_modules', '7zip-bin', 'win', arch, '7za.exe']
}
const dir = platform === 'darwin' ? 'mac' : platform
return ['node_modules', '7zip-bin', dir, arch, '7za']
}
// app-builder-lib logs download progress to stdout, which would corrupt the
// single-path contract the PowerShell gates parse. Divert it to stderr so a
// cold toolset cache stays debuggable without breaking the caller.
//
// Why refcounted: patching `process.stdout.write` is process-global, so two
// concurrent callers would each capture the other's patched function as their
// "original" and the last `finally` would restore a diverting stub permanently.
let stdoutDivertDepth = 0
let originalStdoutWrite = null
async function withStdoutDivertedToStderr(run) {
if (stdoutDivertDepth === 0) {
originalStdoutWrite = process.stdout.write
process.stdout.write = (chunk, encoding, callback) =>
process.stderr.write(chunk, encoding, callback)
}
stdoutDivertDepth += 1
try {
return await run()
} finally {
stdoutDivertDepth -= 1
if (stdoutDivertDepth === 0) {
process.stdout.write = originalStdoutWrite
originalStdoutWrite = null
}
}
}
export async function resolve7zaPath(projectDir = process.cwd()) {
const override = process.env.ELECTRON_BUILDER_7ZIP_PATH
if (override && isFile(override)) {
return override
}
const legacy = resolve(projectDir, ...legacy7zaRelativePath())
if (isFile(legacy)) {
return legacy
}
// app-builder-lib reads the same env var and hard-fails on a stale value, so a
// dangling override must be cleared rather than passed through to the download.
const restoreOverride = override !== undefined
if (restoreOverride) {
delete process.env.ELECTRON_BUILDER_7ZIP_PATH
}
try {
// The toolset is cached after the first download, so a release build has
// already paid this cost by the time the signing gate runs.
const { getPath7za } = require('app-builder-lib/out/toolsets/7zip.js')
const toolsetPath = await withStdoutDivertedToStderr(() => getPath7za())
if (!existsSync(toolsetPath)) {
throw new Error(`app-builder-lib returned a 7za path that does not exist: ${toolsetPath}`)
}
return toolsetPath
} finally {
if (restoreOverride) {
process.env.ELECTRON_BUILDER_7ZIP_PATH = override
}
}
}
if (import.meta.filename === process.argv[1]) {
try {
process.stdout.write(`${await resolve7zaPath()}\n`)
} catch (error) {
process.stderr.write(`Could not resolve a 7za executable: ${error.message}\n`)
process.exit(1)
}
}