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(guard): unescape a substitution body as bash does, scan backticks inside double quotes, rescan any word with a separator (#3791)

Dragan Spiridonovb880b825ff1f
diff --git a/.claude/helpers/hook-handler.cjs b/.claude/helpers/hook-handler.cjs
index 953071aaca..4a9b8adbd3 100755
--- a/.claude/helpers/hook-handler.cjs
+++ b/.claude/helpers/hook-handler.cjs
@@ -501,7 +501,7 @@ const handlers = {
if (!started) return false
// Literal shell strings (e.g. sh -c 'rm -rf /') also carried the old guard.
// Bound rescanning to four levels; beyond that retain its conservative check.
- if (word.includes('rm') && /[\s;&|()\x60]/.test(word)) {
+ if (/[\s;&|()\x60]/.test(word)) {
if (depth < 4 ? hasRootDelete(word, depth + 1) : word.includes('rm -rf /')) return true
}
if (!inRm) inRm = word === 'rm' || word.endsWith('/rm')
@@ -514,6 +514,21 @@ const handlers = {
word = ''; started = false
return inRm && recursive && force && root
}
+ // A command substitution's body, from just after its opening backtick. Bash removes a
+ // backslash only before $, a backtick or a backslash (and " inside double quotes) and
+ // drops backslash-newline; any other backslash stays for the nested scan.
+ const substitution = (from, inDouble) => {
+ let body = '', j = from
+ for (; j < command.length && command[j] !== '\x60'; j++) {
+ const next = command[j + 1]
+ if (command[j] === '\\' && j + 1 < command.length) {
+ if (next === '\n') { j++; continue }
+ if (next === '$' || next === '\x60' || next === '\\' || (inDouble && next === '"')) { body += next; j++; continue }
+ }
+ body += command[j]
+ }
+ return [j, depth < 4 ? hasRootDelete(body, depth + 1) : body.includes('rm -rf /')]
+ }
const finishCommand = () => {
const denied = inRm && recursive && force && root
inRm = optionsEnded = recursive = force = root = false
@@ -525,6 +540,11 @@ const handlers = {
redirect = false
if (quote) {
if (char === quote) quote = ''
+ else if (quote === '"' && char === '\x60') {
+ const [end, denied] = substitution(i + 1, true)
+ if (denied) return true
+ i = end
+ }
else if (quote === '"' && char === '\\' && i + 1 < command.length &&
(command[i + 1] === '"' || command[i + 1] === '\\' || command[i + 1] === '$' ||
command.charCodeAt(i + 1) === 96 || command[i + 1] === '\n')) {
@@ -539,12 +559,9 @@ const handlers = {
} else if (char === '\x60') {
// Command substitution: scan the body as its own command; the substitution
// stays inside the enclosing word, so the rm being parsed keeps its state.
- let body = ''
- for (i++; i < command.length && command[i] !== '\x60'; i++) {
- if (command[i] === '\\' && i + 1 < command.length) i++
- body += command[i]
- }
- if (depth < 4 ? hasRootDelete(body, depth + 1) : body.includes('rm -rf /')) return true
+ const [end, denied] = substitution(i + 1, false)
+ if (denied) return true
+ i = end
} else if (char === '$' && (command[i + 1] === "'" || command[i + 1] === '"')) {
// ANSI-C ($'...') and locale ($"...") quoting: the $ is not part of the word.
} else if (char === '"' || char === "'") {
diff --git a/plugins/ruflo-mods/hooks/guard/dangerous-command.ts b/plugins/ruflo-mods/hooks/guard/dangerous-command.ts
index 0acb7cd90c..5068615522 100644
--- a/plugins/ruflo-mods/hooks/guard/dangerous-command.ts
+++ b/plugins/ruflo-mods/hooks/guard/dangerous-command.ts
@@ -26,7 +26,7 @@ function hasRootDelete(command: string, depth = 0): boolean {
if (!started) return false
// Literal shell strings (e.g. sh -c 'rm -rf /') also carried the old guard.
// Bound rescanning to four levels; beyond that retain its conservative check.
- if (word.includes('rm') && /[\s;&|()\x60]/.test(word)) {
+ if (/[\s;&|()\x60]/.test(word)) {
if (depth < 4 ? hasRootDelete(word, depth + 1) : word.includes('rm -rf /')) return true
}
if (!inRm) inRm = word === 'rm' || word.endsWith('/rm')
@@ -39,6 +39,21 @@ function hasRootDelete(command: string, depth = 0): boolean {
word = ''; started = false
return inRm && recursive && force && root
}
+ // A command substitution's body, from just after its opening backtick. Bash removes a
+ // backslash only before $, a backtick or a backslash (and " inside double quotes) and
+ // drops backslash-newline; any other backslash stays for the nested scan.
+ const substitution = (from: number, inDouble: boolean): [number, boolean] => {
+ let body = '', j = from
+ for (; j < command.length && command[j] !== '\x60'; j++) {
+ const next = command[j + 1]
+ if (command[j] === '\\' && j + 1 < command.length) {
+ if (next === '\n') { j++; continue }
+ if (next === '$' || next === '\x60' || next === '\\' || (inDouble && next === '"')) { body += next; j++; continue }
+ }
+ body += command[j]
+ }
+ return [j, depth < 4 ? hasRootDelete(body, depth + 1) : body.includes('rm -rf /')]
+ }
const finishCommand = () => {
const denied = inRm && recursive && force && root
inRm = optionsEnded = recursive = force = root = false
@@ -50,6 +65,11 @@ function hasRootDelete(command: string, depth = 0): boolean {
redirect = false
if (quote) {
if (char === quote) quote = ''
+ else if (quote === '"' && char === '\x60') {
+ const [end, denied] = substitution(i + 1, true)
+ if (denied) return true
+ i = end
+ }
else if (quote === '"' && char === '\\' && i + 1 < command.length &&
(command[i + 1] === '"' || command[i + 1] === '\\' || command[i + 1] === '$' ||
command.charCodeAt(i + 1) === 96 || command[i + 1] === '\n')) {
@@ -64,12 +84,9 @@ function hasRootDelete(command: string, depth = 0): boolean {
} else if (char === '\x60') {
// Command substitution: scan the body as its own command; the substitution
// stays inside the enclosing word, so the rm being parsed keeps its state.
- let body = ''
- for (i++; i < command.length && command[i] !== '\x60'; i++) {
- if (command[i] === '\\' && i + 1 < command.length) i++
- body += command[i]
- }
- if (depth < 4 ? hasRootDelete(body, depth + 1) : body.includes('rm -rf /')) return true
+ const [end, denied] = substitution(i + 1, false)
+ if (denied) return true
+ i = end
} else if (char === '$' && (command[i + 1] === "'" || command[i + 1] === '"')) {
// ANSI-C ($'...') and locale ($"...") quoting: the $ is not part of the word.
} else if (char === '"' || char === "'") {
diff --git a/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs b/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs
index 953071aaca..4a9b8adbd3 100755
--- a/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs
+++ b/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs
@@ -501,7 +501,7 @@ const handlers = {
if (!started) return false
// Literal shell strings (e.g. sh -c 'rm -rf /') also carried the old guard.
// Bound rescanning to four levels; beyond that retain its conservative check.
- if (word.includes('rm') && /[\s;&|()\x60]/.test(word)) {
+ if (/[\s;&|()\x60]/.test(word)) {
if (depth < 4 ? hasRootDelete(word, depth + 1) : word.includes('rm -rf /')) return true
}
if (!inRm) inRm = word === 'rm' || word.endsWith('/rm')
@@ -514,6 +514,21 @@ const handlers = {
word = ''; started = false
return inRm && recursive && force && root
}
+ // A command substitution's body, from just after its opening backtick. Bash removes a
+ // backslash only before $, a backtick or a backslash (and " inside double quotes) and
+ // drops backslash-newline; any other backslash stays for the nested scan.
+ const substitution = (from, inDouble) => {
+ let body = '', j = from
+ for (; j < command.length && command[j] !== '\x60'; j++) {
+ const next = command[j + 1]
+ if (command[j] === '\\' && j + 1 < command.length) {
+ if (next === '\n') { j++; continue }
+ if (next === '$' || next === '\x60' || next === '\\' || (inDouble && next === '"')) { body += next; j++; continue }
+ }
+ body += command[j]
+ }
+ return [j, depth < 4 ? hasRootDelete(body, depth + 1) : body.includes('rm -rf /')]
+ }
const finishCommand = () => {
const denied = inRm && recursive && force && root
inRm = optionsEnded = recursive = force = root = false
@@ -525,6 +540,11 @@ const handlers = {
redirect = false
if (quote) {
if (char === quote) quote = ''
+ else if (quote === '"' && char === '\x60') {
+ const [end, denied] = substitution(i + 1, true)
+ if (denied) return true
+ i = end
+ }
else if (quote === '"' && char === '\\' && i + 1 < command.length &&
(command[i + 1] === '"' || command[i + 1] === '\\' || command[i + 1] === '$' ||
command.charCodeAt(i + 1) === 96 || command[i + 1] === '\n')) {
@@ -539,12 +559,9 @@ const handlers = {
} else if (char === '\x60') {
// Command substitution: scan the body as its own command; the substitution
// stays inside the enclosing word, so the rm being parsed keeps its state.
- let body = ''
- for (i++; i < command.length && command[i] !== '\x60'; i++) {
- if (command[i] === '\\' && i + 1 < command.length) i++
- body += command[i]
- }
- if (depth < 4 ? hasRootDelete(body, depth + 1) : body.includes('rm -rf /')) return true
+ const [end, denied] = substitution(i + 1, false)
+ if (denied) return true
+ i = end
} else if (char === '$' && (command[i + 1] === "'" || command[i + 1] === '"')) {
// ANSI-C ($'...') and locale ($"...") quoting: the $ is not part of the word.
} else if (char === '"' || char === "'") {
diff --git a/v3/@claude-flow/cli/__tests__/mods/mods-guard.test.ts b/v3/@claude-flow/cli/__tests__/mods/mods-guard.test.ts
index e434035288..576d0154b4 100644
--- a/v3/@claude-flow/cli/__tests__/mods/mods-guard.test.ts
+++ b/v3/@claude-flow/cli/__tests__/mods/mods-guard.test.ts
@@ -314,6 +314,13 @@ describe('#3698 root deletion guard parity', () => {
['rm -rf /tmp/x `:` /', true],
['rm -r `:` -f /', true],
['git commit -m "fix `rm` docs"', false],
+ // A substitution body is unescaped as bash does it, and one inside double quotes is a substitution too.
+ ['echo `rm -rf \\( /`', true],
+ ['echo `rm -rf \\\n/`', true],
+ ['rm -rf "/`true`"', true],
+ ['echo "`r\\`\\`m -rf /`"', true],
+ ["sh -c 'r``m -rf /'", true],
+ ['echo "`date`" /tmp', false],
['echo `ls` /tmp', false],
['format c: /q /y', true],
['del /s /q c:\\', true],
diff --git a/v3/@claude-flow/cli/src/init/helpers-generator.ts b/v3/@claude-flow/cli/src/init/helpers-generator.ts
index 058c69e366..a55fc7271f 100644
--- a/v3/@claude-flow/cli/src/init/helpers-generator.ts
+++ b/v3/@claude-flow/cli/src/init/helpers-generator.ts
@@ -459,7 +459,7 @@ const ROOT_DELETE_CHECK_SOURCE = String.raw`function hasRootDelete(command, dept
if (!started) return false
// Literal shell strings (e.g. sh -c 'rm -rf /') also carried the old guard.
// Bound rescanning to four levels; beyond that retain its conservative check.
- if (word.includes('rm') && /[\s;&|()\x60]/.test(word)) {
+ if (/[\s;&|()\x60]/.test(word)) {
if (depth < 4 ? hasRootDelete(word, depth + 1) : word.includes('rm -rf /')) return true
}
if (!inRm) inRm = word === 'rm' || word.endsWith('/rm')
@@ -472,6 +472,21 @@ const ROOT_DELETE_CHECK_SOURCE = String.raw`function hasRootDelete(command, dept
word = ''; started = false
return inRm && recursive && force && root
}
+ // A command substitution's body, from just after its opening backtick. Bash removes a
+ // backslash only before $, a backtick or a backslash (and " inside double quotes) and
+ // drops backslash-newline; any other backslash stays for the nested scan.
+ const substitution = (from, inDouble) => {
+ let body = '', j = from
+ for (; j < command.length && command[j] !== '\x60'; j++) {
+ const next = command[j + 1]
+ if (command[j] === '\\' && j + 1 < command.length) {
+ if (next === '\n') { j++; continue }
+ if (next === '$' || next === '\x60' || next === '\\' || (inDouble && next === '"')) { body += next; j++; continue }
+ }
+ body += command[j]
+ }
+ return [j, depth < 4 ? hasRootDelete(body, depth + 1) : body.includes('rm -rf /')]
+ }
const finishCommand = () => {
const denied = inRm && recursive && force && root
inRm = optionsEnded = recursive = force = root = false
@@ -483,6 +498,11 @@ const ROOT_DELETE_CHECK_SOURCE = String.raw`function hasRootDelete(command, dept
redirect = false
if (quote) {
if (char === quote) quote = ''
+ else if (quote === '"' && char === '\x60') {
+ const [end, denied] = substitution(i + 1, true)
+ if (denied) return true
+ i = end
+ }
else if (quote === '"' && char === '\\' && i + 1 < command.length &&
(command[i + 1] === '"' || command[i + 1] === '\\' || command[i + 1] === '$' ||
command.charCodeAt(i + 1) === 96 || command[i + 1] === '\n')) {
@@ -497,12 +517,9 @@ const ROOT_DELETE_CHECK_SOURCE = String.raw`function hasRootDelete(command, dept
} else if (char === '\x60') {
// Command substitution: scan the body as its own command; the substitution
// stays inside the enclosing word, so the rm being parsed keeps its state.
- let body = ''
- for (i++; i < command.length && command[i] !== '\x60'; i++) {
- if (command[i] === '\\' && i + 1 < command.length) i++
- body += command[i]
- }
- if (depth < 4 ? hasRootDelete(body, depth + 1) : body.includes('rm -rf /')) return true
+ const [end, denied] = substitution(i + 1, false)
+ if (denied) return true
+ i = end
} else if (char === '$' && (command[i + 1] === "'" || command[i + 1] === '"')) {
// ANSI-C ($'...') and locale ($"...") quoting: the $ is not part of the word.
} else if (char === '"' || char === "'") {