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
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
|