fix: clear stale metadataFetchErr on successful OAuth discovery fallback - #801
Conversation
|
Connected to Huly®: MCP_G-368 |
WalkthroughClearer metadata error handling: Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested labels
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@client/transport/oauth.go`:
- Line 406: When a discovery path succeeds you must clear any prior error so
stale errors don't persist; ensure every success branch that assigns
h.serverMetadata (including the OpenID discovery success branch that currently
sets h.serverMetadata but can still return the old h.metadataFetchErr) also sets
h.metadataFetchErr = nil. Locate the success branches in the function handling
discovery (where h.serverMetadata is assigned during OAuth and OpenID discovery)
and add h.metadataFetchErr = nil immediately after assigning h.serverMetadata so
all successful discovery paths uniformly clear the error.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 15a181cc-4a69-40bd-834c-dee17f787a2a
📒 Files selected for processing (1)
client/transport/oauth.go
There was a problem hiding this comment.
🧹 Nitpick comments (1)
client/transport/oauth_test.go (1)
1389-1434: Convert this new scenario to table-driven test format.The case is good, but this new test should follow the repo’s
tests := []struct{ name, ... }pattern for consistency and easier future expansion.As per coding guidelines, "Write table-driven tests using a tests := []struct{ name, ... } pattern".
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@client/transport/oauth_test.go` around lines 1389 - 1434, The test TestOAuthHandler_GetServerMetadata_AuthServerReturnsHTML should be converted into a table-driven test using the repository's tests := []struct{ name string; ... } pattern; refactor the function to define a tests slice (including this scenario as one entry with a descriptive name), loop over tests with t.Run(tc.name, func(t *testing.T){...}), and move the setup of authServer, mcpServer, OAuthConfig, handler (NewOAuthHandler, SetBaseURL) and the call to handler.GetServerMetadata into the per-test subtest body so the same scaffolding can be reused and additional cases added consistently. Ensure each subtest uses require/ assert as before and preserves the same assertions for metadata.AuthorizationEndpoint, TokenEndpoint, and RegistrationEndpoint.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@client/transport/oauth_test.go`:
- Around line 1389-1434: The test
TestOAuthHandler_GetServerMetadata_AuthServerReturnsHTML should be converted
into a table-driven test using the repository's tests := []struct{ name string;
... } pattern; refactor the function to define a tests slice (including this
scenario as one entry with a descriptive name), loop over tests with
t.Run(tc.name, func(t *testing.T){...}), and move the setup of authServer,
mcpServer, OAuthConfig, handler (NewOAuthHandler, SetBaseURL) and the call to
handler.GetServerMetadata into the per-test subtest body so the same scaffolding
can be reused and additional cases added consistently. Ensure each subtest uses
require/ assert as before and preserves the same assertions for
metadata.AuthorizationEndpoint, TokenEndpoint, and RegistrationEndpoint.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 95f12808-6bbc-4068-a9e0-b9938a54cf99
📒 Files selected for processing (1)
client/transport/oauth_test.go
f900465 to
172eece
Compare
35a853c to
b9f2111
Compare
There was a problem hiding this comment.
♻️ Duplicate comments (1)
client/transport/oauth.go (1)
422-422:⚠️ Potential issue | 🟠 MajorStale
metadataFetchErrcan still leak on successful OpenID discoveryGood partial fix, but this still fails when OAuth metadata decode fails first and OpenID discovery succeeds. At Line 475/Line 477,
h.serverMetadatacan be set while an oldh.metadataFetchErrremains, so Line 490 still returns the stale error.Proposed centralized fix
func (h *OAuthHandler) fetchMetadataFromURL(ctx context.Context, metadataURL string) { @@ var metadata AuthServerMetadata if err := json.NewDecoder(resp.Body).Decode(&metadata); err != nil { h.metadataFetchErr = fmt.Errorf("failed to decode metadata response: %w", err) return } h.serverMetadata = &metadata + h.metadataFetchErr = nil }Also applies to: 451-451, 487-487
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@client/transport/oauth.go` at line 422, When h.serverMetadata is assigned on successful OpenID discovery, clear any previous h.metadataFetchErr so a stale error cannot be returned later; update every code path that sets h.serverMetadata (the discovery/parse success branches that currently assign h.serverMetadata) to also set h.metadataFetchErr = nil immediately after the assignment and do both updates while holding the same lock/mutex used for metadata access to avoid races.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@client/transport/oauth.go`:
- Line 422: When h.serverMetadata is assigned on successful OpenID discovery,
clear any previous h.metadataFetchErr so a stale error cannot be returned later;
update every code path that sets h.serverMetadata (the discovery/parse success
branches that currently assign h.serverMetadata) to also set h.metadataFetchErr
= nil immediately after the assignment and do both updates while holding the
same lock/mutex used for metadata access to avoid races.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 3ba72806-dd5e-474e-aa06-19ac69791542
📒 Files selected for processing (2)
client/transport/oauth.goclient/transport/oauth_test.go
✅ Files skipped from review due to trivial changes (1)
- client/transport/oauth_test.go
Description
Clear
metadataFetchErrwhen getDefaultEndpoints() fallback succeeds, so stale decode errors from earlier discovery attempts don't block OAuth.Type of Change
Checklist
Summary by CodeRabbit
Summary by CodeRabbit