: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(security): discourage new INTERNAL_API_SECRET callers (#67488)
Tom Piccirello2392c87fc27b
diff --git a/.agents/security.md b/.agents/security.mdindex 0cad1c19f3e8..d091f422f132 100644--- a/.agents/security.md+++ b/.agents/security.md@@ -22,6 +22,7 @@ Default to the smallest permission, narrowest field set, and shortest scope that- **Don't extend `SECRET_KEY` to new purposes.** Do not add code that reads `settings.SECRET_KEY` (or derives keys from it) for a new job. Mint a dedicated setting in `posthog/settings/` read from its own env var — e.g. `<PURPOSE>_SIGNING_KEY` / `<PURPOSE>_SECRET_KEYS`. A genuinely new purpose must NOT fall back to `SECRET_KEY`; fail closed when unprovisioned instead. Canonical example: `RECORDING_API_JWT_SECRET` (`posthog/settings/session_replay_v2.py`) — no `SECRET_KEY` default, empty in prod so only designated minters can sign. (Older keys like `JWT_SIGNING_KEY`, `TEMPORAL_SECRET_KEY`, `FLAGS_SECRET_KEYS` default to `SECRET_KEY` for self-hosted back-compat; that is migration debt, not the pattern to copy for new keys.)- **Support rotation from day one.** Read the key as a list, newest first: the first element signs/encrypts, all elements are tried for verify/decrypt. Prefer a single comma-separated env var (`KEY="<new>,<old>"`) parsed with `get_list()` — the Fernet `new,old` convention. Examples: `RECORDING_API_JWT_SECRET` (`recording_api_jwt.py`), `FLAGS_SECRET_KEYS` (`encrypted_flag_payloads.py`). For encryption use `MultiFernet` (encrypt with first, decrypt with any) — see `posthog/helpers/encrypted_fields.py`. Encrypting data at rest? ship a re-encryption path that re-wraps onto the primary key, like `posthog/management/commands/reencrypt_fields.py`. The primary + `_FALLBACKS` pair (`JWT_SIGNING_KEY` / `JWT_SIGNING_KEY_FALLBACKS`) is the older two-var variant; single-var comma-separated is preferred for new keys.- **Unique value per environment — including prod US and prod EU.** Never share a key value across environments; provision each (dev, prod-US, prod-EU) independently so one region can rotate without breaking another and a leak in one doesn't compromise the rest. Enforced at provisioning, not in code — so never hardcode a shared prod default. Fail closed when a prod key is unset (mirror the `SECRET_KEY` startup guard in `posthog/settings/access.py` and `RECORDING_API_JWT_SECRET`'s empty-in-prod default).+- **Don't extend `INTERNAL_API_SECRET` to new service-to-service calls.** It's a single fleet-wide shared secret already trusted by many services across Django, Node, and Rust — one leak reaches all of them — so it must not grow new callers or protected endpoints (it's being actively retired edge-by-edge; adding one moves it backwards). For a new internal call, in order of preference: (1) **mint a scoped JWT (strongly preferred)** — a dedicated per-audience signing key (never `INTERNAL_API_SECRET`/`SECRET_KEY`/`JWT_SIGNING_KEY`) with claims pinning the token to its team and operation, verified per-route; canonical example `RECORDING_API_JWT_SECRET` (mint in `posthog/session_recordings/recordings/recording_api_jwt.py`, verify in `nodejs/src/session-replay/recording-api/auth.ts`). (2) If a JWT genuinely doesn't fit, **a dedicated static secret** scoped to that one caller→callee pair (`<PURPOSE>_API_SECRET`, its own env var, rotatable, empty-in-prod-fail-closed) — never reuse `INTERNAL_API_SECRET`. The goal is blast radius: no single "master" credential that authenticates to a dozen services — a scoped JWT limits a leak to one team + operation; a dedicated secret limits it to one hop.## SQL Securitydiff --git a/AGENTS.md b/AGENTS.mdindex f4ba25ef6693..fcb4f681e6f5 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -103,7 +103,7 @@ Examples:## Security-See [.agents/security.md](.agents/security.md) for SQL, HogQL, and semgrep security guidelines.+See [.agents/security.md](.agents/security.md) for security guidelines — least privilege, secrets & service-to-service auth (don't add new `INTERNAL_API_SECRET` callers), SQL, HogQL, and semgrep.## Architecture guidelinesdiff --git a/nodejs/src/common/api/middleware/internal-api-auth.ts b/nodejs/src/common/api/middleware/internal-api-auth.tsindex d32a52b81dde..dab7582d7b44 100644--- a/nodejs/src/common/api/middleware/internal-api-auth.ts+++ b/nodejs/src/common/api/middleware/internal-api-auth.ts@@ -10,6 +10,9 @@ import { logger } from '~/common/utils/logger'* calls (e.g., Django -> Node.js CDP API). The primary protection comes from Contour* routing configuration at the infrastructure level, which restricts access to internal* endpoints. This middleware adds an additional layer of verification using a shared secret.+ *+ * Do not gate new endpoints on this shared secret — prefer a scoped JWT (see recording-api/auth.ts)+ * or a dedicated per-purpose secret. See .agents/security.md ("Secrets & key management").*/export const INTERNAL_SERVICE_CALL_HEADER_NAME = 'X-Internal-Api-Secret'diff --git a/posthog/settings/data_stores.py b/posthog/settings/data_stores.pyindex 762ea1be8b66..4885702107dc 100644--- a/posthog/settings/data_stores.py+++ b/posthog/settings/data_stores.py@@ -532,6 +532,8 @@ if not CDP_API_URL:# internal API requests fail closed at request time (InternalAPIAuthentication) rather than silently# running on a known-public value. Stripped at load so a mounted secret's trailing newline can't# cause a spurious mismatch; get_list already strips the fallbacks.+# Do not add new callers or protected endpoints to this shared secret — mint a scoped JWT (see+# RECORDING_API_JWT_SECRET) or add a dedicated per-purpose secret. See .agents/security.md.LOCAL_DEV_INTERNAL_API_SECRET = "posthog123"INTERNAL_API_SECRET = get_from_env("INTERNAL_API_SECRET", LOCAL_DEV_INTERNAL_API_SECRET if DEBUG or TEST else ""