From e63e7542a292302bbc24c7724b85ed3e1a0d371f Mon Sep 17 00:00:00 2001 From: PerryLink Date: Fri, 23 Oct 2026 14:26:36 +0000 Subject: [PATCH] jsonrpc2: reject a numeric ID that is not an integer in range MakeID converted every float64 to int64 and truncated the json.Number fallback the same way, so a fractional or out-of-range ID came back as a different number: MakeID(1.5) -> ID{1} MakeID(1e30) -> ID{-9223372036854775808} MakeID(NaN) -> ID{-9223372036854775808} DecodeMessage reaches this path with UseNumber, so a peer that sends {"jsonrpc":"2.0","id":1.5,...} is answered with id 1 -- an ID it never sent, which it cannot match. The doc comment already promised that a json.Number must be an integer literal in the int64 range. Validate the integral value and the int64 range, and report ErrParse otherwise. Integer IDs written with an exponent (1e3) still parse. --- internal/jsonrpc2/messages.go | 21 +++++-- internal/jsonrpc2/messages_test.go | 93 ++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 internal/jsonrpc2/messages_test.go diff --git a/internal/jsonrpc2/messages.go b/internal/jsonrpc2/messages.go index dc445445c..7c75e7a67 100644 --- a/internal/jsonrpc2/messages.go +++ b/internal/jsonrpc2/messages.go @@ -9,6 +9,7 @@ import ( "encoding/json" "errors" "fmt" + "math" internaljson "github.com/modelcontextprotocol/go-sdk/internal/json" ) @@ -21,8 +22,9 @@ type ID struct { // MakeID coerces the given Go value to an ID. The value should be the JSON // decoding of a Request identifier: nil, float64, json.Number, or string. -// Decoding with json.Number keeps integer IDs above 2^53 exact; a json.Number -// must be an integer literal in the int64 range. +// Decoding with json.Number keeps integer IDs above 2^53 exact; a float64 or +// json.Number must hold an integer in the int64 range, and a fractional or +// out-of-range value is rejected instead of being truncated. // // Returns an error if the value type was not a valid Request ID type. // @@ -34,14 +36,14 @@ func MakeID(v any) (ID, error) { case nil: return ID{}, nil case float64: - return Int64ID(int64(v)), nil + return int64ID(v) case json.Number: if i, err := v.Int64(); err == nil { return Int64ID(i), nil } // Fallback to maintain backward compatibility if f, err := v.Float64(); err == nil { - return Int64ID(int64(f)), nil + return int64ID(f) } return ID{}, fmt.Errorf("%w: invalid numeric ID %q", ErrParse, v) case string: @@ -50,6 +52,17 @@ func MakeID(v any) (ID, error) { return ID{}, fmt.Errorf("%w: invalid ID type %T", ErrParse, v) } +// int64ID converts a numeric ID that holds an integer in the int64 range, and +// rejects everything else. Truncating a fractional ID would answer a request +// with an ID the client never sent, and converting a float outside the int64 +// range to int64 has an implementation-defined result. +func int64ID(f float64) (ID, error) { + if f != math.Trunc(f) || f < math.MinInt64 || f >= math.MaxInt64 { + return ID{}, fmt.Errorf("%w: invalid numeric ID %v", ErrParse, f) + } + return Int64ID(int64(f)), nil +} + // Message is the interface to all jsonrpc2 message types. // They share no common functionality, but are a closed set of concrete types // that are allowed to implement this interface. The message types are *Request diff --git a/internal/jsonrpc2/messages_test.go b/internal/jsonrpc2/messages_test.go new file mode 100644 index 000000000..343c5bd18 --- /dev/null +++ b/internal/jsonrpc2/messages_test.go @@ -0,0 +1,93 @@ +// Copyright 2026 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 jsonrpc2 + +import ( + "encoding/json" + "errors" + "math" + "testing" +) + +func TestMakeID(t *testing.T) { + tests := []struct { + name string + in any + want any // nil, int64 or string + wantErr bool + }{ + {name: "nil", in: nil}, + {name: "string", in: "abc", want: "abc"}, + {name: "float64 integer", in: float64(42), want: int64(42)}, + {name: "float64 negative integer", in: float64(-42), want: int64(-42)}, + {name: "float64 fraction", in: 1.5, wantErr: true}, + {name: "float64 above range", in: 1e30, wantErr: true}, + {name: "float64 below range", in: -1e30, wantErr: true}, + {name: "float64 NaN", in: math.NaN(), wantErr: true}, + {name: "float64 infinity", in: math.Inf(1), wantErr: true}, + {name: "json.Number integer", in: json.Number("42"), want: int64(42)}, + {name: "json.Number exponent", in: json.Number("1e3"), want: int64(1000)}, + {name: "json.Number fraction", in: json.Number("1.5"), wantErr: true}, + {name: "json.Number above range", in: json.Number("1e30"), wantErr: true}, + {name: "json.Number invalid", in: json.Number("abc"), wantErr: true}, + {name: "bool", in: true, wantErr: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + id, err := MakeID(tt.in) + if gotErr := err != nil; gotErr != tt.wantErr { + t.Fatalf("MakeID(%v) error = %v, wantErr %v", tt.in, err, tt.wantErr) + } + if err != nil { + if !errors.Is(err, ErrParse) { + t.Fatalf("MakeID(%v) error = %v, want a parse error", tt.in, err) + } + return + } + switch want := tt.want.(type) { + case nil: + if id != (ID{}) { + t.Fatalf("MakeID(%v) = %v, want the zero ID", tt.in, id) + } + case int64: + if id != Int64ID(want) { + t.Fatalf("MakeID(%v) = %v, want %v", tt.in, id, Int64ID(want)) + } + case string: + if id != StringID(want) { + t.Fatalf("MakeID(%v) = %v, want %v", tt.in, id, StringID(want)) + } + } + }) + } +} + +// A fractional ID used to be truncated to the integer part, so a response +// carried an ID the client never sent; an out-of-range one became a random +// int64. Both have to fail the message instead. +func TestDecodeMessageRejectsNonIntegerID(t *testing.T) { + for _, data := range []string{ + `{"jsonrpc":"2.0","id":1.5,"method":"m"}`, + `{"jsonrpc":"2.0","id":1e30,"method":"m"}`, + } { + if _, err := DecodeMessage([]byte(data)); !errors.Is(err, ErrParse) { + t.Errorf("DecodeMessage(%s) error = %v, want a parse error", data, err) + } + } +} + +func TestDecodeMessageKeepsIntegerID(t *testing.T) { + msg, err := DecodeMessage([]byte(`{"jsonrpc":"2.0","id":42,"method":"m"}`)) + if err != nil { + t.Fatalf("DecodeMessage: %v", err) + } + req, ok := msg.(*Request) + if !ok { + t.Fatalf("DecodeMessage returned %T, want *Request", msg) + } + if req.ID != Int64ID(42) { + t.Fatalf("ID = %v, want %v", req.ID, Int64ID(42)) + } +}