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
15 changed files with 127 additions and 20 deletions

6
go.mod
View file

@ -6,7 +6,13 @@ require (
github.com/golang-jwt/jwt/v5 v5.2.2 github.com/golang-jwt/jwt/v5 v5.2.2
github.com/google/go-cmp v0.7.0 github.com/google/go-cmp v0.7.0
github.com/google/jsonschema-go v0.4.2 github.com/google/jsonschema-go v0.4.2
github.com/segmentio/encoding v0.5.3
github.com/yosida95/uritemplate/v3 v3.0.2 github.com/yosida95/uritemplate/v3 v3.0.2
golang.org/x/oauth2 v0.30.0 golang.org/x/oauth2 v0.30.0
golang.org/x/tools v0.34.0 golang.org/x/tools v0.34.0
) )
require (
github.com/segmentio/asm v1.1.3 // indirect
golang.org/x/sys v0.35.0 // indirect
)

6
go.sum
View file

@ -4,9 +4,15 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/jsonschema-go v0.4.2 h1:tmrUohrwoLZZS/P3x7ex0WAVknEkBZM46iALbcqoRA8= github.com/google/jsonschema-go v0.4.2 h1:tmrUohrwoLZZS/P3x7ex0WAVknEkBZM46iALbcqoRA8=
github.com/google/jsonschema-go v0.4.2/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE= github.com/google/jsonschema-go v0.4.2/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE=
github.com/segmentio/asm v1.1.3 h1:WM03sfUOENvvKexOLp+pCqgb/WDjsi7EK8gIsICtzhc=
github.com/segmentio/asm v1.1.3/go.mod h1:Ld3L4ZXGNcSLRg4JBsZ3//1+f/TjYl0Mzen/DQy1EJg=
github.com/segmentio/encoding v0.5.3 h1:OjMgICtcSFuNvQCdwqMCv9Tg7lEOXGwm1J5RPQccx6w=
github.com/segmentio/encoding v0.5.3/go.mod h1:HS1ZKa3kSN32ZHVZ7ZLPLXWvOVIiZtyJnO1gPH1sKt0=
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4= github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4= github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI= golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI=
golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU= golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU=
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo= golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo=
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg= golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg=

19
internal/json/json.go Normal file
View file

@ -0,0 +1,19 @@
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// Package json provides internal JSON utilities.
package json
import (
"bytes"
"github.com/segmentio/encoding/json"
)
func Unmarshal(data []byte, v any) error {
dec := json.NewDecoder(bytes.NewReader(data))
dec.DontMatchCaseInsensitiveStructFields()
return dec.Decode(v)
}

View file

@ -0,0 +1,63 @@
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package json
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestUnmarshalCaseSensitivity(t *testing.T) {
type Nested struct {
Field string `json:"field"`
}
type Target struct {
Field string
TaggedField string `json:"custom_tag"`
Nested *Nested
}
tests := []struct {
name string
json string
want Target
}{
{
name: "exact match",
json: `{"Field": "value", "custom_tag": "tagged", "Nested": {"field": "nested"}}`,
want: Target{
Field: "value",
TaggedField: "tagged",
Nested: &Nested{
Field: "nested",
},
},
},
{
name: "case mismatch",
json: `{"field": "value", "Custom_tag": "tagged", "Nested": {"Field": "nested"}}`,
want: Target{
Field: "",
TaggedField: "",
Nested: &Nested{
Field: "",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got Target
if err := Unmarshal([]byte(tt.json), &got); err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("Unmarshal mismatch (-want +got):\n%s", diff)
}
})
}
}

View file

@ -6,13 +6,14 @@ package jsonrpc2
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/modelcontextprotocol/go-sdk/internal/json"
) )
// Binder builds a connection configuration. // Binder builds a connection configuration.

View file

@ -8,6 +8,8 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
) )
// ID is a Request identifier, which is defined by the spec to be a string, integer, or null. // ID is a Request identifier, which is defined by the spec to be a string, integer, or null.
@ -167,7 +169,7 @@ func EncodeIndent(msg Message, prefix, indent string) ([]byte, error) {
func DecodeMessage(data []byte) (Message, error) { func DecodeMessage(data []byte) (Message, error) {
msg := wireCombined{} msg := wireCombined{}
if err := json.Unmarshal(data, &msg); err != nil { if err := internaljson.Unmarshal(data, &msg); err != nil {
return nil, fmt.Errorf("unmarshaling jsonrpc message: %w", err) return nil, fmt.Errorf("unmarshaling jsonrpc message: %w", err)
} }
if msg.VersionTag != wireVersion { if msg.VersionTag != wireVersion {

View file

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

View file

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

View file

@ -24,6 +24,7 @@ import (
"time" "time"
"github.com/google/jsonschema-go/jsonschema" "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/jsonrpc2"
"github.com/modelcontextprotocol/go-sdk/internal/util" "github.com/modelcontextprotocol/go-sdk/internal/util"
"github.com/modelcontextprotocol/go-sdk/jsonrpc" "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. // Unmarshal and validate args.
var in In var in In
if input != nil { 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) return nil, fmt.Errorf("%w: %v", jsonrpc2.ErrInvalidParams, err)
} }
} }
@ -1325,7 +1326,7 @@ func initializeMethodInfo() methodInfo {
info.unmarshalParams = func(m json.RawMessage) (Params, error) { info.unmarshalParams = func(m json.RawMessage) (Params, error) {
var params *initializeParamsV2 var params *initializeParamsV2
if m != nil { 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) return nil, fmt.Errorf("unmarshaling %q into a %T: %w", m, params, err)
} }
} }

View file

@ -23,6 +23,7 @@ import (
"time" "time"
"github.com/modelcontextprotocol/go-sdk/auth" "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/jsonrpc2"
"github.com/modelcontextprotocol/go-sdk/jsonrpc" "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) { unmarshalParams: func(m json.RawMessage) (Params, error) {
var p P var p P
if m != nil { 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) return nil, fmt.Errorf("unmarshaling %q into a %T: %w", m, p, err)
} }
} }

View file

@ -28,6 +28,7 @@ import (
"time" "time"
"github.com/modelcontextprotocol/go-sdk/auth" "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/jsonrpc2"
"github.com/modelcontextprotocol/go-sdk/internal/xcontext" "github.com/modelcontextprotocol/go-sdk/internal/xcontext"
"github.com/modelcontextprotocol/go-sdk/jsonrpc" "github.com/modelcontextprotocol/go-sdk/jsonrpc"
@ -1074,7 +1075,7 @@ func (c *streamableServerConn) servePOST(w http.ResponseWriter, req *http.Reques
isInitialize = true isInitialize = true
// Extract the protocol version from InitializeParams. // Extract the protocol version from InitializeParams.
var params 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 initializeProtocolVersion = params.ProtocolVersion
} }
} }

View file

@ -11,6 +11,7 @@ import (
"strings" "strings"
"github.com/google/jsonschema-go/jsonschema" "github.com/google/jsonschema-go/jsonschema"
internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
) )
// A ToolHandler handles a call to tools/call. // 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 { if resolved != nil {
v := make(map[string]any) v := make(map[string]any)
if len(data) > 0 { 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) return nil, fmt.Errorf("unmarshaling arguments: %w", err)
} }
} }

View file

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

View file

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

View file

@ -17,6 +17,8 @@ import (
"io" "io"
"net/http" "net/http"
"time" "time"
internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
) )
// ClientRegistrationMetadata represents the client metadata fields for the DCR POST request (RFC 7591). // ClientRegistrationMetadata represents the client metadata fields for the DCR POST request (RFC 7591).
@ -144,7 +146,7 @@ func (r *ClientRegistrationResponse) UnmarshalJSON(data []byte) error {
}{ }{
alias: (*alias)(r), alias: (*alias)(r),
} }
if err := json.Unmarshal(data, &aux); err != nil { if err := internaljson.Unmarshal(data, &aux); err != nil {
return err return err
} }
if aux.ClientIDIssuedAt != 0 { if aux.ClientIDIssuedAt != 0 {
@ -206,7 +208,7 @@ func RegisterClient(ctx context.Context, registrationEndpoint string, clientMeta
if resp.StatusCode == http.StatusCreated { if resp.StatusCode == http.StatusCreated {
var regResponse ClientRegistrationResponse var regResponse ClientRegistrationResponse
if err := json.Unmarshal(body, &regResponse); err != nil { if err := internaljson.Unmarshal(body, &regResponse); err != nil {
return nil, fmt.Errorf("failed to decode successful registration response: %w (%s)", err, string(body)) return nil, fmt.Errorf("failed to decode successful registration response: %w (%s)", err, string(body))
} }
if regResponse.ClientID == "" { if regResponse.ClientID == "" {
@ -221,7 +223,7 @@ func RegisterClient(ctx context.Context, registrationEndpoint string, clientMeta
if resp.StatusCode == http.StatusBadRequest { if resp.StatusCode == http.StatusBadRequest {
var regError ClientRegistrationError var regError ClientRegistrationError
if err := json.Unmarshal(body, &regError); err != nil { if err := internaljson.Unmarshal(body, &regError); err != nil {
return nil, fmt.Errorf("failed to decode registration error response: %w (%s)", err, string(body)) return nil, fmt.Errorf("failed to decode registration error response: %w (%s)", err, string(body))
} }
return nil, &regError return nil, &regError