Skip to content

The agent that grows with you

fix: webhook cron_job replays fire once and route hot-reload keeps the connector

Teknium4795e942bcdd
diff --git a/gateway/platforms/webhook.py b/gateway/platforms/webhook.py
index 69b71e7440f..989aeb7bb36 100644
--- a/gateway/platforms/webhook.py
+++ b/gateway/platforms/webhook.py
@@ -223,9 +223,11 @@ class WebhookAdapter(BasePlatformAdapter):
@property
def token(self):
- # Bind native replay to the currently configured signing credentials.
+ # Bind native replay to the configured signing credentials. Static routes only: dynamic
+ # subscriptions hot-reload on every POST, and an unrelated ``webhook subscribe`` must not
+ # re-key the connector of already-admitted deliveries (their send() would see profile_mismatch).
return json.dumps([self._global_secret, {name: route.get("secret", self._global_secret)
- for name, route in self._routes.items()}], sort_keys=True)
+ for name, route in self._static_routes.items()}], sort_keys=True)
# --- Lifecycle ---
@@ -676,7 +678,13 @@ class WebhookAdapter(BasePlatformAdapter):
"webhook-id", headers.get("X-Request-ID", uuid.uuid4().hex))))
now = time.time() # idempotency: skip duplicate deliveries (webhook retries)
delivery_identity = _WebhookDeliveryIdentity.from_parts(profile, route_name, delivery_id)
- if route_config.get("deliver_only") and not self._record_delivery_id(delivery_identity, now):
+ coalesce = route_config.get("coalesce")
+ # deliver_only, cron_job and coalesced events act before (or without) a durable admission, so
+ # the ledger cannot dedupe a provider retry or a replayed signed delivery; the route-scoped
+ # in-memory guard is their only replay defence (main dedupes every route here).
+ non_durable = bool(route_config.get("deliver_only") or route_config.get("cron_job")
+ or isinstance(coalesce, dict))
+ if non_durable and not self._record_delivery_id(delivery_identity, now):
logger.info("[webhook] Skipping duplicate delivery %s on route %s", delivery_id, route_name)
return web.json_response({"status": "duplicate", "delivery_id": delivery_id}, status=200)
if route_config.get("cron_job"):
@@ -684,12 +692,6 @@ class WebhookAdapter(BasePlatformAdapter):
if route_config.get("deliver_only"):
return await self._handle_deliver_only(prompt, payload, route_config, route_name, event_type, delivery_id,
profile)
- coalesce = route_config.get("coalesce")
- # A coalesced event waits in memory before it is durably admitted, so the ledger cannot dedupe
- # a provider retry inside the window; the in-memory idempotency guard fills that gap here.
- if isinstance(coalesce, dict) and not self._record_delivery_id(delivery_identity, now):
- logger.info("[webhook] Skipping duplicate delivery %s on route %s", delivery_id, route_name)
- return web.json_response({"status": "duplicate", "delivery_id": delivery_id}, status=200)
if isinstance(coalesce, dict) and self._coalescer.enqueue(
route_name=route_name, coalesce=coalesce, payload=payload, event_type=event_type, prompt=prompt,
delivery_id=delivery_id, now=now, route_config=route_config, profile=profile):
diff --git a/tests/gateway/test_webhook_replay_scope.py b/tests/gateway/test_webhook_replay_scope.py
new file mode 100644
index 00000000000..c7508ccca8f
--- /dev/null
+++ b/tests/gateway/test_webhook_replay_scope.py
@@ -0,0 +1,45 @@
+"""Webhook replay/credential invariants that the durable ledger does not cover (PR #106742 security lane)."""
+import hashlib
+import hmac
+
+import pytest
+from aiohttp import web
+from aiohttp.test_utils import TestClient, TestServer
+
+from gateway.config import PlatformConfig
+from gateway.platforms.webhook import WebhookAdapter
+
+
+def _adapter(routes):
+ return WebhookAdapter(PlatformConfig(enabled=True, extra={"host": "127.0.0.1", "port": 0, "routes": routes}))
+
+
+def _signed(secret, body, delivery):
+ return {"X-Hub-Signature-256": "sha256=" + hmac.new(secret.encode(), body, hashlib.sha256).hexdigest(),
+ "X-GitHub-Delivery": delivery, "X-GitHub-Event": "issues", "Content-Type": "application/json"}
+
+
+async def test_cron_route_replay_fires_the_job_once():
+ """cron_job routes never reach the durable ledger, so a replayed signed delivery must not re-fire."""
+ a = _adapter({"nightly": {"secret": "s", "prompt": "x", "cron_job": "job1"}})
+ fired = []
+ a._handle_cron_trigger = lambda *args, **kw: (fired.append(args[4]),
+ web.json_response({"status": "accepted"}, status=202))[1]
+ app = web.Application()
+ app.router.add_post("/webhooks/{route_name}", a._handle_webhook)
+ body = b'{"k":1}'
+ async with TestClient(TestServer(app)) as c:
+ statuses = [(await c.post("/webhooks/nightly", data=body, headers=_signed("s", body, "CAP"))).status
+ for _ in range(3)]
+ assert fired == ["CAP"], (statuses, fired)
+
+
+def test_adding_a_dynamic_route_keeps_the_adapter_credential():
+ """An unrelated hot-reloaded subscription must not re-key the connector of admitted deliveries."""
+ from gateway.run import GatewayRunner
+ a = _adapter({"victim": {"secret": "v", "prompt": "x"}})
+ before = GatewayRunner._adapter_credential_fingerprint(a)
+ a._dynamic_routes = {"agent-made": {"secret": "n", "prompt": "y"}}
+ a._routes = {**a._dynamic_routes, **a._static_routes}
+ assert GatewayRunner._adapter_credential_fingerprint(a) == before