all: use case-sensitive JSON unmarshaling (#807)

As reported in
https://github.com/modelcontextprotocol/go-sdk/issues/805, case
insensitive unmarshaling can pose security issues. This change adjusts
all non-test and non-example unmarshaling to use a case sensitive mode.

This introduces a new external dependency
`github.com/segmentio/encoding`.
This commit is contained in:
Maciej Kisiel 2026-02-18 16:38:36 +01:00 • committed by Maciek Kisiel
commit 6e8ca568e4
No known key found for this signature in database
8 changed files with 127 additions and 20 deletions

View file

@ -6,7 +6,6 @@ package mcp
import (
"context"
"encoding/json"
"errors"
"fmt"
"iter"
@ -18,7 +17,7 @@ import (
"time"
"github.com/google/jsonschema-go/jsonschema"
"github.com/modelcontextprotocol/go-sdk/internal/json"
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
"github.com/modelcontextprotocol/go-sdk/jsonrpc"
)

View file

@ -13,6 +13,8 @@ package mcp
import (
"encoding/json"
"fmt"
internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
)
// Optional annotations for the client. The client can use annotations to inform
@ -140,7 +142,7 @@ func (x *CallToolResult) UnmarshalJSON(data []byte) error {
res
Content []*wireContent `json:"content"`
}
if err := json.Unmarshal(data, &wire); err != nil {
if err := internaljson.Unmarshal(data, &wire); err != nil {
return err
}
var err error
@ -286,7 +288,7 @@ type CompleteReference struct {
func (r *CompleteReference) UnmarshalJSON(data []byte) error {
type wireCompleteReference CompleteReference // for naive unmarshaling
var r2 wireCompleteReference
if err := json.Unmarshal(data, &r2); err != nil {
if err := internaljson.Unmarshal(data, &r2); err != nil {
return err
}
switch r2.Type {
@ -402,7 +404,7 @@ func (r *CreateMessageResult) UnmarshalJSON(data []byte) error {
result
Content *wireContent `json:"content"`
}
if err := json.Unmarshal(data, &wire); err != nil {
if err := internaljson.Unmarshal(data, &wire); err != nil {
return err
}
var err error
@ -838,7 +840,7 @@ func (m *PromptMessage) UnmarshalJSON(data []byte) error {
msg
Content *wireContent `json:"content"`
}
if err := json.Unmarshal(data, &wire); err != nil {
if err := internaljson.Unmarshal(data, &wire); err != nil {
return err
}
var err error
@ -1014,7 +1016,7 @@ func (m *SamplingMessage) UnmarshalJSON(data []byte) error {
msg
Content *wireContent `json:"content"`
}
if err := json.Unmarshal(data, &wire); err != nil {
if err := internaljson.Unmarshal(data, &wire); err != nil {
return err
}
var err error

View file

@ -24,6 +24,7 @@ import (
"time"
"github.com/google/jsonschema-go/jsonschema"
internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
"github.com/modelcontextprotocol/go-sdk/internal/util"
"github.com/modelcontextprotocol/go-sdk/jsonrpc"
@ -326,7 +327,7 @@ func toolForErr[In, Out any](t *Tool, h ToolHandlerFor[In, Out], cache *SchemaCa
// Unmarshal and validate args.
var in In
if input != nil {
if err := json.Unmarshal(input, &in); err != nil {
if err := internaljson.Unmarshal(input, &in); err != nil {
return nil, fmt.Errorf("%w: %v", jsonrpc2.ErrInvalidParams, err)
}
}
@ -1325,7 +1326,7 @@ func initializeMethodInfo() methodInfo {
info.unmarshalParams = func(m json.RawMessage) (Params, error) {
var params *initializeParamsV2
if m != nil {
if err := json.Unmarshal(m, &params); err != nil {
if err := internaljson.Unmarshal(m, &params); err != nil {
return nil, fmt.Errorf("unmarshaling %q into a %T: %w", m, params, err)
}
}

View file

@ -23,6 +23,7 @@ import (
"time"
"github.com/modelcontextprotocol/go-sdk/auth"
internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
"github.com/modelcontextprotocol/go-sdk/jsonrpc"
)
@ -283,7 +284,7 @@ func newMethodInfo[P paramsPtr[T], R Result, T any](flags methodFlags) methodInf
unmarshalParams: func(m json.RawMessage) (Params, error) {
var p P
if m != nil {
if err := json.Unmarshal(m, &p); err != nil {
if err := internaljson.Unmarshal(m, &p); err != nil {
return nil, fmt.Errorf("unmarshaling %q into a %T: %w", m, p, err)
}
}

View file

@ -28,6 +28,7 @@ import (
"time"
"github.com/modelcontextprotocol/go-sdk/auth"
internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
"github.com/modelcontextprotocol/go-sdk/internal/xcontext"
"github.com/modelcontextprotocol/go-sdk/jsonrpc"
@ -1074,7 +1075,7 @@ func (c *streamableServerConn) servePOST(w http.ResponseWriter, req *http.Reques
isInitialize = true
// Extract the protocol version from InitializeParams.
var params InitializeParams
if err := json.Unmarshal(jreq.Params, &params); err == nil {
if err := internaljson.Unmarshal(jreq.Params, &params); err == nil {
initializeProtocolVersion = params.ProtocolVersion
}
}

View file

@ -11,6 +11,7 @@ import (
"strings"
"github.com/google/jsonschema-go/jsonschema"
internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
)
// A ToolHandler handles a call to tools/call.
@ -83,7 +84,7 @@ func applySchema(data json.RawMessage, resolved *jsonschema.Resolved) (json.RawM
if resolved != nil {
v := make(map[string]any)
if len(data) > 0 {
if err := json.Unmarshal(data, &v); err != nil {
if err := internaljson.Unmarshal(data, &v); err != nil {
return nil, fmt.Errorf("unmarshaling arguments: %w", err)
}
}

View file

@ -15,6 +15,7 @@ import (
"os"
"sync"
internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
"github.com/modelcontextprotocol/go-sdk/internal/xcontext"
"github.com/modelcontextprotocol/go-sdk/jsonrpc"
@ -189,7 +190,7 @@ type canceller struct {
func (c *canceller) Preempt(ctx context.Context, req *jsonrpc.Request) (result any, err error) {
if req.Method == notificationCancelled {
var params CancelledParams
if err := json.Unmarshal(req.Params, &params); err != nil {
if err := internaljson.Unmarshal(req.Params, &params); err != nil {
return nil, err
}
id, err := jsonrpc2.MakeID(params.RequestID)
@ -565,7 +566,7 @@ func (t *ioConn) Read(ctx context.Context) (jsonrpc.Message, error) {
func readBatch(data []byte) (msgs []jsonrpc.Message, isBatch bool, _ error) {
// Try to read an array of messages first.
var rawBatch []json.RawMessage
if err := json.Unmarshal(data, &rawBatch); err == nil {
if err := internaljson.Unmarshal(data, &rawBatch); err == nil {
if len(rawBatch) == 0 {
return nil, true, fmt.Errorf("empty batch")
}

View file

@ -7,6 +7,8 @@ package mcp
import (
"crypto/rand"
"encoding/json"
internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
)
func assert(cond bool, msg string) {
@ -36,7 +38,7 @@ func remarshal(from, to any) error {
if err != nil {
return err
}
if err := json.Unmarshal(data, to); err != nil {
if err := internaljson.Unmarshal(data, to); err != nil {
return err
}
return nil