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 command injection in github-safe.js (audit_1776853149979)

Reuven3baebe1777af
diff --git a/v3/@claude-flow/cli/.claude/helpers/github-safe.js b/v3/@claude-flow/cli/.claude/helpers/github-safe.js
index f1e8a93a5b..6392c512e5 100755
--- a/v3/@claude-flow/cli/.claude/helpers/github-safe.js
+++ b/v3/@claude-flow/cli/.claude/helpers/github-safe.js
@@ -2,14 +2,23 @@
/**
* Safe GitHub CLI Helper
- * Prevents timeout issues when using gh commands with special characters
- *
+ *
+ * Prevents two classes of issue when calling `gh`:
+ * 1. Timeout / shell-quoting bugs when issue/PR bodies contain backticks,
+ * `$(...)`, or other special characters — bodies go through a temp file
+ * via `--body-file` instead of being inlined in the command line.
+ * 2. Command injection (audit_1776853149979 finding) — args were
+ * previously concatenated into a shell string and passed to
+ * `execSync`, so a caller passing `; rm -rf …` would have it
+ * executed by /bin/sh. Now uses `execFileSync('gh', argArray)`
+ * which goes through execve directly with no shell interpretation.
+ *
* Usage:
* ./github-safe.js issue comment 123 "Message with `backticks`"
* ./github-safe.js pr create --title "Title" --body "Complex body"
*/
-import { execSync } from 'child_process';
+import { execFileSync } from 'child_process';
import { writeFileSync, unlinkSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
@@ -27,80 +36,86 @@ Usage:
./github-safe.js issue create --title <title> --body <body>
./github-safe.js pr create --title <title> --body <body>
-This helper prevents timeout issues with special characters like:
-- Backticks in code examples
-- Command substitution \$(...)
-- Directory paths
-- Special shell characters
+Bodies with backticks, command substitution, and other special shell
+characters are routed through a tempfile via --body-file. All gh args
+are passed via execFileSync (no shell interpretation).
`);
process.exit(1);
}
+// Whitelist of gh top-level commands we forward. Restricting this is
+// defense-in-depth even though execFileSync would already block shell
+// metacharacters in args.
+const ALLOWED_COMMANDS = new Set(['issue', 'pr', 'repo', 'api', 'workflow', 'run', 'release', 'auth', 'gist']);
+
const [command, subcommand, ...restArgs] = args;
-// Handle commands that need body content
-if ((command === 'issue' || command === 'pr') &&
+if (!ALLOWED_COMMANDS.has(command)) {
+ console.error(`Refusing to forward unknown gh command: ${command}`);
+ console.error(`Allowed: ${Array.from(ALLOWED_COMMANDS).join(', ')}`);
+ process.exit(1);
+}
+
+// Build the final argv that gets passed to execFileSync.
+// IMPORTANT: never join into a shell string. Each element is one argv slot.
+function runGh(argv) {
+ try {
+ execFileSync('gh', argv, {
+ stdio: 'inherit',
+ timeout: 30000,
+ // shell:false is the default — kept explicit so a future refactor
+ // doesn't accidentally turn it on.
+ shell: false,
+ });
+ } catch (error) {
+ console.error('Error:', error?.message ?? String(error));
+ process.exit(1);
+ }
+}
+
+// Handle commands that take body content
+if ((command === 'issue' || command === 'pr') &&
(subcommand === 'comment' || subcommand === 'create')) {
-
+
let bodyIndex = -1;
let body = '';
-
+
if (subcommand === 'comment' && restArgs.length >= 2) {
- // Simple format: github-safe.js issue comment 123 "body"
+ // Positional: github-safe.js issue comment 123 "body"
body = restArgs[1];
bodyIndex = 1;
} else {
- // Flag format: --body "content"
bodyIndex = restArgs.indexOf('--body');
if (bodyIndex !== -1 && bodyIndex < restArgs.length - 1) {
body = restArgs[bodyIndex + 1];
}
}
-
+
if (body) {
- // Use temporary file for body content
const tmpFile = join(tmpdir(), `gh-body-${randomBytes(8).toString('hex')}.tmp`);
-
try {
writeFileSync(tmpFile, body, 'utf8');
-
- // Build new command with --body-file
- const newArgs = [...restArgs];
+
+ // Build the gh argv with --body-file instead of --body / inline body
+ const finalArgs = [command, subcommand, ...restArgs];
+ const offset = 2; // command + subcommand
if (subcommand === 'comment' && bodyIndex === 1) {
- // Replace body with --body-file
- newArgs[1] = '--body-file';
- newArgs.push(tmpFile);
+ finalArgs[offset + 1] = '--body-file';
+ finalArgs.push(tmpFile);
} else if (bodyIndex !== -1) {
- // Replace --body with --body-file
- newArgs[bodyIndex] = '--body-file';
- newArgs[bodyIndex + 1] = tmpFile;
+ finalArgs[offset + bodyIndex] = '--body-file';
+ finalArgs[offset + bodyIndex + 1] = tmpFile;
}
-
- // Execute safely
- const ghCommand = `gh ${command} ${subcommand} ${newArgs.join(' ')}`;
- console.log(`Executing: ${ghCommand}`);
-
- const result = execSync(ghCommand, {
- stdio: 'inherit',
- timeout: 30000 // 30 second timeout
- });
-
- } catch (error) {
- console.error('Error:', error.message);
- process.exit(1);
+
+ runGh(finalArgs);
} finally {
- // Clean up
- try {
- unlinkSync(tmpFile);
- } catch (e) {
- // Ignore cleanup errors
- }
+ try { unlinkSync(tmpFile); } catch { /* ignore cleanup errors */ }
}
} else {
- // No body content, execute normally
- execSync(`gh ${args.join(' ')}`, { stdio: 'inherit' });
+ // No body — forward all args as-is (still via execFileSync)
+ runGh(args);
}
} else {
- // Other commands, execute normally
- execSync(`gh ${args.join(' ')}`, { stdio: 'inherit' });
+ // Other gh subcommands — forward as-is
+ runGh(args);
}