Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/client/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
6 changes: 5 additions & 1 deletion src/client/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down