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): decode $'...' escapes as bash does; adversarial and benign tables for the root-delete guard (#3791)

ruv6258d6012767
diff --git a/.claude/helpers/hook-handler.cjs b/.claude/helpers/hook-handler.cjs
index dc03f9693b..1c9d524404 100755
--- a/.claude/helpers/hook-handler.cjs
+++ b/.claude/helpers/hook-handler.cjs
@@ -486,6 +486,7 @@ const handlers = {
// Join literal quote fragments and normalize root paths without shell evaluation.
function hasRootDelete(command, depth = 0) {
let word = '', quote = '', started = false, redirect = false
+ let ansiNul = false // a NUL inside $'...' ends that string, as in bash
let inRm = false, optionsEnded = false, recursive = false, force = false, root = false
const isRoot = (operand) => {
if (!operand.startsWith('/')) return false
@@ -497,6 +498,23 @@ const handlers = {
}
return parts.length === 0 || /[*?\[]/.test(parts[0])
}
+ // One backslash escape inside $'...', from the character after the backslash.
+ // Returns the text it stands for and the index of its last character. An unknown escape keeps its backslash.
+ // The command arrives lowercased, so an uppercase-U escape reads as a lowercase one: eight digits starting 0000 are one code point.
+ const ansiEscape = (s, at) => {
+ const c = s[at]
+ const simple = { a: '\x07', b: '\b', e: '\x1b', f: '\f', n: '\n', r: '\r', t: '\t', v: '\v', '\\': '\\', "'": "'", '"': '"', '?': '?' }
+ if (simple[c] !== undefined) return [simple[c], at]
+ const digits = (from, max, pattern) => pattern.exec(s.slice(from, from + max))?.[0]
+ const octal = digits(at, 3, /^[0-7]+/)
+ if (octal) return [String.fromCharCode(parseInt(octal, 8) & 255), at + octal.length - 1]
+ const hex = c === 'x' ? digits(at + 1, 2, /^[0-9a-f]+/) : undefined
+ if (hex) return [String.fromCharCode(parseInt(hex, 16)), at + hex.length]
+ const wide = c === 'u' ? (digits(at + 1, 8, /^0000[0-9a-f]{4}$/) ?? digits(at + 1, 4, /^[0-9a-f]+/)) : undefined
+ if (wide) return [String.fromCodePoint(parseInt(wide, 16)), at + wide.length]
+ if (c === 'c' && at + 1 < s.length) return [String.fromCharCode(s.charCodeAt(at + 1) & 31), at + 1]
+ return ['\\' + c, at]
+ }
const finishWord = () => {
if (!started) return false
// Literal shell strings (e.g. sh -c 'rm -rf /') also carried the old guard.
@@ -539,6 +557,15 @@ const handlers = {
const redirectionAmpersand = char === '&' && (redirect || command[i + 1] === '>')
redirect = false
if (quote) {
+ if (quote === "$'") {
+ if (char === "'") { quote = ''; ansiNul = false }
+ else if (char === '\\' && i + 1 < command.length) {
+ const [text, last] = ansiEscape(command, i + 1)
+ i = last
+ if (!ansiNul) { const nul = text.indexOf('\0'); if (nul < 0) word += text; else { word += text.slice(0, nul); ansiNul = true } }
+ } else if (!ansiNul) word += char
+ continue
+ }
if (char === quote) quote = ''
else if (quote === '"' && char === '\x60' && command.indexOf('\x60', i + 1) > i) {
const [end, denied] = substitution(i + 1, true)
@@ -568,7 +595,8 @@ const handlers = {
if (denied) return true
i = end; started = true
} else if (char === '$' && (command[i + 1] === "'" || command[i + 1] === '"')) {
- // ANSI-C ($'...') and locale ($"...") quoting: the $ is not part of the word.
+ // ANSI-C ($'...': escapes decoded) and locale ($"...") quoting: the $ is not part of the word.
+ quote = command[i + 1] === "'" ? "$'" : '"'; started = true; i++
} else if (char === '"' || char === "'") {
quote = char; started = true
} else if (char === '#' && !started) {
diff --git a/plugins/ruflo-mods/hooks/guard/dangerous-command.ts b/plugins/ruflo-mods/hooks/guard/dangerous-command.ts
index 461c94afd7..2136e75e99 100644
--- a/plugins/ruflo-mods/hooks/guard/dangerous-command.ts
+++ b/plugins/ruflo-mods/hooks/guard/dangerous-command.ts
@@ -11,6 +11,7 @@ export const DANGEROUS_COMMANDS: readonly string[] = ['rm -rf /', 'format c:', '
// It joins quote fragments and escapes, but never evaluates expansions or links.
function hasRootDelete(command: string, depth = 0): boolean {
let word = '', quote = '', started = false, redirect = false
+ let ansiNul = false // a NUL inside $'...' ends that string, as in bash
let inRm = false, optionsEnded = false, recursive = false, force = false, root = false
const isRoot = (operand: string) => {
if (!operand.startsWith('/')) return false
@@ -22,6 +23,23 @@ function hasRootDelete(command: string, depth = 0): boolean {
}
return parts.length === 0 || /[*?\[]/.test(parts[0])
}
+ // One backslash escape inside $'...', from the character after the backslash.
+ // Returns the text it stands for and the index of its last character. An unknown escape keeps its backslash.
+ // The command arrives lowercased, so an uppercase-U escape reads as a lowercase one: eight digits starting 0000 are one code point.
+ const ansiEscape = (s: string, at: number): [string, number] => {
+ const c = s[at]
+ const simple: Record<string, string> = { a: '\x07', b: '\b', e: '\x1b', f: '\f', n: '\n', r: '\r', t: '\t', v: '\v', '\\': '\\', "'": "'", '"': '"', '?': '?' }
+ if (simple[c] !== undefined) return [simple[c], at]
+ const digits = (from: number, max: number, pattern: RegExp) => pattern.exec(s.slice(from, from + max))?.[0]
+ const octal = digits(at, 3, /^[0-7]+/)
+ if (octal) return [String.fromCharCode(parseInt(octal, 8) & 255), at + octal.length - 1]
+ const hex = c === 'x' ? digits(at + 1, 2, /^[0-9a-f]+/) : undefined
+ if (hex) return [String.fromCharCode(parseInt(hex, 16)), at + hex.length]
+ const wide = c === 'u' ? (digits(at + 1, 8, /^0000[0-9a-f]{4}$/) ?? digits(at + 1, 4, /^[0-9a-f]+/)) : undefined
+ if (wide) return [String.fromCodePoint(parseInt(wide, 16)), at + wide.length]
+ if (c === 'c' && at + 1 < s.length) return [String.fromCharCode(s.charCodeAt(at + 1) & 31), at + 1]
+ return ['\\' + c, at]
+ }
const finishWord = () => {
if (!started) return false
// Literal shell strings (e.g. sh -c 'rm -rf /') also carried the old guard.
@@ -64,6 +82,15 @@ function hasRootDelete(command: string, depth = 0): boolean {
const redirectionAmpersand = char === '&' && (redirect || command[i + 1] === '>')
redirect = false
if (quote) {
+ if (quote === "$'") {
+ if (char === "'") { quote = ''; ansiNul = false }
+ else if (char === '\\' && i + 1 < command.length) {
+ const [text, last] = ansiEscape(command, i + 1)
+ i = last
+ if (!ansiNul) { const nul = text.indexOf('\0'); if (nul < 0) word += text; else { word += text.slice(0, nul); ansiNul = true } }
+ } else if (!ansiNul) word += char
+ continue
+ }
if (char === quote) quote = ''
else if (quote === '"' && char === '\x60' && command.indexOf('\x60', i + 1) > i) {
const [end, denied] = substitution(i + 1, true)
@@ -93,7 +120,8 @@ function hasRootDelete(command: string, depth = 0): boolean {
if (denied) return true
i = end; started = true
} else if (char === '$' && (command[i + 1] === "'" || command[i + 1] === '"')) {
- // ANSI-C ($'...') and locale ($"...") quoting: the $ is not part of the word.
+ // ANSI-C ($'...': escapes decoded) and locale ($"...") quoting: the $ is not part of the word.
+ quote = command[i + 1] === "'" ? "$'" : '"'; started = true; i++
} else if (char === '"' || char === "'") {
quote = char; started = true
} else if (char === '#' && !started) {
diff --git a/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs b/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs
index dc03f9693b..1c9d524404 100755
--- a/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs
+++ b/v3/@claude-flow/cli/.claude/helpers/hook-handler.cjs
@@ -486,6 +486,7 @@ const handlers = {
// Join literal quote fragments and normalize root paths without shell evaluation.
function hasRootDelete(command, depth = 0) {
let word = '', quote = '', started = false, redirect = false
+ let ansiNul = false // a NUL inside $'...' ends that string, as in bash
let inRm = false, optionsEnded = false, recursive = false, force = false, root = false
const isRoot = (operand) => {
if (!operand.startsWith('/')) return false
@@ -497,6 +498,23 @@ const handlers = {
}
return parts.length === 0 || /[*?\[]/.test(parts[0])
}
+ // One backslash escape inside $'...', from the character after the backslash.
+ // Returns the text it stands for and the index of its last character. An unknown escape keeps its backslash.
+ // The command arrives lowercased, so an uppercase-U escape reads as a lowercase one: eight digits starting 0000 are one code point.
+ const ansiEscape = (s, at) => {
+ const c = s[at]
+ const simple = { a: '\x07', b: '\b', e: '\x1b', f: '\f', n: '\n', r: '\r', t: '\t', v: '\v', '\\': '\\', "'": "'", '"': '"', '?': '?' }
+ if (simple[c] !== undefined) return [simple[c], at]
+ const digits = (from, max, pattern) => pattern.exec(s.slice(from, from + max))?.[0]
+ const octal = digits(at, 3, /^[0-7]+/)
+ if (octal) return [String.fromCharCode(parseInt(octal, 8) & 255), at + octal.length - 1]
+ const hex = c === 'x' ? digits(at + 1, 2, /^[0-9a-f]+/) : undefined
+ if (hex) return [String.fromCharCode(parseInt(hex, 16)), at + hex.length]
+ const wide = c === 'u' ? (digits(at + 1, 8, /^0000[0-9a-f]{4}$/) ?? digits(at + 1, 4, /^[0-9a-f]+/)) : undefined
+ if (wide) return [String.fromCodePoint(parseInt(wide, 16)), at + wide.length]
+ if (c === 'c' && at + 1 < s.length) return [String.fromCharCode(s.charCodeAt(at + 1) & 31), at + 1]
+ return ['\\' + c, at]
+ }
const finishWord = () => {
if (!started) return false
// Literal shell strings (e.g. sh -c 'rm -rf /') also carried the old guard.
@@ -539,6 +557,15 @@ const handlers = {
const redirectionAmpersand = char === '&' && (redirect || command[i + 1] === '>')
redirect = false
if (quote) {
+ if (quote === "$'") {
+ if (char === "'") { quote = ''; ansiNul = false }
+ else if (char === '\\' && i + 1 < command.length) {
+ const [text, last] = ansiEscape(command, i + 1)
+ i = last
+ if (!ansiNul) { const nul = text.indexOf('\0'); if (nul < 0) word += text; else { word += text.slice(0, nul); ansiNul = true } }
+ } else if (!ansiNul) word += char
+ continue
+ }
if (char === quote) quote = ''
else if (quote === '"' && char === '\x60' && command.indexOf('\x60', i + 1) > i) {
const [end, denied] = substitution(i + 1, true)
@@ -568,7 +595,8 @@ const handlers = {
if (denied) return true
i = end; started = true
} else if (char === '$' && (command[i + 1] === "'" || command[i + 1] === '"')) {
- // ANSI-C ($'...') and locale ($"...") quoting: the $ is not part of the word.
+ // ANSI-C ($'...': escapes decoded) and locale ($"...") quoting: the $ is not part of the word.
+ quote = command[i + 1] === "'" ? "$'" : '"'; started = true; i++
} else if (char === '"' || char === "'") {
quote = char; started = true
} else if (char === '#' && !started) {
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 f9e410283c..1703d0e662 100644
--- a/v3/@claude-flow/cli/__tests__/mods/mods-guard.test.ts
+++ b/v3/@claude-flow/cli/__tests__/mods/mods-guard.test.ts
@@ -332,6 +332,92 @@ describe('#3698 root deletion guard parity', () => {
['rm "`true`"-rf /*', true],
['bash -c "`true`#x; rm -rf /"', true],
['echo `ls` /tmp', false],
+ // #3791 adversarial table. Wrappers and flag spellings that still run rm on the root.
+ ['command rm -rf /', true],
+ ['env rm -rf /', true],
+ ['sudo rm -rf /', true],
+ ['sudo -u root rm -rf /', true],
+ ['nice -n 5 rm -rf /', true],
+ ['\\rm -rf /', true],
+ ['rm -Rf /', true],
+ ['rm -rfv /', true],
+ ['rm -vrf /', true],
+ ['rm -f -r /', true],
+ ['rm -rf -- /', true],
+ ['rm -rf -v /', true],
+ ['rm --recursive -f /', true],
+ ['rm -r --force /', true],
+ ['rm --recurs --forc /', true],
+ ['rm --recursiv --force /*', true],
+ ['rm -r --f /', true],
+ ["'rm' -rf /", true],
+ ['r\\m -rf /', true],
+ ['"r"m -rf /', true],
+ ['$"rm" --r --f /', true],
+ ['echo $(rm -rf /)', true],
+ ['echo `rm --r --f /`', true],
+ ['cd /tmp && echo "$(date)" `rm -rf /`', true],
+ ["eval 'rm -rf /'", true],
+ ['true; rm -rf /', true],
+ ['false || rm -rf /', true],
+ // Benign look-alikes that must not trip the guard.
+ ['echo `date` /tmp', false],
+ ['echo `date`', false],
+ ['rm --r /tmp/x /', false],
+ ['rm --rec /tmp/x /', false],
+ ['rm -r /tmp/x /', false],
+ ['rm -f /', false],
+ ['rm /', false],
+ ['rm --force --verbose /', false],
+ ['rm --recursive /tmp/ruflo-test', false],
+ ['rm -rf /tmp/ruflo-test', false],
+ ['rm -rf ./build ../dist node_modules', false],
+ ['rm -rf ~/scratch', false],
+ ['rm -rf /home/user/project', false],
+ ['rm -rf /var/tmp/x/../y', false],
+ ['rmdir /', false],
+ ['farm -rf /', false],
+ ['alarm -rf /', false],
+ ['rm-tool -rf /', false],
+ ['git rm -r --cached /tmp/x', false],
+ ['ls -rf /', false],
+ ['printf "%s" `date` > /tmp/x; ls /', false],
+ // Documented limits: nothing is evaluated (variables, brace expansion, find), so these are not caught.
+ ['rm -rf "$HOME"', false],
+ ['rm -rf ${HOME}/..', false],
+ ['rm -rf $x', false],
+ ['rm -rf /${x}', false],
+ ['find / -delete', false],
+ // $'...' is decoded as bash does: escapes stand for the characters they name, \0 ends the string, \' does not close it.
+ ["rm -rf $'/'", true],
+ ["rm -rf $'\\x2f'", true],
+ ["rm -rf $'\\X2F'", true],
+ ["rm -rf $'\\057'", true],
+ ["rm -rf $'\\u002f'", true],
+ ["rm -rf $'\\U0000002f'", true],
+ ["$'\\x72m' -rf /", true],
+ ["rm $'\\x2drf' /", true],
+ ["rm -rf $'/\\x00etc'", true],
+ ["rm -rf $'/\\0etc'", true],
+ ["bash -c $'rm -rf \\x2f'", true],
+ ["bash -c $'echo hi\\nrm -rf /'", true],
+ ["echo $'\\'' ; rm -rf / ; echo $'\\''", true],
+ ["echo $'a\\\\' ; rm -rf /", true],
+ ["rm -rf $'\\x2ftmp'", false],
+ ["rm -rf $'/'tmp", false],
+ ["rm -rf $'/tmp'", false],
+ ['rm -rf "$\'/\'"', false],
+ ["rm -rf '$'/", false],
+ ["echo $'\\x2f'", false],
+ ["ls $'\\x2f'", false],
+ ["echo $'\\'' /tmp", false],
+ ["echo $'\\\\'; ls /", false],
+ ["rm -rf $'\\\\' /tmp", false],
+ ["rm -r $'\\x2d' /tmp", false],
+ ['echo "$"\'rm -rf /\'', false],
+ // Brace expansion is not evaluated either (documented limit).
+ ['{rm,-rf,/}', false],
+ ['rm -rf /{tmp,}', false],
['format c: /q /y', true],
['del /s /q c:\\', true],
[':(){:|:&};:', true],
diff --git a/v3/@claude-flow/cli/src/init/helpers-generator.ts b/v3/@claude-flow/cli/src/init/helpers-generator.ts
index 1595a0beaf..e7e3846864 100644
--- a/v3/@claude-flow/cli/src/init/helpers-generator.ts
+++ b/v3/@claude-flow/cli/src/init/helpers-generator.ts
@@ -444,6 +444,7 @@ module.exports = commands;
// Literal-word root guard mirrored in the mod and both shipped classic helpers.
const ROOT_DELETE_CHECK_SOURCE = String.raw`function hasRootDelete(command, depth = 0) {
let word = '', quote = '', started = false, redirect = false
+ let ansiNul = false // a NUL inside $'...' ends that string, as in bash
let inRm = false, optionsEnded = false, recursive = false, force = false, root = false
const isRoot = (operand) => {
if (!operand.startsWith('/')) return false
@@ -455,6 +456,23 @@ const ROOT_DELETE_CHECK_SOURCE = String.raw`function hasRootDelete(command, dept
}
return parts.length === 0 || /[*?\[]/.test(parts[0])
}
+ // One backslash escape inside $'...', from the character after the backslash.
+ // Returns the text it stands for and the index of its last character. An unknown escape keeps its backslash.
+ // The command arrives lowercased, so an uppercase-U escape reads as a lowercase one: eight digits starting 0000 are one code point.
+ const ansiEscape = (s, at) => {
+ const c = s[at]
+ const simple = { a: '\x07', b: '\b', e: '\x1b', f: '\f', n: '\n', r: '\r', t: '\t', v: '\v', '\\': '\\', "'": "'", '"': '"', '?': '?' }
+ if (simple[c] !== undefined) return [simple[c], at]
+ const digits = (from, max, pattern) => pattern.exec(s.slice(from, from + max))?.[0]
+ const octal = digits(at, 3, /^[0-7]+/)
+ if (octal) return [String.fromCharCode(parseInt(octal, 8) & 255), at + octal.length - 1]
+ const hex = c === 'x' ? digits(at + 1, 2, /^[0-9a-f]+/) : undefined
+ if (hex) return [String.fromCharCode(parseInt(hex, 16)), at + hex.length]
+ const wide = c === 'u' ? (digits(at + 1, 8, /^0000[0-9a-f]{4}$/) ?? digits(at + 1, 4, /^[0-9a-f]+/)) : undefined
+ if (wide) return [String.fromCodePoint(parseInt(wide, 16)), at + wide.length]
+ if (c === 'c' && at + 1 < s.length) return [String.fromCharCode(s.charCodeAt(at + 1) & 31), at + 1]
+ return ['\\' + c, at]
+ }
const finishWord = () => {
if (!started) return false
// Literal shell strings (e.g. sh -c 'rm -rf /') also carried the old guard.
@@ -497,6 +515,15 @@ const ROOT_DELETE_CHECK_SOURCE = String.raw`function hasRootDelete(command, dept
const redirectionAmpersand = char === '&' && (redirect || command[i + 1] === '>')
redirect = false
if (quote) {
+ if (quote === "$'") {
+ if (char === "'") { quote = ''; ansiNul = false }
+ else if (char === '\\' && i + 1 < command.length) {
+ const [text, last] = ansiEscape(command, i + 1)
+ i = last
+ if (!ansiNul) { const nul = text.indexOf('\0'); if (nul < 0) word += text; else { word += text.slice(0, nul); ansiNul = true } }
+ } else if (!ansiNul) word += char
+ continue
+ }
if (char === quote) quote = ''
else if (quote === '"' && char === '\x60' && command.indexOf('\x60', i + 1) > i) {
const [end, denied] = substitution(i + 1, true)
@@ -526,7 +553,8 @@ const ROOT_DELETE_CHECK_SOURCE = String.raw`function hasRootDelete(command, dept
if (denied) return true
i = end; started = true
} else if (char === '$' && (command[i + 1] === "'" || command[i + 1] === '"')) {
- // ANSI-C ($'...') and locale ($"...") quoting: the $ is not part of the word.
+ // ANSI-C ($'...': escapes decoded) and locale ($"...") quoting: the $ is not part of the word.
+ quote = command[i + 1] === "'" ? "$'" : '"'; started = true; i++
} else if (char === '"' || char === "'") {
quote = char; started = true
} else if (char === '#' && !started) {