diff --git a/server/routes/api/oauthClients/oauthClients.ts b/server/routes/api/oauthClients/oauthClients.ts index bfb3a8f899..9d9454b16c 100644 --- a/server/routes/api/oauthClients/oauthClients.ts +++ b/server/routes/api/oauthClients/oauthClients.ts @@ -17,6 +17,7 @@ import { import type { APIContext } from "@server/types"; import { RateLimiterStrategy } from "@server/utils/RateLimiter"; import { ClientMetadataHelper } from "@server/utils/oauth/ClientMetadataHelper"; +import { OAuthHelper } from "@server/utils/oauth/OAuthHelper"; import pagination from "../middlewares/pagination"; import * as T from "./schema"; @@ -92,7 +93,13 @@ router.post( } authorize(user, "read", oauthClient); - if (redirectUri && !oauthClient.redirectUris.includes(redirectUri)) { + if ( + redirectUri && + !OAuthHelper.isRegisteredRedirectUri( + oauthClient.redirectUris, + redirectUri + ) + ) { throw ValidationError("redirect_uri is invalid"); } diff --git a/server/routes/oauth/oauth.test.ts b/server/routes/oauth/oauth.test.ts index 0a2b9578d1..087603ec74 100644 --- a/server/routes/oauth/oauth.test.ts +++ b/server/routes/oauth/oauth.test.ts @@ -900,6 +900,40 @@ describe("client ID metadata documents", () => { expect(res.headers.get("location")).toBeNull(); }); + it("should authorize a loopback redirect URI on another port", async () => { + const subdomain = buildSubdomain(); + const team = await buildTeam({ subdomain }); + const user = await buildUser({ teamId: team.id }); + const clientId = `https://${randomUUID()}.example.com/oauth/client.json`; + msw.use( + http.get(clientId, () => + HttpResponse.json({ + client_id: clientId, + client_name: "Example CLI", + redirect_uris: ["http://localhost/callback"], + }) + ) + ); + + const res = await server.post("/oauth/authorize", user, { + redirect: "manual", + headers: { host: `${subdomain}.outline.dev` }, + body: { + client_id: clientId, + response_type: "code", + redirect_uri: "http://localhost:54321/callback", + state: "state", + code_challenge: codeChallenge, + code_challenge_method: "S256", + }, + }); + + expect(res.status).toEqual(302); + expect(res.headers.get("location")).toMatch( + /^http:\/\/localhost:54321\/callback\?code=/ + ); + }); + it("should not authorize a redirect URI missing from the document", async () => { const subdomain = buildSubdomain(); const team = await buildTeam({ subdomain }); diff --git a/server/utils/oauth/OAuthHelper.ts b/server/utils/oauth/OAuthHelper.ts index f998a8f33c..12f3b1d368 100644 --- a/server/utils/oauth/OAuthHelper.ts +++ b/server/utils/oauth/OAuthHelper.ts @@ -88,6 +88,42 @@ export class OAuthHelper { return isUrl(uri, { requireHttps: true }); } + /** + * Whether a requested redirect URI matches one of the registered redirect + * URIs. Loopback URIs match on any port, because native apps choose a free + * port at run time (RFC 8252 ยง7.3). All other URIs must match exactly. + * + * @param redirectUris - the registered redirect URIs. + * @param uri - the requested redirect URI. + * @returns true if the URI matches a registered redirect URI. + */ + public static isRegisteredRedirectUri( + redirectUris: string[], + uri: string + ): boolean { + if (redirectUris.includes(uri)) { + return true; + } + if (!isLoopbackUri(uri)) { + return false; + } + + const withoutPort = (value: string) => { + try { + const url = new URL(value); + url.port = ""; + return url.toString(); + } catch { + return undefined; + } + }; + const requested = withoutPort(uri); + return redirectUris.some( + (registered) => + isLoopbackUri(registered) && withoutPort(registered) === requested + ); + } + /** * Returns the origin that identifies this deployment as an OAuth issuer and * resource server. Cloud-hosted deployments use the request origin so each diff --git a/server/utils/oauth/OAuthInterface.test.ts b/server/utils/oauth/OAuthInterface.test.ts index b40e1ef24c..c1e7fb4233 100644 --- a/server/utils/oauth/OAuthInterface.test.ts +++ b/server/utils/oauth/OAuthInterface.test.ts @@ -254,6 +254,49 @@ describe("OAuthInterface", () => { expect(result).toBe(true); }); + it("should allow a loopback redirect URI on any port per RFC 8252", async () => { + const loopbackClient = { + ...client, + redirectUris: ["http://localhost/callback", "http://127.0.0.1:8080/cb"], + }; + for (const redirectUri of [ + "http://localhost:54321/callback", + "http://127.0.0.1:43123/cb", + ]) { + const result = await OAuthInterface.validateRedirectUri( + redirectUri, + loopbackClient + ); + expect(result).toBe(true); + } + }); + + it("should not ignore the host or path of a loopback redirect URI", async () => { + const loopbackClient = { + ...client, + redirectUris: ["http://localhost/callback"], + }; + for (const redirectUri of [ + "http://127.0.0.1:54321/callback", + "http://localhost:54321/other", + "https://localhost:54321/callback", + ]) { + const result = await OAuthInterface.validateRedirectUri( + redirectUri, + loopbackClient + ); + expect(result).toBe(false); + } + }); + + it("should not ignore the port of a non-loopback redirect URI", async () => { + const result = await OAuthInterface.validateRedirectUri( + "https://example.com:8443/callback", + client + ); + expect(result).toBe(false); + }); + it("should reject HTTPS loopback redirect URIs (must be HTTP per RFC 8252)", async () => { const httpsLoopbackClient = { ...client, diff --git a/server/utils/oauth/OAuthInterface.ts b/server/utils/oauth/OAuthInterface.ts index 9a433fb443..e976e91a3e 100644 --- a/server/utils/oauth/OAuthInterface.ts +++ b/server/utils/oauth/OAuthInterface.ts @@ -358,7 +358,8 @@ export const OAuthInterface: RefreshTokenModel & * @returns True if the URI is valid, false otherwise. */ async validateRedirectUri(uri, client) { - if (!client.redirectUris?.includes(uri)) { + const redirectUris = [client.redirectUris ?? []].flat(); + if (!OAuthHelper.isRegisteredRedirectUri(redirectUris, uri)) { return false; }