fix(sso): retain discovery error handling for invalid URLs

This commit is contained in:
Taesu
2026-10-13 09:24:58 +09:00
parent 4f57f434e0
commit dcc481a4ea
3 changed files with 21 additions and 4 deletions
+1 -1
View File
@@ -2,4 +2,4 @@
"@better-auth/sso": patch
---
Fix SSO discovery and SAML callback URLs for custom base paths, and prevent SAML logout self-redirects.
Preserve registered OIDC and SAML callback URLs and provider IDs when composing URLs. Report invalid discovery issuers as configuration errors and prevent SAML logout self-redirects under custom auth base paths.
+10 -2
View File
@@ -62,6 +62,14 @@ const createMockDiscoveryDocument = (
describe("OIDC Discovery", () => {
describe("computeDiscoveryUrl", () => {
it.each([
"urn:example:id",
"not-a-url",
"https://idp.example.com/a\\b",
])("reports an invalid issuer as a discovery error: %s", (issuer) => {
expect(() => computeDiscoveryUrl(issuer)).toThrow(DiscoveryError);
});
it("should compute discovery URL from issuer without trailing slash", () => {
const url = computeDiscoveryUrl("https://idp.example.com");
expect(url).toBe(
@@ -90,9 +98,9 @@ describe("OIDC Discovery", () => {
);
});
it("does not leave a double slash after a trailing issuer path", () => {
it("preserves repeated slashes in the configured issuer path", () => {
expect(computeDiscoveryUrl("https://idp.example.com/tenant/v1//")).toBe(
"https://idp.example.com/tenant/v1/.well-known/openid-configuration",
"https://idp.example.com/tenant/v1//.well-known/openid-configuration",
);
});
});
+10 -1
View File
@@ -122,7 +122,16 @@ export async function discoverOIDCConfig(
* Handles trailing slashes correctly.
*/
export function computeDiscoveryUrl(issuer: string): string {
return appendURLPath(issuer, "/.well-known/openid-configuration");
try {
return appendURLPath(issuer, "/.well-known/openid-configuration");
} catch (error) {
if (!(error instanceof TypeError)) throw error;
throw new DiscoveryError(
"discovery_invalid_url",
`The issuer URL "${issuer}" cannot be used for discovery.`,
{ url: issuer },
);
}
}
/**