go-sdk icon indicating copy to clipboard operation
go-sdk copied to clipboard

oauthex: RegisterClient fails when the registration response carries "jwks" as a JSON object

Open clearly opened this issue 1 month ago • 2 comments

Describe the bug

oauthex.ClientRegistrationMetadata.JWKS is declared as string (oauthex/dcr.go:80 in v1.8.0, still the same on main):

JWKS string `json:"jwks,omitempty"`

RFC 7591 §2 defines jwks as a JSON object: "The value of this field MUST be a JSON object containing a valid JWK Set." The client information response (§3.2.1) MUST echo all registered metadata. So a spec-conforming authorization server that returns jwks, even an empty {}, makes RegisterClient fail with a decode error, and dynamic client registration through auth.AuthorizationCodeHandler fails with it.

We hit this with Ory Hydra v26.2.0, which includes "jwks": {} in every DCR response, so DCR against Hydra never succeeds. The same type is used when sending a registration request, so a caller can't send a by-value JWK Set correctly either: it would be marshalled as a JSON string.

To Reproduce

package main

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/modelcontextprotocol/go-sdk/oauthex"
)

func main() {
	// Registration endpoint returning an RFC 7591 response whose "jwks" is an
	// object (shape mirrors Ory Hydra v26.2.0).
	as := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusCreated)
		fmt.Fprint(w, `{
		  "client_id": "example-client-id",
		  "redirect_uris": ["http://127.0.0.1:7777/callback"],
		  "token_endpoint_auth_method": "none",
		  "jwks": {}
		}`)
	}))
	defer as.Close()

	res, err := oauthex.RegisterClient(context.Background(), as.URL+"/register",
		&oauthex.ClientRegistrationMetadata{
			ClientName:   "repro",
			RedirectURIs: []string{"http://127.0.0.1:7777/callback"},
		}, as.Client())
	if err != nil {
		fmt.Println("RegisterClient error:", err)
		return
	}
	fmt.Println("registered client_id:", res.ClientID)
}

Expected behavior

registered client_id: example-client-id

Actual behavior (v1.8.0 and main @ 3ebc6d1)

RegisterClient error: failed to decode successful registration response: json: cannot unmarshal "{}\n\t\t}" into Go struct field struct { ClientIDIssuedAt int64 "json:\"client_id_issued_at,omitempty\""; ClientSecretExpiresAt int64 "json:\"client_secret_expires_at,omitempty\""; *oauthex.alias }.jwks of type string (<response body>)

(I've cut the response body that the error appends. That echo is being reported separately through the security advisory process.)

Environment

  • go-sdk v1.8.0; also reproduced on main at 3ebc6d1
  • go version go1.27.2 linux/amd64 (repro module declares go 1.25.0)
  • Authorization server where we found it: Ory Hydra v26.2.0 (dev mode)

References

  • RFC 7591 §2, Client Metadata, jwks: https://www.rfc-editor.org/rfc/rfc7591#section-2
  • RFC 7591 §3.2.1, Client Information Response: https://www.rfc-editor.org/rfc/rfc7591#section-3.2.1
  • RFC 7517 §5, JWK Set Format (a JSON object with a keys member): https://www.rfc-editor.org/rfc/rfc7517#section-5
  • MCP 2026-07-28, Client Registration → Dynamic Client Registration: https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/client-registration

Suggested fix

Changing the field's type is a breaking change under CONTRIBUTING's versioning policy, so here are two options:

  1. No API change (minimal): decode jwks leniently. In ClientRegistrationResponse.UnmarshalJSON, shadow jwks in the aux struct as json.RawMessage. If it's a JSON string, unquote it. Otherwise store the raw JSON text in JWKS. On marshal, emit JWKS as raw JSON when it holds a JSON object. Document JWKS as "the JWK Set as JSON text."
  2. Proper typing (proposal): add a JWKSet json.RawMessage field (or a typed JWK Set), deprecate JWKS string, and have both marshal and unmarshal use the new field.

Either way, a regression test should decode {"client_id":"x","jwks":{}} and {"client_id":"x","jwks":{"keys":[]}}.

Additional context

Our workaround is an http.RoundTripper on AuthorizationCodeHandlerConfig.Client that rewrites "jwks":{…} out of registration responses before the SDK decodes them.

clearly avatar Oct 20 '26 20:10 clearly

I'd like to send a fix for this. Since changing ClientRegistrationMetadata.JWKS away from string would break the API, I'd go with the first option in the issue: in ClientRegistrationResponse.UnmarshalJSON (oauthex/dcr.go), add a JWKS json.RawMessage field with the jwks tag to the aux struct so it shadows the embedded one, unquote it if it's a JSON string, and otherwise store the raw JSON text in JWKS, with MarshalJSON emitting it back as an object. I'd add table tests for "jwks": {}, {"keys": []} and the legacy string form, and leave the request side and the error-body echo out of this change. Is that acceptable as a bug fix, or since it changes what JWKS can hold, would you rather it go through the proposal process (e.g. a new JWKSet json.RawMessage field and deprecating JWKS)? Happy to open the PR once there's a direction.

kerthans avatar Oct 21 '26 02:10 kerthans

@kerthans you can open the PR, but i would add a custom Marshal and UnmarshalJSON directly to the ClientRegistrationMetadata rather than the ClientRegistrationResponse. In this way we can target both conversion scenarios.

guglielmo-san avatar Oct 21 '26 16:10 guglielmo-san