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): reject an unknown app name at provider build time (#103573)

Julian Bez7c6312d0934f
diff --git a/.agents/skills/adding-inbound-webhooks/SKILL.md b/.agents/skills/adding-inbound-webhooks/SKILL.md
index e4089207539f..f3d89eb2988b 100644
--- a/.agents/skills/adding-inbound-webhooks/SKILL.md
+++ b/.agents/skills/adding-inbound-webhooks/SKILL.md
@@ -64,6 +64,7 @@ Copy `github/` for the full shape, or `vapi/` for a small one.
- `throttle_class` names a DRF throttle from `posthog.rate_limit`, run in front of verification. Set one when the endpoint is public and its verification is expensive, such as a JWT signing-key lookup.
- `retry_status` is the status answered instead of the receipt when ingress cannot vouch that the delivery was accepted: an ownership lookup failed, the forward to the owning region failed, a consumer raised, or the budget skipped a consumer. Set it when the provider redelivers on a non-2xx, and leave it `None` when it does not, because the non-2xx then only loses the delivery. A retry replays the delivery against every consumer on the endpoint, and dedup is what stops the ones that already accepted it from running twice.
- A `build_<provider>_provider(...)` function returning it. Secrets and verifiers a product owns are **passed into this builder**, never imported: nothing under `posthog/ingress/` may import a product.
+ A builder that takes an `app` name calls `require_known_app(provider, app, SPECS)` from `posthog/ingress/providers.py` first, so a typo raises `UnknownApp` at import instead of serving an endpoint no consumer is registered against.
### Picking a scheme
diff --git a/posthog/ingress/README.md b/posthog/ingress/README.md
index 2b8fce81fa8c..a739bbaae337 100644
--- a/posthog/ingress/README.md
+++ b/posthog/ingress/README.md
@@ -230,6 +230,7 @@ Add a `<provider>/` subpackage with a `provider.py` holding three things (see `g
- `SPECS` — one `ProviderSpec` per app, naming the event types the app is subscribed to. The registry validates consumers against these.
- A `WebhookProvider` subclass — its `scheme()` (from `verify/`), its `deliveries()` (how to read event type, delivery id and context off the request), and any status codes its protocol fixes. The defaults are 403 on a bad signature, 500 when unconfigured, and 202 on success, with a short body naming the reason on the two rejections. An incarnation that answers 404 to withhold the endpoint's existence sets `explains_rejections = False` so the body stays empty as well. Three more attributes are optional: `parse()`, which decodes the body, `throttle_class`, which caps request volume, and `retry_status`, which a provider that redelivers on a non-2xx sets so an unaccepted delivery is not receipted. See [The lanes one request runs through](#the-lanes-one-request-runs-through).
- A `build_<provider>_provider(...)` function returning that provider, which the URLconf hands to `build_webhook_view()`.
+ A builder that takes an `app` name checks it with `require_known_app(provider, app, SPECS)` before anything else, so a typo raises `UnknownApp` at import. Without that check the endpoint builds and serves: consumers register against the declared app names, so nothing matches and every delivery is receipted and dropped, or the app has no secret getter and every delivery answers `NOT_CONFIGURED`.
Add the module to `_INCARNATION_MODULES` in `posthog/ingress/providers.py`, so the registry finds its specs and any core consumers.
diff --git a/posthog/ingress/github/provider.py b/posthog/ingress/github/provider.py
index 3066a30eb41b..91e83e6862af 100644
--- a/posthog/ingress/github/provider.py
+++ b/posthog/ingress/github/provider.py
@@ -13,7 +13,7 @@ from django.http import HttpRequest
from django.utils import timezone
from posthog.ingress.contracts import ProviderSpec, WebhookConsumer, WebhookDelivery
-from posthog.ingress.providers import WebhookProvider
+from posthog.ingress.providers import WebhookProvider, require_known_app
from posthog.ingress.verify.schemes import HmacSha256, SignatureScheme
from posthog.models.instance_setting import get_instance_setting
@@ -72,12 +72,10 @@ class GitHubProvider(WebhookProvider):
provider = "github"
def __init__(self, app: str) -> None:
- secret_getter = _SECRET_GETTERS.get(app)
- if secret_getter is None:
- raise ValueError(f"Unknown GitHub app {app!r}, expected one of {sorted(_SECRET_GETTERS)}")
+ require_known_app(self.provider, app, SPECS)
self.app = app
self._scheme = HmacSha256(
- secret_getter=secret_getter,
+ secret_getter=_SECRET_GETTERS[app],
signature_header="X-Hub-Signature-256",
prefix="sha256=",
)
diff --git a/posthog/ingress/mailgun/provider.py b/posthog/ingress/mailgun/provider.py
index 0d91ba8a3898..961a189e3ff6 100644
--- a/posthog/ingress/mailgun/provider.py
+++ b/posthog/ingress/mailgun/provider.py
@@ -15,7 +15,7 @@ from django.http.multipartparser import MultiPartParserError
from django.utils import timezone
from posthog.ingress.contracts import ProviderSpec, WebhookDelivery
-from posthog.ingress.providers import InvalidPayload, WebhookProvider
+from posthog.ingress.providers import InvalidPayload, WebhookProvider, require_known_app
from posthog.ingress.verify.schemes import HmacSha256, SignatureScheme, Verification, VerificationOutcome
# These go to the HMAC scheme as header names, because the scheme reads a mapping and does not
@@ -65,11 +65,9 @@ class MailgunProvider(WebhookProvider):
forward_timeout_seconds = 10.0
def __init__(self, app: str, *, signing_key_getter: Callable[[], str | None]) -> None:
- event_type = _APP_EVENT_TYPES.get(app)
- if event_type is None:
- raise ValueError(f"Unknown Mailgun app {app!r}, expected one of {sorted(_APP_EVENT_TYPES)}")
+ require_known_app(self.provider, app, SPECS)
self.app = app
- self.event_type = event_type
+ self.event_type = _APP_EVENT_TYPES[app]
self._scheme = HmacSha256(
secret_getter=signing_key_getter,
signature_header=SIGNATURE_FIELD,
diff --git a/posthog/ingress/providers.py b/posthog/ingress/providers.py
index f1a7c4b57072..05bffdedd583 100644
--- a/posthog/ingress/providers.py
+++ b/posthog/ingress/providers.py
@@ -47,6 +47,23 @@ class InvalidPayload(Exception):
"""
+class UnknownApp(ValueError):
+ """A builder was handed an app name no spec of that provider declares."""
+
+
+def require_known_app(provider: str, app: str, specs: Sequence[ProviderSpec]) -> None:
+ """Refuse an app name the provider's `SPECS` do not declare.
+
+ A typo otherwise builds a working endpoint: consumers register against the declared names,
+ so nothing matches and every delivery is receipted and dropped, or the app has no secret
+ getter and every delivery answers `NOT_CONFIGURED`. Both fail at the first real delivery
+ rather than at import, so the mistake is caught here instead.
+ """
+ known = sorted(spec.app for spec in specs if spec.provider == provider)
+ if app not in known:
+ raise UnknownApp(f"Unknown {provider} app {app!r}, expected one of {known}")
+
+
def decode_json(raw: str | bytes) -> Any:
"""Decode a body this package accepts, or raise `InvalidPayload` for one it cannot read.
diff --git a/posthog/ingress/slack/provider.py b/posthog/ingress/slack/provider.py
index 2ce8544708b6..8f3f07ab07d2 100644
--- a/posthog/ingress/slack/provider.py
+++ b/posthog/ingress/slack/provider.py
@@ -16,7 +16,7 @@ from django.http import HttpRequest, HttpResponse, JsonResponse
from django.utils import timezone
from posthog.ingress.contracts import ProviderSpec, WebhookDelivery
-from posthog.ingress.providers import InvalidPayload, WebhookProvider, decode_json
+from posthog.ingress.providers import InvalidPayload, WebhookProvider, decode_json, require_known_app
from posthog.ingress.verify.schemes import HmacSha256, SignatureScheme
SLACK_EVENT_TYPES = frozenset(
@@ -67,6 +67,7 @@ class SlackProvider(WebhookProvider):
retry_status = 502
def __init__(self, *, app: str = "supporthog", secret_getter: Callable[[], str | None]) -> None:
+ require_known_app(self.provider, app, SPECS)
self.app = app
self._scheme = HmacSha256(
secret_getter=secret_getter,
diff --git a/posthog/ingress/sns/provider.py b/posthog/ingress/sns/provider.py
index 354b631cfd6d..c5fb29eca3a9 100644
--- a/posthog/ingress/sns/provider.py
+++ b/posthog/ingress/sns/provider.py
@@ -12,7 +12,7 @@ from django.http import HttpRequest
from django.utils import timezone
from posthog.ingress.contracts import ProviderSpec, WebhookDelivery
-from posthog.ingress.providers import WebhookProvider
+from posthog.ingress.providers import WebhookProvider, require_known_app
from posthog.ingress.verify.schemes import SignatureScheme, SnsSignature
SNS_EVENT_TYPES = frozenset({"SubscriptionConfirmation", "Notification", "UnsubscribeConfirmation"})
@@ -30,6 +30,7 @@ class SnsProvider(WebhookProvider):
verify_message: Callable[[Mapping[str, Any]], bool],
allowed_topic_arns: Callable[[], frozenset[str]],
) -> None:
+ require_known_app(self.provider, app, SPECS)
self.app = app
self._scheme = SnsSignature(verify_message=verify_message, allowed_topic_arns=allowed_topic_arns)
diff --git a/posthog/ingress/test/test_github.py b/posthog/ingress/test/test_github.py
index d8356b4c8b24..aac668b0c5ae 100644
--- a/posthog/ingress/test/test_github.py
+++ b/posthog/ingress/test/test_github.py
@@ -41,10 +41,6 @@ class TestGitHubProvider(SimpleTestCase):
VerificationOutcome.INVALID,
)
- def test_an_unknown_app_is_refused_at_build(self) -> None:
- with self.assertRaises(ValueError):
- build_github_provider("gitlab")
-
def test_every_declared_app_has_a_spec_and_the_core_consumers_fit_it(self) -> None:
self.assertEqual({spec.app for spec in SPECS}, {"posthog", "stamphog"})
declared = {(spec.provider, spec.app): spec.event_types for spec in SPECS}
diff --git a/posthog/ingress/test/test_mailgun.py b/posthog/ingress/test/test_mailgun.py
index 4d206bb26292..bacc5e0ca03b 100644
--- a/posthog/ingress/test/test_mailgun.py
+++ b/posthog/ingress/test/test_mailgun.py
@@ -103,7 +103,3 @@ class TestMailgunProvider(SimpleTestCase):
payload = _provider().parse(request)
self.assertEqual(len(payload[FILES_KEY]), MAX_FILES)
-
- def test_an_unknown_app_is_refused_at_build(self) -> None:
- with self.assertRaises(ValueError):
- _provider("events")
diff --git a/posthog/ingress/test/test_providers.py b/posthog/ingress/test/test_providers.py
new file mode 100644
index 000000000000..115609b2fdd2
--- /dev/null
+++ b/posthog/ingress/test/test_providers.py
@@ -0,0 +1,68 @@
+from collections.abc import Callable, Mapping
+from typing import Any
+
+from django.test import SimpleTestCase
+
+from parameterized import parameterized
+
+from posthog.ingress.github.provider import build_github_provider
+from posthog.ingress.mailgun.provider import build_mailgun_provider
+from posthog.ingress.providers import UnknownApp, WebhookProvider
+from posthog.ingress.slack.provider import build_slack_provider
+from posthog.ingress.sns.provider import build_sns_provider
+
+
+def _github(app: str) -> WebhookProvider:
+ return build_github_provider(app)
+
+
+def _mailgun(app: str) -> WebhookProvider:
+ return build_mailgun_provider(app, signing_key_getter=lambda: "signing-key")
+
+
+def _slack(app: str) -> WebhookProvider:
+ return build_slack_provider(secret_getter=lambda: "signing-secret", app=app)
+
+
+def _verified(message: Mapping[str, Any]) -> bool:
+ return True
+
+
+def _no_topics() -> frozenset[str]:
+ return frozenset()
+
+
+def _sns(app: str) -> WebhookProvider:
+ return build_sns_provider(verify_message=_verified, allowed_topic_arns=_no_topics, app=app)
+
+
+# One row per builder that takes an app name: the provider, the builder, a declared app, and a
+# plausible typo of one.
+BUILDERS = [
+ ("github", _github, "posthog", "gitlab"),
+ ("mailgun", _mailgun, "inbound", "events"),
+ ("slack", _slack, "supporthog", "supporthog_events"),
+ ("sns", _sns, "default", "defaults"),
+]
+
+
+class TestProviderAppNames(SimpleTestCase):
+ @parameterized.expand(BUILDERS)
+ def test_an_unknown_app_is_refused_at_build(
+ self, provider: str, builder: Callable[[str], WebhookProvider], declared_app: str, unknown_app: str
+ ) -> None:
+ # Without this the endpoint builds and serves: consumers register against the declared
+ # app names, so nothing matches the typo and every delivery is receipted and dropped.
+ with self.assertRaises(UnknownApp) as caught:
+ builder(unknown_app)
+
+ message = str(caught.exception)
+ self.assertIn(provider, message)
+ self.assertIn(unknown_app, message)
+ self.assertIn(declared_app, message)
+
+ @parameterized.expand(BUILDERS)
+ def test_a_declared_app_builds(
+ self, _provider: str, builder: Callable[[str], WebhookProvider], declared_app: str, _unknown_app: str
+ ) -> None:
+ self.assertEqual(builder(declared_app).app, declared_app)