Skip to content

Clean up JSON-RPC error codes and leaked error text on denial paths #6039

Closed

Description

@jhrozek

Two small consistency problems found while fixing #5950, both deliberately left out of that fix because neither is the envelope bug and both are independently reviewable. Grouped here because they live in the same handful of functions.

1. HTTP statuses used as JSON-RPC error codes

Several paths pass the HTTP status straight through as the JSON-RPC error.code:

  • pkg/authz/response_filter.go — writeErrorResponse uses a literal 500
  • pkg/webhook/validating/middleware.go and pkg/webhook/mutating/middleware.go — sendErrorResponse does jsonrpc2.NewError(int64(statusCode), message), so the code is whatever the status is: 403, 413, 422, or 500

These are legal (they sit outside the reserved -32768..-32000 range) but semantically wrong, and the draft spec is explicit that the standard codes are the right home for general failures: "MCP uses the standard JSON-RPC 2.0 error codes (-32700, -32600 to -32603) for general protocol failures."

Suggested mapping:

  • 500 → -32603 (INTERNAL_ERROR). Covers "Error filtering response", plus the webhook internal failures: failed to read body, webhook error, unsupported/malformed/invalid patch, patch not scoped to mcp_request, apply/extract failures.
  • 413 → -32600 (INVALID_REQUEST). An oversized body is a malformed request, not a novel error class.
  • 422 → mcp.JSONRPCCodeDenied. pkg/webhook/mutating/middleware.go uses 422 for "Request denied by webhook policy", which is semantically a denial that happens to carry 422 as the webhook signal. Emitting 422 as the JSON-RPC code makes one denial look unlike every other denial, defeating the stated purpose of the JSONRPCCodeDenied constant ("so a denial is represented by one code across thv run and vMCP").
  • 403 stays as-is. It is the one case where status and code legitimately coincide, and pkg/mcp/errors.go documents why. The draft spec backs it: "New error codes for purposes not defined by this specification SHOULD be allocated outside the JSON-RPC reserved range."

That 403 happens to read sensibly is probably what let the pattern spread to the three cases where it does not.

Why this was deferred rather than folded into #5950: it is a client-visible behavior change. The codes are asserted across pkg/vmcp/server/*_test.go and documented at docs/arch/10-virtual-mcp-architecture.md:631, so it needs its own diff and its own review.

Impact is modest and worth stating plainly: the message text and HTTP status are both correct today, so nothing hangs or misreports. It only affects clients that branch on JSON-RPC codes, which will not recognize 500 as internal-error or 422 as a denial.

2. Denial paths leak internal error text

pkg/authz/middleware.go forwards the authorizer error verbatim to the caller:

handleUnauthorized(w, parsedRequest.ID,
    fmt.Errorf("unknown MCP method: %s (not configured for authorization)", parsedRequest.Method))

and authorizeAndServe passes Cedar evaluation errors straight through the same way. pkg/authz/response_filter.go similarly sends fmt.Sprintf("Error filtering response: %v", err), where err can originate in policy evaluation and may name tools.

Meanwhile pkg/webhook/validating/middleware.go:165 deliberately does the opposite, with the comment "Prevent information leaks by ignoring the webhook message". So the codebase contradicts itself on the same question.

Scoping this honestly: the recipient is already authenticated, just not authorized, and what leaks is policy-engine structure — not credentials and not another user data. It is a hardening and consistency item rather than a vulnerability. The decision needed is which convention to standardize on: scrub everywhere and log the detail server-side, or accept that authenticated callers may see policy detail. Right now we do both, in adjacent files.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions