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): linear-time encoded-powershell match (repeated-token case was quadratic) in both scanners; re-sign helpers

r66b42ec5321c
diff --git a/.claude/helpers/hook-handler.cjs b/.claude/helpers/hook-handler.cjs
index ee09c9a559..bc9da7e758 100755
--- a/.claude/helpers/hook-handler.cjs
+++ b/.claude/helpers/hook-handler.cjs
@@ -251,28 +251,40 @@ const SR_DOWNLOADER_WORDS = ['curl', 'wget'];
const SR_STANDALONE_RISK_PATTERNS = [
/\bInvoke-Expression\b/i,
/\biex\s*\(/i,
- // Linear-time, not `.*` — see settings-risk-scanner.ts's identical fix
- // (review on PR #4053 measured the old pattern at ~1.9s on 64,000 hostile
- // spaces; a 1MB hook command would stall this unconditional boot path for
- // minutes).
- /powershell(\.exe)?\s+(?:\S+\s+)*?-e(nc(odedcommand)?)?\s+\S/i,
/\/dev\/tcp\//i,
/\bnc\s+-e\b/i,
/base64\s+(-d|--decode)\b/i,
];
const SR_RISKY_PREAPPROVE_WORDS = [...SR_DANGEROUS_COMMAND_WORDS, ...SR_SHELL_EXEC_WORDS, ...SR_DOWNLOADER_WORDS];
+// Encoded PowerShell (`powershell[.exe] ... -e|-enc|-encodedcommand <payload>`),
+// matched on whitespace-separated tokens in ONE linear pass. The earlier regex
+// (`powershell\\s+(?:\\S+\\s+)*?-e...`) is quadratic on repeated "powershell "
+// tokens (every start rescans the tail: 256KB measured ~2.5s, 1MB ~40s) because
+// the lazy token loop re-runs from each start. Same semantics: a token ending in
+// powershell/powershell.exe, then LATER a token that is exactly -e/-enc/
+// -encodedcommand, then at least one more token.
+function srHasEncodedPowershell(command) {
+ let seenPs = false;
+ let pendingEnc = false;
+ for (const raw of String(command).split(/\s+/)) {
+ if (!raw) continue;
+ if (pendingEnc) return true;
+ const t = raw.toLowerCase();
+ if (seenPs && (t === '-e' || t === '-enc' || t === '-encodedcommand')) pendingEnc = true;
+ else if (t.endsWith('powershell') || t.endsWith('powershell.exe')) seenPs = true;
+ }
+ return false;
+}
+
function srWordBoundaryMatches(text, word) {
return new RegExp(`\\b${word}\\b`, 'i').test(text);
}
function srScanCommandStringForRisk(command) {
const reasons = [];
- for (const pattern of SR_STANDALONE_RISK_PATTERNS) {
- if (pattern.test(command)) {
- reasons.push('matches a known-dangerous execution pattern (encoded/remote command execution)');
- break;
- }
+ if (srHasEncodedPowershell(command) || SR_STANDALONE_RISK_PATTERNS.some((pattern) => pattern.test(command))) {
+ reasons.push('matches a known-dangerous execution pattern (encoded/remote command execution)');
}
const hasDownloader = SR_DOWNLOADER_WORDS.some((w) => srWordBoundaryMatches(command, w));
const hasShellExec = SR_SHELL_EXEC_WORDS.some((w) => srWordBoundaryMatches(command, w));
diff --git a/v3/@claude-flow/cli/.claude/helpers/helpers.manifest.json b/v3/@claude-flow/cli/.claude/helpers/helpers.manifest.json
index cd2869f06a..794bd702d7 100644
--- a/v3/@claude-flow/cli/.claude/helpers/helpers.manifest.json
+++ b/v3/@claude-flow/cli/.claude/helpers/helpers.manifest.json
@@ -1,14 +1,14 @@
{
"manifest": {
- "version": "3.56.4",
+ "version": "3.56.5",
"files": {
"auto-memory-hook.mjs": "381412515fb490def7d821ddfe1ebf8d9bee76e49a86e182806c0274f568bf15",
- "hook-handler.cjs": "38fc235079ddcf74b62452a91b9e56703026a1a7682dcce3c635be5ad1c8ea19",
+ "hook-handler.cjs": "0f855c667b8d2d04cb4fe91b14b351d14c3dfb30516dcd9d7ae156acbe0a689d",
"intelligence.cjs": "ac5033a77bcea9796856629d5d319bde9ab6b7edf0dcd0d4af187ce30af90836",
"statusline.cjs": "d72417ecb38634011a19f095a30e07b9929e6f97663520172ece9be7491b2eaa",
"router.cjs": "16648b3c0d73b6681ac2d8d13def6a68286c57f5a11004de5b19a0fa088c833d"
}
},
- "signature": "Dhlixv3hNK+hc+t5recMVcHtVZ6ilyT4uN4YLuFsoSVwbTI6dt6VfzyZZXDjgfm6ei8NR9IHIjjmVgKkUmRBDw==",
+ "signature": "8XUUrD8gMoKnYWmR6xkxOp5Z54ZeHtDWBb57RVfKLXpc3wj6LIqqvRPOcTgGJQzL98PCkbYffoBEtH5TeCTfBA==",
"algorithm": "ed25519"
}
diff --git a/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs b/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs
index ee09c9a559..bc9da7e758 100755
--- a/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs
+++ b/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs
@@ -251,28 +251,40 @@ const SR_DOWNLOADER_WORDS = ['curl', 'wget'];
const SR_STANDALONE_RISK_PATTERNS = [
/\bInvoke-Expression\b/i,
/\biex\s*\(/i,
- // Linear-time, not `.*` — see settings-risk-scanner.ts's identical fix
- // (review on PR #4053 measured the old pattern at ~1.9s on 64,000 hostile
- // spaces; a 1MB hook command would stall this unconditional boot path for
- // minutes).
- /powershell(\.exe)?\s+(?:\S+\s+)*?-e(nc(odedcommand)?)?\s+\S/i,
/\/dev\/tcp\//i,
/\bnc\s+-e\b/i,
/base64\s+(-d|--decode)\b/i,
];
const SR_RISKY_PREAPPROVE_WORDS = [...SR_DANGEROUS_COMMAND_WORDS, ...SR_SHELL_EXEC_WORDS, ...SR_DOWNLOADER_WORDS];
+// Encoded PowerShell (`powershell[.exe] ... -e|-enc|-encodedcommand <payload>`),
+// matched on whitespace-separated tokens in ONE linear pass. The earlier regex
+// (`powershell\\s+(?:\\S+\\s+)*?-e...`) is quadratic on repeated "powershell "
+// tokens (every start rescans the tail: 256KB measured ~2.5s, 1MB ~40s) because
+// the lazy token loop re-runs from each start. Same semantics: a token ending in
+// powershell/powershell.exe, then LATER a token that is exactly -e/-enc/
+// -encodedcommand, then at least one more token.
+function srHasEncodedPowershell(command) {
+ let seenPs = false;
+ let pendingEnc = false;
+ for (const raw of String(command).split(/\s+/)) {
+ if (!raw) continue;
+ if (pendingEnc) return true;
+ const t = raw.toLowerCase();
+ if (seenPs && (t === '-e' || t === '-enc' || t === '-encodedcommand')) pendingEnc = true;
+ else if (t.endsWith('powershell') || t.endsWith('powershell.exe')) seenPs = true;
+ }
+ return false;
+}
+
function srWordBoundaryMatches(text, word) {
return new RegExp(`\\b${word}\\b`, 'i').test(text);
}
function srScanCommandStringForRisk(command) {
const reasons = [];
- for (const pattern of SR_STANDALONE_RISK_PATTERNS) {
- if (pattern.test(command)) {
- reasons.push('matches a known-dangerous execution pattern (encoded/remote command execution)');
- break;
- }
+ if (srHasEncodedPowershell(command) || SR_STANDALONE_RISK_PATTERNS.some((pattern) => pattern.test(command))) {
+ reasons.push('matches a known-dangerous execution pattern (encoded/remote command execution)');
}
const hasDownloader = SR_DOWNLOADER_WORDS.some((w) => srWordBoundaryMatches(command, w));
const hasShellExec = SR_SHELL_EXEC_WORDS.some((w) => srWordBoundaryMatches(command, w));
diff --git a/v3/@claude-flow/cli/__tests__/session-start-hook-settings-risk-scan.test.ts b/v3/@claude-flow/cli/__tests__/session-start-hook-settings-risk-scan.test.ts
index 22d442577d..b41f49a6c8 100644
--- a/v3/@claude-flow/cli/__tests__/session-start-hook-settings-risk-scan.test.ts
+++ b/v3/@claude-flow/cli/__tests__/session-start-hook-settings-risk-scan.test.ts
@@ -184,6 +184,42 @@ describe('hook-handler.cjs session-restore — ReDoS guard on the powershell pat
expect(Date.now() - start).toBeLessThan(200);
});
+ it('stays linear on repeated "powershell " tokens (a lazy token loop turns quadratic there) in BOTH scanners', () => {
+ delete require_.cache[require_.resolve(HOOK_PATH)];
+ const hook = require_(HOOK_PATH);
+ const inputs = [
+ 'powershell '.repeat(90_000), // ~1MB, no -enc: every start rescans the tail in a backtracking pattern
+ 'powershell a '.repeat(80_000),
+ 'powershell.exe '.repeat(70_000),
+ ];
+ for (const hostile of inputs) {
+ const cjsStart = Date.now();
+ hook.srScanCommandStringForRisk(hostile);
+ expect(Date.now() - cjsStart).toBeLessThan(500);
+ const tsStart = Date.now();
+ tsScanCommandStringForRisk(hostile);
+ expect(Date.now() - tsStart).toBeLessThan(500);
+ }
+ });
+
+ it('keeps detecting encoded powershell across token shapes, identically in both scanners', () => {
+ delete require_.cache[require_.resolve(HOOK_PATH)];
+ const hook = require_(HOOK_PATH);
+ const hits = [
+ 'powershell -e SQBFAFgA',
+ 'POWERSHELL.EXE -NoProfile -Enc SQBFAFgA',
+ 'cmd /c powershell -ExecutionPolicy Bypass -EncodedCommand SQBFAFgA',
+ 'xpowershell -enc AAAA',
+ ];
+ const misses = ['powershell -enc', 'powershell -encoded SQB', 'powershell -Command Get-Date', 'echo powershell', 'pwsh -e'];
+ for (const c of hits) {
+ expect({ c, ts: tsScanCommandStringForRisk(c).length > 0, cjs: hook.srScanCommandStringForRisk(c).length > 0 }).toEqual({ c, ts: true, cjs: true });
+ }
+ for (const c of misses) {
+ expect({ c, ts: tsScanCommandStringForRisk(c).length > 0, cjs: hook.srScanCommandStringForRisk(c).length > 0 }).toEqual({ c, ts: false, cjs: false });
+ }
+ });
+
it('still detects a real EncodedCommand payload after the linear-time rewrite', () => {
delete require_.cache[require_.resolve(HOOK_PATH)];
const hook = require_(HOOK_PATH);
diff --git a/v3/@claude-flow/cli/src/init/settings-risk-scanner.ts b/v3/@claude-flow/cli/src/init/settings-risk-scanner.ts
index 08e15a1750..33970862b2 100644
--- a/v3/@claude-flow/cli/src/init/settings-risk-scanner.ts
+++ b/v3/@claude-flow/cli/src/init/settings-risk-scanner.ts
@@ -53,14 +53,6 @@ const DOWNLOADER_WORDS = ['curl', 'wget'];
const STANDALONE_RISK_PATTERNS: RegExp[] = [
/\bInvoke-Expression\b/i,
/\biex\s*\(/i,
- // Linear-time, not `.*` — the previous `\s+.*-e(...)` backtracked
- // quadratically on hostile input (64,000 spaces with no match measured at
- // ~1.9s; a 1MB hook command would stall whatever calls this for minutes).
- // Flagged by review on the 2026-10-11 dream-cycle PR (#4053) that ported
- // this pattern into hook-handler.cjs's automatic SessionStart path, where
- // that stall is unconditional on every session boot — fixed here too since
- // this is the shared root definition.
- /powershell(\.exe)?\s+(?:\S+\s+)*?-e(nc(odedcommand)?)?\s+\S/i,
/\/dev\/tcp\//i,
/\bnc\s+-e\b/i,
/base64\s+(-d|--decode)\b/i,
@@ -72,6 +64,26 @@ const STANDALONE_RISK_PATTERNS: RegExp[] = [
// review step for exactly the commands download-and-execute attacks use.
const RISKY_PREAPPROVE_WORDS = [...DANGEROUS_COMMAND_WORDS, ...SHELL_EXEC_WORDS, ...DOWNLOADER_WORDS];
+// Encoded PowerShell (`powershell[.exe] ... -e|-enc|-encodedcommand <payload>`),
+// matched on whitespace-separated tokens in ONE linear pass. The earlier regex
+// (`powershell\\s+(?:\\S+\\s+)*?-e...`) is quadratic on repeated "powershell "
+// tokens (every start rescans the tail: 256KB measured ~2.5s, 1MB ~40s) because
+// the lazy token loop re-runs from each start. Same semantics: a token ending in
+// powershell/powershell.exe, then LATER a token that is exactly -e/-enc/
+// -encodedcommand, then at least one more token.
+function hasEncodedPowershell(command: string): boolean {
+ let seenPs = false;
+ let pendingEnc = false;
+ for (const raw of command.split(/\s+/)) {
+ if (!raw) continue;
+ if (pendingEnc) return true;
+ const t = raw.toLowerCase();
+ if (seenPs && (t === '-e' || t === '-enc' || t === '-encodedcommand')) pendingEnc = true;
+ else if (t.endsWith('powershell') || t.endsWith('powershell.exe')) seenPs = true;
+ }
+ return false;
+}
+
function wordBoundaryMatches(text: string, word: string): boolean {
return new RegExp(`\\b${word}\\b`, 'i').test(text);
}
@@ -80,11 +92,8 @@ function wordBoundaryMatches(text: string, word: string): boolean {
export function scanCommandStringForRisk(command: string): string[] {
const reasons: string[] = [];
- for (const pattern of STANDALONE_RISK_PATTERNS) {
- if (pattern.test(command)) {
- reasons.push('matches a known-dangerous execution pattern (encoded/remote command execution)');
- break;
- }
+ if (hasEncodedPowershell(command) || STANDALONE_RISK_PATTERNS.some((pattern) => pattern.test(command))) {
+ reasons.push('matches a known-dangerous execution pattern (encoded/remote command execution)');
}
const hasDownloader = DOWNLOADER_WORDS.some((w) => wordBoundaryMatches(command, w));