Skip to content

Commit b5832b6

Browse files
committed
feat: enhance trusted origins resolution for SSO provider registration
1 parent 698729d commit b5832b6

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

server/utils/auth.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ function resolveTrustedOrigins(baseUrl: string): string[] | ((request?: Request)
4848
const isSsoFlow = url.includes("/sso/") || url.includes("/sign-in/sso");
4949
if (!isSsoFlow) return staticOrigins;
5050

51+
const allOrigins = [...staticOrigins];
52+
53+
// During SSO provider registration, also trust the issuer being registered
54+
// so better-auth can fetch its OIDC discovery document.
55+
try {
56+
const cloned = request.clone();
57+
const body = await cloned.json();
58+
if (body?.issuer) {
59+
allOrigins.push(new URL(body.issuer).origin);
60+
}
61+
} catch {
62+
// Not all SSO requests have a parseable JSON body
63+
}
64+
5165
// Dynamically load registered SSO provider issuers
5266
try {
5367
const providers = await db
@@ -60,11 +74,12 @@ function resolveTrustedOrigins(baseUrl: string): string[] | ((request?: Request)
6074
})
6175
.filter((o): o is string => o !== null);
6276

63-
return Array.from(new Set([...staticOrigins, ...idpOrigins]));
77+
allOrigins.push(...idpOrigins);
6478
} catch {
65-
// Table may not exist yet (pre-migration) — fall back to static
66-
return staticOrigins;
79+
// Table may not exist yet (pre-migration) — fall back
6780
}
81+
82+
return Array.from(new Set(allOrigins));
6883
};
6984
}
7085

0 commit comments

Comments
 (0)