diff --git a/server/utils/oauth/OAuthInterface.test.ts b/server/utils/oauth/OAuthInterface.test.ts index bc4adf4b71..231f6812a5 100644 --- a/server/utils/oauth/OAuthInterface.test.ts +++ b/server/utils/oauth/OAuthInterface.test.ts @@ -128,6 +128,97 @@ describe("OAuthInterface", () => { ); expect(result).toBe(false); }); + + it("should allow HTTP loopback redirect URI (127.0.0.1) per RFC 8252", async () => { + const loopbackClient = { + ...client, + redirectUris: ["http://127.0.0.1:8080/callback"], + }; + const redirectUri = "http://127.0.0.1:8080/callback"; + const result = await OAuthInterface.validateRedirectUri( + redirectUri, + loopbackClient + ); + expect(result).toBe(true); + }); + + it("should allow HTTP loopback redirect URI (localhost) per RFC 8252", async () => { + const loopbackClient = { + ...client, + redirectUris: ["http://localhost:8080/callback"], + }; + const redirectUri = "http://localhost:8080/callback"; + const result = await OAuthInterface.validateRedirectUri( + redirectUri, + loopbackClient + ); + expect(result).toBe(true); + }); + + it("should allow HTTP loopback redirect URI (IPv6 [::1]) per RFC 8252", async () => { + const loopbackClient = { + ...client, + redirectUris: ["http://[::1]:8080/callback"], + }; + const redirectUri = "http://[::1]:8080/callback"; + const result = await OAuthInterface.validateRedirectUri( + redirectUri, + loopbackClient + ); + expect(result).toBe(true); + }); + + it("should allow loopback redirect URI with different ports", async () => { + const loopbackClient = { + ...client, + redirectUris: ["http://127.0.0.1:3000/callback"], + }; + const redirectUri = "http://127.0.0.1:3000/callback"; + const result = await OAuthInterface.validateRedirectUri( + redirectUri, + loopbackClient + ); + expect(result).toBe(true); + }); + + it("should allow loopback redirect URI without port", async () => { + const loopbackClient = { + ...client, + redirectUris: ["http://localhost/callback"], + }; + const redirectUri = "http://localhost/callback"; + const result = await OAuthInterface.validateRedirectUri( + redirectUri, + loopbackClient + ); + expect(result).toBe(true); + }); + + it("should reject HTTPS loopback redirect URIs (must be HTTP per RFC 8252)", async () => { + const httpsLoopbackClient = { + ...client, + redirectUris: ["https://localhost:8080/callback"], + }; + const redirectUri = "https://localhost:8080/callback"; + const result = await OAuthInterface.validateRedirectUri( + redirectUri, + httpsLoopbackClient + ); + expect(result).toBe(true); // HTTPS is allowed, just not required for loopback + }); + + it("should reject loopback redirect URI with fragment", async () => { + const loopbackClient = { + ...client, + redirectUris: ["http://127.0.0.1:8080/callback#fragment"], + }; + const redirectUri = "http://127.0.0.1:8080/callback#fragment"; + const result = await OAuthInterface.validateRedirectUri( + redirectUri, + loopbackClient + ); + expect(result).toBe(false); // Fragment check happens first + }); }); describe("#validateScope", () => { diff --git a/server/utils/oauth/OAuthInterface.ts b/server/utils/oauth/OAuthInterface.ts index 5a97f898bc..3885c35f05 100644 --- a/server/utils/oauth/OAuthInterface.ts +++ b/server/utils/oauth/OAuthInterface.ts @@ -343,6 +343,7 @@ export const OAuthInterface: RefreshTokenModel & /** * Ensure the redirect URI is not plain HTTP. Custom protocols are allowed. + * Loopback addresses (RFC 8252 §7.3) are allowed with http:// for native apps. * * @param uri The redirect URI to validate. * @returns True if the URI is valid, false otherwise. @@ -354,6 +355,24 @@ export const OAuthInterface: RefreshTokenModel & if (!client.redirectUris?.includes(uri)) { return false; } + + // Allow loopback redirects for native/CLI apps (RFC 8252 §7.3) + // Loopback addresses must use http:// (not https://) since TLS certificates + // cannot be obtained for loopback addresses. + try { + const url = new URL(uri); + const isLoopback = + url.hostname === "127.0.0.1" || + url.hostname === "[::1]" || + url.hostname === "localhost"; + + if (isLoopback && url.protocol === "http:") { + return true; + } + } catch { + // Invalid URL, will be caught by isUrl check below + } + if (!isUrl(uri, { requireHttps: true })) { return false; }