timecs 2.0.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.
- app.py +3014 -0
- ast_scanner.py +36187 -0
- banner.py +146 -0
- ci_reporter.py +570 -0
- cli.py +2347 -0
- config_loader.py +307 -0
- cross_file_engine.py +1781 -0
- delivery_models.py +145 -0
- delivery_repository.py +78 -0
- diff_scan.py +127 -0
- durable_delivery_sink.py +103 -0
- event_bus.py +155 -0
- event_publisher.py +345 -0
- event_taxonomy.py +313 -0
- function_summarizer.py +354 -0
- html_auditor.py +274 -0
- iac_auditor.py +400 -0
- in_app_adapter.py +115 -0
- js_scanner.py +895 -0
- manifest_parser.py +960 -0
- master_rules_bank.py +389 -0
- notification_models.py +346 -0
- notification_policy.py +309 -0
- notification_repository.py +118 -0
- notification_routes.py +394 -0
- notification_service.py +199 -0
- osv_client.py +437 -0
- remediation/__init__.py +39 -0
- remediation/base.py +102 -0
- remediation/contracts.py +196 -0
- remediation/cwe1188_binding.py +78 -0
- remediation/cwe1336_jinja.py +91 -0
- remediation/cwe22_path_traversal.py +348 -0
- remediation/cwe295_ssl.py +87 -0
- remediation/cwe327_weak_hash.py +78 -0
- remediation/cwe377_tempfile.py +77 -0
- remediation/cwe489_debug.py +80 -0
- remediation/cwe502_yaml.py +89 -0
- remediation/cwe614_cookie.py +91 -0
- remediation/cwe78_cmdi.py +415 -0
- remediation/cwe798_credentials.py +153 -0
- remediation/cwe89_sqli.py +425 -0
- remediation/cwe916_password_hash.py +131 -0
- remediation/cwe95_eval.py +77 -0
- remediation/dispatcher.py +114 -0
- remediation/orchestrator.py +541 -0
- remediation/patch_engine.py +213 -0
- remediation/verification.py +253 -0
- rule_engine.py +1535 -0
- sarif_adapter.py +719 -0
- sarif_exporter.py +88 -0
- sca_reachability/__init__.py +28 -0
- sca_reachability/ast_analyzer.py +366 -0
- sca_reachability/call_graph.py +203 -0
- sca_reachability/contracts.py +365 -0
- sca_reachability/dist_import_map.py +46 -0
- sca_reachability/engine.py +603 -0
- sca_reachability/resolver.py +169 -0
- scanners/__init__.py +70 -0
- scanners/container_scanner.py +323 -0
- scanners/cve_index.py +521 -0
- scanners/iac_scanner.py +328 -0
- scanners/sbom.py +183 -0
- scanners/sca_scanner.py +667 -0
- scanners/secret_scanner.py +345 -0
- scanners/yaml_lite.py +217 -0
- secret_filters.py +435 -0
- secret_scanner.py +943 -0
- sqlite_delivery_repository.py +277 -0
- sqlite_notification_repository.py +441 -0
- ssrf_guard.py +276 -0
- staged_scanner.py +492 -0
- suppression_resolver.py +282 -0
- symbol_indexer.py +424 -0
- tcs/__init__.py +1 -0
- tcs/analysis/__init__.py +1 -0
- tcs/analysis/def_use.py +408 -0
- tcs/analysis/function_contracts.py +290 -0
- tcs/parallel_analyzer.py +276 -0
- tcs/parallel_scanner.py +630 -0
- tcs/treesitter_packer.py +1446 -0
- tcs_cli.py +2086 -0
- tcs_config.py +218 -0
- tcs_gui.py +2123 -0
- timecs-2.0.0.dist-info/METADATA +142 -0
- timecs-2.0.0.dist-info/RECORD +94 -0
- timecs-2.0.0.dist-info/WHEEL +5 -0
- timecs-2.0.0.dist-info/entry_points.txt +4 -0
- timecs-2.0.0.dist-info/licenses/LICENSE +201 -0
- timecs-2.0.0.dist-info/top_level.txt +49 -0
- version_matcher.py +934 -0
- webhook_adapter.py +495 -0
- webhook_client.py +483 -0
- webhook_formatters.py +245 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"""
|
|
2
|
+
TimeCodeSecurity - Vector D Deterministic AST Remediation Engine.
|
|
3
|
+
Orchestrates:
|
|
4
|
+
1. Finding intake
|
|
5
|
+
2. AST parse & rule dispatch
|
|
6
|
+
3. AST transformation & source splicing
|
|
7
|
+
4. Syntax & compilation validation gate (D-1)
|
|
8
|
+
5. Vector B closed-loop re-scan verification gate (D-2)
|
|
9
|
+
6. Unified diff generation
|
|
10
|
+
7. Frozen RemediationRecord production
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
14
|
+
import ast
|
|
15
|
+
import difflib
|
|
16
|
+
|
|
17
|
+
from remediation.contracts import (
|
|
18
|
+
PatchStatus,
|
|
19
|
+
RemediationRule,
|
|
20
|
+
RemediationRecord,
|
|
21
|
+
)
|
|
22
|
+
from remediation.dispatcher import RuleDispatcher
|
|
23
|
+
from remediation.verification import RemediationVerifier
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class RemediationEngine:
|
|
27
|
+
"""
|
|
28
|
+
Production deterministic AST remediation engine.
|
|
29
|
+
Applies AST-guided vulnerability patches and enforces strict verification gates.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(
|
|
33
|
+
self,
|
|
34
|
+
dispatcher: Optional[RuleDispatcher] = None,
|
|
35
|
+
verifier: Optional[RemediationVerifier] = None
|
|
36
|
+
):
|
|
37
|
+
self.dispatcher = dispatcher or RuleDispatcher()
|
|
38
|
+
self.verifier = verifier or RemediationVerifier()
|
|
39
|
+
|
|
40
|
+
def remediate(
|
|
41
|
+
self,
|
|
42
|
+
finding: Dict[str, Any],
|
|
43
|
+
source_code: str,
|
|
44
|
+
file_path: Optional[str] = None
|
|
45
|
+
) -> RemediationRecord:
|
|
46
|
+
"""
|
|
47
|
+
Executes the end-to-end deterministic remediation pipeline for a given finding.
|
|
48
|
+
Never mutates the input source_code in place.
|
|
49
|
+
"""
|
|
50
|
+
finding_id = str(finding.get("id", "TimeCodeSecurity-REM-000"))
|
|
51
|
+
cwe = str(finding.get("cwe", "UNKNOWN_CWE"))
|
|
52
|
+
line_number = int(finding.get("line_number", 0))
|
|
53
|
+
original_file = str(file_path or finding.get("file", "source.py"))
|
|
54
|
+
original_snippet = str(finding.get("code_snippet", ""))
|
|
55
|
+
|
|
56
|
+
# Rule mapping fallback for reporting non-SUCCESS states
|
|
57
|
+
default_rule = RemediationRule.SQLI_PARAMETERIZE
|
|
58
|
+
if cwe == "CWE-78":
|
|
59
|
+
default_rule = RemediationRule.CMD_INJECTION_SPLIT
|
|
60
|
+
elif cwe == "CWE-22":
|
|
61
|
+
default_rule = RemediationRule.PATH_TRAVERSAL_RESOLVE
|
|
62
|
+
elif cwe in ("CWE-614", "CWE-1275", "CWE-668"):
|
|
63
|
+
default_rule = RemediationRule.COOKIE_SECURE_FLAGS
|
|
64
|
+
elif cwe in ("CWE-1336", "CWE-116"):
|
|
65
|
+
default_rule = RemediationRule.TEMPLATE_AUTOESCAPE
|
|
66
|
+
elif cwe in ("CWE-327", "CWE-328"):
|
|
67
|
+
default_rule = RemediationRule.WEAK_HASH_REPLACE
|
|
68
|
+
elif cwe in ("CWE-916", "CWE-759"):
|
|
69
|
+
default_rule = RemediationRule.PASSWORD_HASH_KDF
|
|
70
|
+
elif cwe == "CWE-502":
|
|
71
|
+
default_rule = RemediationRule.YAML_SAFE_LOADER
|
|
72
|
+
elif cwe == "CWE-798":
|
|
73
|
+
default_rule = RemediationRule.CREDENTIAL_ENVIRON_GET
|
|
74
|
+
elif cwe == "CWE-489":
|
|
75
|
+
default_rule = RemediationRule.DEBUG_FLAG_DISABLE
|
|
76
|
+
elif cwe == "CWE-295":
|
|
77
|
+
default_rule = RemediationRule.SSL_VERIFY_ENABLE
|
|
78
|
+
elif cwe == "CWE-377":
|
|
79
|
+
default_rule = RemediationRule.TEMPFILE_SECURE
|
|
80
|
+
elif cwe == "CWE-95":
|
|
81
|
+
default_rule = RemediationRule.EVAL_LITERAL_REPLACE
|
|
82
|
+
elif cwe == "CWE-1188":
|
|
83
|
+
default_rule = RemediationRule.NETWORK_BINDING_LOCALHOST
|
|
84
|
+
|
|
85
|
+
# 1. Syntax check on original input source
|
|
86
|
+
try:
|
|
87
|
+
tree = ast.parse(source_code, filename=original_file)
|
|
88
|
+
except (SyntaxError, ValueError) as se:
|
|
89
|
+
return RemediationRecord(
|
|
90
|
+
finding_id=finding_id,
|
|
91
|
+
cwe=cwe,
|
|
92
|
+
remediation_rule=default_rule,
|
|
93
|
+
original_file=original_file,
|
|
94
|
+
line_number=line_number,
|
|
95
|
+
patch_status=PatchStatus.FAILED_SYNTAX,
|
|
96
|
+
original_code_snippet=original_snippet,
|
|
97
|
+
patched_code_snippet="",
|
|
98
|
+
unified_diff="",
|
|
99
|
+
verification_passed=False,
|
|
100
|
+
limitations=(f"Original source code contains syntax error: {se}",)
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
# 2. Rule Dispatcher
|
|
104
|
+
transformer = self.dispatcher.get_transformer(cwe=cwe)
|
|
105
|
+
if not transformer:
|
|
106
|
+
return RemediationRecord(
|
|
107
|
+
finding_id=finding_id,
|
|
108
|
+
cwe=cwe,
|
|
109
|
+
remediation_rule=default_rule,
|
|
110
|
+
original_file=original_file,
|
|
111
|
+
line_number=line_number,
|
|
112
|
+
patch_status=PatchStatus.UNSUPPORTED_CWE,
|
|
113
|
+
original_code_snippet=original_snippet,
|
|
114
|
+
patched_code_snippet="",
|
|
115
|
+
unified_diff="",
|
|
116
|
+
verification_passed=False,
|
|
117
|
+
limitations=(f"Automated remediation for {cwe} is not supported in Phase 2",)
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
# 3. AST Transformation & Structural Validation
|
|
121
|
+
status, patched_source, patched_snippet, limitations = transformer.transform(tree, finding, source_code)
|
|
122
|
+
if status != PatchStatus.SUCCESS or patched_source is None:
|
|
123
|
+
return RemediationRecord(
|
|
124
|
+
finding_id=finding_id,
|
|
125
|
+
cwe=cwe,
|
|
126
|
+
remediation_rule=transformer.rule,
|
|
127
|
+
original_file=original_file,
|
|
128
|
+
line_number=line_number,
|
|
129
|
+
patch_status=status,
|
|
130
|
+
original_code_snippet=original_snippet,
|
|
131
|
+
patched_code_snippet=patched_snippet or "",
|
|
132
|
+
unified_diff="",
|
|
133
|
+
verification_passed=False,
|
|
134
|
+
limitations=limitations or ("Code construct or SQL pattern is unsupported for automated parameterization",)
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
# 5. Syntax Gate (D-1)
|
|
138
|
+
try:
|
|
139
|
+
patched_tree = ast.parse(patched_source, filename=original_file)
|
|
140
|
+
compile(patched_tree, original_file, "exec")
|
|
141
|
+
except (SyntaxError, ValueError) as se:
|
|
142
|
+
return RemediationRecord(
|
|
143
|
+
finding_id=finding_id,
|
|
144
|
+
cwe=cwe,
|
|
145
|
+
remediation_rule=transformer.rule,
|
|
146
|
+
original_file=original_file,
|
|
147
|
+
line_number=line_number,
|
|
148
|
+
patch_status=PatchStatus.FAILED_SYNTAX,
|
|
149
|
+
original_code_snippet=original_snippet,
|
|
150
|
+
patched_code_snippet=patched_snippet or "",
|
|
151
|
+
unified_diff="",
|
|
152
|
+
verification_passed=False,
|
|
153
|
+
limitations=(f"Patched code failed syntax or compile validation: {se}",)
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
# 6. Vector B Closed-Loop Re-Scan Verification Gate (D-2)
|
|
157
|
+
verify_passed, verify_err = self.verifier.verify(
|
|
158
|
+
original_file=original_file,
|
|
159
|
+
original_source=source_code,
|
|
160
|
+
patched_source=patched_source,
|
|
161
|
+
target_finding=finding
|
|
162
|
+
)
|
|
163
|
+
if not verify_passed:
|
|
164
|
+
return RemediationRecord(
|
|
165
|
+
finding_id=finding_id,
|
|
166
|
+
cwe=cwe,
|
|
167
|
+
remediation_rule=transformer.rule,
|
|
168
|
+
original_file=original_file,
|
|
169
|
+
line_number=line_number,
|
|
170
|
+
patch_status=PatchStatus.FAILED_VERIFICATION,
|
|
171
|
+
original_code_snippet=original_snippet,
|
|
172
|
+
patched_code_snippet=patched_snippet or "",
|
|
173
|
+
unified_diff="",
|
|
174
|
+
verification_passed=False,
|
|
175
|
+
limitations=(f"Re-scan verification failed: {verify_err}",)
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
# 7. Unified Diff Generation
|
|
179
|
+
is_crlf = "\r\n" in source_code
|
|
180
|
+
eol = "\r\n" if is_crlf else "\n"
|
|
181
|
+
|
|
182
|
+
orig_lines = [l if (l.endswith("\n") or l.endswith("\r\n")) else l + eol for l in source_code.splitlines(keepends=True)]
|
|
183
|
+
patched_lines = [l if (l.endswith("\n") or l.endswith("\r\n")) else l + eol for l in patched_source.splitlines(keepends=True)]
|
|
184
|
+
norm_file = original_file.replace("\\", "/")
|
|
185
|
+
diff_lines = list(difflib.unified_diff(
|
|
186
|
+
orig_lines,
|
|
187
|
+
patched_lines,
|
|
188
|
+
fromfile=f"a/{norm_file}",
|
|
189
|
+
tofile=f"b/{norm_file}"
|
|
190
|
+
))
|
|
191
|
+
cleaned_diff_lines = []
|
|
192
|
+
for dl in diff_lines:
|
|
193
|
+
if not (dl.endswith("\n") or dl.endswith("\r\n")):
|
|
194
|
+
cleaned_diff_lines.append(dl + eol)
|
|
195
|
+
else:
|
|
196
|
+
cleaned_diff_lines.append(dl)
|
|
197
|
+
diff_str = "".join(cleaned_diff_lines)
|
|
198
|
+
|
|
199
|
+
# 8. Success Record
|
|
200
|
+
return RemediationRecord(
|
|
201
|
+
finding_id=finding_id,
|
|
202
|
+
cwe=cwe,
|
|
203
|
+
remediation_rule=transformer.rule,
|
|
204
|
+
original_file=original_file,
|
|
205
|
+
line_number=line_number,
|
|
206
|
+
patch_status=PatchStatus.SUCCESS,
|
|
207
|
+
original_code_snippet=original_snippet,
|
|
208
|
+
patched_code_snippet=patched_snippet or "",
|
|
209
|
+
unified_diff=diff_str,
|
|
210
|
+
verification_passed=True,
|
|
211
|
+
limitations=limitations,
|
|
212
|
+
patched_source=patched_source
|
|
213
|
+
)
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"""
|
|
2
|
+
TimeCodeSecurity - Vector D Closed-Loop Re-Scan Verification Engine.
|
|
3
|
+
Performs authoritative re-scan of patched code using the Vector B SAST engine (execute_tcs_scan)
|
|
4
|
+
to prove that:
|
|
5
|
+
1. The specific target vulnerability finding disappears.
|
|
6
|
+
2. All unrelated findings remain intact (negative space check).
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
10
|
+
from ast_scanner import TaintTracker
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def scan_source_for_verification(file_path: str, source_code: str) -> List[Dict[str, Any]]:
|
|
14
|
+
"""
|
|
15
|
+
Authoritative SAST re-scan invocation using core Vector B TaintTracker directly.
|
|
16
|
+
Completely decoupled from CLI presentation layers.
|
|
17
|
+
"""
|
|
18
|
+
tracker = TaintTracker(files={file_path: source_code}, audit_all=True)
|
|
19
|
+
sources, sinks, edges = tracker.analyze()
|
|
20
|
+
|
|
21
|
+
sinks_by_id = {s.id: s for s in sinks}
|
|
22
|
+
sources_by_id = {s.id: s for s in sources}
|
|
23
|
+
|
|
24
|
+
findings = []
|
|
25
|
+
seen = set()
|
|
26
|
+
vuln_idx = 1
|
|
27
|
+
target_lines = source_code.splitlines()
|
|
28
|
+
|
|
29
|
+
for edge in edges:
|
|
30
|
+
sink = sinks_by_id.get(edge.target_id)
|
|
31
|
+
if not sink:
|
|
32
|
+
continue
|
|
33
|
+
|
|
34
|
+
cwe = sink.metadata.get("cwe", "UNKNOWN_CWE")
|
|
35
|
+
sink_loc = sink.location
|
|
36
|
+
dedup_key = (sink_loc.file, sink_loc.line_start, cwe)
|
|
37
|
+
if dedup_key in seen:
|
|
38
|
+
continue
|
|
39
|
+
seen.add(dedup_key)
|
|
40
|
+
|
|
41
|
+
code_snippet = ""
|
|
42
|
+
if 1 <= sink_loc.line_start <= len(target_lines):
|
|
43
|
+
code_snippet = target_lines[sink_loc.line_start - 1].strip()
|
|
44
|
+
|
|
45
|
+
source_node = sources_by_id.get(edge.source_id)
|
|
46
|
+
source_loc = source_node.location if source_node else None
|
|
47
|
+
|
|
48
|
+
pg_dict = edge.proof_graph.to_dict() if edge.proof_graph else None
|
|
49
|
+
|
|
50
|
+
findings.append({
|
|
51
|
+
"id": f"TimeCodeSecurity-VULN-{vuln_idx:03d}",
|
|
52
|
+
"cwe": cwe,
|
|
53
|
+
"category": sink.metadata.get("sink_type", "UNKNOWN_VULNERABILITY"),
|
|
54
|
+
"file": sink_loc.file,
|
|
55
|
+
"line_number": sink_loc.line_start,
|
|
56
|
+
"sink_symbol": sink.symbol,
|
|
57
|
+
"source_symbol": source_node.symbol if source_node else "USER_INPUT",
|
|
58
|
+
"source_line": source_loc.line_start if source_loc else None,
|
|
59
|
+
"code_snippet": code_snippet,
|
|
60
|
+
"proof_graph": pg_dict,
|
|
61
|
+
"confidence": float(edge.confidence),
|
|
62
|
+
})
|
|
63
|
+
vuln_idx += 1
|
|
64
|
+
|
|
65
|
+
return findings
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def roundtrip_validate(file_path: str, patched_source: str) -> Tuple[bool, str, Dict[str, Any]]:
|
|
69
|
+
"""
|
|
70
|
+
Strict teacher gate for a completed fix: the whole patched file must re-scan clean.
|
|
71
|
+
|
|
72
|
+
`RemediationVerifier.verify` proves two narrower things - the target finding disappeared and
|
|
73
|
+
no pre-existing finding was lost - so a patch that trades one flaw for another (a CWE-117
|
|
74
|
+
"sanitized" variable the sanitizer registry does not recognise, or a route that drops
|
|
75
|
+
`@csrf_exempt` but still has no CSRF guard) still passes it. Zero active findings is the same
|
|
76
|
+
condition the published score is built from (app.py derives security_score purely from active
|
|
77
|
+
findings, so 0 active <=> 100/100), which makes this the honest backing for a "confirmed
|
|
78
|
+
secure" claim.
|
|
79
|
+
|
|
80
|
+
Returns (is_valid, rejection_reason, stats).
|
|
81
|
+
"""
|
|
82
|
+
findings = scan_source_for_verification(file_path, patched_source)
|
|
83
|
+
residual = [f for f in findings if not f.get("suppressed", False)]
|
|
84
|
+
stats: Dict[str, Any] = {
|
|
85
|
+
"total_flaws": len(findings),
|
|
86
|
+
"active_flaws": len(residual),
|
|
87
|
+
"suppressed_flaws": len(findings) - len(residual),
|
|
88
|
+
"residual": [
|
|
89
|
+
f"{f.get('cwe')} at line {f.get('line_number')} ({f.get('code_snippet', '')})"
|
|
90
|
+
for f in residual
|
|
91
|
+
],
|
|
92
|
+
}
|
|
93
|
+
if residual:
|
|
94
|
+
return False, "Residual flaw(s) after roundtrip re-scan: " + "; ".join(stats["residual"]), stats
|
|
95
|
+
return True, "", stats
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class RemediationVerifier:
|
|
99
|
+
"""
|
|
100
|
+
Closed-loop re-scan verifier using Vector B engine.
|
|
101
|
+
Ensures zero false-fixes and mathematically proves vulnerability remediation.
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
def _extract_graph_signature(self, finding: Dict[str, Any]) -> Dict[str, Any]:
|
|
105
|
+
"""Extracts durable semantic graph and sink signatures from a finding."""
|
|
106
|
+
cwe = finding.get("cwe")
|
|
107
|
+
sink_symbol = finding.get("sink_symbol")
|
|
108
|
+
pg = finding.get("proof_graph") or {}
|
|
109
|
+
nodes = pg.get("nodes", [])
|
|
110
|
+
|
|
111
|
+
sink_node = next((n for n in nodes if n.get("node_type") == "SINK"), None)
|
|
112
|
+
flow_nodes = [n for n in nodes if n.get("node_type") != "SINK"]
|
|
113
|
+
|
|
114
|
+
scope_id = ""
|
|
115
|
+
if sink_node:
|
|
116
|
+
scope_id = str(sink_node.get("scope_id", ""))
|
|
117
|
+
elif nodes:
|
|
118
|
+
scope_id = str(nodes[0].get("scope_id", ""))
|
|
119
|
+
|
|
120
|
+
if ":" in scope_id:
|
|
121
|
+
scope_sig = ":".join(scope_id.split(":")[1:])
|
|
122
|
+
else:
|
|
123
|
+
scope_sig = scope_id
|
|
124
|
+
|
|
125
|
+
# Normalize upstream expression snippets to capture the exact vulnerability pattern
|
|
126
|
+
upstream_exprs = tuple(
|
|
127
|
+
str(n.get("expression_snippet", "")).strip() for n in flow_nodes if n.get("expression_snippet")
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
"cwe": str(cwe),
|
|
132
|
+
"sink_symbol": str(sink_symbol) if sink_symbol else None,
|
|
133
|
+
"scope_sig": scope_sig,
|
|
134
|
+
"upstream_exprs": upstream_exprs,
|
|
135
|
+
"code_snippet": str(finding.get("code_snippet", "")).strip(),
|
|
136
|
+
"line_number": int(finding.get("line_number", 0)),
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
def is_matching_finding(self, finding_a: Dict[str, Any], finding_b: Dict[str, Any]) -> bool:
|
|
140
|
+
"""
|
|
141
|
+
Determines if two findings refer to the exact same vulnerability instance
|
|
142
|
+
using durable semantic identity (file + sink semantic identity + CWE + graph/sink signature).
|
|
143
|
+
"""
|
|
144
|
+
sig_a = self._extract_graph_signature(finding_a)
|
|
145
|
+
sig_b = self._extract_graph_signature(finding_b)
|
|
146
|
+
|
|
147
|
+
# 1. CWE must match
|
|
148
|
+
if sig_a["cwe"] != sig_b["cwe"]:
|
|
149
|
+
return False
|
|
150
|
+
|
|
151
|
+
# 2. Sink symbol must match if both present
|
|
152
|
+
if sig_a["sink_symbol"] and sig_b["sink_symbol"]:
|
|
153
|
+
if sig_a["sink_symbol"] != sig_b["sink_symbol"]:
|
|
154
|
+
return False
|
|
155
|
+
|
|
156
|
+
# 3. Enclosing scope must match if both present
|
|
157
|
+
if sig_a["scope_sig"] and sig_b["scope_sig"]:
|
|
158
|
+
if sig_a["scope_sig"] != sig_b["scope_sig"]:
|
|
159
|
+
return False
|
|
160
|
+
|
|
161
|
+
# 4. Graph signature: upstream vulnerability flow expressions
|
|
162
|
+
# If both findings have upstream flow nodes, they must share the vulnerable dataflow expression
|
|
163
|
+
if sig_a["upstream_exprs"] and sig_b["upstream_exprs"]:
|
|
164
|
+
has_matching_flow = any(
|
|
165
|
+
ea == eb for ea in sig_a["upstream_exprs"] for eb in sig_b["upstream_exprs"]
|
|
166
|
+
)
|
|
167
|
+
return has_matching_flow
|
|
168
|
+
|
|
169
|
+
# 5. Fallback if proof graph is not present (supporting line provenance and snippet)
|
|
170
|
+
if sig_a["code_snippet"] and sig_b["code_snippet"] and sig_a["code_snippet"] == sig_b["code_snippet"]:
|
|
171
|
+
return True
|
|
172
|
+
|
|
173
|
+
line_a = sig_a["line_number"]
|
|
174
|
+
line_b = sig_b["line_number"]
|
|
175
|
+
if line_a and line_b:
|
|
176
|
+
return abs(line_a - line_b) <= 2
|
|
177
|
+
|
|
178
|
+
return True
|
|
179
|
+
|
|
180
|
+
def _is_same_sink_instance(
|
|
181
|
+
self,
|
|
182
|
+
finding_a: Dict[str, Any],
|
|
183
|
+
finding_b: Dict[str, Any]
|
|
184
|
+
) -> bool:
|
|
185
|
+
"""
|
|
186
|
+
True when two findings point at the identical source statement, i.e. the
|
|
187
|
+
same underlying sink reported under consolidated CWE ids. Snippet equality
|
|
188
|
+
is checked by containment because scanner and CLI snippets may differ only
|
|
189
|
+
by surrounding statement text.
|
|
190
|
+
"""
|
|
191
|
+
line_a = int(finding_a.get("line_number", 0) or finding_a.get("line", 0) or 0)
|
|
192
|
+
line_b = int(finding_b.get("line_number", 0) or finding_b.get("line", 0) or 0)
|
|
193
|
+
if not line_a or line_a != line_b:
|
|
194
|
+
return False
|
|
195
|
+
|
|
196
|
+
snippet_a = str(finding_a.get("code_snippet", "")).strip()
|
|
197
|
+
snippet_b = str(finding_b.get("code_snippet", "")).strip()
|
|
198
|
+
if snippet_a and snippet_b:
|
|
199
|
+
return snippet_a in snippet_b or snippet_b in snippet_a
|
|
200
|
+
|
|
201
|
+
return True
|
|
202
|
+
|
|
203
|
+
def verify(
|
|
204
|
+
self,
|
|
205
|
+
original_file: str,
|
|
206
|
+
original_source: str,
|
|
207
|
+
patched_source: str,
|
|
208
|
+
target_finding: Dict[str, Any]
|
|
209
|
+
) -> Tuple[bool, Optional[str]]:
|
|
210
|
+
"""
|
|
211
|
+
Executes Vector B closed-loop re-scan verification.
|
|
212
|
+
Returns:
|
|
213
|
+
(verification_passed, error_message_if_any)
|
|
214
|
+
"""
|
|
215
|
+
# 1. Authoritative scan on original source via core TaintTracker
|
|
216
|
+
orig_findings = scan_source_for_verification(original_file, original_source)
|
|
217
|
+
|
|
218
|
+
# 2. Authoritative scan on patched source via core TaintTracker
|
|
219
|
+
patched_findings = scan_source_for_verification(original_file, patched_source)
|
|
220
|
+
|
|
221
|
+
# 3. Verify that the target finding disappears in patched source
|
|
222
|
+
for pf in patched_findings:
|
|
223
|
+
if self.is_matching_finding(pf, target_finding):
|
|
224
|
+
return (
|
|
225
|
+
False,
|
|
226
|
+
f"Target finding {target_finding.get('cwe')} at line {target_finding.get('line_number')} "
|
|
227
|
+
f"is still detected by Vector B after patching"
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
# 4. Negative Space Check: Verify that all unrelated findings remain intact.
|
|
231
|
+
# Findings that share the target's exact sink (same line + same code snippet)
|
|
232
|
+
# are the same vulnerability reported under consolidated CWE ids, so clearing
|
|
233
|
+
# them alongside the target is expected, not a regression.
|
|
234
|
+
unrelated_orig = [
|
|
235
|
+
f for f in orig_findings
|
|
236
|
+
if not self.is_matching_finding(f, target_finding)
|
|
237
|
+
and not self._is_same_sink_instance(f, target_finding)
|
|
238
|
+
]
|
|
239
|
+
for uf in unrelated_orig:
|
|
240
|
+
matched_in_patched = any(
|
|
241
|
+
self.is_matching_finding(pf, uf) or (
|
|
242
|
+
pf.get("cwe") == uf.get("cwe") and pf.get("sink_symbol") == uf.get("sink_symbol")
|
|
243
|
+
)
|
|
244
|
+
for pf in patched_findings
|
|
245
|
+
)
|
|
246
|
+
if not matched_in_patched:
|
|
247
|
+
return (
|
|
248
|
+
False,
|
|
249
|
+
f"Unrelated finding {uf.get('id', 'N/A')} ({uf.get('cwe')} at line {uf.get('line_number')}) "
|
|
250
|
+
f"was unexpectedly removed or modified by the patch"
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
return (True, None)
|