Skip to content

Emit conformant JSON-RPC on authz denial responses - #5955

Closed
stantheman0128 wants to merge 1 commit into
stacklok:mainfrom
stantheman0128:fix/5950-authz-jsonrpc
Closed

stantheman0128 wants to merge 1 commit into
stacklok:mainfrom
stantheman0128:fix/5950-authz-jsonrpc

Conversation

@stantheman0128

Copy link
Copy Markdown
Contributor

Summary

Authz denial responses were not valid JSON-RPC 2.0. handleUnauthorized encoded a *jsonrpc2.Response with encoding/json directly. That struct has no json tags / MarshalJSON, so the wire body used capitalized keys (Result / Error / ID) and omitted the mandatory "jsonrpc":"2.0" field. Spec-strict MCP clients cannot read error / id on a 403.

What changed

  • Build the denial body as a lowercase map[string]any with "jsonrpc":"2.0", "id", and "error" (same pattern as classificationErrorBody / filteredToolCallErrorBody)
  • Apply the same envelope to the sibling response-filter failure path (writeErrorResponse / filteringErrorBody), which had the same direct-marshal bug
  • Add wire-byte regression tests for both helpers plus an HTTP product-path test for server/discover 403

Fixes #5950

Type of change

  • Bug fix

Test plan

  • Unit / package tests (go test ./pkg/authz/ ...)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

Evidence

Before (issue report, POST /mcp server/discover with authz enabled):

{"Result":null,"Error":{"code":403,"message":"unknown MCP method: server/discover (not configured for authorization)"},"ID":{}}

After fix, HTTP product-path wire bytes from TestHandleUnauthorizedHTTPWireBytes:

{"error":{"code":403,"message":"unknown MCP method: server/discover (not configured for authorization)"},"id":42,"jsonrpc":"2.0"}

Targeted tests (Windows):

go test ./pkg/authz/ -count=1 -run "UnauthorizedErrorBody|HandleUnauthorizedHTTP|FilteringErrorBody" -v
--- PASS: TestUnauthorizedErrorBodyJSONRPCConformant (0.00s)
--- PASS: TestFilteringErrorBodyJSONRPCConformant (0.00s)
--- PASS: TestHandleUnauthorizedHTTPWireBytes (0.00s)
PASS
ok  	github.com/stacklok/toolhive/pkg/authz	3.155s

Full package:

go test ./pkg/authz/ -count=1
ok  	github.com/stacklok/toolhive/pkg/authz	2.624s

What was not tested

  • Live authz-enabled proxy against a real MCP go-sdk client on this box (no local authz proxy harness here). Product path covered via ParsingMiddleware + Middleware HTTP recorder with the issue's server/discover method.
  • Full task lint-fix / e2e suite (targeted package tests only).

Does this introduce a user-facing change?

Yes. Authorization-denied MCP responses now use a JSON-RPC 2.0 envelope (jsonrpc, id, error lowercase). Clients that already accepted the capitalized keys via case-insensitive unmarshal remain compatible; spec-strict clients can now parse the denial.

Special notes for reviewers

  • AI assistance: Cursor/Grok; human reviewed; Evidence is from commands run on this machine.
  • Sibling coverage: response-filter internal error encoding used the same broken marshal path and is fixed in this PR.

AI disclosure

This change was developed with AI assistance (Cursor/Grok). The author reviewed the diff, ran the Evidence commands above, and takes responsibility for the patch.

@jhrozek

jhrozek commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Thanks for taking a crack at this, and for the clear write-up plus the up-front note about AI assistance.

I'm closing this one on process grounds rather than technical ones. Our contributing guide asks that you comment on an issue and wait for a maintainer to assign it before starting work: https://github.com/stacklok/toolhive/blob/main/CONTRIBUTING.md#claiming-an-issue. #5950 had no comments and no assignee when this PR showed up. That step is there specifically to avoid duplicated effort, and that's what happened here — the fix was already scoped on our side, with a wider blast radius than this PR covers (the same direct-marshal bug exists in the validating and mutating webhook middlewares, and their tests currently assert the capitalized keys, so those two sites would have stayed broken behind a closed issue).

One other thing for next time: commits need a Signed-off-by trailer for the DCO.

For what it's worth, driving the assertion through ParsingMiddleware + Middleware with the real server/discover body was the right instinct, and we'll be doing the same in the fix that lands.

If you'd like to pick something else up, drop a comment on an issue that interests you and we'll get you assigned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Authz-denied responses emit non-conformant JSON-RPC (capitalized keys, no jsonrpc field)

2 participants