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
suppression_resolver.py
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Inline Suppression Resolver for TimeCodeSecurity.
|
|
3
|
+
Deterministically parses in-source suppression directives and applies
|
|
4
|
+
non-destructive suppression flags to AST scan findings.
|
|
5
|
+
|
|
6
|
+
Supported syntax:
|
|
7
|
+
# tcs:ignore CWE-89
|
|
8
|
+
# tcs:ignore CWE-89: justification text
|
|
9
|
+
# tcs:ignore cwe-89
|
|
10
|
+
# tcs:ignore all
|
|
11
|
+
# tcs:ignore all: justification text
|
|
12
|
+
# tcs:ignore CWE-89, CWE-78: justification text
|
|
13
|
+
# tcs: ignore[CWE-611]
|
|
14
|
+
# tcs: ignore[ALL]: justification text
|
|
15
|
+
|
|
16
|
+
The bracketed form is scoped to the one line it sits on: it silences a finding only when the
|
|
17
|
+
finding's own line is that line and only for the CWE names inside the brackets. The older
|
|
18
|
+
unbracketed form keeps its historical scope (same line, or the comment block immediately above
|
|
19
|
+
a statement), because `app.py` and `tcs_cli.py` already ship that behaviour.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
import io
|
|
23
|
+
import re
|
|
24
|
+
import tokenize
|
|
25
|
+
from typing import Dict, List, Optional, Set, Any, Tuple
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# Regex pattern to identify tcs:ignore comment directives
|
|
29
|
+
TCS_IGNORE_PATTERN = re.compile(r'#\s*tcs\s*:\s*ignore\b\s*(.*)', re.IGNORECASE)
|
|
30
|
+
|
|
31
|
+
# Bracketed, exact-line form: `# tcs: ignore[CWE-611]` or `# tcs: ignore[ALL]`.
|
|
32
|
+
TCS_IGNORE_BRACKET_PATTERN = re.compile(
|
|
33
|
+
r'#\s*tcs\s*:\s*ignore\s*\[([^\]]*)\]\s*(.*)', re.IGNORECASE)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def normalize_cwe_target(val: str) -> Optional[str]:
|
|
37
|
+
"""
|
|
38
|
+
Normalizes a directive target string (e.g. 'cwe-89', '89', 'all') to standard format.
|
|
39
|
+
Returns: 'CWE-<NUM>', 'ALL', or None if unrecognized.
|
|
40
|
+
"""
|
|
41
|
+
v = val.strip().upper()
|
|
42
|
+
if not v:
|
|
43
|
+
return None
|
|
44
|
+
if v == "ALL":
|
|
45
|
+
return "ALL"
|
|
46
|
+
if v.startswith("CWE-"):
|
|
47
|
+
num_part = v[4:].strip()
|
|
48
|
+
if num_part.isdigit():
|
|
49
|
+
return f"CWE-{num_part}"
|
|
50
|
+
return v
|
|
51
|
+
if v.startswith("CWE"):
|
|
52
|
+
num_part = v[3:].strip("-")
|
|
53
|
+
if num_part.isdigit():
|
|
54
|
+
return f"CWE-{num_part}"
|
|
55
|
+
if v.isdigit():
|
|
56
|
+
return f"CWE-{v}"
|
|
57
|
+
return None
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def parse_directive_payload(payload: str) -> Optional[Dict[str, Any]]:
|
|
61
|
+
"""
|
|
62
|
+
Parses the text following '# tcs:ignore'.
|
|
63
|
+
Returns a dict with 'targets' (Set[str]) and 'justification' (Optional[str]),
|
|
64
|
+
or None if malformed.
|
|
65
|
+
"""
|
|
66
|
+
payload = payload.strip()
|
|
67
|
+
if not payload:
|
|
68
|
+
return None
|
|
69
|
+
|
|
70
|
+
justification: Optional[str] = None
|
|
71
|
+
if ":" in payload:
|
|
72
|
+
targets_part, just_part = payload.split(":", 1)
|
|
73
|
+
justification = just_part.strip() or None
|
|
74
|
+
else:
|
|
75
|
+
targets_part = payload
|
|
76
|
+
|
|
77
|
+
targets_part = targets_part.strip()
|
|
78
|
+
if not targets_part:
|
|
79
|
+
return None
|
|
80
|
+
|
|
81
|
+
raw_items = [t.strip() for t in re.split(r'[,;]+', targets_part) if t.strip()]
|
|
82
|
+
if not raw_items:
|
|
83
|
+
return None
|
|
84
|
+
|
|
85
|
+
normalized_targets: Set[str] = set()
|
|
86
|
+
for item in raw_items:
|
|
87
|
+
norm = normalize_cwe_target(item)
|
|
88
|
+
if norm:
|
|
89
|
+
normalized_targets.add(norm)
|
|
90
|
+
|
|
91
|
+
if not normalized_targets:
|
|
92
|
+
return None
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
"targets": normalized_targets,
|
|
96
|
+
"justification": justification
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def extract_file_directives(source_code: str) -> Dict[int, List[Dict[str, Any]]]:
|
|
101
|
+
"""
|
|
102
|
+
Extracts all valid tcs:ignore directives from source_code indexed by 1-based line number.
|
|
103
|
+
Uses Python tokenize to ignore string literals that happen to contain '# tcs:ignore'.
|
|
104
|
+
Falls back to line-by-line scanning if tokenization encounters a syntax error.
|
|
105
|
+
"""
|
|
106
|
+
directives_by_line: Dict[int, List[Dict[str, Any]]] = {}
|
|
107
|
+
comments: List[Tuple[int, str]] = []
|
|
108
|
+
|
|
109
|
+
try:
|
|
110
|
+
reader = io.StringIO(source_code).readline
|
|
111
|
+
tokens = tokenize.generate_tokens(reader)
|
|
112
|
+
for tok in tokens:
|
|
113
|
+
if tok.type == tokenize.COMMENT:
|
|
114
|
+
comments.append((tok.start[0], tok.string))
|
|
115
|
+
except Exception:
|
|
116
|
+
for idx, line in enumerate(source_code.splitlines(), start=1):
|
|
117
|
+
if "#" in line:
|
|
118
|
+
comments.append((idx, line[line.index("#"):]))
|
|
119
|
+
|
|
120
|
+
for line_num, comment_str in comments:
|
|
121
|
+
bracket = TCS_IGNORE_BRACKET_PATTERN.search(comment_str)
|
|
122
|
+
if bracket:
|
|
123
|
+
parsed = parse_directive_payload(bracket.group(1))
|
|
124
|
+
if parsed:
|
|
125
|
+
tail = bracket.group(2).strip()
|
|
126
|
+
if tail.startswith(":") and tail[1:].strip():
|
|
127
|
+
parsed["justification"] = tail[1:].strip()
|
|
128
|
+
parsed["line"] = line_num
|
|
129
|
+
parsed["exact_line"] = True
|
|
130
|
+
directives_by_line.setdefault(line_num, []).append(parsed)
|
|
131
|
+
continue
|
|
132
|
+
m = TCS_IGNORE_PATTERN.search(comment_str)
|
|
133
|
+
if not m:
|
|
134
|
+
continue
|
|
135
|
+
parsed = parse_directive_payload(m.group(1))
|
|
136
|
+
if parsed:
|
|
137
|
+
parsed["line"] = line_num
|
|
138
|
+
parsed["exact_line"] = False
|
|
139
|
+
directives_by_line.setdefault(line_num, []).append(parsed)
|
|
140
|
+
|
|
141
|
+
return directives_by_line
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def find_matching_directive(
|
|
145
|
+
sink_line: int,
|
|
146
|
+
cwe: str,
|
|
147
|
+
directives_by_line: Dict[int, List[Dict[str, Any]]],
|
|
148
|
+
source_lines: List[str]
|
|
149
|
+
) -> Optional[Dict[str, Any]]:
|
|
150
|
+
"""
|
|
151
|
+
Finds an applicable directive for a sink at sink_line targeting the given CWE.
|
|
152
|
+
Checks:
|
|
153
|
+
1. Same-line (sink_line)
|
|
154
|
+
2. Previous-line (sink_line - 1)
|
|
155
|
+
3. Upward contiguous comment block immediately preceding the sink statement.
|
|
156
|
+
Stops search upward immediately if any code statement is encountered.
|
|
157
|
+
"""
|
|
158
|
+
norm_cwe = cwe.upper()
|
|
159
|
+
|
|
160
|
+
# 1. Check same line
|
|
161
|
+
if sink_line in directives_by_line:
|
|
162
|
+
for d in directives_by_line[sink_line]:
|
|
163
|
+
if "ALL" in d["targets"] or norm_cwe in d["targets"]:
|
|
164
|
+
return d
|
|
165
|
+
|
|
166
|
+
# 2. Check previous lines (moving upward from sink_line - 1)
|
|
167
|
+
curr_line = sink_line - 1
|
|
168
|
+
max_steps = 10
|
|
169
|
+
steps = 0
|
|
170
|
+
|
|
171
|
+
while curr_line >= 1 and steps < max_steps:
|
|
172
|
+
steps += 1
|
|
173
|
+
line_content = source_lines[curr_line - 1] if curr_line - 1 < len(source_lines) else ""
|
|
174
|
+
stripped = line_content.strip()
|
|
175
|
+
|
|
176
|
+
# If line has directives, check them. A bracketed directive is scoped to its own
|
|
177
|
+
# line only, so it is not allowed to travel upward onto a later statement.
|
|
178
|
+
if curr_line in directives_by_line:
|
|
179
|
+
for d in directives_by_line[curr_line]:
|
|
180
|
+
if d.get("exact_line"):
|
|
181
|
+
continue
|
|
182
|
+
if "ALL" in d["targets"] or norm_cwe in d["targets"]:
|
|
183
|
+
return d
|
|
184
|
+
curr_line -= 1
|
|
185
|
+
continue
|
|
186
|
+
|
|
187
|
+
# If empty or blank line, allow bridging comment block to statement
|
|
188
|
+
if not stripped:
|
|
189
|
+
curr_line -= 1
|
|
190
|
+
continue
|
|
191
|
+
|
|
192
|
+
# If ordinary comment, continue scanning upward
|
|
193
|
+
if stripped.startswith("#"):
|
|
194
|
+
curr_line -= 1
|
|
195
|
+
continue
|
|
196
|
+
|
|
197
|
+
# Encountered actual code statement: stop searching
|
|
198
|
+
break
|
|
199
|
+
|
|
200
|
+
return None
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def resolve_line_suppressions(
|
|
204
|
+
findings: List[Dict[str, Any]],
|
|
205
|
+
normalized_files: Dict[str, str]
|
|
206
|
+
) -> List[Dict[str, Any]]:
|
|
207
|
+
"""Mark findings silenced by an exact-line `# tcs: ignore[CWE-XXX]` directive.
|
|
208
|
+
|
|
209
|
+
Only the bracketed form is consulted here, and only on the finding's own line: a
|
|
210
|
+
`# tcs: ignore[CWE-611]` written next to a statement cannot hide a finding on the line
|
|
211
|
+
above or below it. Files without the substring `tcs` are never tokenized, so a tree
|
|
212
|
+
that uses no directives pays one case-folded scan per file and nothing else.
|
|
213
|
+
"""
|
|
214
|
+
if not findings:
|
|
215
|
+
return findings
|
|
216
|
+
|
|
217
|
+
exact_directives: Dict[str, Dict[int, List[Dict[str, Any]]]] = {}
|
|
218
|
+
for fpath, content in normalized_files.items():
|
|
219
|
+
if "tcs" not in content.lower():
|
|
220
|
+
continue
|
|
221
|
+
by_line: Dict[int, List[Dict[str, Any]]] = {}
|
|
222
|
+
for line, entries in extract_file_directives(content).items():
|
|
223
|
+
kept = [entry for entry in entries if entry.get("exact_line")]
|
|
224
|
+
if kept:
|
|
225
|
+
by_line[line] = kept
|
|
226
|
+
if by_line:
|
|
227
|
+
exact_directives[fpath] = by_line
|
|
228
|
+
|
|
229
|
+
for finding in findings:
|
|
230
|
+
line = finding.get("line")
|
|
231
|
+
directives = exact_directives.get(str(finding.get("file", "")))
|
|
232
|
+
if not directives or not isinstance(line, int):
|
|
233
|
+
continue
|
|
234
|
+
cwe = str(finding.get("cwe", "")).upper()
|
|
235
|
+
for directive in directives.get(line, []):
|
|
236
|
+
if "ALL" in directive["targets"] or cwe in directive["targets"]:
|
|
237
|
+
finding["suppressed"] = True
|
|
238
|
+
finding["suppression_justification"] = directive.get("justification")
|
|
239
|
+
finding["suppression_kind"] = "inSourceLine"
|
|
240
|
+
break
|
|
241
|
+
|
|
242
|
+
return findings
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def resolve_suppressions(
|
|
246
|
+
findings: List[Dict[str, Any]],
|
|
247
|
+
normalized_files: Dict[str, str]
|
|
248
|
+
) -> List[Dict[str, Any]]:
|
|
249
|
+
"""
|
|
250
|
+
Evaluates in-source suppression directives against detected findings.
|
|
251
|
+
Modifies findings non-destructively by attaching suppression status and justification.
|
|
252
|
+
"""
|
|
253
|
+
# Pre-extract directives per file
|
|
254
|
+
file_directives: Dict[str, Dict[int, List[Dict[str, Any]]]] = {}
|
|
255
|
+
file_lines: Dict[str, List[str]] = {}
|
|
256
|
+
|
|
257
|
+
for fpath, content in normalized_files.items():
|
|
258
|
+
file_directives[fpath] = extract_file_directives(content)
|
|
259
|
+
file_lines[fpath] = content.splitlines()
|
|
260
|
+
|
|
261
|
+
for finding in findings:
|
|
262
|
+
fpath = finding.get("file", "")
|
|
263
|
+
sink_line = int(finding.get("line_number") or 1)
|
|
264
|
+
cwe = str(finding.get("cwe", ""))
|
|
265
|
+
|
|
266
|
+
dirs = file_directives.get(fpath, {})
|
|
267
|
+
lines = file_lines.get(fpath, [])
|
|
268
|
+
|
|
269
|
+
matched = find_matching_directive(sink_line, cwe, dirs, lines)
|
|
270
|
+
|
|
271
|
+
if matched is not None:
|
|
272
|
+
finding["suppressed"] = True
|
|
273
|
+
finding["suppression_justification"] = matched.get("justification")
|
|
274
|
+
finding["suppression_kind"] = "inSource"
|
|
275
|
+
finding["active"] = False
|
|
276
|
+
else:
|
|
277
|
+
finding["suppressed"] = False
|
|
278
|
+
finding["suppression_justification"] = None
|
|
279
|
+
finding["suppression_kind"] = None
|
|
280
|
+
finding["active"] = True
|
|
281
|
+
|
|
282
|
+
return findings
|