Org-scoped client-id metadata documents registered their callback with an executor_org query param on redirect_uri, but the client sends the bare callback and the org is carried in the OAuth state (#1147). Providers that exact-match redirect_uri (PostHog) rejected the authorize request with "Mismatching redirect URI". Org targets keep their distinct client_id URL but now register the same bare callback redirect_uri as every other target.
Unified · Split
| @@ -0,0 +1,12 @@ | |||
| 1 | --- | ||
| 2 | "@executor-js/api": patch | ||
| 3 | --- | ||
| 4 | |||
| 5 | Fix OAuth "Mismatching redirect URI" for org-scoped client-id metadata documents | ||
| 6 | |||
| 7 | Org-scoped client-id metadata documents registered their callback as | ||
| 8 | `redirect_uri` with an `executor_org` query param, but the client always sends | ||
| 9 | the bare callback and the org is carried in the OAuth `state`. Providers that | ||
| 10 | compare `redirect_uri` as an exact string (such as PostHog) rejected the | ||
| 11 | authorize request. Org targets now keep their distinct `client_id` URL but | ||
| 12 | register the same bare callback `redirect_uri` as every other target. | ||
| @@ -3,7 +3,7 @@ import { describe, expect, it } from "@effect/vitest"; · 2 unmodified lines | |||
| 3 | import { oauthClientIdMetadataDocumentFromRequest } from "./oauth-client-metadata"; | 3 | import { oauthClientIdMetadataDocumentFromRequest } from "./oauth-client-metadata"; |
| 4 | 4 | ||
| 5 | describe("OAuth client ID metadata document", () => { | 5 | describe("OAuth client ID metadata document", () => { |
| 6 | it("builds an org-scoped hosted document from a target path", () => { | 6 | it("builds an org-scoped hosted document with a bare callback redirect_uri", () => { |
| 7 | const metadata = oauthClientIdMetadataDocumentFromRequest({ | 7 | const metadata = oauthClientIdMetadataDocumentFromRequest({ |
| 8 | requestUrl: "/api/oauth/client-id-metadata/acme.json", | 8 | requestUrl: "/api/oauth/client-id-metadata/acme.json", |
| 9 | webRequest: new Request("http://127.0.0.1:42384/api/oauth/client-id-metadata/acme.json", { | 9 | webRequest: new Request("http://127.0.0.1:42384/api/oauth/client-id-metadata/acme.json", { |
| @@ -12,12 +12,13 @@ describe("OAuth client ID metadata document", () => { · 2 unmodified lines | |||
| 12 | mountPrefix: "/api", | 12 | mountPrefix: "/api", |
| 13 | }); | 13 | }); |
| 14 | 14 | ||
| 15 | // The org is identified by the per-org client_id URL, not by a query param | ||
| 16 | // on redirect_uri (which the client never sends, and the callback reads | ||
| 17 | // from `state`). redirect_uri must stay bare for exact-match providers. | ||
| 15 | expect(metadata.client_id).toBe( | 18 | expect(metadata.client_id).toBe( |
| 16 | "http://100.81.219.45:42384/api/oauth/client-id-metadata/acme.json", | 19 | "http://100.81.219.45:42384/api/oauth/client-id-metadata/acme.json", |
| 17 | ); | 20 | ); |
| 18 | expect(metadata.redirect_uris).toEqual([ | 21 | expect(metadata.redirect_uris).toEqual(["http://100.81.219.45:42384/api/oauth/callback"]); |
| 19 | "http://100.81.219.45:42384/api/oauth/callback?executor_org=acme", | ||
| 20 | ]); | ||
| 21 | expect(metadata.token_endpoint_auth_method).toBe("none"); | 22 | expect(metadata.token_endpoint_auth_method).toBe("none"); |
| 22 | expect(metadata.application_type).toBe("web"); | 23 | expect(metadata.application_type).toBe("web"); |
| 23 | }); | 24 | }); |
| @@ -65,7 +66,7 @@ describe("OAuth client ID metadata document", () => { · 41 unmodified lines | |||
| 65 | expect(metadata.application_type).toBe("native"); | 66 | expect(metadata.application_type).toBe("native"); |
| 66 | }); | 67 | }); |
| 67 | 68 | ||
| 68 | it("still reads the org selector from the legacy query form", () => { | 69 | it("ignores a legacy executor_org query param when building redirect_uri", () => { |
| 69 | const metadata = oauthClientIdMetadataDocumentFromRequest({ | 70 | const metadata = oauthClientIdMetadataDocumentFromRequest({ |
| 70 | requestUrl: "/api/oauth/client-id-metadata.json?executor_org=acme", | 71 | requestUrl: "/api/oauth/client-id-metadata.json?executor_org=acme", |
| 71 | webRequest: new Request("http://127.0.0.1:42384/api/oauth/client-id-metadata.json", { | 72 | webRequest: new Request("http://127.0.0.1:42384/api/oauth/client-id-metadata.json", { |
| @@ -74,11 +75,11 @@ describe("OAuth client ID metadata document", () => { · 2 unmodified lines | |||
| 74 | mountPrefix: "/api", | 75 | mountPrefix: "/api", |
| 75 | }); | 76 | }); |
| 76 | 77 | ||
| 78 | // A stray executor_org query param is inert: it stays on the client_id URL | ||
| 79 | // (which is just the request URL) but never leaks into redirect_uri. | ||
| 77 | expect(metadata.client_id).toBe( | 80 | expect(metadata.client_id).toBe( |
| 78 | "http://100.81.219.45:42384/api/oauth/client-id-metadata.json?executor_org=acme", | 81 | "http://100.81.219.45:42384/api/oauth/client-id-metadata.json?executor_org=acme", |
| 79 | ); | 82 | ); |
| 80 | expect(metadata.redirect_uris).toEqual([ | 83 | expect(metadata.redirect_uris).toEqual(["http://100.81.219.45:42384/api/oauth/callback"]); |
| 81 | "http://100.81.219.45:42384/api/oauth/callback?executor_org=acme", | ||
| 82 | ]); | ||
| 83 | }); | 84 | }); |
| 84 | }); | 85 | }); |
| @@ -8,7 +8,6 @@ export const OAUTH_CLIENT_ID_METADATA_DOCUMENT_TARGET_PATH_PREFIX = · 7 unmodified lines | |||
| 8 | `${OAUTH_CLIENT_ID_METADATA_DOCUMENT_BASE_PATH}/` as const; | 8 | `${OAUTH_CLIENT_ID_METADATA_DOCUMENT_BASE_PATH}/` as const; |
| 9 | export const OAUTH_CLIENT_ID_METADATA_DOCUMENT_DEFAULT_TARGET = "default" as const; | 9 | export const OAUTH_CLIENT_ID_METADATA_DOCUMENT_DEFAULT_TARGET = "default" as const; |
| 10 | export const OAUTH_CLIENT_ID_METADATA_DOCUMENT_LOCAL_TARGET = "local" as const; | 10 | export const OAUTH_CLIENT_ID_METADATA_DOCUMENT_LOCAL_TARGET = "local" as const; |
| 11 | const OAUTH_CALLBACK_ORG_QUERY_PARAM = "executor_org" as const; | ||
| 12 | 11 | ||
| 13 | type MetadataTarget = | 12 | type MetadataTarget = |
| 14 | | typeof OAUTH_CLIENT_ID_METADATA_DOCUMENT_DEFAULT_TARGET | 13 | | typeof OAUTH_CLIENT_ID_METADATA_DOCUMENT_DEFAULT_TARGET |
| @@ -80,17 +79,6 @@ const metadataTargetFromPath = ({ · 65 unmodified lines | |||
| 80 | return target ? target : undefined; | 79 | return target ? target : undefined; |
| 81 | }; | 80 | }; |
| 82 | 81 | ||
| 83 | const orgSlugFromMetadataTarget = (target: MetadataTarget | undefined): string | undefined => { | ||
| 84 | if (!target) return undefined; | ||
| 85 | if ( | ||
| 86 | target === OAUTH_CLIENT_ID_METADATA_DOCUMENT_DEFAULT_TARGET || | ||
| 87 | target === OAUTH_CLIENT_ID_METADATA_DOCUMENT_LOCAL_TARGET | ||
| 88 | ) { | ||
| 89 | return undefined; | ||
| 90 | } | ||
| 91 | return target; | ||
| 92 | }; | ||
| 93 | |||
| 94 | export const oauthClientIdMetadataDocumentUrlFromRequest = ({ | 82 | export const oauthClientIdMetadataDocumentUrlFromRequest = ({ |
| 95 | requestUrl, | 83 | requestUrl, |
| 96 | webRequest, | 84 | webRequest, |
| @@ -148,11 +136,14 @@ export const oauthClientIdMetadataDocumentFromRequest = ({ · 51 unmodified lines | |||
| 148 | }; | 136 | }; |
| 149 | } | 137 | } |
| 150 | 138 | ||
| 139 | // The org selector travels in the OAuth `state` (see #1147 and apps/cloud | ||
| 140 | // start.ts, which reads it back from state on the callback), never as a | ||
| 141 | // provider-facing query param on redirect_uri. Org targets get a distinct | ||
| 142 | // `client_id` URL, but all targets register the SAME bare callback so the | ||
| 143 | // redirect_uri the client sends matches this document exactly. Providers | ||
| 144 | // (e.g. PostHog) compare redirect_uri as an exact string, so an extra query | ||
| 145 | // param here would fail with "Mismatching redirect URI". | ||
| 151 | const redirectUri = new URL(callbackPathWithMountPrefix(mountPrefix), url.origin); | 146 | const redirectUri = new URL(callbackPathWithMountPrefix(mountPrefix), url.origin); |
| 152 | const orgSlug = | ||
| 153 | orgSlugFromMetadataTarget(target) ?? | ||
| 154 | url.searchParams.get(OAUTH_CALLBACK_ORG_QUERY_PARAM)?.trim(); | ||
| 155 | if (orgSlug) redirectUri.searchParams.set(OAUTH_CALLBACK_ORG_QUERY_PARAM, orgSlug); | ||
| 156 | 147 | ||
| 157 | return { | 148 | return { |
| 158 | client_id: url.toString(), | 149 | client_id: url.toString(), |