fix: retry next endpoint on CORS error during auth server discovery (#827)

This commit is contained in:
Xiao Yijun 2025-08-04 22:43:09 +08:00 • committed by GitHub
commit f657ead472
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 1 deletions

View file

@ -899,6 +899,18 @@ describe("OAuth Authorization", () => {
"MCP-Protocol-Version": "2025-01-01"
});
});
it("returns undefined when all URLs fail with CORS errors", async () => {
// All fetch attempts fail with CORS errors (TypeError)
mockFetch.mockImplementation(() => Promise.reject(new TypeError("CORS error")));
const metadata = await discoverAuthorizationServerMetadata("https://auth.example.com/tenant1");
expect(metadata).toBeUndefined();
// Verify that all discovery URLs were attempted
expect(mockFetch).toHaveBeenCalledTimes(8); // 4 URLs × 2 attempts each (with and without headers)
});
});
describe("startAuthorization", () => {

View file

@ -758,7 +758,11 @@ export async function discoverAuthorizationServerMetadata(
const response = await fetchWithCorsRetry(endpointUrl, headers, fetchFn);
if (!response) {
throw new Error(`CORS error trying to load ${type === 'oauth' ? 'OAuth' : 'OpenID provider'} metadata from ${endpointUrl}`);
/**
* CORS error occurred - don't throw as the endpoint may not allow CORS,
* continue trying other possible endpoints
*/
continue;
}
if (!response.ok) {