Files
ruflo/v3/@claude-flow/cli/scripts/verify-helpers.mjs
T
5cdb56feb7 fix: sign companion helpers, guard every .claude-flow write (#4746, integrates #4991) (#5039)
* fix: sign companion helpers and refuse symlinked .claude-flow writes (#4746)

* fix: sign companion helpers and refuse symlinked .claude-flow writes (#4746)

* fix: sign companion helpers and refuse symlinked .claude-flow writes (#4746)

* fix: sign companion helpers and refuse symlinked .claude-flow writes (#4746)

* fix: sign companion helpers and refuse symlinked .claude-flow writes (#4746)

* fix: sign companion helpers and refuse symlinked .claude-flow writes (#4746)

* fix: sign companion helpers and refuse symlinked .claude-flow writes (#4746)

* fix(ruflo-mods): route every .claude-flow write through the link guard (#4746)

The PR guarded only the session heartbeat and the compaction snapshot. Edit
records, the learning-loop ledger, guidance observations, context-delegation
metrics, cost attribution and the console toast ring still wrote through
$.fs.write, which follows links. All of them now go through one helper,
projectWrite (state.ts), over writeWithoutLinks: a symlinked .claude-flow or
any symlinked folder below it refuses the write. Appends are read-then-
rewrite, so they are covered by the same path.

The cost hook's project check no longer counts a symlinked .claude-flow as
the project's state (the live engine reports the target's kind plus isLink).

The mods harness gains liveLinkStat (stat as the live engine answers it), and
mods-every-write-guarded-4746.test.ts drives every writing hook entry point:
a control run proves each path writes into a real .claude-flow, and runs with
a symlinked .claude-flow and with symlinked data/cost/metrics/sessions folders
must leave the outside directory byte-identical. Without this change those two
runs fail with six files written outside the project.

Co-Authored-By: RuFlo <ruv@ruv.net>

* fix(cli): init --upgrade installs the signed companion bytes (#4746)

session.cjs and memory.cjs joined the signed critical set in this PR, but
executeUpgrade still regenerated both from the CLI's generators, whose output
is not byte-identical to the packaged copies. The next run's integrity check
then reported both as tampered: once (healed) on a normal install, on every
run under .LOCKED, RUFLO_HELPERS_LOCKED or an ahead stamp, where the refresh
only inspects.

ensureCommonJsCompanions takes an optional sourceDir. The upgrade passes the
package's own .claude/helpers (findPackageHelpersDir, the source the startup
refresh verifies against): a companion present there as a regular file is
copied byte-for-byte through the existing link-safe atomic write; only a
missing one (or one shipped as a link) comes from the generators. The startup
ensure for missing companions is unchanged.

upgrade-companion-integrity-4746.test.ts upgrades a project and runs the
startup integrity check against a throwaway-key manifest over the package's
companion bytes; without this change it reports
['session.cjs', 'memory.cjs'] as tampered.

Co-Authored-By: RuFlo <ruv@ruv.net>

* chore(ruflo-mods): 0.17.3 — every .claude-flow write is link-guarded (#4746)

Co-Authored-By: RuFlo <ruv@ruv.net>

* test(ruflo-mods): cover the toast writer session.start binds (#4746)

With engine.create, the $.ruflo noun's toast writer is the one in use, so the
binder in session.start was guarded but never exercised. A run without
engine.create drives it; reverting session.ts alone now fails that case
(console/toasts/mods.jsonl written outside the project). Reverting any other
call-site file alone fails the existing cases.

Co-Authored-By: RuFlo <ruv@ruv.net>

* chore(helpers): re-sign manifest with session.cjs and memory.cjs (#4746)

Co-Authored-By: RuFlo <ruv@ruv.net>

---------

Co-authored-by: Aditya Chandrajit Deore <adityadeoreofficial@gmail.com>
Co-authored-by: r <r@x>
2026-11-19 01:21:48 -05:00

76 lines
3.6 KiB
JavaScript

#!/usr/bin/env node
/**
* Verify .claude/helpers/helpers.manifest.json against the shipped critical
* helpers (ADR-174, issue #2593). Runs in prepublishOnly AFTER sign-helpers.mjs
* so npm publish fails closed if the manifest ever drifts from disk again.
*
* Checks:
* 1. Ed25519 signature over the canonical manifest bytes is valid under
* RUFLO_HELPERS_PUBKEY (same primitive as helper-signing.ts).
* 2. manifest.version matches package.json version.
* 3. Every critical helper on disk hashes to the manifest entry.
*
* Optional arg: path to a helpers dir (e.g. an extracted `npm pack` tarball).
* Defaults to <cli-pkg>/.claude/helpers.
*/
import { readFileSync, existsSync } from 'node:fs';
import { createHash, verify as edVerify } from 'node:crypto';
import { join, dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const PKG_ROOT = join(__dirname, '..');
const HELPERS_DIR = resolve(process.argv[2] || join(PKG_ROOT, '.claude', 'helpers'));
// Keep in sync with sign-helpers.mjs:CRITICAL and src/init/helper-refresh.ts:CRITICAL_HELPERS.
const CRITICAL = ['auto-memory-hook.mjs', 'hook-handler.cjs', 'intelligence.cjs', 'statusline.cjs', 'router.cjs', 'learning-loop.cjs', 'session.cjs', 'memory.cjs'];
function die(msg) { console.error(`[verify-helpers] ${msg}`); process.exit(1); }
// #2675 — helper-signing.ts is the sole key source. prepare-publish.mjs and
// stable-npm-release.yml both build before verification, so importing emitted
// runtime code preserves the exact key users will trust after installation.
let RUFLO_HELPERS_PUBKEY;
try {
({ RUFLO_HELPERS_PUBKEY } = await import('../dist/src/init/helper-signing.js'));
} catch {
die('compiled helper-signing.js is unavailable; run `npm run build` before verification');
}
const manifestPath = join(HELPERS_DIR, 'helpers.manifest.json');
if (!existsSync(manifestPath)) die(`missing manifest: ${manifestPath}`);
let signed;
try { signed = JSON.parse(readFileSync(manifestPath, 'utf-8')); }
catch (e) { die(`manifest is not valid JSON: ${e.message}`); }
if (!signed || signed.algorithm !== 'ed25519' || !signed.signature || !signed.manifest?.files) {
die('manifest is malformed (missing algorithm/signature/files)');
}
// Canonical bytes — sorted file keys, MUST match sign-helpers.mjs + helper-signing.ts.
const sortedFiles = {};
for (const k of Object.keys(signed.manifest.files).sort()) sortedFiles[k] = signed.manifest.files[k];
const canonical = Buffer.from(JSON.stringify({ version: signed.manifest.version, files: sortedFiles }), 'utf-8');
if (!edVerify(null, canonical, RUFLO_HELPERS_PUBKEY, Buffer.from(signed.signature, 'base64'))) {
die('Ed25519 signature does not verify against RUFLO_HELPERS_PUBKEY');
}
const pkgVersion = JSON.parse(readFileSync(join(PKG_ROOT, 'package.json'), 'utf-8')).version;
if (signed.manifest.version !== pkgVersion) {
die(`manifest version ${signed.manifest.version} != package.json ${pkgVersion} — re-run sign-helpers.mjs`);
}
for (const name of CRITICAL) {
const p = join(HELPERS_DIR, name);
if (!existsSync(p)) die(`critical helper missing on disk: ${p}`);
const actual = createHash('sha256').update(readFileSync(p)).digest('hex');
const expected = signed.manifest.files[name];
if (!expected) die(`manifest has no entry for ${name}`);
if (actual !== expected) {
die(`hash drift for ${name}\n manifest: ${expected}\n on disk: ${actual}\n → re-run sign-helpers.mjs`);
}
}
console.log(`[verify-helpers] ok — ${CRITICAL.length} helpers match signed manifest @ ${signed.manifest.version}`);