fix: Match loopback redirect URIs on any port

Native apps such as Claude Code register http://localhost/callback and
then listen on a free port, so an exact match rejected them. Loopback
redirect URIs now match on any port (RFC 8252 section 7.3); the scheme,
host and path must still match, and other URIs still match exactly.
This commit is contained in:
Tom Moor committed 2026-10-11 22:22:34 -04:00
1 parent 85ce837357
commit fae03e0dd1
5 files changed
+123 -2

No files matched your search

@@ -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");
}
+34
View File
@@ -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 });
+36
View File
@@ -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
+43
View File
@@ -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,
+2 -1
View File
@@ -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;
}