Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(ingress): ask PandaDoc for a redelivery when recording a signature fails (#103809)

Julian Bezdeb05d6d8801
diff --git a/posthog/ingress/pandadoc/README.md b/posthog/ingress/pandadoc/README.md
index 2376777a5d8a..5b3030922204 100644
--- a/posthog/ingress/pandadoc/README.md
+++ b/posthog/ingress/pandadoc/README.md
@@ -31,6 +31,11 @@ PandaDoc batches several events into one body, so one request becomes one delive
A bad signature answers 404 instead of 403, so a prober cannot tell a wrong secret from an unknown route.
A missing secret answers 404 too, and neither rejection carries a body, because the reason would hand back what the status code withholds.
+`retry_status` is 500, which is what this endpoint answered before it moved here, and PandaDoc redelivers after a non-2xx.
+So a consumer that raised, a forward that never landed, and a consumer the request budget skipped all cost the request its receipt, and the delivery comes back rather than being lost.
+The 500 is reachable only after a valid signature, so it withholds the endpoint's existence exactly as the two 404s above do.
+A redelivery replays the whole batched body against the consumer, which is why the consumer carries its own idempotency.
+
## Consumers
`legal_documents_signatures`, declared in `products/legal_documents/backend/webhook_consumers.py`.
diff --git a/posthog/ingress/pandadoc/provider.py b/posthog/ingress/pandadoc/provider.py
index 68eb9d4501af..38e685d687eb 100644
--- a/posthog/ingress/pandadoc/provider.py
+++ b/posthog/ingress/pandadoc/provider.py
@@ -36,6 +36,9 @@ class PandaDocProvider(WebhookProvider):
unconfigured_status = 404
# A body naming the reason would hand back what the two 404s above withhold.
explains_rejections = False
+ # 500 is what this endpoint answered before it moved here, and PandaDoc redelivers after one,
+ # so a signature the consumer failed to record is sent again instead of being lost.
+ retry_status = 500
def __init__(self, *, enabled: Callable[[], bool] | None = None) -> None:
self._enabled = enabled
diff --git a/posthog/ingress/test/test_views.py b/posthog/ingress/test/test_views.py
index 3b251af0cde3..0be1db34c556 100644
--- a/posthog/ingress/test/test_views.py
+++ b/posthog/ingress/test/test_views.py
@@ -490,6 +490,15 @@ class _DispatchingViewTestCase(SimpleTestCase):
},
)
+ def _pandadoc_request(self, *, signing_secret: str = SECRET):
+ body = json.dumps([{"event": "document_state_changed"}]).encode()
+ return self.factory.post(
+ "/webhooks/pandadoc/",
+ data=body,
+ content_type="application/json",
+ headers={"X-PandaDoc-Signature": hmac.digest(signing_secret.encode(), body, "sha256").hex()},
+ )
+
class TestRegionalForwarding(_DispatchingViewTestCase):
def setUp(self) -> None:
@@ -736,6 +745,41 @@ class TestUnacceptedDelivery(_DispatchingViewTestCase):
warnings = {call.args[0]: call.kwargs for call in logger.warning.call_args_list}
self.assertEqual(warnings.get("ingress_delivery_retry_requested", {}).get("consumers"), warned_about)
+ @parameterized.expand(
+ [
+ ("a_consumer_that_raised_asks_for_a_redelivery", True, SECRET, SECRET, 500, 1),
+ ("a_delivery_that_applied_is_receipted", False, SECRET, SECRET, 202, 1),
+ ("a_bad_signature_still_withholds_the_endpoint", False, "wrong-secret", SECRET, 404, 0),
+ ("a_missing_secret_still_withholds_the_endpoint", False, SECRET, "", 404, 0),
+ ]
+ )
+ def test_pandadoc_answers_a_retryable_status_only_after_a_valid_signature(
+ self,
+ _name: str,
+ handler_raises: bool,
+ signing_secret: str,
+ configured_secret: str,
+ status: int,
+ handler_calls: int,
+ ) -> None:
+ # Recording a signature used to answer 500 from the product's own view, which PandaDoc
+ # redelivers after. Dropping back to the 202 receipt loses the signature silently.
+ if handler_raises:
+ self.handler.side_effect = RuntimeError("the signature write failed")
+ view = self._view(
+ [_consumer(PANDADOC_SPEC, name="legal_documents_signatures", handler=self.handler)],
+ provider=build_pandadoc_provider(),
+ )
+
+ with (
+ override_settings(PANDADOC_WEBHOOK_SECRET=configured_secret),
+ patch("posthog.ingress.dispatch.dispatcher.capture_exception"),
+ ):
+ response = view(self._pandadoc_request(signing_secret=signing_secret))
+
+ self.assertEqual(response.status_code, status)
+ self.assertEqual(self.handler.call_count, handler_calls)
+
def test_a_consumer_the_budget_skipped_costs_the_receipt_the_same_way(self) -> None:
elapsed = {"seconds": 0.0}