Skip to content

fix(mcp): build well-known OAuth body before writing 200 status#2465

Merged
bflad merged 1 commit into
mainfrom
bflad/age-1970-bug-well-known-oauth-handlers-send-200-ok-before-error-paths
May 1, 2026
Merged

fix(mcp): build well-known OAuth body before writing 200 status#2465
bflad merged 1 commit into
mainfrom
bflad/age-1970-bug-well-known-oauth-handlers-send-200-ok-before-error-paths

Conversation

@bflad
Copy link
Copy Markdown
Member

@bflad bflad commented Apr 28, 2026

Summary

  • Linear: AGE-1970
  • The two .well-known OAuth metadata handlers in server/internal/mcp/impl.go called w.WriteHeader(http.StatusOK) before building the JSON body. If body construction failed (json.Marshal error or an unrecognized OAuthServerMetadataResultKind), the handler returned an error to oops.ErrHandle, whose subsequent WriteHeader(code) was silently dropped by net/http — clients saw 200 OK with an error JSON body shaped like {"name":"unexpected","message":"..."}.
  • Extracted body-build-then-write into two package-level helpers (writeOAuthServerMetadataResponse, writeOAuthProtectedResourceMetadataResponse) so the body is fully built before any header is committed. Each helper documents the ordering invariant.
  • Added server/internal/mcp/wellknown_oauth_test.go with happy-path coverage plus a regression test that passes a bogus result kind and asserts the helper returns an error and leaves the ResponseWriter untouched (no Content-Type, empty body) — catches reintroductions of the original ordering.

Notes

https://linear.app/speakeasy/issue/AGE-1970/bug-well-known-oauth-handlers-send-200-ok-before-error-paths-masking

The two .well-known OAuth metadata handlers called WriteHeader(200) before
constructing the JSON body. If body construction failed (marshal error or
unrecognized result kind), oops.ErrHandle's second WriteHeader was silently
dropped by net/http, so clients saw 200 OK with an error JSON body.

Extract the body-build-then-write logic into two package-level helpers and
add a regression test that asserts the helper does not touch the
ResponseWriter when the result kind is unrecognized.
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 28, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
gram-docs-redirect Ready Ready Preview, Comment Apr 28, 2026 1:37pm

Request Review

@linear
Copy link
Copy Markdown

linear Bot commented Apr 28, 2026

Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 28, 2026

🦋 Changeset detected

Latest commit: 1171755

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
server Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@bflad bflad added this pull request to the merge queue May 1, 2026
Merged via the queue into main with commit 88174e4 May 1, 2026
28 of 29 checks passed
@bflad bflad deleted the bflad/age-1970-bug-well-known-oauth-handlers-send-200-ok-before-error-paths branch May 1, 2026 09:30
@github-actions github-actions Bot locked and limited conversation to collaborators May 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants