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(oauth-proxy): reject duplicate OAuth params in authorize (#72655)

Rafael Audibert6711a7da6200
diff --git a/posthog/api/oauth/test_views.py b/posthog/api/oauth/test_views.py
index b0a724fd0579..7e4c6fa34dc0 100644
--- a/posthog/api/oauth/test_views.py
+++ b/posthog/api/oauth/test_views.py
@@ -255,6 +255,18 @@ class TestOAuthAPI(APIBaseTest):
self.assertEqual(response.json()["error"], "invalid_request")
self.assertEqual(response.json()["error_description"], "Missing client_id parameter.")
+ @parameterized.expand(["client_id", "response_type", "redirect_uri", "scope", "state"])
+ def test_authorize_rejects_duplicate_param(self, param):
+ # Duplicate OAuth params (HTTP parameter pollution) must be rejected as fatal errors,
+ # so a proxy/parser reading first-vs-last can't route a victim's code elsewhere.
+ url = f"{self.base_authorization_url}&{param}=dup_one&{param}=dup_two"
+
+ response = self.client.get(url)
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertEqual(response.json()["error"], "invalid_request")
+ self.assertEqual(response.json()["error_description"], f"Duplicate {param} parameter.")
+
def test_authorize_invalid_client_id(self):
url = self.base_authorization_url
diff --git a/services/oauth-proxy/src/handlers/authorize.ts b/services/oauth-proxy/src/handlers/authorize.ts
index 4bac80a0275c..6b1ef28abde4 100644
--- a/services/oauth-proxy/src/handlers/authorize.ts
+++ b/services/oauth-proxy/src/handlers/authorize.ts
@@ -12,6 +12,31 @@ const REGION_PICKER_HEADERS: Record<string, string> = {
'Referrer-Policy': 'no-referrer',
}
+// OAuth request parameters MUST NOT be included more than once (RFC 6749 §3.1).
+// We enforce it because the proxy reads these with `.get()` (first value) for KV
+// keying but forwards the last value to the regional server via `.set()`. A
+// duplicated `state` splits those two reads, letting an attacker route a victim's
+// callback to a preloaded redirect URI. `resource` (RFC 8707) is intentionally
+// excluded — it is allowed to repeat.
+const SINGLE_VALUED_PARAMS = [
+ 'state',
+ 'client_id',
+ 'redirect_uri',
+ 'response_type',
+ 'scope',
+ 'code_challenge',
+ 'code_challenge_method',
+] as const
+
+function findDuplicatedParam(url: URL): string | null {
+ for (const param of SINGLE_VALUED_PARAMS) {
+ if (url.searchParams.getAll(param).length > 1) {
+ return param
+ }
+ }
+ return null
+}
+
/**
* OAuth Authorization — region picker + redirect.
*
@@ -24,6 +49,17 @@ const REGION_PICKER_HEADERS: Record<string, string> = {
export async function handleAuthorize(request: Request, kv: KVNamespace): Promise<Response> {
const url = new URL(request.url)
+ const duplicatedParam = findDuplicatedParam(url)
+ if (duplicatedParam) {
+ return new Response(
+ JSON.stringify({
+ error: 'invalid_request',
+ error_description: `Duplicate ${duplicatedParam} parameter is not allowed`,
+ }),
+ { status: 400, headers: { 'Content-Type': 'application/json' } }
+ )
+ }
+
// If region is already selected (via query param from the picker page),
// redirect to the regional authorize endpoint
const selectedRegion = url.searchParams.get('_region') as Region | null
diff --git a/services/oauth-proxy/tests/authorize.test.ts b/services/oauth-proxy/tests/authorize.test.ts
index 057b659021e0..5471ecca732d 100644
--- a/services/oauth-proxy/tests/authorize.test.ts
+++ b/services/oauth-proxy/tests/authorize.test.ts
@@ -202,6 +202,28 @@ describe('handleAuthorize', () => {
expect(callbackPuts).toHaveLength(0)
})
+ it.each([
+ 'state',
+ 'client_id',
+ 'redirect_uri',
+ 'response_type',
+ 'scope',
+ 'code_challenge',
+ 'code_challenge_method',
+ ])('rejects duplicate %s to prevent parameter-pollution code theft', async (param) => {
+ const request = new Request(
+ `https://oauth.posthog.com/oauth/authorize/?client_id=abc&response_type=code&${param}=first&${param}=second&_region=us`
+ )
+ const response = await handleAuthorize(request, mockKV)
+
+ expect(response.status).toBe(400)
+ const data = (await response.json()) as Record<string, unknown>
+ expect(data.error).toBe('invalid_request')
+ expect(data.error_description).toBe(`Duplicate ${param} parameter is not allowed`)
+ // Nothing gets keyed in KV when the request is rejected up front.
+ expect(vi.mocked(mockKV.put)).not.toHaveBeenCalled()
+ })
+
it('sets security and cache headers on region picker page', async () => {
const request = new Request('https://oauth.posthog.com/oauth/authorize/?client_id=abc&response_type=code')
const response = await handleAuthorize(request, mockKV)