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.
Files changed (94) hide show
  1. app.py +3014 -0
  2. ast_scanner.py +36187 -0
  3. banner.py +146 -0
  4. ci_reporter.py +570 -0
  5. cli.py +2347 -0
  6. config_loader.py +307 -0
  7. cross_file_engine.py +1781 -0
  8. delivery_models.py +145 -0
  9. delivery_repository.py +78 -0
  10. diff_scan.py +127 -0
  11. durable_delivery_sink.py +103 -0
  12. event_bus.py +155 -0
  13. event_publisher.py +345 -0
  14. event_taxonomy.py +313 -0
  15. function_summarizer.py +354 -0
  16. html_auditor.py +274 -0
  17. iac_auditor.py +400 -0
  18. in_app_adapter.py +115 -0
  19. js_scanner.py +895 -0
  20. manifest_parser.py +960 -0
  21. master_rules_bank.py +389 -0
  22. notification_models.py +346 -0
  23. notification_policy.py +309 -0
  24. notification_repository.py +118 -0
  25. notification_routes.py +394 -0
  26. notification_service.py +199 -0
  27. osv_client.py +437 -0
  28. remediation/__init__.py +39 -0
  29. remediation/base.py +102 -0
  30. remediation/contracts.py +196 -0
  31. remediation/cwe1188_binding.py +78 -0
  32. remediation/cwe1336_jinja.py +91 -0
  33. remediation/cwe22_path_traversal.py +348 -0
  34. remediation/cwe295_ssl.py +87 -0
  35. remediation/cwe327_weak_hash.py +78 -0
  36. remediation/cwe377_tempfile.py +77 -0
  37. remediation/cwe489_debug.py +80 -0
  38. remediation/cwe502_yaml.py +89 -0
  39. remediation/cwe614_cookie.py +91 -0
  40. remediation/cwe78_cmdi.py +415 -0
  41. remediation/cwe798_credentials.py +153 -0
  42. remediation/cwe89_sqli.py +425 -0
  43. remediation/cwe916_password_hash.py +131 -0
  44. remediation/cwe95_eval.py +77 -0
  45. remediation/dispatcher.py +114 -0
  46. remediation/orchestrator.py +541 -0
  47. remediation/patch_engine.py +213 -0
  48. remediation/verification.py +253 -0
  49. rule_engine.py +1535 -0
  50. sarif_adapter.py +719 -0
  51. sarif_exporter.py +88 -0
  52. sca_reachability/__init__.py +28 -0
  53. sca_reachability/ast_analyzer.py +366 -0
  54. sca_reachability/call_graph.py +203 -0
  55. sca_reachability/contracts.py +365 -0
  56. sca_reachability/dist_import_map.py +46 -0
  57. sca_reachability/engine.py +603 -0
  58. sca_reachability/resolver.py +169 -0
  59. scanners/__init__.py +70 -0
  60. scanners/container_scanner.py +323 -0
  61. scanners/cve_index.py +521 -0
  62. scanners/iac_scanner.py +328 -0
  63. scanners/sbom.py +183 -0
  64. scanners/sca_scanner.py +667 -0
  65. scanners/secret_scanner.py +345 -0
  66. scanners/yaml_lite.py +217 -0
  67. secret_filters.py +435 -0
  68. secret_scanner.py +943 -0
  69. sqlite_delivery_repository.py +277 -0
  70. sqlite_notification_repository.py +441 -0
  71. ssrf_guard.py +276 -0
  72. staged_scanner.py +492 -0
  73. suppression_resolver.py +282 -0
  74. symbol_indexer.py +424 -0
  75. tcs/__init__.py +1 -0
  76. tcs/analysis/__init__.py +1 -0
  77. tcs/analysis/def_use.py +408 -0
  78. tcs/analysis/function_contracts.py +290 -0
  79. tcs/parallel_analyzer.py +276 -0
  80. tcs/parallel_scanner.py +630 -0
  81. tcs/treesitter_packer.py +1446 -0
  82. tcs_cli.py +2086 -0
  83. tcs_config.py +218 -0
  84. tcs_gui.py +2123 -0
  85. timecs-2.0.0.dist-info/METADATA +142 -0
  86. timecs-2.0.0.dist-info/RECORD +94 -0
  87. timecs-2.0.0.dist-info/WHEEL +5 -0
  88. timecs-2.0.0.dist-info/entry_points.txt +4 -0
  89. timecs-2.0.0.dist-info/licenses/LICENSE +201 -0
  90. timecs-2.0.0.dist-info/top_level.txt +49 -0
  91. version_matcher.py +934 -0
  92. webhook_adapter.py +495 -0
  93. webhook_client.py +483 -0
  94. webhook_formatters.py +245 -0
staged_scanner.py ADDED
@@ -0,0 +1,492 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ TimeCodeSecurity Git-Staged Scanner Module.
4
+
5
+ Provides pre-commit and staged scanning capabilities with full cross-file
6
+ semantic equivalence.
7
+ Reads files directly from Git Index Stage 0 (:0:<path>) without dirty working-tree drift.
8
+ Extracts transitive dependency closures statically via AST imports with zero broad rglob walks.
9
+ Filters findings to report only those affecting staged files (loaded != affected).
10
+ """
11
+
12
+ import sys
13
+ import os
14
+ import ast
15
+ import subprocess
16
+ from pathlib import Path
17
+ from typing import Dict, List, Set, Tuple, Optional, Any
18
+ from benchmark.manifest import ALL_44_CWES
19
+ from rule_engine import get_active_cwe_count
20
+
21
+ # Resilience constants aligned with tcs_cli.py
22
+ MAX_FILE_SIZE_BYTES = 5 * 1024 * 1024 # 5 MB
23
+ MAX_LINE_LENGTH_CHARS = 10000
24
+ BINARY_PREFIX_BYTES = 8192
25
+
26
+
27
+ def get_git_repo_root(cwd: Optional[Path] = None) -> Optional[Path]:
28
+ """
29
+ Resolves the root directory of the current Git repository.
30
+ Returns None if the directory is not inside a git repository or git fails.
31
+ """
32
+ check_dir = cwd if cwd is not None else Path.cwd()
33
+ try:
34
+ res = subprocess.run(
35
+ ["git", "rev-parse", "--show-toplevel"],
36
+ cwd=str(check_dir),
37
+ stdout=subprocess.PIPE,
38
+ stderr=subprocess.PIPE,
39
+ text=True,
40
+ check=False
41
+ )
42
+ if res.returncode == 0 and res.stdout.strip():
43
+ return Path(res.stdout.strip()).resolve()
44
+ except Exception:
45
+ pass
46
+ return None
47
+
48
+
49
+ def get_staged_git_files(
50
+ repo_root: Path
51
+ ) -> Tuple[List[str], List[str], List[Tuple[str, str]], List[str]]:
52
+ """
53
+ Queries git index for staged changes using NUL-delimited status.
54
+ Returns:
55
+ (staged_python_files, deleted_files, renamed_files, non_python_staged)
56
+ All paths are normalized POSIX relative strings.
57
+ """
58
+ try:
59
+ res = subprocess.run(
60
+ ["git", "diff", "--cached", "--name-status", "-z"],
61
+ cwd=str(repo_root),
62
+ stdout=subprocess.PIPE,
63
+ stderr=subprocess.PIPE,
64
+ check=False
65
+ )
66
+ except Exception:
67
+ return [], [], [], []
68
+
69
+ if res.returncode != 0:
70
+ return [], [], [], []
71
+
72
+ raw = res.stdout
73
+ parts = raw.split(b"\x00")
74
+
75
+ staged_py: List[str] = []
76
+ staged_d: List[str] = []
77
+ staged_r: List[Tuple[str, str]] = []
78
+ non_py: List[str] = []
79
+
80
+ idx = 0
81
+ while idx < len(parts):
82
+ token = parts[idx].decode("utf-8", errors="replace").strip()
83
+ if not token:
84
+ idx += 1
85
+ continue
86
+ status = token[0]
87
+ if status in ("A", "C", "M"):
88
+ if idx + 1 < len(parts):
89
+ path = parts[idx + 1].decode("utf-8", errors="replace").replace("\\", "/")
90
+ if path.endswith(".py"):
91
+ staged_py.append(path)
92
+ else:
93
+ non_py.append(path)
94
+ idx += 2
95
+ else:
96
+ idx += 1
97
+ elif status == "D":
98
+ if idx + 1 < len(parts):
99
+ path = parts[idx + 1].decode("utf-8", errors="replace").replace("\\", "/")
100
+ staged_d.append(path)
101
+ idx += 2
102
+ else:
103
+ idx += 1
104
+ elif status.startswith("R"):
105
+ if idx + 2 < len(parts):
106
+ old_p = parts[idx + 1].decode("utf-8", errors="replace").replace("\\", "/")
107
+ new_p = parts[idx + 2].decode("utf-8", errors="replace").replace("\\", "/")
108
+ staged_r.append((old_p, new_p))
109
+ if new_p.endswith(".py"):
110
+ staged_py.append(new_p)
111
+ else:
112
+ non_py.append(new_p)
113
+ idx += 3
114
+ else:
115
+ idx += 1
116
+ else:
117
+ if idx + 1 < len(parts):
118
+ idx += 2
119
+ else:
120
+ idx += 1
121
+
122
+ return staged_py, staged_d, staged_r, non_py
123
+
124
+
125
+ def read_git_index_blob(repo_root: Path, rel_path: str) -> Optional[str]:
126
+ """
127
+ Extracts the EXACT content of a file from Git index stage 0 (:0:<path>).
128
+ Enforces:
129
+ - 5MB size ceiling
130
+ - Binary blob rejection (NUL bytes in prefix)
131
+ - Max line length resilience (10,000 characters)
132
+ Returns decoded UTF-8 string or None if unreadable / skipped.
133
+ """
134
+ posix_path = rel_path.replace("\\", "/")
135
+ try:
136
+ res = subprocess.run(
137
+ ["git", "show", f":0:{posix_path}"],
138
+ cwd=str(repo_root),
139
+ stdout=subprocess.PIPE,
140
+ stderr=subprocess.PIPE,
141
+ check=False
142
+ )
143
+ except Exception:
144
+ return None
145
+
146
+ if res.returncode == 0:
147
+ if b"\x00" in res.stdout[:BINARY_PREFIX_BYTES]:
148
+ return None
149
+ if len(res.stdout) > MAX_FILE_SIZE_BYTES:
150
+ return None
151
+ text = res.stdout.decode("utf-8", errors="replace")
152
+ for line in text.splitlines():
153
+ if len(line) > MAX_LINE_LENGTH_CHARS:
154
+ return None
155
+ return text
156
+ return None
157
+
158
+
159
+ class StaticImportDeclaration:
160
+ def __init__(self, module: str, level: int, names: List[str]):
161
+ self.module = module
162
+ self.level = level
163
+ self.names = names
164
+
165
+ def __repr__(self):
166
+ return f"StaticImport(mod='{self.module}', lvl={self.level}, names={self.names})"
167
+
168
+
169
+ def extract_static_imports(code: str) -> List[StaticImportDeclaration]:
170
+ """
171
+ Extracts all import declarations statically via AST walking.
172
+ Handles 'import x', 'import x as y', 'from x import y', and relative imports.
173
+ """
174
+ imports: List[StaticImportDeclaration] = []
175
+ try:
176
+ tree = ast.parse(code)
177
+ except SyntaxError:
178
+ return imports
179
+
180
+ for node in ast.walk(tree):
181
+ if isinstance(node, ast.Import):
182
+ for alias in node.names:
183
+ imports.append(StaticImportDeclaration(module=alias.name, level=0, names=[alias.asname or alias.name]))
184
+ elif isinstance(node, ast.ImportFrom):
185
+ mod = node.module or ""
186
+ names = [alias.asname or alias.name for alias in node.names]
187
+ imports.append(StaticImportDeclaration(module=mod, level=getattr(node, "level", 0), names=names))
188
+ return imports
189
+
190
+
191
+ def resolve_candidate_module_paths(importing_file_rel: str, imp: StaticImportDeclaration) -> List[str]:
192
+ """
193
+ Computes exact candidate module filepaths for an import declaration WITHOUT any filesystem walking.
194
+ Handles:
195
+ - from db import execute_helper (level 0)
196
+ - import db (level 0)
197
+ - from package.db import execute_helper (level 0)
198
+ - import package.db (level 0)
199
+ - from .db import execute_helper (level 1)
200
+ - from .subpackage.db import execute_helper (level 1)
201
+ - from ..common.db import execute_helper (level 2)
202
+ """
203
+ candidates: List[str] = []
204
+ file_dir = Path(importing_file_rel).parent
205
+
206
+ mod_rel_path = imp.module.replace(".", "/") if imp.module else ""
207
+
208
+ if imp.level > 0:
209
+ # Relative import
210
+ base_dir = file_dir
211
+ for _ in range(imp.level - 1):
212
+ base_dir = base_dir.parent
213
+
214
+ if mod_rel_path:
215
+ cand1 = (base_dir / f"{mod_rel_path}.py").as_posix()
216
+ cand2 = (base_dir / mod_rel_path / "__init__.py").as_posix()
217
+ candidates.extend([cand1, cand2])
218
+ else:
219
+ # e.g., from . import db
220
+ for name in imp.names:
221
+ cand1 = (base_dir / f"{name}.py").as_posix()
222
+ cand2 = (base_dir / name / "__init__.py").as_posix()
223
+ candidates.extend([cand1, cand2])
224
+ else:
225
+ # Absolute import: check relative to importing file dir, and relative to repo root
226
+ search_roots = [file_dir, Path(".")]
227
+
228
+ for root in search_roots:
229
+ cand1 = (root / f"{mod_rel_path}.py").as_posix()
230
+ cand2 = (root / mod_rel_path / "__init__.py").as_posix()
231
+ candidates.extend([cand1, cand2])
232
+
233
+ # If it's a dotted import (e.g. from db.util import helper), also check top component
234
+ top_comp = imp.module.split(".")[0]
235
+ if top_comp != imp.module:
236
+ for root in search_roots:
237
+ candidates.append((root / f"{top_comp}.py").as_posix())
238
+ candidates.append((root / top_comp / "__init__.py").as_posix())
239
+
240
+ # Normalize clean paths (strip leading ./ or .)
241
+ normalized: List[str] = []
242
+ for c in candidates:
243
+ norm = Path(c).as_posix()
244
+ if norm.startswith("./"):
245
+ norm = norm[2:]
246
+ if norm and norm != "." and norm not in normalized:
247
+ normalized.append(norm)
248
+ return normalized
249
+
250
+
251
+ class GitIndexReader:
252
+ """
253
+ Batched Git Index Reader utilizing persistent 'git cat-file --batch'
254
+ to eliminate per-file process spawning overhead on Windows.
255
+ Enforces 5MB size limit, binary prefix rejection, and max line length resilience.
256
+ """
257
+ def __init__(self, repo_root: Path):
258
+ self.repo_root = repo_root
259
+ self._proc: Optional[subprocess.Popen] = None
260
+
261
+ def __enter__(self):
262
+ try:
263
+ self._proc = subprocess.Popen(
264
+ ["git", "cat-file", "--batch"],
265
+ cwd=str(self.repo_root),
266
+ stdin=subprocess.PIPE,
267
+ stdout=subprocess.PIPE,
268
+ stderr=subprocess.PIPE
269
+ )
270
+ except Exception:
271
+ self._proc = None
272
+ return self
273
+
274
+ def __exit__(self, exc_type, exc_val, exc_tb):
275
+ self.close()
276
+
277
+ def close(self):
278
+ if self._proc:
279
+ try:
280
+ if self._proc.stdin:
281
+ self._proc.stdin.close()
282
+ self._proc.wait(timeout=1)
283
+ except Exception:
284
+ try:
285
+ self._proc.kill()
286
+ except Exception:
287
+ pass
288
+ self._proc = None
289
+
290
+ def read_blob(self, rel_path: str) -> Optional[str]:
291
+ if not self._proc or not self._proc.stdin or not self._proc.stdout:
292
+ return read_git_index_blob(self.repo_root, rel_path)
293
+
294
+ posix_path = rel_path.replace("\\", "/")
295
+ query = f":0:{posix_path}\n".encode("utf-8")
296
+ try:
297
+ self._proc.stdin.write(query)
298
+ self._proc.stdin.flush()
299
+ header = self._proc.stdout.readline()
300
+ if not header or b"missing" in header:
301
+ return None
302
+ parts = header.split()
303
+ if len(parts) < 3:
304
+ return None
305
+ size = int(parts[2])
306
+ raw = self._proc.stdout.read(size)
307
+ self._proc.stdout.read(1) # trailing newline delimiter
308
+
309
+ if size > MAX_FILE_SIZE_BYTES:
310
+ return None
311
+ if b"\x00" in raw[:BINARY_PREFIX_BYTES]:
312
+ return None
313
+ text = raw.decode("utf-8", errors="replace")
314
+ for line in text.splitlines():
315
+ if len(line) > MAX_LINE_LENGTH_CHARS:
316
+ return None
317
+ return text
318
+ except Exception:
319
+ return read_git_index_blob(self.repo_root, rel_path)
320
+
321
+
322
+ def build_staged_dependency_closure(
323
+ repo_root: Path,
324
+ staged_py: List[str],
325
+ staged_deleted: Optional[List[str]] = None,
326
+ unstaged_source_policy: str = "index",
327
+ use_batch: bool = True
328
+ ) -> Tuple[Dict[str, str], Set[str], List[str]]:
329
+ """
330
+ Static Dependency Closure:
331
+ 1. Staged root files loaded strictly from Git index stage 0 (using batched git cat-file).
332
+ 2. Transitive candidate paths computed directly from AST import declarations.
333
+ 3. Lookups perform direct single-path index queries (Zero broad rglob!).
334
+ 4. Detects imports referencing files staged for deletion or unresolvable relative imports.
335
+ Returns:
336
+ (files_for_tracker, staged_set, unresolved_dependencies)
337
+ """
338
+ if os.environ.get("TCS_DISABLE_GIT_BATCH") == "1":
339
+ use_batch = False
340
+
341
+ files: Dict[str, str] = {}
342
+ staged_set: Set[str] = set(staged_py)
343
+ unresolved_dependencies: List[str] = []
344
+ staged_deleted_set: Set[str] = {p.replace("\\", "/") for p in staged_deleted} if staged_deleted else set()
345
+
346
+ with GitIndexReader(repo_root) as reader:
347
+ def _get_blob(p: str) -> Optional[str]:
348
+ if use_batch:
349
+ return reader.read_blob(p)
350
+ return read_git_index_blob(repo_root, p)
351
+
352
+ # 1. Load staged files from Git index stage 0
353
+ for sf in staged_py:
354
+ content = _get_blob(sf)
355
+ if content is not None:
356
+ files[sf] = content
357
+
358
+ to_process = list(files.keys())
359
+ visited_files = set(to_process)
360
+
361
+ while to_process:
362
+ curr_file = to_process.pop(0)
363
+ curr_code = files.get(curr_file, "")
364
+ imports = extract_static_imports(curr_code)
365
+
366
+ for imp in imports:
367
+ candidates = resolve_candidate_module_paths(curr_file, imp)
368
+ found_dep = False
369
+
370
+ # Check if candidate matches a module staged for deletion
371
+ deleted_target = None
372
+ if staged_deleted_set:
373
+ for cand in candidates:
374
+ if cand in staged_deleted_set:
375
+ deleted_target = cand
376
+ break
377
+
378
+ if deleted_target is not None:
379
+ unresolved_dependencies.append(
380
+ f"{curr_file}: imports '{imp.module}' which is staged for deletion ({deleted_target})"
381
+ )
382
+ continue
383
+
384
+ for cand in candidates:
385
+ if cand in visited_files:
386
+ found_dep = True
387
+ break
388
+
389
+ dep_content = None
390
+ if unstaged_source_policy == "index":
391
+ dep_content = _get_blob(cand)
392
+ elif unstaged_source_policy == "working_tree":
393
+ cand_path = repo_root / cand
394
+ if cand_path.is_file():
395
+ try:
396
+ dep_content = cand_path.read_text(encoding="utf-8", errors="replace")
397
+ except Exception:
398
+ dep_content = None
399
+
400
+ if dep_content is not None:
401
+ visited_files.add(cand)
402
+ files[cand] = dep_content
403
+ to_process.append(cand)
404
+ found_dep = True
405
+ break
406
+
407
+ if not found_dep and imp.module:
408
+ # Distinguish standard library / third-party packages from local broken relative imports
409
+ if imp.level > 0 or imp.module.startswith("."):
410
+ unresolved_dependencies.append(f"{curr_file}: unresolved relative import '{imp.module}'")
411
+
412
+ return files, staged_set, unresolved_dependencies
413
+
414
+
415
+ def filter_findings_for_staged(findings: List[Dict[str, Any]], staged_set: Set[str]) -> List[Dict[str, Any]]:
416
+ """
417
+ Negative Space Filter:
418
+ Only findings whose ProofGraph touches at least one staged file are reported.
419
+ Enforces invariant: loaded_for_analysis != affected_by_change
420
+ """
421
+ affected: List[Dict[str, Any]] = []
422
+ for f in findings:
423
+ pg = f.get("proof_graph")
424
+ if not pg:
425
+ if f.get("file") in staged_set or f.get("source_file") in staged_set:
426
+ affected.append(f)
427
+ continue
428
+
429
+ nodes = pg.get("nodes", [])
430
+ graph_files = {n.get("file_path") for n in nodes if n.get("file_path")}
431
+ if graph_files.intersection(staged_set):
432
+ affected.append(f)
433
+ return affected
434
+
435
+
436
+ def recompute_summary_metrics(
437
+ original_summary: Dict[str, Any],
438
+ filtered_findings: List[Dict[str, Any]],
439
+ total_files: int,
440
+ lines_scanned: int
441
+ ) -> Dict[str, Any]:
442
+ """
443
+ Recomputes summary counts, score, and risk level after findings have been filtered
444
+ for staged changes.
445
+ """
446
+ active_findings = [f for f in filtered_findings if not f.get("suppressed", False)]
447
+ suppressed_findings = [f for f in filtered_findings if f.get("suppressed", False)]
448
+
449
+ total_vulnerabilities = len(filtered_findings)
450
+ active_vulnerabilities = len(active_findings)
451
+ suppressed_vulnerabilities = len(suppressed_findings)
452
+
453
+ critical_count = sum(1 for f in active_findings if f.get("severity") == "CRITICAL")
454
+ high_count = sum(1 for f in active_findings if f.get("severity") == "HIGH")
455
+ medium_count = sum(1 for f in active_findings if f.get("severity") == "MEDIUM")
456
+ low_count = sum(1 for f in active_findings if f.get("severity") == "LOW")
457
+
458
+ security_score = max(0, 100 - (critical_count * 25 + high_count * 15 + medium_count * 5))
459
+
460
+ if active_vulnerabilities == 0:
461
+ risk_level = "CLEAN"
462
+ risk_message = f"NO VULNERABILITIES DETECTED within current TimeCodeSecurity analysis scope ({get_active_cwe_count()} supported CWE classes)."
463
+ elif critical_count > 0:
464
+ risk_level = "CRITICAL"
465
+ risk_message = "CRITICAL RISK: Arbitrary code execution or high-impact injection detected."
466
+ elif high_count > 0:
467
+ risk_level = "HIGH"
468
+ risk_message = "HIGH RISK: Injection or data traversal vulnerabilities detected."
469
+ elif medium_count > 0:
470
+ risk_level = "MEDIUM"
471
+ risk_message = "MEDIUM RISK: Potential data-flow flaws detected."
472
+ else:
473
+ risk_level = "LOW"
474
+ risk_message = "LOW RISK: Minor security notices."
475
+
476
+ summary = dict(original_summary)
477
+ summary.update({
478
+ "total_files": total_files,
479
+ "lines_scanned": lines_scanned,
480
+ "total_vulnerabilities": total_vulnerabilities,
481
+ "active_vulnerabilities": active_vulnerabilities,
482
+ "suppressed_vulnerabilities": suppressed_vulnerabilities,
483
+ "critical_count": critical_count,
484
+ "high_count": high_count,
485
+ "medium_count": medium_count,
486
+ "low_count": low_count,
487
+ "security_score": security_score,
488
+ "score_label": "Security Health Score",
489
+ "risk_level": risk_level,
490
+ "risk_message": risk_message
491
+ })
492
+ return summary