empathy-framework 4.7.1__py3-none-any.whl → 4.9.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.9.0.dist-info}/METADATA +65 -2
- {empathy_framework-4.7.1.dist-info → empathy_framework-4.9.0.dist-info}/RECORD +69 -59
- {empathy_framework-4.7.1.dist-info → empathy_framework-4.9.0.dist-info}/WHEEL +1 -1
- {empathy_framework-4.7.1.dist-info → empathy_framework-4.9.0.dist-info}/entry_points.txt +2 -1
- {empathy_framework-4.7.1.dist-info → empathy_framework-4.9.0.dist-info}/top_level.txt +0 -1
- empathy_os/__init__.py +2 -0
- 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} +0 -26
- empathy_os/cli_minimal.py +662 -0
- empathy_os/cli_router.py +384 -0
- empathy_os/cli_unified.py +13 -2
- empathy_os/memory/short_term.py +146 -414
- empathy_os/memory/types.py +441 -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 +27 -176
- 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 +48 -16
- empathy_os/project_index/scanner_parallel.py +291 -0
- empathy_os/workflow_commands.py +9 -9
- empathy_os/workflows/__init__.py +31 -2
- empathy_os/workflows/base.py +295 -317
- empathy_os/workflows/bug_predict.py +10 -2
- 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 +512 -0
- empathy_os/workflows/perf_audit.py +129 -23
- empathy_os/workflows/routing.py +163 -0
- empathy_os/workflows/secure_release.py +1 -0
- empathy_os/workflows/security_audit.py +1 -0
- empathy_os/workflows/security_audit_phase3.py +352 -0
- empathy_os/workflows/telemetry_mixin.py +269 -0
- empathy_os/workflows/test_gen.py +7 -7
- empathy_os/dashboard/__init__.py +0 -15
- empathy_os/dashboard/server.py +0 -941
- empathy_os/vscode_bridge 2.py +0 -173
- empathy_os/workflows/progressive/README 2.md +0 -454
- empathy_os/workflows/progressive/__init__ 2.py +0 -92
- empathy_os/workflows/progressive/cli 2.py +0 -242
- empathy_os/workflows/progressive/core 2.py +0 -488
- empathy_os/workflows/progressive/orchestrator 2.py +0 -701
- empathy_os/workflows/progressive/reports 2.py +0 -528
- empathy_os/workflows/progressive/telemetry 2.py +0 -280
- empathy_os/workflows/progressive/test_gen 2.py +0 -514
- empathy_os/workflows/progressive/workflow 2.py +0 -628
- 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.9.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""Tier routing strategies for workflow execution.
|
|
2
|
+
|
|
3
|
+
Provides pluggable routing algorithms to determine which model tier
|
|
4
|
+
should handle each workflow stage.
|
|
5
|
+
|
|
6
|
+
Copyright 2025 Smart-AI-Memory
|
|
7
|
+
Licensed under Fair Source License 0.9
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from abc import ABC, abstractmethod
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
from typing import TYPE_CHECKING
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
from empathy_os.workflows.base import ModelTier
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class RoutingContext:
|
|
22
|
+
"""Context information for routing decisions.
|
|
23
|
+
|
|
24
|
+
Attributes:
|
|
25
|
+
task_type: Type of task (analyze, generate, review, etc.)
|
|
26
|
+
input_size: Estimated input tokens
|
|
27
|
+
complexity: Task complexity (simple, moderate, complex)
|
|
28
|
+
budget_remaining: Remaining budget in USD
|
|
29
|
+
latency_sensitivity: Latency requirements (low, medium, high)
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
task_type: str
|
|
33
|
+
input_size: int
|
|
34
|
+
complexity: str # "simple" | "moderate" | "complex"
|
|
35
|
+
budget_remaining: float
|
|
36
|
+
latency_sensitivity: str # "low" | "medium" | "high"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class TierRoutingStrategy(ABC):
|
|
40
|
+
"""Abstract base class for tier routing strategies.
|
|
41
|
+
|
|
42
|
+
Subclasses implement different routing algorithms:
|
|
43
|
+
- CostOptimizedRouting: Minimize cost
|
|
44
|
+
- PerformanceOptimizedRouting: Minimize latency
|
|
45
|
+
- BalancedRouting: Balance cost and performance
|
|
46
|
+
- HybridRouting: User-configured tier mappings
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
@abstractmethod
|
|
50
|
+
def route(self, context: RoutingContext) -> ModelTier:
|
|
51
|
+
"""Route task to appropriate tier.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
context: Routing context with task information
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
ModelTier to use for this task
|
|
58
|
+
"""
|
|
59
|
+
pass
|
|
60
|
+
|
|
61
|
+
@abstractmethod
|
|
62
|
+
def can_fallback(self, tier: ModelTier) -> bool:
|
|
63
|
+
"""Whether fallback to cheaper tier is allowed.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
tier: The tier that failed or exceeded budget
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
True if fallback is allowed, False otherwise
|
|
70
|
+
"""
|
|
71
|
+
pass
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class CostOptimizedRouting(TierRoutingStrategy):
|
|
75
|
+
"""Route to cheapest tier that can handle the task.
|
|
76
|
+
|
|
77
|
+
Default strategy. Prioritizes cost savings over speed.
|
|
78
|
+
|
|
79
|
+
Example:
|
|
80
|
+
>>> strategy = CostOptimizedRouting()
|
|
81
|
+
>>> tier = strategy.route(context) # CHEAP for simple tasks
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
def route(self, context: RoutingContext) -> ModelTier:
|
|
85
|
+
"""Route based on task complexity, preferring cheaper tiers."""
|
|
86
|
+
from empathy_os.workflows.base import ModelTier
|
|
87
|
+
|
|
88
|
+
if context.complexity == "simple":
|
|
89
|
+
return ModelTier.CHEAP
|
|
90
|
+
elif context.complexity == "complex":
|
|
91
|
+
return ModelTier.PREMIUM
|
|
92
|
+
return ModelTier.CAPABLE
|
|
93
|
+
|
|
94
|
+
def can_fallback(self, tier: ModelTier) -> bool:
|
|
95
|
+
"""Allow fallback except for CHEAP tier."""
|
|
96
|
+
from empathy_os.workflows.base import ModelTier
|
|
97
|
+
|
|
98
|
+
return tier != ModelTier.CHEAP
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class PerformanceOptimizedRouting(TierRoutingStrategy):
|
|
102
|
+
"""Route to fastest tier regardless of cost.
|
|
103
|
+
|
|
104
|
+
Use for latency-sensitive workflows like interactive tools.
|
|
105
|
+
|
|
106
|
+
Example:
|
|
107
|
+
>>> strategy = PerformanceOptimizedRouting()
|
|
108
|
+
>>> tier = strategy.route(context) # PREMIUM for high latency sensitivity
|
|
109
|
+
"""
|
|
110
|
+
|
|
111
|
+
def route(self, context: RoutingContext) -> ModelTier:
|
|
112
|
+
"""Route based on latency requirements."""
|
|
113
|
+
from empathy_os.workflows.base import ModelTier
|
|
114
|
+
|
|
115
|
+
if context.latency_sensitivity == "high":
|
|
116
|
+
return ModelTier.PREMIUM
|
|
117
|
+
return ModelTier.CAPABLE
|
|
118
|
+
|
|
119
|
+
def can_fallback(self, tier: ModelTier) -> bool:
|
|
120
|
+
"""Never fallback - performance is priority."""
|
|
121
|
+
return False
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class BalancedRouting(TierRoutingStrategy):
|
|
125
|
+
"""Balance cost and performance with budget awareness.
|
|
126
|
+
|
|
127
|
+
Adjusts tier selection based on remaining budget and task complexity.
|
|
128
|
+
|
|
129
|
+
Example:
|
|
130
|
+
>>> strategy = BalancedRouting(total_budget=50.0)
|
|
131
|
+
>>> tier = strategy.route(context) # Adapts based on budget
|
|
132
|
+
"""
|
|
133
|
+
|
|
134
|
+
def __init__(self, total_budget: float):
|
|
135
|
+
"""Initialize with total budget.
|
|
136
|
+
|
|
137
|
+
Args:
|
|
138
|
+
total_budget: Total budget in USD for this workflow execution
|
|
139
|
+
"""
|
|
140
|
+
self.total_budget = total_budget
|
|
141
|
+
|
|
142
|
+
def route(self, context: RoutingContext) -> ModelTier:
|
|
143
|
+
"""Route based on budget ratio and complexity."""
|
|
144
|
+
from empathy_os.workflows.base import ModelTier
|
|
145
|
+
|
|
146
|
+
budget_ratio = context.budget_remaining / self.total_budget
|
|
147
|
+
|
|
148
|
+
# Low budget - use cheap tier
|
|
149
|
+
if budget_ratio < 0.2:
|
|
150
|
+
return ModelTier.CHEAP
|
|
151
|
+
|
|
152
|
+
# High budget + complex task - use premium
|
|
153
|
+
if budget_ratio > 0.7 and context.complexity == "complex":
|
|
154
|
+
return ModelTier.PREMIUM
|
|
155
|
+
|
|
156
|
+
# Default to capable
|
|
157
|
+
return ModelTier.CAPABLE
|
|
158
|
+
|
|
159
|
+
def can_fallback(self, tier: ModelTier) -> bool:
|
|
160
|
+
"""Allow fallback when budget-constrained."""
|
|
161
|
+
return True
|
|
162
|
+
|
|
163
|
+
|
|
@@ -0,0 +1,352 @@
|
|
|
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 for eval/exec only
|
|
226
|
+
# Keep subprocess findings from regex detection
|
|
227
|
+
if file_path.endswith(".py"):
|
|
228
|
+
try:
|
|
229
|
+
# Separate eval/exec findings from subprocess/os.system findings
|
|
230
|
+
# Eval/exec findings will be replaced with AST-based findings
|
|
231
|
+
# Subprocess/os.system findings will be kept from regex detection
|
|
232
|
+
eval_exec_findings = []
|
|
233
|
+
subprocess_findings = []
|
|
234
|
+
|
|
235
|
+
for finding in original_findings:
|
|
236
|
+
match_text = finding.get("match", "").lower()
|
|
237
|
+
if "eval" in match_text or "exec" in match_text:
|
|
238
|
+
eval_exec_findings.append(finding)
|
|
239
|
+
else:
|
|
240
|
+
# subprocess, os.system, or other command injection patterns
|
|
241
|
+
subprocess_findings.append(finding)
|
|
242
|
+
|
|
243
|
+
# Use AST to validate eval/exec findings (reduces false positives)
|
|
244
|
+
ast_findings = analyze_file_for_eval_exec(file_path)
|
|
245
|
+
|
|
246
|
+
# Check if this is a test file (downgrade severity)
|
|
247
|
+
from .security_audit import TEST_FILE_PATTERNS
|
|
248
|
+
is_test_file = any(re.search(pat, file_path) for pat in TEST_FILE_PATTERNS)
|
|
249
|
+
|
|
250
|
+
# Convert AST findings to format compatible with original
|
|
251
|
+
filtered = []
|
|
252
|
+
for finding in ast_findings:
|
|
253
|
+
filtered.append({
|
|
254
|
+
"type": "command_injection",
|
|
255
|
+
"file": file_path,
|
|
256
|
+
"line": finding["line"],
|
|
257
|
+
"match": f"{finding['function']}(",
|
|
258
|
+
"severity": "low" if is_test_file else "critical",
|
|
259
|
+
"owasp": "A03:2021 Injection",
|
|
260
|
+
"context": finding.get("context", ""),
|
|
261
|
+
"is_test": is_test_file,
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
# Keep subprocess/os.system findings (not filtered by AST)
|
|
265
|
+
filtered.extend(subprocess_findings)
|
|
266
|
+
|
|
267
|
+
return filtered
|
|
268
|
+
|
|
269
|
+
except Exception as e:
|
|
270
|
+
logger.debug(f"AST analysis failed for {file_path}, falling back to regex: {e}")
|
|
271
|
+
# Fall back to original findings if AST fails
|
|
272
|
+
pass
|
|
273
|
+
|
|
274
|
+
# Step 3: For non-Python files or if AST fails, filter original findings
|
|
275
|
+
try:
|
|
276
|
+
file_content = Path(file_path).read_text(encoding="utf-8", errors="ignore")
|
|
277
|
+
|
|
278
|
+
filtered = []
|
|
279
|
+
for finding in original_findings:
|
|
280
|
+
line_num = finding.get("line", 0)
|
|
281
|
+
lines = file_content.split("\n")
|
|
282
|
+
|
|
283
|
+
if 0 < line_num <= len(lines):
|
|
284
|
+
line_content = lines[line_num - 1]
|
|
285
|
+
|
|
286
|
+
# Skip if in docstring or comment
|
|
287
|
+
if is_in_docstring_or_comment(line_content, file_content, line_num):
|
|
288
|
+
continue
|
|
289
|
+
|
|
290
|
+
filtered.append(finding)
|
|
291
|
+
|
|
292
|
+
return filtered
|
|
293
|
+
|
|
294
|
+
except Exception as e:
|
|
295
|
+
logger.debug(f"Enhanced filtering failed for {file_path}: {e}")
|
|
296
|
+
return original_findings
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
# =============================================================================
|
|
300
|
+
# Integration with SecurityAuditWorkflow
|
|
301
|
+
# =============================================================================
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def apply_phase3_filtering(findings: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
305
|
+
"""Apply Phase 3 AST-based filtering to command injection findings.
|
|
306
|
+
|
|
307
|
+
This is the main entry point for Phase 3 improvements.
|
|
308
|
+
|
|
309
|
+
Args:
|
|
310
|
+
findings: List of command injection findings from regex-based detection
|
|
311
|
+
(should only contain command_injection type)
|
|
312
|
+
|
|
313
|
+
Returns:
|
|
314
|
+
Filtered list with false positives removed
|
|
315
|
+
"""
|
|
316
|
+
if not findings:
|
|
317
|
+
return []
|
|
318
|
+
|
|
319
|
+
# Group findings by file
|
|
320
|
+
by_file: dict[str, list[dict[str, Any]]] = {}
|
|
321
|
+
for finding in findings:
|
|
322
|
+
file_path = finding.get("file", "")
|
|
323
|
+
if file_path not in by_file:
|
|
324
|
+
by_file[file_path] = []
|
|
325
|
+
by_file[file_path].append(finding)
|
|
326
|
+
|
|
327
|
+
# Apply enhanced detection per file
|
|
328
|
+
filtered_findings = []
|
|
329
|
+
for file_path, file_findings in by_file.items():
|
|
330
|
+
enhanced = enhanced_command_injection_detection(file_path, file_findings)
|
|
331
|
+
filtered_findings.extend(enhanced)
|
|
332
|
+
|
|
333
|
+
return filtered_findings
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
if __name__ == "__main__":
|
|
337
|
+
# Test on known files
|
|
338
|
+
test_files = [
|
|
339
|
+
"src/empathy_os/workflows/bug_predict.py",
|
|
340
|
+
"src/empathy_os/orchestration/execution_strategies.py",
|
|
341
|
+
"tests/test_bug_predict_workflow.py",
|
|
342
|
+
]
|
|
343
|
+
|
|
344
|
+
for file in test_files:
|
|
345
|
+
if Path(file).exists():
|
|
346
|
+
findings = analyze_file_for_eval_exec(file)
|
|
347
|
+
print(f"\n{file}:")
|
|
348
|
+
print(f" Actual eval/exec calls: {len(findings)}")
|
|
349
|
+
for f in findings:
|
|
350
|
+
print(f" Line {f['line']}: {f['function']}() in {f.get('context', 'module')}")
|
|
351
|
+
else:
|
|
352
|
+
print(f"\n{file}: Not found")
|