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
config_loader.py ADDED
@@ -0,0 +1,307 @@
1
+ """
2
+ TimeCodeSecurity Configuration Loader.
3
+ Loads, validates, and manages .tcs.yml configuration objects with strict schema
4
+ validation, deterministic error handling, and immutable configuration structures.
5
+ """
6
+
7
+ from __future__ import annotations
8
+ import os
9
+ from pathlib import Path
10
+ from dataclasses import dataclass
11
+ from typing import Optional, Set, FrozenSet, Dict, Any, List, Union
12
+
13
+ SUPPORTED_RULES: FrozenSet[str] = frozenset({
14
+ "CWE-22",
15
+ "CWE-78",
16
+ "CWE-89",
17
+ "CWE-95",
18
+ "CWE-502",
19
+ "CWE-1336"
20
+ })
21
+
22
+ SUPPORTED_FORMATS: FrozenSet[str] = frozenset({"table", "json", "sarif"})
23
+ SUPPORTED_VERSION: int = 1
24
+
25
+
26
+ class ConfigValidationError(Exception):
27
+ """Raised when .tcs.yml fails syntax, structure, or policy validation."""
28
+ pass
29
+
30
+
31
+ @dataclass(frozen=True)
32
+ class RulesConfig:
33
+ enabled: FrozenSet[str]
34
+ disabled: FrozenSet[str]
35
+
36
+
37
+ @dataclass(frozen=True)
38
+ class ScanConfig:
39
+ sca: bool
40
+ secrets: bool
41
+
42
+
43
+ @dataclass(frozen=True)
44
+ class SuppressionConfig:
45
+ enabled: bool
46
+
47
+
48
+ @dataclass(frozen=True)
49
+ class OutputConfig:
50
+ format: str
51
+
52
+
53
+ @dataclass(frozen=True)
54
+ class TCSConfig:
55
+ version: int
56
+ rules: RulesConfig
57
+ scan: ScanConfig
58
+ suppression: SuppressionConfig
59
+ output: OutputConfig
60
+ config_path: Optional[str] = None
61
+
62
+ @property
63
+ def effective_rules(self) -> FrozenSet[str]:
64
+ """
65
+ Derives the effective set of active rules:
66
+ - If enabled is empty, start with all known SUPPORTED_RULES.
67
+ - If enabled is populated, start only with enabled rules.
68
+ - Subtract disabled rules.
69
+ """
70
+ base_set = self.rules.enabled if self.rules.enabled else SUPPORTED_RULES
71
+ return base_set - self.rules.disabled
72
+
73
+ def is_rule_enabled(self, cwe_id: str) -> bool:
74
+ """Check whether a specific CWE is active under this configuration.
75
+
76
+ An unconfigured scan runs the FULL engine taxonomy, which is what `cli.py scan`
77
+ emits; SUPPORTED_RULES only ever narrows what a .tcs.yml may ask for. Once the
78
+ file names rules explicitly, the allow/deny lists gate the scan as before.
79
+ """
80
+ norm_cwe = cwe_id.strip().upper()
81
+ if not self.rules.enabled and not self.rules.disabled:
82
+ return True
83
+ return norm_cwe in self.effective_rules
84
+
85
+
86
+ DEFAULT_CONFIG: TCSConfig = TCSConfig(
87
+ version=SUPPORTED_VERSION,
88
+ rules=RulesConfig(
89
+ enabled=frozenset(),
90
+ disabled=frozenset()
91
+ ),
92
+ scan=ScanConfig(
93
+ sca=False,
94
+ secrets=False
95
+ ),
96
+ suppression=SuppressionConfig(
97
+ enabled=True
98
+ ),
99
+ output=OutputConfig(
100
+ format="table"
101
+ ),
102
+ config_path=None
103
+ )
104
+
105
+
106
+ def parse_config(raw_yaml: str, config_path: Optional[str] = None) -> TCSConfig:
107
+ """
108
+ Parses and strictly validates a raw YAML configuration string against schema v1.
109
+ """
110
+ try:
111
+ import yaml
112
+ except ImportError:
113
+ raise ConfigValidationError(
114
+ "PyYAML is required to parse configuration files, but is not installed. "
115
+ "Install it via 'pip install -r requirements.txt' or 'pip install pyyaml'."
116
+ )
117
+
118
+ try:
119
+ data = yaml.safe_load(raw_yaml)
120
+ except Exception as e:
121
+ raise ConfigValidationError(f"Malformed YAML in configuration: {e}")
122
+
123
+ if data is None:
124
+ raise ConfigValidationError("Configuration root must be a mapping/dictionary, got empty document.")
125
+
126
+ if not isinstance(data, dict):
127
+ raise ConfigValidationError(f"Configuration root must be a mapping/dictionary, got {type(data).__name__}.")
128
+
129
+ # 1. Top-Level Keys
130
+ allowed_top_keys = {"version", "rules", "scan", "suppression", "output"}
131
+ unknown_top_keys = set(data.keys()) - allowed_top_keys
132
+ if unknown_top_keys:
133
+ raise ConfigValidationError(f"Unknown top-level configuration key(s): {sorted(unknown_top_keys)}")
134
+
135
+ # 2. Version
136
+ if "version" not in data:
137
+ raise ConfigValidationError("Missing required 'version' field in configuration.")
138
+
139
+ ver = data["version"]
140
+ if type(ver) is not int or ver != SUPPORTED_VERSION:
141
+ raise ConfigValidationError(
142
+ f"Unsupported configuration version: {ver!r}. Only version {SUPPORTED_VERSION} is supported."
143
+ )
144
+
145
+ # 3. Rules Section
146
+ raw_rules = data.get("rules", {})
147
+ if raw_rules is None:
148
+ raw_rules = {}
149
+ elif not isinstance(raw_rules, dict):
150
+ raise ConfigValidationError(f"'rules' section must be a mapping, got {type(raw_rules).__name__}.")
151
+
152
+ allowed_rules_keys = {"enabled", "disabled"}
153
+ unknown_rules_keys = set(raw_rules.keys()) - allowed_rules_keys
154
+ if unknown_rules_keys:
155
+ raise ConfigValidationError(f"Unknown key(s) in 'rules' section: {sorted(unknown_rules_keys)}")
156
+
157
+ # 3a. Enabled rules
158
+ raw_enabled = raw_rules.get("enabled", [])
159
+ if raw_enabled is None:
160
+ raw_enabled = []
161
+ elif not isinstance(raw_enabled, list):
162
+ raise ConfigValidationError(f"'rules.enabled' must be a list, got {type(raw_enabled).__name__}.")
163
+
164
+ enabled_set: Set[str] = set()
165
+ for item in raw_enabled:
166
+ if not isinstance(item, str):
167
+ raise ConfigValidationError(
168
+ f"Invalid rule identifier type in 'rules.enabled': expected string, got {type(item).__name__}."
169
+ )
170
+ norm_id = item.strip().upper()
171
+ if norm_id not in SUPPORTED_RULES:
172
+ raise ConfigValidationError(
173
+ f"Unknown rule identifier in 'rules.enabled': '{item}'. Supported rules: {sorted(SUPPORTED_RULES)}"
174
+ )
175
+ enabled_set.add(norm_id)
176
+
177
+ # 3b. Disabled rules
178
+ raw_disabled = raw_rules.get("disabled", [])
179
+ if raw_disabled is None:
180
+ raw_disabled = []
181
+ elif not isinstance(raw_disabled, list):
182
+ raise ConfigValidationError(f"'rules.disabled' must be a list, got {type(raw_disabled).__name__}.")
183
+
184
+ disabled_set: Set[str] = set()
185
+ for item in raw_disabled:
186
+ if not isinstance(item, str):
187
+ raise ConfigValidationError(
188
+ f"Invalid rule identifier type in 'rules.disabled': expected string, got {type(item).__name__}."
189
+ )
190
+ norm_id = item.strip().upper()
191
+ if norm_id not in SUPPORTED_RULES:
192
+ raise ConfigValidationError(
193
+ f"Unknown rule identifier in 'rules.disabled': '{item}'. Supported rules: {sorted(SUPPORTED_RULES)}"
194
+ )
195
+ disabled_set.add(norm_id)
196
+
197
+ # 3c. Contradiction Check
198
+ overlap = enabled_set & disabled_set
199
+ if overlap:
200
+ raise ConfigValidationError(
201
+ f"Contradictory rule configuration: Rule(s) {sorted(overlap)} cannot be present in both 'enabled' and 'disabled'."
202
+ )
203
+
204
+ # 4. Scan Section
205
+ raw_scan = data.get("scan", {})
206
+ if raw_scan is None:
207
+ raw_scan = {}
208
+ elif not isinstance(raw_scan, dict):
209
+ raise ConfigValidationError(f"'scan' section must be a mapping, got {type(raw_scan).__name__}.")
210
+
211
+ allowed_scan_keys = {"sca", "secrets"}
212
+ unknown_scan_keys = set(raw_scan.keys()) - allowed_scan_keys
213
+ if unknown_scan_keys:
214
+ raise ConfigValidationError(f"Unknown key(s) in 'scan' section: {sorted(unknown_scan_keys)}")
215
+
216
+ sca_val = raw_scan.get("sca", False)
217
+ if not isinstance(sca_val, bool):
218
+ raise ConfigValidationError(f"'scan.sca' must be a boolean (true/false), got {type(sca_val).__name__}.")
219
+
220
+ secrets_val = raw_scan.get("secrets", False)
221
+ if not isinstance(secrets_val, bool):
222
+ raise ConfigValidationError(f"'scan.secrets' must be a boolean (true/false), got {type(secrets_val).__name__}.")
223
+
224
+ # 5. Suppression Section
225
+ raw_supp = data.get("suppression", {})
226
+ if raw_supp is None:
227
+ raw_supp = {}
228
+ elif not isinstance(raw_supp, dict):
229
+ raise ConfigValidationError(f"'suppression' section must be a mapping, got {type(raw_supp).__name__}.")
230
+
231
+ allowed_supp_keys = {"enabled"}
232
+ unknown_supp_keys = set(raw_supp.keys()) - allowed_supp_keys
233
+ if unknown_supp_keys:
234
+ raise ConfigValidationError(f"Unknown key(s) in 'suppression' section: {sorted(unknown_supp_keys)}")
235
+
236
+ supp_val = raw_supp.get("enabled", True)
237
+ if not isinstance(supp_val, bool):
238
+ raise ConfigValidationError(f"'suppression.enabled' must be a boolean (true/false), got {type(supp_val).__name__}.")
239
+
240
+ # 6. Output Section
241
+ raw_output = data.get("output", {})
242
+ if raw_output is None:
243
+ raw_output = {}
244
+ elif not isinstance(raw_output, dict):
245
+ raise ConfigValidationError(f"'output' section must be a mapping, got {type(raw_output).__name__}.")
246
+
247
+ allowed_output_keys = {"format"}
248
+ unknown_output_keys = set(raw_output.keys()) - allowed_output_keys
249
+ if unknown_output_keys:
250
+ raise ConfigValidationError(f"Unknown key(s) in 'output' section: {sorted(unknown_output_keys)}")
251
+
252
+ fmt_val = raw_output.get("format", "table")
253
+ if not isinstance(fmt_val, str) or fmt_val.lower() not in SUPPORTED_FORMATS:
254
+ raise ConfigValidationError(
255
+ f"Invalid 'output.format': {fmt_val!r}. Supported formats are: {sorted(SUPPORTED_FORMATS)}"
256
+ )
257
+
258
+ return TCSConfig(
259
+ version=ver,
260
+ rules=RulesConfig(
261
+ enabled=frozenset(enabled_set),
262
+ disabled=frozenset(disabled_set)
263
+ ),
264
+ scan=ScanConfig(
265
+ sca=sca_val,
266
+ secrets=secrets_val
267
+ ),
268
+ suppression=SuppressionConfig(
269
+ enabled=supp_val
270
+ ),
271
+ output=OutputConfig(
272
+ format=fmt_val.lower()
273
+ ),
274
+ config_path=config_path
275
+ )
276
+
277
+
278
+ def load_config(
279
+ path: Optional[Union[str, Path]] = None,
280
+ base_dir: Optional[Union[str, Path]] = None
281
+ ) -> TCSConfig:
282
+ """
283
+ Loads and validates configuration.
284
+ - If `path` is specified: loads that exact file or raises ConfigValidationError if missing.
285
+ - If `path` is None: looks for `./.tcs.yml` under `base_dir` (or cwd). Returns DEFAULT_CONFIG if absent.
286
+ """
287
+ if path is not None:
288
+ p = Path(os.path.expanduser(str(path))).resolve()
289
+ if not p.exists() or not p.is_file():
290
+ raise ConfigValidationError(f"Configuration file not found: {path}")
291
+ try:
292
+ content = p.read_text(encoding="utf-8")
293
+ except Exception as e:
294
+ raise ConfigValidationError(f"Unable to read configuration file '{path}': {e}")
295
+ return parse_config(content, config_path=str(p))
296
+
297
+ # Default discovery: look for .tcs.yml in base_dir or cwd
298
+ base = Path(base_dir).resolve() if base_dir is not None else Path.cwd().resolve()
299
+ default_config_file = base / ".tcs.yml"
300
+ if default_config_file.exists() and default_config_file.is_file():
301
+ try:
302
+ content = default_config_file.read_text(encoding="utf-8")
303
+ except Exception as e:
304
+ raise ConfigValidationError(f"Unable to read configuration file '{default_config_file}': {e}")
305
+ return parse_config(content, config_path=str(default_config_file))
306
+
307
+ return DEFAULT_CONFIG