@@ -3802,3 +3802,105 @@ func TestStreamableServerRejectsDuplicateInFlightRequestID(t *testing.T) {
38023802 default :
38033803 }
38043804}
3805+
3806+ // TestStreamableMaxRequestBodyBytes verifies that the streamable HTTP handler
3807+ // enforces StreamableHTTPOptions.MaxRequestBodyBytes on incoming request
3808+ // bodies. The limit must apply uniformly regardless of transfer encoding:
3809+ // Content-Length, Transfer-Encoding: chunked, or HTTP/2 (which has no
3810+ // Content-Length).
3811+ func TestStreamableMaxRequestBodyBytes (t * testing.T ) {
3812+ // A minimally-valid JSON-RPC initialize request. The body-size check runs
3813+ // before parsing, so exact contents don't matter for the reject cases,
3814+ // but we use a real one for the "under limit" success case.
3815+ validInit := []byte (`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"t","version":"v1"}}}` )
3816+
3817+ // Build a payload of the requested size by padding a JSON object. The
3818+ // server doesn't need to parse it; the MaxBytesReader trips first.
3819+ oversizedBody := func (n int ) []byte {
3820+ b := make ([]byte , n )
3821+ b [0 ] = '{'
3822+ for i := 1 ; i < n - 1 ; i ++ {
3823+ b [i ] = ' '
3824+ }
3825+ b [n - 1 ] = '}'
3826+ return b
3827+ }
3828+
3829+ tests := []struct {
3830+ name string
3831+ limit int64 // value assigned to MaxRequestBodyBytes
3832+ body []byte
3833+ wantStatus int
3834+ wantStatusDesc string
3835+ }{
3836+ {
3837+ name : "default limit rejects oversized" ,
3838+ limit : 0 , // triggers DefaultMaxRequestBodyBytes (4 MiB)
3839+ body : oversizedBody (DefaultMaxRequestBodyBytes + 1 ),
3840+ wantStatus : http .StatusRequestEntityTooLarge ,
3841+ wantStatusDesc : "413 for body > 4MiB default" ,
3842+ },
3843+ {
3844+ name : "custom limit rejects oversized" ,
3845+ limit : 1024 ,
3846+ body : oversizedBody (2048 ),
3847+ wantStatus : http .StatusRequestEntityTooLarge ,
3848+ wantStatusDesc : "413 for body > custom 1KiB limit" ,
3849+ },
3850+ {
3851+ name : "chunked encoding does not bypass limit" ,
3852+ limit : 1024 ,
3853+ body : oversizedBody (4096 ),
3854+ wantStatus : http .StatusRequestEntityTooLarge ,
3855+ wantStatusDesc : "413 for chunked body > limit (regression: Content-Length-only checks are bypassable)" ,
3856+ },
3857+ {
3858+ name : "negative limit disables the check" ,
3859+ limit : - 1 ,
3860+ body : oversizedBody (2 * DefaultMaxRequestBodyBytes ),
3861+ wantStatus : http .StatusBadRequest , // body is not valid JSON-RPC; but the size check must not fire
3862+ wantStatusDesc : "not 413 when limit is disabled" ,
3863+ },
3864+ {
3865+ name : "under limit succeeds" ,
3866+ limit : 64 * 1024 ,
3867+ body : validInit ,
3868+ wantStatus : http .StatusOK ,
3869+ wantStatusDesc : "200 for a valid initialize under the limit" ,
3870+ },
3871+ }
3872+
3873+ for _ , tc := range tests {
3874+ t .Run (tc .name , func (t * testing.T ) {
3875+ server := NewServer (testImpl , nil )
3876+ handler := NewStreamableHTTPHandler (
3877+ func (* http.Request ) * Server { return server },
3878+ & StreamableHTTPOptions {
3879+ Stateless : true ,
3880+ MaxRequestBodyBytes : tc .limit ,
3881+ },
3882+ )
3883+ ts := httptest .NewServer (handler )
3884+ defer ts .Close ()
3885+
3886+ req , err := http .NewRequest (http .MethodPost , ts .URL , bytes .NewReader (tc .body ))
3887+ if err != nil {
3888+ t .Fatalf ("NewRequest: %v" , err )
3889+ }
3890+ req .Header .Set ("Content-Type" , "application/json" )
3891+ req .Header .Set ("Accept" , "application/json, text/event-stream" )
3892+
3893+ resp , err := ts .Client ().Do (req )
3894+ if err != nil {
3895+ t .Fatalf ("Do: %v" , err )
3896+ }
3897+ defer resp .Body .Close ()
3898+ body , _ := io .ReadAll (resp .Body )
3899+
3900+ if resp .StatusCode != tc .wantStatus {
3901+ t .Fatalf ("%s: status = %d, want %d (body=%q)" ,
3902+ tc .wantStatusDesc , resp .StatusCode , tc .wantStatus , string (body ))
3903+ }
3904+ })
3905+ }
3906+ }
0 commit comments