Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

fix(oauth): accept loopback redirect URIs from MCP clients that omit application_type (#1304)

Goldflag24eee008646d
diff --git a/server/src/lib/oauth.test.ts b/server/src/lib/oauth.test.ts
index 9a75a5375..bb7eed28d 100644
--- a/server/src/lib/oauth.test.ts
+++ b/server/src/lib/oauth.test.ts
@@ -186,6 +186,45 @@ describe("Better Auth 1.7 migration and OAuth integration", () => {
await database.exec(`UPDATE "oauthClient" SET disabled = false WHERE "clientId" = 'existing-public'`);
});
+ it("infers the application type for clients that omit it", async () => {
+ const register = (body: Record<string, unknown>) =>
+ request("/oauth2/register", {
+ client_name: "Untyped client",
+ token_endpoint_auth_method: "none",
+ grant_types: ["authorization_code", "refresh_token"],
+ response_types: ["code"],
+ ...body,
+ });
+
+ const loopback = await register({ redirect_uris: ["http://localhost:8787/callback"] });
+ expect(loopback.status, await loopback.clone().text()).toBe(201);
+ expect((await loopback.json()).application_type).toBe("native");
+
+ const mixed = await register({
+ redirect_uris: ["http://127.0.0.1:8787/callback", "https://client.example.com/callback"],
+ });
+ expect(mixed.status, await mixed.clone().text()).toBe(201);
+
+ const hosted = await register({ redirect_uris: ["https://client.example.com/callback"] });
+ expect(hosted.status, await hosted.clone().text()).toBe(201);
+ expect((await hosted.json()).application_type).toBe("web");
+
+ // The provider's URL parser strips embedded tabs, so this is https too.
+ const normalized = await register({ redirect_uris: ["ht\ttps://client.example.com/callback"] });
+ expect(normalized.status, await normalized.clone().text()).toBe(201);
+ expect((await normalized.json()).application_type).toBe("web");
+
+ // Inference never widens what is accepted: plain http off loopback is
+ // still refused, and an explicit "web" keeps the strict rule.
+ const remoteHttp = await register({ redirect_uris: ["http://client.example.com/callback"] });
+ expect(remoteHttp.status).toBe(400);
+ const explicitWeb = await register({
+ application_type: "web",
+ redirect_uris: ["http://localhost:8787/callback"],
+ });
+ expect(explicitWeb.status).toBe(400);
+ });
+
it("completes DCR, login, consent, PKCE, refresh and bearer verification", async () => {
const registration = await request("/oauth2/register", {
client_name: "Migration integration client",
diff --git a/server/src/lib/oauth.ts b/server/src/lib/oauth.ts
index 08a3b22d4..0bf00bd75 100644
--- a/server/src/lib/oauth.ts
+++ b/server/src/lib/oauth.ts
@@ -1,6 +1,6 @@
import { mcp } from "@better-auth/mcp";
import { getOAuthProviderApi } from "@better-auth/oauth-provider";
-import { createAuthEndpoint } from "better-auth/api";
+import { createAuthEndpoint, createAuthMiddleware } from "better-auth/api";
import { jwt } from "better-auth/plugins";
import { z } from "zod";
import { ALL_SCOPE_STRINGS, OIDC_STANDARD_SCOPES } from "./scopes.js";
@@ -12,6 +12,19 @@ export function getAuthBaseUrl(env: NodeJS.ProcessEnv = process.env): string {
.replace(/\/api\/auth$/, "");
}
+function hasNonHttpsRedirectUri(redirectUris: unknown): boolean {
+ if (!Array.isArray(redirectUris)) return false;
+ return redirectUris.some(uri => {
+ // Parse like the provider does, so the scheme seen here is the one it
+ // validates. Unparseable entries are left for the provider to reject.
+ try {
+ return typeof uri === "string" && new URL(uri).protocol !== "https:";
+ } catch {
+ return false;
+ }
+ });
+}
+
export function createOAuthPlugins(baseUrl: string) {
const resource = `${baseUrl}/api/mcp`;
const url = new URL(baseUrl);
@@ -45,6 +58,22 @@ export function createOAuthPlugins(baseUrl: string) {
...(supportsOAuth ? [jwt(), mcp(options)] : []),
{
id: "rybbit-oauth",
+ hooks: {
+ before: [
+ {
+ matcher: (ctx: { path?: string }) => ctx.path === "/oauth2/register",
+ // The provider defaults an omitted application_type to "web",
+ // which forbids http://localhost callbacks. Most MCP clients
+ // register a loopback callback and never send the field, so infer
+ // "native" for them. Native validation still vets every URI.
+ handler: createAuthMiddleware(async ctx => {
+ const body = ctx.body as { application_type?: unknown; redirect_uris?: unknown } | undefined;
+ if (!body || body.application_type !== undefined || !hasNonHttpsRedirectUri(body.redirect_uris)) return;
+ return { context: { body: { ...body, application_type: "native" } } };
+ }),
+ },
+ ],
+ },
endpoints: {
// Keep verification inside a Better Auth endpoint context so the
// provider checks signatures, expiry, linked sessions and client state.