diff --git a/tests/tools/test_approval.py b/tests/tools/test_approval.py index 99edb3b182..675fcf1e01 100644 --- a/tests/tools/test_approval.py +++ b/tests/tools/test_approval.py @@ -649,3 +649,172 @@ class TestNormalizationBypass: assert dangerous is False +class TestHeredocScriptExecution: + """Script execution via heredoc bypasses the -e/-c flag patterns. + + `python3 << 'EOF'` feeds arbitrary code through stdin without any + flag that the original patterns check for. See security audit Test 3. + """ + + def test_python3_heredoc_detected(self): + # The heredoc body also contains `rm -rf /` which fires the + # "delete in root path" pattern first (patterns are ordered). + # The heredoc pattern also matches — either detection is correct. + cmd = "python3 << 'EOF'\nimport os; os.system('rm -rf /')\nEOF" + dangerous, _, desc = detect_dangerous_command(cmd) + assert dangerous is True + + def test_python_heredoc_detected(self): + cmd = 'python << "PYEOF"\nprint("pwned")\nPYEOF' + dangerous, _, desc = detect_dangerous_command(cmd) + assert dangerous is True + + def test_perl_heredoc_detected(self): + cmd = "perl <<'END'\nsystem('whoami');\nEND" + dangerous, _, desc = detect_dangerous_command(cmd) + assert dangerous is True + + def test_ruby_heredoc_detected(self): + cmd = "ruby <