Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(plugins): skip npm lifecycle scripts for untrusted plugin installs (#3557)

ruv7d55ac6e77c5
diff --git a/v3/@claude-flow/cli/__tests__/plugins-install-scripts-3557.test.ts b/v3/@claude-flow/cli/__tests__/plugins-install-scripts-3557.test.ts
new file mode 100644
index 0000000000..e3a0d6c12c
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/plugins-install-scripts-3557.test.ts
@@ -0,0 +1,89 @@
+/**
+ * #3557 follow-up — an untrusted plugin's npm lifecycle scripts must not run
+ * on a default install. This drives REAL npm against a local tarball (no
+ * network): the mock only rewrites the package spec to the tarball path and
+ * delegates to the real execFile, so npm itself decides whether postinstall runs.
+ */
+import { describe, it, expect, vi, beforeAll, afterAll, beforeEach } from 'vitest';
+import * as fs from 'fs';
+import * as os from 'os';
+import * as path from 'path';
+import { execFileSync } from 'child_process';
+
+const PKG = 'ruflo-script-probe-3557';
+const state: { tarball: string } = { tarball: '' };
+
+vi.mock('child_process', async (importOriginal) => {
+ const real = await importOriginal<typeof import('child_process')>();
+ return {
+ ...real,
+ execFile: (file: string, args: string[], opts: unknown, cb: (...a: unknown[]) => void) => {
+ const mapped = args.map((a) => (a === PKG ? state.tarball : a));
+ return real.execFile(file, mapped, opts as object, cb as never);
+ },
+ };
+});
+
+import { PluginManager } from '../src/plugins/manager.js';
+
+describe('#3557 install scripts are skipped for untrusted plugins', () => {
+ let work: string;
+ let sentinel: string;
+
+ beforeAll(() => {
+ work = fs.mkdtempSync(path.join(os.tmpdir(), 'plugins-scripts-3557-'));
+ sentinel = path.join(work, 'POSTINSTALL_RAN');
+ const src = path.join(work, 'src');
+ fs.mkdirSync(src);
+ fs.writeFileSync(
+ path.join(src, 'package.json'),
+ JSON.stringify({
+ name: PKG,
+ version: '1.0.0',
+ scripts: {
+ postinstall: `node -e "require('fs').writeFileSync('${sentinel}', 'ran')"`,
+ },
+ 'claude-flow': { trustLevel: 'untrusted', permissions: ['shell:exec'], hooks: ['pre-task'] },
+ }),
+ );
+ const out = execFileSync('npm', ['pack', '--pack-destination', work, '--ignore-scripts', '--json'], {
+ cwd: src,
+ encoding: 'utf-8',
+ });
+ state.tarball = path.join(work, JSON.parse(out)[0].filename);
+ }, 60000);
+
+ afterAll(() => fs.rmSync(work, { recursive: true, force: true }));
+
+ beforeEach(() => {
+ fs.rmSync(sentinel, { force: true });
+ });
+
+ it('a default install does NOT run postinstall and records scriptsRun:false', async () => {
+ const base = fs.mkdtempSync(path.join(work, 'default-'));
+ const manager = new PluginManager(base);
+ await manager.initialize();
+
+ const result = await manager.installFromNpm(PKG, undefined, { verify: true });
+
+ expect(result.success).toBe(true);
+ expect(fs.existsSync(path.join(base, '.claude-flow', 'plugins', 'node_modules', PKG, 'package.json'))).toBe(true);
+ expect(fs.existsSync(sentinel)).toBe(false);
+ expect(result.plugin!.scriptsRun).toBe(false);
+ expect(result.plugin!.hooks).toEqual([]);
+ expect(result.warnings!.join(' ')).toMatch(/Install scripts were skipped.*--trust/);
+ }, 120000);
+
+ it('with --trust, postinstall runs and scriptsRun:true is recorded', async () => {
+ const base = fs.mkdtempSync(path.join(work, 'trusted-'));
+ const manager = new PluginManager(base);
+ await manager.initialize();
+
+ const result = await manager.installFromNpm(PKG, undefined, { verify: true, trust: true });
+
+ expect(result.success).toBe(true);
+ expect(fs.existsSync(sentinel)).toBe(true);
+ expect(result.plugin!.scriptsRun).toBe(true);
+ expect(result.plugin!.hooks).toEqual(['pre-task']);
+ }, 120000);
+});
diff --git a/v3/@claude-flow/cli/src/commands/plugins.ts b/v3/@claude-flow/cli/src/commands/plugins.ts
index e989e46c07..c2b017a6fa 100644
--- a/v3/@claude-flow/cli/src/commands/plugins.ts
+++ b/v3/@claude-flow/cli/src/commands/plugins.ts
@@ -329,6 +329,7 @@ const installCommand: Command = {
`Trust: ${installed.trustLevel ?? 'not declared'}`,
`Permissions: ${installed.permissions?.join(', ') || 'none declared'}`,
`Verification: ${installed.verification ?? 'n/a'}`,
+ `Install scripts: ${installed.source === 'local' ? 'n/a (local link)' : installed.scriptsRun ? 'ran' : 'skipped (use --trust to run)'}`,
``,
`Hooks registered: ${installed.hooks?.length || 0}`,
`Commands added: ${installed.commands?.length || 0}`,
diff --git a/v3/@claude-flow/cli/src/plugins/manager.ts b/v3/@claude-flow/cli/src/plugins/manager.ts
index 4580a6994c..8d65e513b7 100644
--- a/v3/@claude-flow/cli/src/plugins/manager.ts
+++ b/v3/@claude-flow/cli/src/plugins/manager.ts
@@ -14,6 +14,7 @@ import {
parseSha256Checksum,
readDeclaredTrust,
sha256Hex,
+ shouldRunInstallScripts,
type TrustDecision,
} from './trust-policy.js';
@@ -95,6 +96,8 @@ export interface InstalledPlugin {
permissions?: string[];
/** How the install was verified. */
verification?: 'checksum' | 'npm-integrity' | 'policy' | 'skipped';
+ /** Whether npm lifecycle scripts ran during install (false = `--ignore-scripts`). */
+ scriptsRun?: boolean;
/** Hooks/commands the plugin declared but that were not registered, and why. */
withheld?: { hooks: string[]; commands: string[]; reasons: string[] };
}
@@ -253,7 +256,7 @@ export class PluginManager {
const expected = verify ? parseSha256Checksum(opts.expectedChecksum) : null;
if (expected) {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ruflo-plugin-verify-'));
- const packed = await runNpm(['pack', versionSpec, '--pack-destination', tmpDir, '--json'], 120000);
+ const packed = await runNpm(['pack', versionSpec, '--pack-destination', tmpDir, '--json', '--ignore-scripts'], 120000);
const info = JSON.parse(packed.stdout) as Array<{ filename?: string }>;
const filename = info[0]?.filename;
if (!filename) throw new Error(`npm pack returned no tarball for ${versionSpec}`);
@@ -279,7 +282,18 @@ export class PluginManager {
// Use npm to install (array form prevents shell injection)
console.log(`[PluginManager] Installing ${versionSpec}...`);
- await runNpm(['install', '--prefix', this.config.pluginsDir, installTarget], 120000);
+ // #3557 follow-up: lifecycle scripts run before the package's own trust
+ // declaration can be read, so untrusted installs skip them entirely.
+ const scriptsRun = shouldRunInstallScripts(opts);
+ const installArgs = ['install', '--prefix', this.config.pluginsDir, installTarget];
+ if (!scriptsRun) {
+ installArgs.push('--ignore-scripts');
+ warnings.push(
+ `Install scripts were skipped for ${packageName} (--ignore-scripts): it is not registry-vouched and --trust was not given. ` +
+ `Reinstall with --trust to run them.`,
+ );
+ }
+ await runNpm(installArgs, 120000);
// Get installed version
const packageJsonPath = path.join(installDir, packageName, 'package.json');
@@ -306,6 +320,7 @@ export class PluginManager {
trustLevel: trusted.trustLevel,
permissions: trusted.permissions,
verification,
+ scriptsRun,
...(trusted.withheld ? { withheld: trusted.withheld } : {}),
};
@@ -378,6 +393,8 @@ export class PluginManager {
trustLevel: trusted.trustLevel,
permissions: trusted.permissions,
verification: opts.verify === false ? 'skipped' : 'policy',
+ // A local install links the path; it never runs npm or package scripts.
+ scriptsRun: false,
...(trusted.withheld ? { withheld: trusted.withheld } : {}),
};
@@ -574,8 +591,12 @@ export class PluginManager {
// Validate package name to prevent injection (S-3)
validatePackageName(versionSpec);
- // Reinstall with new version (array form prevents shell injection)
- await runNpm(['install', '--prefix', this.config.pluginsDir, versionSpec], 120000);
+ // Reinstall with new version (array form prevents shell injection).
+ // An install recorded with scriptsRun:false stays script-free on upgrade;
+ // legacy entries (no field) keep their pre-#3557 behaviour.
+ const upgradeArgs = ['install', '--prefix', this.config.pluginsDir, versionSpec];
+ if (existing.scriptsRun === false) upgradeArgs.push('--ignore-scripts');
+ await runNpm(upgradeArgs, 120000);
// Update manifest
const installDir = path.join(this.config.pluginsDir, 'node_modules');
diff --git a/v3/@claude-flow/cli/src/plugins/trust-policy.ts b/v3/@claude-flow/cli/src/plugins/trust-policy.ts
index 66b2c1110f..dbbc382c14 100644
--- a/v3/@claude-flow/cli/src/plugins/trust-policy.ts
+++ b/v3/@claude-flow/cli/src/plugins/trust-policy.ts
@@ -97,6 +97,18 @@ export function evaluatePluginTrust(declared: DeclaredTrust, opts: TrustOptions)
return { allowed: true, verificationSkipped: false, reasons: [], excessPermissions };
}
+/**
+ * Whether npm may run the package's lifecycle scripts (preinstall/install/
+ * postinstall, and those of its dependencies). Scripts execute before the
+ * package's own trust declaration can be read, so only an explicit `--trust`
+ * or a registry-vouched entry allows them; everything else installs with
+ * `--ignore-scripts`.
+ */
+export function shouldRunInstallScripts(opts: { trust?: boolean; registryTrustLevel?: string }): boolean {
+ if (opts.trust === true) return true;
+ return opts.registryTrustLevel !== undefined && REGISTRY_VOUCHED_TRUST_LEVELS.includes(opts.registryTrustLevel);
+}
+
/** A registry checksum we can actually verify: `sha256:` + 64 hex chars. */
export function parseSha256Checksum(checksum: unknown): string | null {
if (typeof checksum !== 'string') return null;