// engine/runtime-security/checkers/command-injection.ts — SPEC-084 AC-04
// Detects shell metacharacters and dangerous commands in user inputs.
// Shell metacharacters that enable command chaining/redirection
const SHELL_METACHAR_PATTERN = /[;|`]|\$\(|&&|\|\||>>|<<|>\s|<\s/;
// Dangerous shell commands (Unix + Windows)
const DANGEROUS_COMMANDS = [
    /\brm\s+-rf\b/i,
    /\bcurl\b.*\|\s*(?:sh|bash)\b/i,
    /\bwget\b/i,
    /\bchmod\b/i,
    /\bchown\b/i,
    /\bsudo\b/i,
    /\beval\b/i,
    /\bexec\b/i,
    /\bpowershell\b/i,
    /\bcmd\s*\/c\b/i,
    /\bdel\s+\/[sfq]\b/i,
    /\bformat\s+[a-z]:/i,
    /\bnet\s+user\b/i,
];
// Multi-language dangerous patterns
const CODE_INJECTION_PATTERNS = [
    /\bos\.system\s*\(/, // Python
    /\bsubprocess\.(?:call|run|Popen)\s*\(/, // Python
    /\bsystem\s*\(/, // Ruby, PHP, C
    /\bexec\s*\(/, // PHP, Node
    /\bshell_exec\s*\(/, // PHP
    /\bRuntime\.getRuntime\(\)\.exec\b/, // Java
    /\bProcessBuilder\b/, // Java
    /\bexec\.Command\s*\(/, // Go
    /\bCommand::new\s*\(/, // Rust
    /\bProcess\.Start\s*\(/, // C#
];
function checkStringForInjection(value, field) {
    const threats = [];
    // Check shell metacharacters
    if (SHELL_METACHAR_PATTERN.test(value)) {
        threats.push({
            type: 'command-injection',
            severity: 'critical',
            message: 'Shell metacharacters detected in input',
            field,
            pattern: 'shell-metachar',
        });
    }
    // Check dangerous commands
    for (const pattern of DANGEROUS_COMMANDS) {
        if (pattern.test(value)) {
            threats.push({
                type: 'command-injection',
                severity: 'critical',
                message: `Dangerous command detected: ${pattern.source}`,
                field,
                pattern: pattern.source,
            });
        }
    }
    // Check code injection patterns
    for (const pattern of CODE_INJECTION_PATTERNS) {
        if (pattern.test(value)) {
            threats.push({
                type: 'command-injection',
                severity: 'warning',
                message: `Code execution pattern detected: ${pattern.source}`,
                field,
                pattern: pattern.source,
            });
        }
    }
    return threats;
}
export const commandInjectionChecker = {
    type: 'command-injection',
    check(_toolName, inputs, _config) {
        const threats = [];
        for (const [key, value] of Object.entries(inputs)) {
            if (typeof value === 'string') {
                threats.push(...checkStringForInjection(value, key));
            }
            if (Array.isArray(value)) {
                for (const item of value) {
                    if (typeof item === 'string') {
                        threats.push(...checkStringForInjection(item, key));
                    }
                }
            }
        }
        return threats;
    },
};
//# sourceMappingURL=command-injection.js.map