MCPcopy Create free account
hub / github.com/freecodexyz/free-code / validateObfuscatedFlags

Function validateObfuscatedFlags

src/tools/BashTool/bashSecurity.ts:1130–1537  ·  view source on GitHub ↗
(context: ValidationContext)

Source from the content-addressed store, hash-verified

1128}
1129
1130function validateObfuscatedFlags(context: ValidationContext): PermissionResult {
1131 // Block shell quoting bypass patterns used to circumvent negative lookaheads we use in our regexes to block known dangerous flags
1132
1133 const { originalCommand, baseCommand } = context
1134
1135 // Echo is safe for obfuscated flags, BUT only for simple echo commands.
1136 // For compound commands (with |, &, ;), we need to check the whole command
1137 // because the dangerous ANSI-C quoting might be after the operator.
1138 const hasShellOperators = /[|&;]/.test(originalCommand)
1139 if (baseCommand === 'echo' && !hasShellOperators) {
1140 return {
1141 behavior: 'passthrough',
1142 message: 'echo command is safe and has no dangerous flags',
1143 }
1144 }
1145
1146 // COMPREHENSIVE OBFUSCATION DETECTION
1147 // These checks catch various ways to hide flags using shell quoting
1148
1149 // 1. Block ANSI-C quoting ($'...') - can encode any character via escape sequences
1150 // Simple pattern that matches $'...' anywhere. This correctly handles:
1151 // - grep '$' file => no match ($ is regex anchor inside quotes, no $'...' structure)
1152 // - 'test'$'-exec' => match (quote concatenation with ANSI-C)
1153 // - Zero-width space and other invisible chars => match
1154 // The pattern requires $' followed by content (can be empty) followed by closing '
1155 if (/\$'[^']*'/.test(originalCommand)) {
1156 logEvent('tengu_bash_security_check_triggered', {
1157 checkId: BASH_SECURITY_CHECK_IDS.OBFUSCATED_FLAGS,
1158 subId: 5,
1159 })
1160 return {
1161 behavior: 'ask',
1162 message: 'Command contains ANSI-C quoting which can hide characters',
1163 }
1164 }
1165
1166 // 2. Block locale quoting ($"...") - can also use escape sequences
1167 // Same simple pattern as ANSI-C quoting above
1168 if (/\$"[^"]*"/.test(originalCommand)) {
1169 logEvent('tengu_bash_security_check_triggered', {
1170 checkId: BASH_SECURITY_CHECK_IDS.OBFUSCATED_FLAGS,
1171 subId: 6,
1172 })
1173 return {
1174 behavior: 'ask',
1175 message: 'Command contains locale quoting which can hide characters',
1176 }
1177 }
1178
1179 // 3. Block empty ANSI-C or locale quotes followed by dash
1180 // $''-exec or $""-exec
1181 if (/\$['"]{2}\s*-/.test(originalCommand)) {
1182 logEvent('tengu_bash_security_check_triggered', {
1183 checkId: BASH_SECURITY_CHECK_IDS.OBFUSCATED_FLAGS,
1184 subId: 9,
1185 })
1186 return {
1187 behavior: 'ask',

Callers

nothing calls this directly

Calls 1

logEventFunction · 0.85

Tested by

no test coverage detected