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:
parent
9600c3bd31
commit
7b8d81c264
9 changed files with 134 additions and 25 deletions
|
|
@ -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"
|
||||
)
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ package mcp
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
|
||||
)
|
||||
|
||||
// A Content is a [TextContent], [ImageContent], [AudioContent],
|
||||
|
|
@ -248,7 +250,7 @@ func (c *ToolResultContent) MarshalJSON() ([]byte, error) {
|
|||
return nil, err
|
||||
}
|
||||
var w wireContent
|
||||
if err := json.Unmarshal(data, &w); err != nil {
|
||||
if err := internaljson.Unmarshal(data, &w); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
contentWire = append(contentWire, &w)
|
||||
|
|
@ -328,11 +330,11 @@ func unmarshalContent(raw json.RawMessage, allow map[string]bool) ([]Content, er
|
|||
}
|
||||
// Try array first, then fall back to single object.
|
||||
var wires []*wireContent
|
||||
if err := json.Unmarshal(raw, &wires); err == nil {
|
||||
if err := internaljson.Unmarshal(raw, &wires); err == nil {
|
||||
return contentsFromWire(wires, allow)
|
||||
}
|
||||
var wire wireContent
|
||||
if err := json.Unmarshal(raw, &wire); err != nil {
|
||||
if err := internaljson.Unmarshal(raw, &wire); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c, err := contentFromWire(&wire, allow)
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"maps"
|
||||
|
||||
internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
|
||||
)
|
||||
|
||||
// Optional annotations for the client. The client can use annotations to inform
|
||||
|
|
@ -141,7 +143,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
|
||||
|
|
@ -315,7 +317,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 {
|
||||
|
|
@ -502,7 +504,7 @@ func (m *SamplingMessageV2) UnmarshalJSON(data []byte) error {
|
|||
msg
|
||||
Content json.RawMessage `json:"content"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &wire); err != nil {
|
||||
if err := internaljson.Unmarshal(data, &wire); err != nil {
|
||||
return err
|
||||
}
|
||||
var err error
|
||||
|
|
@ -542,7 +544,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
|
||||
|
|
@ -605,7 +607,7 @@ func (r *CreateMessageWithToolsResult) UnmarshalJSON(data []byte) error {
|
|||
result
|
||||
Content json.RawMessage `json:"content"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &wire); err != nil {
|
||||
if err := internaljson.Unmarshal(data, &wire); err != nil {
|
||||
return err
|
||||
}
|
||||
var err error
|
||||
|
|
@ -1056,7 +1058,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
|
||||
|
|
@ -1253,7 +1255,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
|
||||
}
|
||||
// Allow text, image, audio, tool_use, and tool_result in sampling messages
|
||||
|
|
|
|||
|
|
@ -25,6 +25,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"
|
||||
|
|
@ -327,7 +328,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)
|
||||
}
|
||||
}
|
||||
|
|
@ -1371,7 +1372,7 @@ func initializeMethodInfo() methodInfo {
|
|||
info.unmarshalParams = func(m json.RawMessage) (Params, error) {
|
||||
var params *initializeParamsV2
|
||||
if m != nil {
|
||||
if err := json.Unmarshal(m, ¶ms); err != nil {
|
||||
if err := internaljson.Unmarshal(m, ¶ms); err != nil {
|
||||
return nil, fmt.Errorf("unmarshaling %q into a %T: %w", m, params, err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,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/mcpgodebug"
|
||||
"github.com/modelcontextprotocol/go-sdk/internal/util"
|
||||
|
|
@ -1105,7 +1106,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, ¶ms); err == nil {
|
||||
if err := internaljson.Unmarshal(jreq.Params, ¶ms); err == nil {
|
||||
initializeProtocolVersion = params.ProtocolVersion
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
@ -193,7 +194,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, ¶ms); err != nil {
|
||||
if err := internaljson.Unmarshal(req.Params, ¶ms); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id, err := jsonrpc2.MakeID(params.RequestID)
|
||||
|
|
@ -569,7 +570,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")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ package mcp
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
|
||||
)
|
||||
|
||||
func assert(cond bool, msg string) {
|
||||
|
|
@ -21,7 +23,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
|
||||
|
|
|
|||
Loading…
Reference in a new issue