fix(mcp): fail clearly when OAuth callback port is in use#31490
Open
WUKUNTAI-0211 wants to merge 1 commit into
Open
fix(mcp): fail clearly when OAuth callback port is in use#31490WUKUNTAI-0211 wants to merge 1 commit into
WUKUNTAI-0211 wants to merge 1 commit into
Conversation
Contributor
|
The following comment was made by an LLM, it may be inaccurate: No duplicate PRs found. The search found two related PRs (#31013 and #30022) that address OAuth callback issues in MCP, but they focus on different aspects (proxied callbacks and IPv4 loopback binding) rather than the port-in-use detection that PR #31490 is addressing. |
Previously, when the OAuth callback port (19876 by default) was already in use, ensureRunning silently returned. The OAuth flow then opened a browser and waited for a callback that would never reach this opencode instance — surfacing as a misleading "Invalid or expired state parameter - potential CSRF attack" error. Now ensureRunning throws with a clear message telling the user to set oauth.callbackPort (or oauth.redirectUri) on the MCP server entry. Both fields already exist in the schema; this just stops the silent fallback from masking the real cause.
bfde9a3 to
47f320c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #30888
Also addresses the same root cause as the long-standing #23562 / #23563 / #23568.
Type of change
What does this PR do?
ensureRunning()inpackages/opencode/src/mcp/oauth-callback.tspreviously silently returned when127.0.0.1:19876(or the configured port) was already in use. That left the OAuth flow waiting on a callback that landed in whatever other process owned the port, and the user saw a misleadingInvalid or expired state parameter - potential CSRF attackmessage — pointing them at a CSRF / cookie / browser issue when the real cause was just "the callback port is occupied".This change makes
ensureRunning()throw a clear error that names the config knobs that already exist for this case:Diff is 9 lines in one file.
Behaviour change to flag: this removes the silent fallback that allowed two opencode instances on the same host to share one callback server. AFAICT that fallback was already broken —
pendingAuthsis in-process memory, so the second instance'sstateis never known to the first instance's HTTP handler and the auth fails anyway (which is exactly what #23562 / #23563 / #23568 are reporting). The simpler fix is to stop pretending the fallback works and tell the user how to pick a different port. Happy to switch to a probe (e.g. GET the path and check for our 400 response body) if you'd rather preserve cross-instance sharing as a real feature later.How did you verify your code works?
Locally reproduced the original bug by holding
:19876with a Python listener and runningopencode mcp auth Notion→ got the misleading CSRF error. After this patch, the same setup produces the new error and exits cleanly:bun turbo typecheck --filter=opencodepasses.Screenshots / recordings
N/A — CLI behaviour change, before/after captured in the block above.
Checklist