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
webhook_adapter.py
ADDED
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
"""
|
|
2
|
+
TimeCodeSecurity Webhook Delivery Adapter & Queue Infrastructure.
|
|
3
|
+
|
|
4
|
+
Defines:
|
|
5
|
+
- WebhookDestinationConfig: Immutable tenant destination configuration.
|
|
6
|
+
- DeliveryResult: Immutable outcome telemetry with ownership attribution.
|
|
7
|
+
- InMemoryDeliveryResultSink: Thread-safe, bounded result sink (deque maxlen=1000).
|
|
8
|
+
- WebhookDeliveryService: Bounded work queue (maxsize=100), fixed worker pool (N=4),
|
|
9
|
+
and idempotent shutdown sequence.
|
|
10
|
+
- WebhookDeliveryAdapter: Consumes NotificationIntents, validates multi-tenant
|
|
11
|
+
destination invariants, and submits delivery tasks.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import collections
|
|
17
|
+
import hashlib
|
|
18
|
+
import logging
|
|
19
|
+
import queue
|
|
20
|
+
import threading
|
|
21
|
+
import time
|
|
22
|
+
import urllib.parse
|
|
23
|
+
from dataclasses import dataclass, field
|
|
24
|
+
from typing import Any, Callable, Dict, List, Optional, Sequence, Union
|
|
25
|
+
|
|
26
|
+
from notification_policy import NotificationIntent
|
|
27
|
+
from webhook_client import (
|
|
28
|
+
MAX_ERROR_MESSAGE_LENGTH,
|
|
29
|
+
DeliveryStatus,
|
|
30
|
+
WebhookAuthConfig,
|
|
31
|
+
WebhookClient,
|
|
32
|
+
)
|
|
33
|
+
from webhook_formatters import (
|
|
34
|
+
MAX_WEBHOOK_PAYLOAD_BYTES,
|
|
35
|
+
PayloadTooLargeError,
|
|
36
|
+
format_webhook_payload,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
logger = logging.getLogger("tcs.webhook_adapter")
|
|
40
|
+
|
|
41
|
+
DEFAULT_SHUTDOWN_TIMEOUT_SECONDS: float = 3.0
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class TenantBoundaryViolationError(Exception):
|
|
45
|
+
"""Raised when destination organization does not match intent organization."""
|
|
46
|
+
pass
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class DeliveryAdmissionError(Exception):
|
|
50
|
+
"""Raised when a delivery task fails admission to the delivery queue."""
|
|
51
|
+
pass
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass(frozen=True)
|
|
55
|
+
class WebhookDestinationConfig:
|
|
56
|
+
"""
|
|
57
|
+
Tenant-configured webhook endpoint configuration.
|
|
58
|
+
Injected into adapter; workers do not dynamically choose arbitrary destinations.
|
|
59
|
+
"""
|
|
60
|
+
destination_id: str
|
|
61
|
+
organization_id: int
|
|
62
|
+
url: str
|
|
63
|
+
auth_config: WebhookAuthConfig = field(default_factory=WebhookAuthConfig)
|
|
64
|
+
format: str = "GENERIC" # "GENERIC" | "SLACK" | "DISCORD"
|
|
65
|
+
|
|
66
|
+
def __post_init__(self) -> None:
|
|
67
|
+
if not isinstance(self.organization_id, int) or self.organization_id <= 0:
|
|
68
|
+
raise ValueError("organization_id must be a positive integer.")
|
|
69
|
+
if not self.url or not isinstance(self.url, str):
|
|
70
|
+
raise ValueError("url must be a non-empty string.")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@dataclass(frozen=True)
|
|
74
|
+
class DeliveryResult:
|
|
75
|
+
"""
|
|
76
|
+
Immutable structured delivery outcome.
|
|
77
|
+
target_ip is retained strictly for internal diagnostics and is NEVER
|
|
78
|
+
exposed in user-facing notifications or external webhook payloads.
|
|
79
|
+
"""
|
|
80
|
+
success: bool
|
|
81
|
+
status: DeliveryStatus
|
|
82
|
+
status_code: Optional[int]
|
|
83
|
+
error_message: Optional[str]
|
|
84
|
+
duration_ms: float
|
|
85
|
+
attempts: int
|
|
86
|
+
idempotency_key: str
|
|
87
|
+
organization_id: int
|
|
88
|
+
event_id: str
|
|
89
|
+
scan_id: Optional[str]
|
|
90
|
+
channel: str
|
|
91
|
+
target_ip: Optional[str] = None
|
|
92
|
+
|
|
93
|
+
def __post_init__(self) -> None:
|
|
94
|
+
if self.error_message and len(self.error_message) > MAX_ERROR_MESSAGE_LENGTH:
|
|
95
|
+
object.__setattr__(self, "error_message", self.error_message[:MAX_ERROR_MESSAGE_LENGTH])
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@dataclass(frozen=True)
|
|
99
|
+
class WebhookDeliveryTask:
|
|
100
|
+
"""Task item enqueued into the bounded worker queue."""
|
|
101
|
+
intent: NotificationIntent
|
|
102
|
+
destination: WebhookDestinationConfig
|
|
103
|
+
idempotency_key: str
|
|
104
|
+
payload_bytes: bytes
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def canonicalize_endpoint(url: str) -> str:
|
|
108
|
+
"""
|
|
109
|
+
Normalizes ONLY scheme, hostname, and default port.
|
|
110
|
+
Preserves EXACT path representation (including trailing slashes and repeated slashes)
|
|
111
|
+
and exact query string ordering.
|
|
112
|
+
"""
|
|
113
|
+
parsed = urllib.parse.urlsplit(url.strip())
|
|
114
|
+
scheme = parsed.scheme.lower()
|
|
115
|
+
hostname = (parsed.hostname or "").lower()
|
|
116
|
+
if hostname.endswith("."):
|
|
117
|
+
hostname = hostname[:-1]
|
|
118
|
+
|
|
119
|
+
port = parsed.port
|
|
120
|
+
port_str = f":{port}" if port is not None and port != 443 else ""
|
|
121
|
+
path = parsed.path
|
|
122
|
+
query_str = f"?{parsed.query}" if parsed.query else ""
|
|
123
|
+
return f"{scheme}://{hostname}{port_str}{path}{query_str}"
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def compute_idempotency_key(organization_id: int, canonical_endpoint: str, dedupe_key: str) -> str:
|
|
127
|
+
"""
|
|
128
|
+
Computes deterministic SHA-256 idempotency key:
|
|
129
|
+
SHA256(str(organization_id) + ":" + canonical_endpoint + ":" + dedupe_key)
|
|
130
|
+
"""
|
|
131
|
+
raw = f"{organization_id}:{canonical_endpoint}:{dedupe_key}"
|
|
132
|
+
return hashlib.sha256(raw.encode("utf-8")).hexdigest()
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
class InMemoryDeliveryResultSink:
|
|
136
|
+
"""
|
|
137
|
+
Thread-safe, bounded in-memory delivery result sink.
|
|
138
|
+
Retains up to 1000 recent results in a circular deque.
|
|
139
|
+
Structured logging strictly avoids secrets, payload bodies, and auth headers.
|
|
140
|
+
"""
|
|
141
|
+
|
|
142
|
+
def __init__(self, maxlen: int = 1000) -> None:
|
|
143
|
+
self._results: collections.deque[DeliveryResult] = collections.deque(maxlen=maxlen)
|
|
144
|
+
self._lock = threading.Lock()
|
|
145
|
+
|
|
146
|
+
def record(self, result: DeliveryResult) -> None:
|
|
147
|
+
with self._lock:
|
|
148
|
+
self._results.append(result)
|
|
149
|
+
|
|
150
|
+
# Structured logging without secrets or payloads
|
|
151
|
+
logger.info(
|
|
152
|
+
"Webhook delivery completed: status=%s success=%s attempts=%d duration_ms=%.1f org_id=%d event_id=%s idempotency_key=%s",
|
|
153
|
+
result.status.value,
|
|
154
|
+
result.success,
|
|
155
|
+
result.attempts,
|
|
156
|
+
result.duration_ms,
|
|
157
|
+
result.organization_id,
|
|
158
|
+
result.event_id,
|
|
159
|
+
result.idempotency_key,
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
def get_recent(self) -> List[DeliveryResult]:
|
|
163
|
+
with self._lock:
|
|
164
|
+
return list(self._results)
|
|
165
|
+
|
|
166
|
+
def clear(self) -> None:
|
|
167
|
+
with self._lock:
|
|
168
|
+
self._results.clear()
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class WebhookDeliveryService:
|
|
172
|
+
"""
|
|
173
|
+
Bounded queue delivery service with fixed worker threads.
|
|
174
|
+
Enforces non-blocking enqueue (put_nowait), admission rejection telemetry,
|
|
175
|
+
and idempotent atomic shutdown.
|
|
176
|
+
"""
|
|
177
|
+
|
|
178
|
+
def __init__(
|
|
179
|
+
self,
|
|
180
|
+
max_workers: int = 4,
|
|
181
|
+
max_queue_size: int = 100,
|
|
182
|
+
result_sink: Optional[InMemoryDeliveryResultSink] = None,
|
|
183
|
+
webhook_client: Optional[WebhookClient] = None,
|
|
184
|
+
clock: Callable[[], float] = time.monotonic,
|
|
185
|
+
) -> None:
|
|
186
|
+
self._queue: queue.Queue[Optional[WebhookDeliveryTask]] = queue.Queue(maxsize=max_queue_size)
|
|
187
|
+
self._max_queue_size = max_queue_size
|
|
188
|
+
self._sink = result_sink or InMemoryDeliveryResultSink()
|
|
189
|
+
self._client = webhook_client or WebhookClient(clock=clock)
|
|
190
|
+
self._clock = clock
|
|
191
|
+
self._shutdown_event = threading.Event()
|
|
192
|
+
self._shutdown_initiated = False
|
|
193
|
+
self._accepting_submissions = True
|
|
194
|
+
self._lock = threading.Lock()
|
|
195
|
+
self._workers: List[threading.Thread] = []
|
|
196
|
+
|
|
197
|
+
for i in range(max_workers):
|
|
198
|
+
t = threading.Thread(
|
|
199
|
+
target=self._worker_loop,
|
|
200
|
+
name=f"tcs-webhook-worker-{i}",
|
|
201
|
+
daemon=True,
|
|
202
|
+
)
|
|
203
|
+
t.start()
|
|
204
|
+
self._workers.append(t)
|
|
205
|
+
|
|
206
|
+
@property
|
|
207
|
+
def sink(self) -> InMemoryDeliveryResultSink:
|
|
208
|
+
return self._sink
|
|
209
|
+
|
|
210
|
+
def enqueue(self, task: WebhookDeliveryTask) -> bool:
|
|
211
|
+
"""
|
|
212
|
+
Non-blocking task admission.
|
|
213
|
+
Uses put_nowait(). Never blocks caller thread.
|
|
214
|
+
Returns True on successful queue admission.
|
|
215
|
+
Returns False on QUEUE_OVERFLOW or SHUTDOWN_ABORTED after recording failure in sink.
|
|
216
|
+
"""
|
|
217
|
+
with self._lock:
|
|
218
|
+
if not self._accepting_submissions or self._shutdown_event.is_set():
|
|
219
|
+
self._sink.record(
|
|
220
|
+
DeliveryResult(
|
|
221
|
+
success=False,
|
|
222
|
+
status=DeliveryStatus.SHUTDOWN_ABORTED,
|
|
223
|
+
status_code=None,
|
|
224
|
+
error_message="Submission rejected: delivery service is shutting down",
|
|
225
|
+
duration_ms=0.0,
|
|
226
|
+
attempts=0,
|
|
227
|
+
idempotency_key=task.idempotency_key,
|
|
228
|
+
organization_id=task.intent.organization_id,
|
|
229
|
+
event_id=task.intent.event_id,
|
|
230
|
+
scan_id=task.intent.scan_id,
|
|
231
|
+
channel="WEBHOOK",
|
|
232
|
+
target_ip=None,
|
|
233
|
+
)
|
|
234
|
+
)
|
|
235
|
+
return False
|
|
236
|
+
|
|
237
|
+
try:
|
|
238
|
+
self._queue.put_nowait(task)
|
|
239
|
+
return True
|
|
240
|
+
except queue.Full:
|
|
241
|
+
self._sink.record(
|
|
242
|
+
DeliveryResult(
|
|
243
|
+
success=False,
|
|
244
|
+
status=DeliveryStatus.QUEUE_OVERFLOW,
|
|
245
|
+
status_code=None,
|
|
246
|
+
error_message=f"Webhook delivery queue full (capacity {self._max_queue_size})",
|
|
247
|
+
duration_ms=0.0,
|
|
248
|
+
attempts=0,
|
|
249
|
+
idempotency_key=task.idempotency_key,
|
|
250
|
+
organization_id=task.intent.organization_id,
|
|
251
|
+
event_id=task.intent.event_id,
|
|
252
|
+
scan_id=task.intent.scan_id,
|
|
253
|
+
channel="WEBHOOK",
|
|
254
|
+
target_ip=None,
|
|
255
|
+
)
|
|
256
|
+
)
|
|
257
|
+
return False
|
|
258
|
+
|
|
259
|
+
def shutdown(self, max_wait_seconds: float = DEFAULT_SHUTDOWN_TIMEOUT_SECONDS) -> None:
|
|
260
|
+
"""
|
|
261
|
+
Idempotent shutdown sequence:
|
|
262
|
+
1. Stop accepting submissions under lock.
|
|
263
|
+
2. Drain all pending tasks from queue and mark SHUTDOWN_ABORTED exactly once.
|
|
264
|
+
3. Signal workers and post sentinels.
|
|
265
|
+
4. Allow in-flight work to complete within monotonic deadline.
|
|
266
|
+
5. Join workers.
|
|
267
|
+
"""
|
|
268
|
+
already_initiated = False
|
|
269
|
+
with self._lock:
|
|
270
|
+
if self._shutdown_initiated:
|
|
271
|
+
already_initiated = True
|
|
272
|
+
else:
|
|
273
|
+
self._shutdown_initiated = True
|
|
274
|
+
self._accepting_submissions = False
|
|
275
|
+
|
|
276
|
+
if already_initiated:
|
|
277
|
+
# Idempotent: wait for workers without duplicating aborts
|
|
278
|
+
deadline = self._clock() + max_wait_seconds
|
|
279
|
+
for worker in self._workers:
|
|
280
|
+
rem = max(0.0, deadline - self._clock())
|
|
281
|
+
worker.join(timeout=rem)
|
|
282
|
+
return
|
|
283
|
+
|
|
284
|
+
# Drain unstarted queue items
|
|
285
|
+
while True:
|
|
286
|
+
try:
|
|
287
|
+
item = self._queue.get_nowait()
|
|
288
|
+
if item is not None:
|
|
289
|
+
self._sink.record(
|
|
290
|
+
DeliveryResult(
|
|
291
|
+
success=False,
|
|
292
|
+
status=DeliveryStatus.SHUTDOWN_ABORTED,
|
|
293
|
+
status_code=None,
|
|
294
|
+
error_message="Aborted due to service shutdown before execution",
|
|
295
|
+
duration_ms=0.0,
|
|
296
|
+
attempts=0,
|
|
297
|
+
idempotency_key=item.idempotency_key,
|
|
298
|
+
organization_id=item.intent.organization_id,
|
|
299
|
+
event_id=item.intent.event_id,
|
|
300
|
+
scan_id=item.intent.scan_id,
|
|
301
|
+
channel="WEBHOOK",
|
|
302
|
+
target_ip=None,
|
|
303
|
+
)
|
|
304
|
+
)
|
|
305
|
+
self._queue.task_done()
|
|
306
|
+
except queue.Empty:
|
|
307
|
+
break
|
|
308
|
+
|
|
309
|
+
# Signal workers
|
|
310
|
+
self._shutdown_event.set()
|
|
311
|
+
for _ in self._workers:
|
|
312
|
+
try:
|
|
313
|
+
self._queue.put_nowait(None) # Sentinel
|
|
314
|
+
except queue.Full:
|
|
315
|
+
pass
|
|
316
|
+
|
|
317
|
+
# Join workers monotonically
|
|
318
|
+
deadline = self._clock() + max_wait_seconds
|
|
319
|
+
for worker in self._workers:
|
|
320
|
+
rem = max(0.0, deadline - self._clock())
|
|
321
|
+
worker.join(timeout=rem)
|
|
322
|
+
|
|
323
|
+
def _worker_loop(self) -> None:
|
|
324
|
+
"""Background worker thread consumption loop."""
|
|
325
|
+
while True:
|
|
326
|
+
try:
|
|
327
|
+
task = self._queue.get(timeout=0.2)
|
|
328
|
+
except queue.Empty:
|
|
329
|
+
if self._shutdown_event.is_set():
|
|
330
|
+
break
|
|
331
|
+
continue
|
|
332
|
+
|
|
333
|
+
if task is None: # Sentinel
|
|
334
|
+
self._queue.task_done()
|
|
335
|
+
break
|
|
336
|
+
|
|
337
|
+
try:
|
|
338
|
+
self._execute_delivery(task)
|
|
339
|
+
except Exception as exc:
|
|
340
|
+
logger.error("Unhandled exception in webhook delivery worker: %s", exc)
|
|
341
|
+
self._sink.record(
|
|
342
|
+
DeliveryResult(
|
|
343
|
+
success=False,
|
|
344
|
+
status=DeliveryStatus.INTERNAL_ERROR,
|
|
345
|
+
status_code=None,
|
|
346
|
+
error_message=f"Internal worker exception: {exc}"[:MAX_ERROR_MESSAGE_LENGTH],
|
|
347
|
+
duration_ms=0.0,
|
|
348
|
+
attempts=0,
|
|
349
|
+
idempotency_key=task.idempotency_key,
|
|
350
|
+
organization_id=task.intent.organization_id,
|
|
351
|
+
event_id=task.intent.event_id,
|
|
352
|
+
scan_id=task.intent.scan_id,
|
|
353
|
+
channel="WEBHOOK",
|
|
354
|
+
target_ip=None,
|
|
355
|
+
)
|
|
356
|
+
)
|
|
357
|
+
finally:
|
|
358
|
+
self._queue.task_done()
|
|
359
|
+
|
|
360
|
+
def _execute_delivery(self, task: WebhookDeliveryTask) -> None:
|
|
361
|
+
"""Executes delivery for a single task."""
|
|
362
|
+
client_res = self._client.send(
|
|
363
|
+
url=task.destination.url,
|
|
364
|
+
payload_bytes=task.payload_bytes,
|
|
365
|
+
auth_config=task.destination.auth_config,
|
|
366
|
+
idempotency_key=task.idempotency_key,
|
|
367
|
+
)
|
|
368
|
+
|
|
369
|
+
res = DeliveryResult(
|
|
370
|
+
success=client_res.success,
|
|
371
|
+
status=client_res.status,
|
|
372
|
+
status_code=client_res.status_code,
|
|
373
|
+
error_message=client_res.error_message,
|
|
374
|
+
duration_ms=client_res.duration_ms,
|
|
375
|
+
attempts=client_res.attempts,
|
|
376
|
+
idempotency_key=task.idempotency_key,
|
|
377
|
+
organization_id=task.intent.organization_id,
|
|
378
|
+
event_id=task.intent.event_id,
|
|
379
|
+
scan_id=task.intent.scan_id,
|
|
380
|
+
channel="WEBHOOK",
|
|
381
|
+
target_ip=client_res.target_ip,
|
|
382
|
+
)
|
|
383
|
+
self._sink.record(res)
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
class WebhookDeliveryAdapter:
|
|
387
|
+
"""
|
|
388
|
+
Webhook Delivery Adapter.
|
|
389
|
+
Consumes NotificationIntents, validates multi-tenant destination boundaries,
|
|
390
|
+
formats payloads (with byte-size limit check), and submits tasks to the service.
|
|
391
|
+
"""
|
|
392
|
+
|
|
393
|
+
def __init__(self, service: WebhookDeliveryService) -> None:
|
|
394
|
+
if not isinstance(service, WebhookDeliveryService):
|
|
395
|
+
raise TypeError(f"Expected WebhookDeliveryService, got {type(service).__name__}")
|
|
396
|
+
self._service = service
|
|
397
|
+
|
|
398
|
+
def deliver(
|
|
399
|
+
self,
|
|
400
|
+
intent: NotificationIntent,
|
|
401
|
+
destination: WebhookDestinationConfig
|
|
402
|
+
) -> str:
|
|
403
|
+
"""
|
|
404
|
+
Dispatches a NotificationIntent to a WebhookDestinationConfig.
|
|
405
|
+
Enforces tenant boundary, pre-network payload byte limit, and queue admission.
|
|
406
|
+
Returns the computed idempotency_key on success.
|
|
407
|
+
Raises:
|
|
408
|
+
- TenantBoundaryViolationError: If destination.organization_id != intent.organization_id.
|
|
409
|
+
- PayloadTooLargeError: If serialized payload exceeds 65536 bytes.
|
|
410
|
+
- DeliveryAdmissionError: If queue is full or service is shutting down.
|
|
411
|
+
"""
|
|
412
|
+
if not isinstance(intent, NotificationIntent):
|
|
413
|
+
raise TypeError(f"Expected NotificationIntent, got {type(intent).__name__}")
|
|
414
|
+
if not isinstance(destination, WebhookDestinationConfig):
|
|
415
|
+
raise TypeError(f"Expected WebhookDestinationConfig, got {type(destination).__name__}")
|
|
416
|
+
|
|
417
|
+
# 1. Hard Multi-Tenant Destination Invariant Check
|
|
418
|
+
if destination.organization_id != intent.organization_id:
|
|
419
|
+
msg = (
|
|
420
|
+
f"Cross-tenant delivery rejected: destination org {destination.organization_id} "
|
|
421
|
+
f"!= intent org {intent.organization_id}"
|
|
422
|
+
)
|
|
423
|
+
# Record security failure in sink
|
|
424
|
+
self._service.sink.record(
|
|
425
|
+
DeliveryResult(
|
|
426
|
+
success=False,
|
|
427
|
+
status=DeliveryStatus.SSRF_BLOCKED,
|
|
428
|
+
status_code=None,
|
|
429
|
+
error_message=msg[:MAX_ERROR_MESSAGE_LENGTH],
|
|
430
|
+
duration_ms=0.0,
|
|
431
|
+
attempts=0,
|
|
432
|
+
idempotency_key="",
|
|
433
|
+
organization_id=intent.organization_id,
|
|
434
|
+
event_id=intent.event_id,
|
|
435
|
+
scan_id=intent.scan_id,
|
|
436
|
+
channel="WEBHOOK",
|
|
437
|
+
target_ip=None,
|
|
438
|
+
)
|
|
439
|
+
)
|
|
440
|
+
raise TenantBoundaryViolationError(msg)
|
|
441
|
+
|
|
442
|
+
# 2. Canonicalize Endpoint and Compute Idempotency Key
|
|
443
|
+
canonical_url = canonicalize_endpoint(destination.url)
|
|
444
|
+
idempotency_key = compute_idempotency_key(
|
|
445
|
+
intent.organization_id,
|
|
446
|
+
canonical_url,
|
|
447
|
+
intent.dedupe_key
|
|
448
|
+
)
|
|
449
|
+
|
|
450
|
+
# 3. Payload Serialization & Pre-Network Byte-Size Limit Check
|
|
451
|
+
try:
|
|
452
|
+
payload_bytes = format_webhook_payload(intent, destination.format)
|
|
453
|
+
except PayloadTooLargeError as exc:
|
|
454
|
+
self._service.sink.record(
|
|
455
|
+
DeliveryResult(
|
|
456
|
+
success=False,
|
|
457
|
+
status=DeliveryStatus.PAYLOAD_TOO_LARGE,
|
|
458
|
+
status_code=None,
|
|
459
|
+
error_message=str(exc)[:MAX_ERROR_MESSAGE_LENGTH],
|
|
460
|
+
duration_ms=0.0,
|
|
461
|
+
attempts=0,
|
|
462
|
+
idempotency_key=idempotency_key,
|
|
463
|
+
organization_id=intent.organization_id,
|
|
464
|
+
event_id=intent.event_id,
|
|
465
|
+
scan_id=intent.scan_id,
|
|
466
|
+
channel="WEBHOOK",
|
|
467
|
+
target_ip=None,
|
|
468
|
+
)
|
|
469
|
+
)
|
|
470
|
+
raise
|
|
471
|
+
|
|
472
|
+
# 4. Construct Task
|
|
473
|
+
task = WebhookDeliveryTask(
|
|
474
|
+
intent=intent,
|
|
475
|
+
destination=destination,
|
|
476
|
+
idempotency_key=idempotency_key,
|
|
477
|
+
payload_bytes=payload_bytes,
|
|
478
|
+
)
|
|
479
|
+
|
|
480
|
+
# 5. Non-Blocking Admission to Queue
|
|
481
|
+
admitted = self._service.enqueue(task)
|
|
482
|
+
if not admitted:
|
|
483
|
+
raise DeliveryAdmissionError(
|
|
484
|
+
f"Webhook task admission failed for intent {intent.intent_id} (queue full or shutting down)"
|
|
485
|
+
)
|
|
486
|
+
|
|
487
|
+
return idempotency_key
|
|
488
|
+
|
|
489
|
+
def dispatch_intent(
|
|
490
|
+
self,
|
|
491
|
+
intent: NotificationIntent,
|
|
492
|
+
destination: WebhookDestinationConfig
|
|
493
|
+
) -> str:
|
|
494
|
+
"""Orchestration alias used by NotificationService.handle_domain_event."""
|
|
495
|
+
return self.deliver(intent, destination)
|