empathy-framework 4.7.1__py3-none-any.whl → 4.8.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {empathy_framework-4.7.1.dist-info → empathy_framework-4.8.0.dist-info}/METADATA +65 -2
- {empathy_framework-4.7.1.dist-info → empathy_framework-4.8.0.dist-info}/RECORD +73 -52
- {empathy_framework-4.7.1.dist-info → empathy_framework-4.8.0.dist-info}/WHEEL +1 -1
- {empathy_framework-4.7.1.dist-info → empathy_framework-4.8.0.dist-info}/entry_points.txt +2 -1
- {empathy_framework-4.7.1.dist-info → empathy_framework-4.8.0.dist-info}/top_level.txt +0 -1
- empathy_os/__init__.py +2 -0
- empathy_os/cache/hash_only.py +6 -3
- empathy_os/cache/hybrid.py +6 -3
- empathy_os/cli/__init__.py +128 -238
- empathy_os/cli/__main__.py +5 -33
- empathy_os/cli/commands/__init__.py +1 -8
- empathy_os/cli/commands/help.py +331 -0
- empathy_os/cli/commands/info.py +140 -0
- empathy_os/cli/commands/inspect.py +437 -0
- empathy_os/cli/commands/metrics.py +92 -0
- empathy_os/cli/commands/orchestrate.py +184 -0
- empathy_os/cli/commands/patterns.py +207 -0
- empathy_os/cli/commands/provider.py +93 -81
- empathy_os/cli/commands/setup.py +96 -0
- empathy_os/cli/commands/status.py +235 -0
- empathy_os/cli/commands/sync.py +166 -0
- empathy_os/cli/commands/tier.py +121 -0
- empathy_os/cli/commands/workflow.py +574 -0
- empathy_os/cli/parsers/__init__.py +62 -0
- empathy_os/cli/parsers/help.py +41 -0
- empathy_os/cli/parsers/info.py +26 -0
- empathy_os/cli/parsers/inspect.py +66 -0
- empathy_os/cli/parsers/metrics.py +42 -0
- empathy_os/cli/parsers/orchestrate.py +61 -0
- empathy_os/cli/parsers/patterns.py +54 -0
- empathy_os/cli/parsers/provider.py +40 -0
- empathy_os/cli/parsers/setup.py +42 -0
- empathy_os/cli/parsers/status.py +47 -0
- empathy_os/cli/parsers/sync.py +31 -0
- empathy_os/cli/parsers/tier.py +33 -0
- empathy_os/cli/parsers/workflow.py +77 -0
- empathy_os/cli/utils/__init__.py +1 -0
- empathy_os/cli/utils/data.py +242 -0
- empathy_os/cli/utils/helpers.py +68 -0
- empathy_os/{cli.py → cli_legacy.py} +27 -27
- empathy_os/cli_minimal.py +662 -0
- empathy_os/cli_router.py +384 -0
- empathy_os/cli_unified.py +38 -2
- empathy_os/memory/__init__.py +19 -5
- empathy_os/memory/short_term.py +14 -404
- empathy_os/memory/types.py +437 -0
- empathy_os/memory/unified.py +61 -48
- empathy_os/models/fallback.py +1 -1
- empathy_os/models/provider_config.py +59 -344
- empathy_os/models/registry.py +31 -180
- empathy_os/monitoring/alerts.py +14 -20
- empathy_os/monitoring/alerts_cli.py +24 -7
- empathy_os/project_index/__init__.py +2 -0
- empathy_os/project_index/index.py +210 -5
- empathy_os/project_index/scanner.py +45 -14
- empathy_os/project_index/scanner_parallel.py +291 -0
- empathy_os/socratic/ab_testing.py +1 -1
- empathy_os/workflows/__init__.py +31 -2
- empathy_os/workflows/base.py +349 -325
- empathy_os/workflows/bug_predict.py +8 -0
- empathy_os/workflows/builder.py +273 -0
- empathy_os/workflows/caching.py +253 -0
- empathy_os/workflows/code_review_pipeline.py +1 -0
- empathy_os/workflows/history.py +510 -0
- empathy_os/workflows/output.py +410 -0
- empathy_os/workflows/perf_audit.py +125 -19
- empathy_os/workflows/progress.py +324 -22
- empathy_os/workflows/routing.py +168 -0
- empathy_os/workflows/secure_release.py +1 -0
- empathy_os/workflows/security_audit.py +190 -0
- empathy_os/workflows/security_audit_phase3.py +328 -0
- empathy_os/workflows/telemetry_mixin.py +269 -0
- empathy_os/dashboard/__init__.py +0 -15
- empathy_os/dashboard/server.py +0 -941
- patterns/README.md +0 -119
- patterns/__init__.py +0 -95
- patterns/behavior.py +0 -298
- patterns/code_review_memory.json +0 -441
- patterns/core.py +0 -97
- patterns/debugging.json +0 -3763
- patterns/empathy.py +0 -268
- patterns/health_check_memory.json +0 -505
- patterns/input.py +0 -161
- patterns/memory_graph.json +0 -8
- patterns/refactoring_memory.json +0 -1113
- patterns/registry.py +0 -663
- patterns/security_memory.json +0 -8
- patterns/structural.py +0 -415
- patterns/validation.py +0 -194
- {empathy_framework-4.7.1.dist-info → empathy_framework-4.8.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -342,11 +342,29 @@ class SecurityAuditWorkflow(BaseWorkflow):
|
|
|
342
342
|
if self._is_detection_code(line_content, match.group()):
|
|
343
343
|
continue
|
|
344
344
|
|
|
345
|
+
# Phase 2: Skip safe SQL parameterization patterns
|
|
346
|
+
if vuln_type == "sql_injection":
|
|
347
|
+
if self._is_safe_sql_parameterization(
|
|
348
|
+
line_content,
|
|
349
|
+
match.group(),
|
|
350
|
+
content,
|
|
351
|
+
):
|
|
352
|
+
continue
|
|
353
|
+
|
|
345
354
|
# Skip fake/test credentials
|
|
346
355
|
if vuln_type == "hardcoded_secret":
|
|
347
356
|
if self._is_fake_credential(match.group()):
|
|
348
357
|
continue
|
|
349
358
|
|
|
359
|
+
# Phase 2: Skip safe random usage (tests, demos, documented)
|
|
360
|
+
if vuln_type == "insecure_random":
|
|
361
|
+
if self._is_safe_random_usage(
|
|
362
|
+
line_content,
|
|
363
|
+
file_name,
|
|
364
|
+
content,
|
|
365
|
+
):
|
|
366
|
+
continue
|
|
367
|
+
|
|
350
368
|
# Skip command_injection in documentation strings
|
|
351
369
|
if vuln_type == "command_injection":
|
|
352
370
|
if self._is_documentation_or_string(
|
|
@@ -380,6 +398,29 @@ class SecurityAuditWorkflow(BaseWorkflow):
|
|
|
380
398
|
except OSError:
|
|
381
399
|
continue
|
|
382
400
|
|
|
401
|
+
# Phase 3: Apply AST-based filtering for command injection
|
|
402
|
+
try:
|
|
403
|
+
from .security_audit_phase3 import apply_phase3_filtering
|
|
404
|
+
|
|
405
|
+
# Separate command injection findings
|
|
406
|
+
cmd_findings = [f for f in findings if f["type"] == "command_injection"]
|
|
407
|
+
other_findings = [f for f in findings if f["type"] != "command_injection"]
|
|
408
|
+
|
|
409
|
+
# Apply Phase 3 filtering to command injection
|
|
410
|
+
filtered_cmd = apply_phase3_filtering(cmd_findings)
|
|
411
|
+
|
|
412
|
+
# Combine back
|
|
413
|
+
findings = other_findings + filtered_cmd
|
|
414
|
+
|
|
415
|
+
logger.info(
|
|
416
|
+
f"Phase 3: Filtered command_injection from {len(cmd_findings)} to {len(filtered_cmd)} "
|
|
417
|
+
f"({len(cmd_findings) - len(filtered_cmd)} false positives removed)"
|
|
418
|
+
)
|
|
419
|
+
except ImportError:
|
|
420
|
+
logger.debug("Phase 3 module not available, skipping AST-based filtering")
|
|
421
|
+
except Exception as e:
|
|
422
|
+
logger.warning(f"Phase 3 filtering failed: {e}")
|
|
423
|
+
|
|
383
424
|
input_tokens = len(str(input_data)) // 4
|
|
384
425
|
output_tokens = len(str(findings)) // 4
|
|
385
426
|
|
|
@@ -541,6 +582,154 @@ class SecurityAuditWorkflow(BaseWorkflow):
|
|
|
541
582
|
|
|
542
583
|
return False
|
|
543
584
|
|
|
585
|
+
def _is_safe_sql_parameterization(self, line_content: str, match_text: str, file_content: str) -> bool:
|
|
586
|
+
"""Check if SQL query uses safe parameterization despite f-string usage.
|
|
587
|
+
|
|
588
|
+
Phase 2 Enhancement: Detects safe patterns like:
|
|
589
|
+
- placeholders = ",".join("?" * len(ids))
|
|
590
|
+
- cursor.execute(f"... IN ({placeholders})", ids)
|
|
591
|
+
|
|
592
|
+
This prevents false positives for the SQLite-recommended pattern
|
|
593
|
+
of building dynamic placeholder strings.
|
|
594
|
+
|
|
595
|
+
Args:
|
|
596
|
+
line_content: The line containing the match (may be incomplete for multi-line)
|
|
597
|
+
match_text: The matched text
|
|
598
|
+
file_content: Full file content for context analysis
|
|
599
|
+
|
|
600
|
+
Returns:
|
|
601
|
+
True if this is safe parameterized SQL, False otherwise
|
|
602
|
+
"""
|
|
603
|
+
# Get the position of the match in the full file content
|
|
604
|
+
match_pos = file_content.find(match_text)
|
|
605
|
+
if match_pos == -1:
|
|
606
|
+
# Try to find cursor.execute
|
|
607
|
+
match_pos = file_content.find("cursor.execute")
|
|
608
|
+
if match_pos == -1:
|
|
609
|
+
return False
|
|
610
|
+
|
|
611
|
+
# Extract a larger context (next 200 chars after match)
|
|
612
|
+
context = file_content[match_pos:match_pos + 200]
|
|
613
|
+
|
|
614
|
+
# Also get lines before the match for placeholder detection
|
|
615
|
+
lines_before = file_content[:match_pos].split("\n")
|
|
616
|
+
recent_lines = lines_before[-10:] if len(lines_before) > 10 else lines_before
|
|
617
|
+
|
|
618
|
+
# Pattern 1: Check if this is a placeholder-based parameterized query
|
|
619
|
+
# Look for: cursor.execute(f"... IN ({placeholders})", params)
|
|
620
|
+
if "placeholders" in context or any("placeholders" in line for line in recent_lines[-5:]):
|
|
621
|
+
# Check if context has both f-string and separate parameters
|
|
622
|
+
# Pattern: f"...{placeholders}..." followed by comma and params
|
|
623
|
+
if re.search(r'f["\'][^"\']*\{placeholders\}[^"\']*["\']\s*,\s*\w+', context):
|
|
624
|
+
return True # Safe - has separate parameters
|
|
625
|
+
|
|
626
|
+
# Also check if recent lines built the placeholders
|
|
627
|
+
for prev_line in reversed(recent_lines):
|
|
628
|
+
if "placeholders" in prev_line and '"?"' in prev_line and "join" in prev_line:
|
|
629
|
+
# Found placeholder construction
|
|
630
|
+
# Now check if the execute has separate parameters
|
|
631
|
+
if "," in context and any(param in context for param in ["run_ids", "ids", "params", "values", ")"]):
|
|
632
|
+
return True
|
|
633
|
+
|
|
634
|
+
# Pattern 2: Check if f-string only builds SQL structure with constants
|
|
635
|
+
# Example: f"SELECT * FROM {TABLE_NAME}" where TABLE_NAME is a constant
|
|
636
|
+
f_string_vars = re.findall(r'\{(\w+)\}', context)
|
|
637
|
+
if f_string_vars:
|
|
638
|
+
# Check if all variables are constants (UPPERCASE or table/column names)
|
|
639
|
+
all_constants = all(
|
|
640
|
+
var.isupper() or "TABLE" in var.upper() or "COLUMN" in var.upper()
|
|
641
|
+
for var in f_string_vars
|
|
642
|
+
)
|
|
643
|
+
if all_constants:
|
|
644
|
+
return True # Safe - using constants, not user data
|
|
645
|
+
|
|
646
|
+
# Pattern 3: Check for security note comments nearby
|
|
647
|
+
# If developers added security notes, it's likely safe
|
|
648
|
+
for prev_line in reversed(recent_lines[-3:]):
|
|
649
|
+
if "security note" in prev_line.lower() and "safe" in prev_line.lower():
|
|
650
|
+
return True
|
|
651
|
+
|
|
652
|
+
return False
|
|
653
|
+
|
|
654
|
+
def _is_safe_random_usage(self, line_content: str, file_path: str, file_content: str) -> bool:
|
|
655
|
+
"""Check if random usage is in a safe context (tests, simulations, non-crypto).
|
|
656
|
+
|
|
657
|
+
Phase 2 Enhancement: Reduces false positives for random module usage
|
|
658
|
+
in test fixtures, A/B testing simulations, and demo code.
|
|
659
|
+
|
|
660
|
+
Args:
|
|
661
|
+
line_content: The line containing the match
|
|
662
|
+
file_path: Path to the file being scanned
|
|
663
|
+
file_content: Full file content for context analysis
|
|
664
|
+
|
|
665
|
+
Returns:
|
|
666
|
+
True if random usage is safe/documented, False if potentially insecure
|
|
667
|
+
"""
|
|
668
|
+
# Check if file is a test file
|
|
669
|
+
is_test = any(pattern in file_path.lower() for pattern in ["/test", "test_", "conftest"])
|
|
670
|
+
|
|
671
|
+
# Check for explicit security notes nearby
|
|
672
|
+
lines = file_content.split("\n")
|
|
673
|
+
line_index = None
|
|
674
|
+
for i, line in enumerate(lines):
|
|
675
|
+
if line_content.strip() in line:
|
|
676
|
+
line_index = i
|
|
677
|
+
break
|
|
678
|
+
|
|
679
|
+
if line_index is not None:
|
|
680
|
+
# Check 5 lines before and after for security notes
|
|
681
|
+
context_start = max(0, line_index - 5)
|
|
682
|
+
context_end = min(len(lines), line_index + 5)
|
|
683
|
+
context = "\n".join(lines[context_start:context_end]).lower()
|
|
684
|
+
|
|
685
|
+
# Look for clarifying comments
|
|
686
|
+
safe_indicators = [
|
|
687
|
+
"security note",
|
|
688
|
+
"not cryptographic",
|
|
689
|
+
"not for crypto",
|
|
690
|
+
"test data",
|
|
691
|
+
"demo data",
|
|
692
|
+
"simulation",
|
|
693
|
+
"reproducible",
|
|
694
|
+
"deterministic",
|
|
695
|
+
"fixed seed",
|
|
696
|
+
"not used for security",
|
|
697
|
+
"not used for secrets",
|
|
698
|
+
"not used for tokens",
|
|
699
|
+
]
|
|
700
|
+
|
|
701
|
+
if any(indicator in context for indicator in safe_indicators):
|
|
702
|
+
return True # Documented as safe
|
|
703
|
+
|
|
704
|
+
# Check for common safe random patterns
|
|
705
|
+
line_lower = line_content.lower()
|
|
706
|
+
|
|
707
|
+
# Pattern 1: Fixed seed (reproducible tests)
|
|
708
|
+
if "random.seed(" in line_lower:
|
|
709
|
+
return True # Fixed seed is for reproducibility, not security
|
|
710
|
+
|
|
711
|
+
# Pattern 2: A/B testing, simulations, demos
|
|
712
|
+
safe_contexts = [
|
|
713
|
+
"simulation",
|
|
714
|
+
"demo",
|
|
715
|
+
"a/b test",
|
|
716
|
+
"ab_test",
|
|
717
|
+
"fixture",
|
|
718
|
+
"mock",
|
|
719
|
+
"example",
|
|
720
|
+
"sample",
|
|
721
|
+
]
|
|
722
|
+
if any(context in file_path.lower() for context in safe_contexts):
|
|
723
|
+
return True
|
|
724
|
+
|
|
725
|
+
# If it's a test file without crypto indicators, it's probably safe
|
|
726
|
+
if is_test:
|
|
727
|
+
crypto_indicators = ["password", "secret", "token", "key", "crypto", "auth"]
|
|
728
|
+
if not any(indicator in file_path.lower() for indicator in crypto_indicators):
|
|
729
|
+
return True
|
|
730
|
+
|
|
731
|
+
return False
|
|
732
|
+
|
|
544
733
|
async def _assess(self, input_data: dict, tier: ModelTier) -> tuple[dict, int, int]:
|
|
545
734
|
"""Risk scoring and severity classification.
|
|
546
735
|
|
|
@@ -674,6 +863,7 @@ class SecurityAuditWorkflow(BaseWorkflow):
|
|
|
674
863
|
"""
|
|
675
864
|
try:
|
|
676
865
|
from .security_adapters import _check_crew_available
|
|
866
|
+
|
|
677
867
|
adapters_available = True
|
|
678
868
|
except ImportError:
|
|
679
869
|
adapters_available = False
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
"""Phase 3 Scanner Improvements - AST-based Command Injection Detection
|
|
2
|
+
|
|
3
|
+
This module provides AST-based analysis for detecting actual eval/exec usage
|
|
4
|
+
vs mentions in comments, docstrings, and documentation.
|
|
5
|
+
|
|
6
|
+
Created: 2026-01-26
|
|
7
|
+
Related: docs/SECURITY_PHASE2_COMPLETE.md
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import ast
|
|
11
|
+
import logging
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
logger = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class EvalExecDetector(ast.NodeVisitor):
|
|
19
|
+
"""AST visitor that detects actual eval() and exec() calls.
|
|
20
|
+
|
|
21
|
+
This visitor walks the AST to find real function calls to eval() and exec(),
|
|
22
|
+
distinguishing them from:
|
|
23
|
+
- String literals mentioning eval/exec
|
|
24
|
+
- Comments mentioning eval/exec
|
|
25
|
+
- Docstrings documenting security policies
|
|
26
|
+
- Detection code checking for eval/exec patterns
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
def __init__(self, file_path: str):
|
|
30
|
+
"""Initialize detector.
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
file_path: Path to file being analyzed (for context)
|
|
34
|
+
"""
|
|
35
|
+
self.file_path = file_path
|
|
36
|
+
self.findings: list[dict[str, Any]] = []
|
|
37
|
+
self._in_docstring = False
|
|
38
|
+
self._current_function = None
|
|
39
|
+
|
|
40
|
+
def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
|
|
41
|
+
"""Visit function definition to track context."""
|
|
42
|
+
self._current_function = node.name
|
|
43
|
+
self.generic_visit(node)
|
|
44
|
+
self._current_function = None
|
|
45
|
+
|
|
46
|
+
def visit_Call(self, node: ast.Call) -> None:
|
|
47
|
+
"""Visit function call nodes to detect eval/exec."""
|
|
48
|
+
# Check if this is a call to eval() or exec()
|
|
49
|
+
func_name = None
|
|
50
|
+
|
|
51
|
+
if isinstance(node.func, ast.Name):
|
|
52
|
+
func_name = node.func.id
|
|
53
|
+
elif isinstance(node.func, ast.Attribute):
|
|
54
|
+
# Handle attribute access like obj.exec()
|
|
55
|
+
func_name = node.func.attr
|
|
56
|
+
|
|
57
|
+
if func_name in ("eval", "exec"):
|
|
58
|
+
# Found a real eval/exec call!
|
|
59
|
+
self.findings.append({
|
|
60
|
+
"type": "command_injection",
|
|
61
|
+
"function": func_name,
|
|
62
|
+
"line": node.lineno,
|
|
63
|
+
"col": node.col_offset,
|
|
64
|
+
"context": self._current_function,
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
self.generic_visit(node)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def analyze_file_for_eval_exec(file_path: str | Path) -> list[dict[str, Any]]:
|
|
71
|
+
"""Analyze a Python file for actual eval/exec usage using AST.
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
file_path: Path to Python file to analyze
|
|
75
|
+
|
|
76
|
+
Returns:
|
|
77
|
+
List of findings (actual eval/exec calls)
|
|
78
|
+
|
|
79
|
+
Example:
|
|
80
|
+
>>> findings = analyze_file_for_eval_exec("myfile.py")
|
|
81
|
+
>>> for finding in findings:
|
|
82
|
+
... print(f"{finding['function']} at line {finding['line']}")
|
|
83
|
+
"""
|
|
84
|
+
file_path = Path(file_path)
|
|
85
|
+
|
|
86
|
+
if not file_path.exists():
|
|
87
|
+
return []
|
|
88
|
+
|
|
89
|
+
try:
|
|
90
|
+
content = file_path.read_text(encoding="utf-8", errors="ignore")
|
|
91
|
+
tree = ast.parse(content, filename=str(file_path))
|
|
92
|
+
|
|
93
|
+
detector = EvalExecDetector(str(file_path))
|
|
94
|
+
detector.visit(tree)
|
|
95
|
+
|
|
96
|
+
return detector.findings
|
|
97
|
+
|
|
98
|
+
except SyntaxError as e:
|
|
99
|
+
logger.debug(f"Syntax error parsing {file_path}: {e}")
|
|
100
|
+
return []
|
|
101
|
+
except Exception as e:
|
|
102
|
+
logger.debug(f"Error analyzing {file_path}: {e}")
|
|
103
|
+
return []
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def is_scanner_implementation_file(file_path: str) -> bool:
|
|
107
|
+
"""Check if file is part of security scanner implementation.
|
|
108
|
+
|
|
109
|
+
Scanner files legitimately contain eval/exec patterns for detection
|
|
110
|
+
purposes and should not be flagged.
|
|
111
|
+
|
|
112
|
+
Args:
|
|
113
|
+
file_path: Path to check
|
|
114
|
+
|
|
115
|
+
Returns:
|
|
116
|
+
True if this is a scanner implementation file
|
|
117
|
+
"""
|
|
118
|
+
scanner_indicators = [
|
|
119
|
+
# Scanner implementation files
|
|
120
|
+
"bug_predict",
|
|
121
|
+
"security_audit",
|
|
122
|
+
"security_scan",
|
|
123
|
+
"vulnerability_scan",
|
|
124
|
+
"owasp",
|
|
125
|
+
"secrets_detector",
|
|
126
|
+
"pii_scrubber",
|
|
127
|
+
|
|
128
|
+
# Pattern/rule definition files
|
|
129
|
+
"patterns.py",
|
|
130
|
+
"rules.py",
|
|
131
|
+
"checks.py",
|
|
132
|
+
|
|
133
|
+
# Test files for security scanners
|
|
134
|
+
"test_bug_predict",
|
|
135
|
+
"test_security",
|
|
136
|
+
"test_scanner",
|
|
137
|
+
]
|
|
138
|
+
|
|
139
|
+
path_lower = file_path.lower()
|
|
140
|
+
return any(indicator in path_lower for indicator in scanner_indicators)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def is_in_docstring_or_comment(line_content: str, file_content: str, line_num: int) -> bool:
|
|
144
|
+
"""Enhanced check if line is in docstring or comment.
|
|
145
|
+
|
|
146
|
+
Phase 3 Enhancement: More robust detection of documentation context.
|
|
147
|
+
|
|
148
|
+
Args:
|
|
149
|
+
line_content: The line to check
|
|
150
|
+
file_content: Full file content
|
|
151
|
+
line_num: Line number (1-indexed)
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
True if line is in docstring or comment
|
|
155
|
+
"""
|
|
156
|
+
line = line_content.strip()
|
|
157
|
+
|
|
158
|
+
# Check for comment lines
|
|
159
|
+
if line.startswith("#"):
|
|
160
|
+
return True
|
|
161
|
+
|
|
162
|
+
# Check for inline comments
|
|
163
|
+
if "#" in line_content and line_content.index("#") < line_content.find("eval") if "eval" in line_content else True:
|
|
164
|
+
return True
|
|
165
|
+
|
|
166
|
+
# Parse file as AST to find docstrings
|
|
167
|
+
try:
|
|
168
|
+
tree = ast.parse(file_content)
|
|
169
|
+
|
|
170
|
+
# Get all docstrings
|
|
171
|
+
docstrings = []
|
|
172
|
+
for node in ast.walk(tree):
|
|
173
|
+
docstring = ast.get_docstring(node)
|
|
174
|
+
if docstring:
|
|
175
|
+
docstrings.append(docstring)
|
|
176
|
+
|
|
177
|
+
# Check if any docstring contains this line content
|
|
178
|
+
for docstring in docstrings:
|
|
179
|
+
if line_content.strip() in docstring:
|
|
180
|
+
return True
|
|
181
|
+
|
|
182
|
+
except SyntaxError:
|
|
183
|
+
pass
|
|
184
|
+
|
|
185
|
+
# Check for security policy patterns
|
|
186
|
+
security_patterns = [
|
|
187
|
+
"no eval",
|
|
188
|
+
"no exec",
|
|
189
|
+
"never use eval",
|
|
190
|
+
"never use exec",
|
|
191
|
+
"avoid eval",
|
|
192
|
+
"avoid exec",
|
|
193
|
+
"security:",
|
|
194
|
+
"- no eval",
|
|
195
|
+
"- no exec",
|
|
196
|
+
]
|
|
197
|
+
|
|
198
|
+
line_lower = line.lower()
|
|
199
|
+
if any(pattern in line_lower for pattern in security_patterns):
|
|
200
|
+
return True
|
|
201
|
+
|
|
202
|
+
return False
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def enhanced_command_injection_detection(
|
|
206
|
+
file_path: str,
|
|
207
|
+
original_findings: list[dict[str, Any]]
|
|
208
|
+
) -> list[dict[str, Any]]:
|
|
209
|
+
"""Enhanced command injection detection with AST-based filtering.
|
|
210
|
+
|
|
211
|
+
Phase 3: Uses AST to distinguish actual eval/exec calls from mentions
|
|
212
|
+
in documentation, comments, and scanner implementation.
|
|
213
|
+
|
|
214
|
+
Args:
|
|
215
|
+
file_path: Path to file being analyzed
|
|
216
|
+
original_findings: Findings from regex-based detection
|
|
217
|
+
|
|
218
|
+
Returns:
|
|
219
|
+
Filtered list of actual vulnerabilities (not false positives)
|
|
220
|
+
"""
|
|
221
|
+
# Step 1: Check if this is a scanner implementation file
|
|
222
|
+
if is_scanner_implementation_file(file_path):
|
|
223
|
+
return [] # Scanner files are allowed to mention eval/exec
|
|
224
|
+
|
|
225
|
+
# Step 2: For Python files, use AST-based detection
|
|
226
|
+
if file_path.endswith(".py"):
|
|
227
|
+
try:
|
|
228
|
+
ast_findings = analyze_file_for_eval_exec(file_path)
|
|
229
|
+
|
|
230
|
+
# Convert AST findings to format compatible with original
|
|
231
|
+
filtered = []
|
|
232
|
+
for finding in ast_findings:
|
|
233
|
+
filtered.append({
|
|
234
|
+
"type": "command_injection",
|
|
235
|
+
"file": file_path,
|
|
236
|
+
"line": finding["line"],
|
|
237
|
+
"match": f"{finding['function']}(",
|
|
238
|
+
"severity": "critical",
|
|
239
|
+
"owasp": "A03:2021 Injection",
|
|
240
|
+
"context": finding.get("context", ""),
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
return filtered
|
|
244
|
+
|
|
245
|
+
except Exception as e:
|
|
246
|
+
logger.debug(f"AST analysis failed for {file_path}, falling back to regex: {e}")
|
|
247
|
+
# Fall back to original findings if AST fails
|
|
248
|
+
pass
|
|
249
|
+
|
|
250
|
+
# Step 3: For non-Python files or if AST fails, filter original findings
|
|
251
|
+
try:
|
|
252
|
+
file_content = Path(file_path).read_text(encoding="utf-8", errors="ignore")
|
|
253
|
+
|
|
254
|
+
filtered = []
|
|
255
|
+
for finding in original_findings:
|
|
256
|
+
line_num = finding.get("line", 0)
|
|
257
|
+
lines = file_content.split("\n")
|
|
258
|
+
|
|
259
|
+
if 0 < line_num <= len(lines):
|
|
260
|
+
line_content = lines[line_num - 1]
|
|
261
|
+
|
|
262
|
+
# Skip if in docstring or comment
|
|
263
|
+
if is_in_docstring_or_comment(line_content, file_content, line_num):
|
|
264
|
+
continue
|
|
265
|
+
|
|
266
|
+
filtered.append(finding)
|
|
267
|
+
|
|
268
|
+
return filtered
|
|
269
|
+
|
|
270
|
+
except Exception as e:
|
|
271
|
+
logger.debug(f"Enhanced filtering failed for {file_path}: {e}")
|
|
272
|
+
return original_findings
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
# =============================================================================
|
|
276
|
+
# Integration with SecurityAuditWorkflow
|
|
277
|
+
# =============================================================================
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def apply_phase3_filtering(findings: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
281
|
+
"""Apply Phase 3 AST-based filtering to command injection findings.
|
|
282
|
+
|
|
283
|
+
This is the main entry point for Phase 3 improvements.
|
|
284
|
+
|
|
285
|
+
Args:
|
|
286
|
+
findings: List of command injection findings from regex-based detection
|
|
287
|
+
(should only contain command_injection type)
|
|
288
|
+
|
|
289
|
+
Returns:
|
|
290
|
+
Filtered list with false positives removed
|
|
291
|
+
"""
|
|
292
|
+
if not findings:
|
|
293
|
+
return []
|
|
294
|
+
|
|
295
|
+
# Group findings by file
|
|
296
|
+
by_file: dict[str, list[dict[str, Any]]] = {}
|
|
297
|
+
for finding in findings:
|
|
298
|
+
file_path = finding.get("file", "")
|
|
299
|
+
if file_path not in by_file:
|
|
300
|
+
by_file[file_path] = []
|
|
301
|
+
by_file[file_path].append(finding)
|
|
302
|
+
|
|
303
|
+
# Apply enhanced detection per file
|
|
304
|
+
filtered_findings = []
|
|
305
|
+
for file_path, file_findings in by_file.items():
|
|
306
|
+
enhanced = enhanced_command_injection_detection(file_path, file_findings)
|
|
307
|
+
filtered_findings.extend(enhanced)
|
|
308
|
+
|
|
309
|
+
return filtered_findings
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
if __name__ == "__main__":
|
|
313
|
+
# Test on known files
|
|
314
|
+
test_files = [
|
|
315
|
+
"src/empathy_os/workflows/bug_predict.py",
|
|
316
|
+
"src/empathy_os/orchestration/execution_strategies.py",
|
|
317
|
+
"tests/test_bug_predict_workflow.py",
|
|
318
|
+
]
|
|
319
|
+
|
|
320
|
+
for file in test_files:
|
|
321
|
+
if Path(file).exists():
|
|
322
|
+
findings = analyze_file_for_eval_exec(file)
|
|
323
|
+
print(f"\n{file}:")
|
|
324
|
+
print(f" Actual eval/exec calls: {len(findings)}")
|
|
325
|
+
for f in findings:
|
|
326
|
+
print(f" Line {f['line']}: {f['function']}() in {f.get('context', 'module')}")
|
|
327
|
+
else:
|
|
328
|
+
print(f"\n{file}: Not found")
|