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
ssrf_guard.py
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
"""
|
|
2
|
+
TimeCodeSecurity Server-Side Request Forgery (SSRF) Guard.
|
|
3
|
+
|
|
4
|
+
Defines the 9-stage validation gate for outbound webhook URLs.
|
|
5
|
+
Strictly standard library only (socket, ipaddress, urllib.parse).
|
|
6
|
+
|
|
7
|
+
Invariants:
|
|
8
|
+
- HTTPS ONLY (rejects http, ftp, and all non-https schemes).
|
|
9
|
+
- Rejects userinfo / embedded credentials (@).
|
|
10
|
+
- Port must be in range 1-65535 (default 443).
|
|
11
|
+
- Validates hostname syntax (ASCII/IDNA, zero whitespace/control characters).
|
|
12
|
+
- Resolves ALL A and AAAA records. If 0 records: DNS_RESOLUTION_FAILED.
|
|
13
|
+
- Inspects EVERY resolved IP address: if ANY is forbidden, the ENTIRE hostname is rejected.
|
|
14
|
+
- Comprehensive IPv4 and IPv6 blocklist (loopback, RFC 1918, link-local, cloud metadata,
|
|
15
|
+
CGNAT 100.64.0.0/10, documentation, multicast, broadcast).
|
|
16
|
+
- IPv4-mapped IPv6 addresses are unwrapped and tested against IPv4 rules.
|
|
17
|
+
- Deterministic IP selection: IPv4 first (sorted numerically), then IPv6 (sorted numerically).
|
|
18
|
+
- Zero nondeterministic or random target selection.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import ipaddress
|
|
24
|
+
import socket
|
|
25
|
+
import urllib.parse
|
|
26
|
+
from dataclasses import dataclass
|
|
27
|
+
from typing import Any, Callable, List, Optional, Sequence, Set, Tuple, Union
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class SSRFValidationError(Exception):
|
|
31
|
+
"""Raised when an outbound URL or IP fails SSRF validation rules."""
|
|
32
|
+
def __init__(self, message: str, status_code: Optional[str] = "SSRF_BLOCKED") -> None:
|
|
33
|
+
super().__init__(message)
|
|
34
|
+
self.status_code = status_code
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class DNSResolutionError(SSRFValidationError):
|
|
38
|
+
"""Raised when DNS resolution yields zero valid records or gaierror."""
|
|
39
|
+
def __init__(self, message: str) -> None:
|
|
40
|
+
super().__init__(message, status_code="DNS_RESOLUTION_FAILED")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# ---------------------------------------------------------------------------
|
|
44
|
+
# Strict Subnet Blocklists
|
|
45
|
+
# ---------------------------------------------------------------------------
|
|
46
|
+
|
|
47
|
+
IPV4_BLOCKED_NETWORKS: Tuple[ipaddress.IPv4Network, ...] = (
|
|
48
|
+
ipaddress.ip_network("127.0.0.0/8"), # Loopback
|
|
49
|
+
ipaddress.ip_network("10.0.0.0/8"), # RFC 1918 Private
|
|
50
|
+
ipaddress.ip_network("172.16.0.0/12"), # RFC 1918 Private
|
|
51
|
+
ipaddress.ip_network("192.168.0.0/16"), # RFC 1918 Private
|
|
52
|
+
ipaddress.ip_network("169.254.0.0/16"), # Link-Local / Cloud Metadata (169.254.169.254)
|
|
53
|
+
ipaddress.ip_network("0.0.0.0/8"), # Current Network / Broadcast
|
|
54
|
+
ipaddress.ip_network("100.64.0.0/10"), # CGNAT (RFC 6598) - blocked by default
|
|
55
|
+
ipaddress.ip_network("192.0.0.0/24"), # IETF Protocol Assignments
|
|
56
|
+
ipaddress.ip_network("192.0.2.0/24"), # TEST-NET-1 (RFC 5737)
|
|
57
|
+
ipaddress.ip_network("198.51.100.0/24"), # TEST-NET-2 (RFC 5737)
|
|
58
|
+
ipaddress.ip_network("203.0.113.0/24"), # TEST-NET-3 (RFC 5737)
|
|
59
|
+
ipaddress.ip_network("198.18.0.0/15"), # Benchmarking (RFC 2544)
|
|
60
|
+
ipaddress.ip_network("224.0.0.0/4"), # Multicast
|
|
61
|
+
ipaddress.ip_network("240.0.0.0/4"), # Reserved / Future Use
|
|
62
|
+
ipaddress.ip_network("255.255.255.255/32"),# Limited Broadcast
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
IPV6_BLOCKED_NETWORKS: Tuple[ipaddress.IPv6Network, ...] = (
|
|
66
|
+
ipaddress.ip_network("::1/128"), # Loopback
|
|
67
|
+
ipaddress.ip_network("::/128"), # Unspecified
|
|
68
|
+
ipaddress.ip_network("fc00::/7"), # Unique Local Addresses (ULA)
|
|
69
|
+
ipaddress.ip_network("fe80::/10"), # Link-Local Unicast
|
|
70
|
+
ipaddress.ip_network("ff00::/8"), # Multicast
|
|
71
|
+
ipaddress.ip_network("::ffff:0:0/96"), # IPv4-mapped IPv6
|
|
72
|
+
ipaddress.ip_network("::/96"), # IPv4-compatible (deprecated)
|
|
73
|
+
ipaddress.ip_network("2001:db8::/32"), # Documentation
|
|
74
|
+
ipaddress.ip_network("100::/64"), # Discard-Only Prefix (RFC 6666)
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@dataclass(frozen=True)
|
|
79
|
+
class SSRFValidationResult:
|
|
80
|
+
"""Immutable result of successful SSRF validation."""
|
|
81
|
+
target_ip: str
|
|
82
|
+
hostname: str
|
|
83
|
+
port: int
|
|
84
|
+
canonical_endpoint: str
|
|
85
|
+
all_resolved_ips: Tuple[str, ...]
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def is_ip_blocked(ip: Union[ipaddress.IPv4Address, ipaddress.IPv6Address]) -> Tuple[bool, str]:
|
|
89
|
+
"""
|
|
90
|
+
Checks whether an IP address belongs to any blocked subnet.
|
|
91
|
+
Unwraps IPv4-mapped IPv6 addresses to test against IPv4 rules.
|
|
92
|
+
"""
|
|
93
|
+
# Check if IPv6 is IPv4-mapped (e.g. ::ffff:192.168.1.1)
|
|
94
|
+
if isinstance(ip, ipaddress.IPv6Address):
|
|
95
|
+
if ip.ipv4_mapped is not None:
|
|
96
|
+
unwrapped_v4 = ip.ipv4_mapped
|
|
97
|
+
for net in IPV4_BLOCKED_NETWORKS:
|
|
98
|
+
if unwrapped_v4 in net:
|
|
99
|
+
return True, f"IPv4-mapped IPv6 {ip} unwrapped to {unwrapped_v4} which is in blocked subnet {net}"
|
|
100
|
+
if unwrapped_v4.is_loopback or unwrapped_v4.is_private or unwrapped_v4.is_link_local:
|
|
101
|
+
return True, f"IPv4-mapped IPv6 {ip} unwrapped to private/loopback {unwrapped_v4}"
|
|
102
|
+
|
|
103
|
+
for net in IPV6_BLOCKED_NETWORKS:
|
|
104
|
+
if ip in net:
|
|
105
|
+
return True, f"IPv6 address {ip} is in blocked subnet {net}"
|
|
106
|
+
if ip.is_loopback or ip.is_private or ip.is_link_local or ip.is_multicast or ip.is_unspecified:
|
|
107
|
+
return True, f"IPv6 address {ip} is private/loopback/unspecified"
|
|
108
|
+
|
|
109
|
+
elif isinstance(ip, ipaddress.IPv4Address):
|
|
110
|
+
for net in IPV4_BLOCKED_NETWORKS:
|
|
111
|
+
if ip in net:
|
|
112
|
+
return True, f"IPv4 address {ip} is in blocked subnet {net}"
|
|
113
|
+
if ip.is_loopback or ip.is_private or ip.is_link_local or ip.is_multicast or ip.is_reserved or ip.is_unspecified:
|
|
114
|
+
return True, f"IPv4 address {ip} is private/loopback/unspecified"
|
|
115
|
+
|
|
116
|
+
return False, ""
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def select_deterministic_ip(
|
|
120
|
+
validated_ips: Sequence[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]]
|
|
121
|
+
) -> str:
|
|
122
|
+
"""
|
|
123
|
+
Deterministically selects an IP address from a sequence of validated addresses.
|
|
124
|
+
Ordering rule:
|
|
125
|
+
1. IPv4 addresses first (sorted by numerical integer value).
|
|
126
|
+
2. IPv6 addresses next (sorted by numerical integer value).
|
|
127
|
+
3. Select the first address.
|
|
128
|
+
"""
|
|
129
|
+
if not validated_ips:
|
|
130
|
+
raise ValueError("Cannot select deterministic IP from empty sequence.")
|
|
131
|
+
|
|
132
|
+
sorted_ips = sorted(
|
|
133
|
+
validated_ips,
|
|
134
|
+
key=lambda ip: (0 if ip.version == 4 else 1, int(ip))
|
|
135
|
+
)
|
|
136
|
+
return str(sorted_ips[0])
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
class SSRFGuard:
|
|
140
|
+
"""
|
|
141
|
+
SSRF validation gatekeeper.
|
|
142
|
+
Performs full syntactic, DNS, and IP inspection before network connection.
|
|
143
|
+
Supports dependency injection for DNS resolution to enable 100% offline unit testing.
|
|
144
|
+
"""
|
|
145
|
+
|
|
146
|
+
def __init__(
|
|
147
|
+
self,
|
|
148
|
+
dns_resolver: Optional[Callable[[str, int], List[Tuple[Any, ...]]]] = None
|
|
149
|
+
) -> None:
|
|
150
|
+
self._dns_resolver = dns_resolver or socket.getaddrinfo
|
|
151
|
+
|
|
152
|
+
def validate_url(self, url: str) -> SSRFValidationResult:
|
|
153
|
+
"""
|
|
154
|
+
Executes the 9-stage SSRF validation pipeline against the provided URL.
|
|
155
|
+
Returns SSRFValidationResult with the pinned target_ip.
|
|
156
|
+
Raises SSRFValidationError or DNSResolutionError on failure.
|
|
157
|
+
"""
|
|
158
|
+
if not url or not isinstance(url, str):
|
|
159
|
+
raise SSRFValidationError("Webhook URL must be a non-empty string.")
|
|
160
|
+
|
|
161
|
+
# Stage 1: URL Parsing & Scheme Validation (HTTPS ONLY)
|
|
162
|
+
try:
|
|
163
|
+
parsed = urllib.parse.urlsplit(url.strip())
|
|
164
|
+
except Exception as exc:
|
|
165
|
+
raise SSRFValidationError(f"Malformed URL: {exc}")
|
|
166
|
+
|
|
167
|
+
scheme = parsed.scheme.lower()
|
|
168
|
+
if scheme != "https":
|
|
169
|
+
raise SSRFValidationError(
|
|
170
|
+
f"Invalid URL scheme '{scheme}'. Webhooks strictly require HTTPS."
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
# Stage 2: Userinfo Rejection (no embedded credentials)
|
|
174
|
+
if "@" in parsed.netloc or parsed.username is not None or parsed.password is not None:
|
|
175
|
+
raise SSRFValidationError(
|
|
176
|
+
"Embedded user credentials (user:pass@) are strictly forbidden in webhook URLs."
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
# Stage 3: Port Validation
|
|
180
|
+
port = parsed.port
|
|
181
|
+
if port is None:
|
|
182
|
+
port = 443
|
|
183
|
+
else:
|
|
184
|
+
if not (1 <= port <= 65535):
|
|
185
|
+
raise SSRFValidationError(f"Invalid port number: {port}. Must be 1-65535.")
|
|
186
|
+
|
|
187
|
+
# Stage 4: Hostname Validation
|
|
188
|
+
raw_hostname = parsed.hostname
|
|
189
|
+
if not raw_hostname:
|
|
190
|
+
raise SSRFValidationError("Missing hostname in webhook URL.")
|
|
191
|
+
|
|
192
|
+
if any(c.isspace() or ord(c) < 32 for c in raw_hostname):
|
|
193
|
+
raise SSRFValidationError("Hostname contains whitespace or control characters.")
|
|
194
|
+
|
|
195
|
+
# Normalize hostname (lowercase, strip single trailing dot)
|
|
196
|
+
hostname = raw_hostname.lower()
|
|
197
|
+
if hostname.endswith("."):
|
|
198
|
+
hostname = hostname[:-1]
|
|
199
|
+
|
|
200
|
+
if not hostname:
|
|
201
|
+
raise SSRFValidationError("Invalid empty hostname after trailing dot removal.")
|
|
202
|
+
|
|
203
|
+
try:
|
|
204
|
+
# Validate IDNA encoding
|
|
205
|
+
hostname.encode("idna")
|
|
206
|
+
except Exception as exc:
|
|
207
|
+
raise SSRFValidationError(f"Invalid IDNA hostname: {exc}")
|
|
208
|
+
|
|
209
|
+
# Stage 5: DNS Resolution (ALL A and AAAA records)
|
|
210
|
+
# Check if hostname is already a raw IP literal
|
|
211
|
+
resolved_records: List[Tuple[Any, ...]] = []
|
|
212
|
+
is_ip_literal = False
|
|
213
|
+
try:
|
|
214
|
+
ip_literal_obj = ipaddress.ip_address(hostname)
|
|
215
|
+
is_ip_literal = True
|
|
216
|
+
family = socket.AF_INET if ip_literal_obj.version == 4 else socket.AF_INET6
|
|
217
|
+
resolved_records = [(family, socket.SOCK_STREAM, 6, "", (str(ip_literal_obj), port))]
|
|
218
|
+
except ValueError:
|
|
219
|
+
is_ip_literal = False
|
|
220
|
+
|
|
221
|
+
if not is_ip_literal:
|
|
222
|
+
try:
|
|
223
|
+
resolved_records = self._dns_resolver(hostname, port)
|
|
224
|
+
except socket.gaierror as exc:
|
|
225
|
+
raise DNSResolutionError(f"DNS resolution failed for hostname '{hostname}': {exc}")
|
|
226
|
+
except Exception as exc:
|
|
227
|
+
raise DNSResolutionError(f"DNS resolver error for hostname '{hostname}': {exc}")
|
|
228
|
+
|
|
229
|
+
if not resolved_records:
|
|
230
|
+
raise DNSResolutionError(f"Zero DNS records resolved for hostname '{hostname}'.")
|
|
231
|
+
|
|
232
|
+
# Stage 6 & 7: Exhaustive IP Inspection (every single IP checked)
|
|
233
|
+
parsed_ips: List[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]] = []
|
|
234
|
+
seen_ip_strs: Set[str] = set()
|
|
235
|
+
|
|
236
|
+
for record in resolved_records:
|
|
237
|
+
sockaddr = record[4]
|
|
238
|
+
ip_str = sockaddr[0]
|
|
239
|
+
if ip_str in seen_ip_strs:
|
|
240
|
+
continue
|
|
241
|
+
seen_ip_strs.add(ip_str)
|
|
242
|
+
|
|
243
|
+
try:
|
|
244
|
+
ip_obj = ipaddress.ip_address(ip_str)
|
|
245
|
+
except ValueError as exc:
|
|
246
|
+
raise SSRFValidationError(f"Unparseable IP address '{ip_str}' resolved: {exc}")
|
|
247
|
+
|
|
248
|
+
# Check if this IP is forbidden
|
|
249
|
+
blocked, reason = is_ip_blocked(ip_obj)
|
|
250
|
+
if blocked:
|
|
251
|
+
raise SSRFValidationError(
|
|
252
|
+
f"SSRF violation: Hostname '{hostname}' resolves to forbidden address {ip_str}. Reason: {reason}"
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
parsed_ips.append(ip_obj)
|
|
256
|
+
|
|
257
|
+
if not parsed_ips:
|
|
258
|
+
raise DNSResolutionError(f"No valid IP addresses extracted from DNS for '{hostname}'.")
|
|
259
|
+
|
|
260
|
+
# Stage 8: Deterministic Target-IP Selection
|
|
261
|
+
target_ip = select_deterministic_ip(parsed_ips)
|
|
262
|
+
|
|
263
|
+
# Stage 9: Build Canonical Endpoint Representation
|
|
264
|
+
# Preserve path and query exactly
|
|
265
|
+
port_str = f":{port}" if port != 443 else ""
|
|
266
|
+
canonical_endpoint = f"{scheme}://{hostname}{port_str}{parsed.path}"
|
|
267
|
+
if parsed.query:
|
|
268
|
+
canonical_endpoint += f"?{parsed.query}"
|
|
269
|
+
|
|
270
|
+
return SSRFValidationResult(
|
|
271
|
+
target_ip=target_ip,
|
|
272
|
+
hostname=hostname,
|
|
273
|
+
port=port,
|
|
274
|
+
canonical_endpoint=canonical_endpoint,
|
|
275
|
+
all_resolved_ips=tuple(str(ip) for ip in parsed_ips),
|
|
276
|
+
)
|