This commit is contained in:
Minh Vu 2026-05-22 21:59:41 +00:00 committed by GitHub
commit f7b8462ab2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 57 additions and 3 deletions

View file

@ -23,6 +23,7 @@ import (
"io"
"math/big"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
@ -132,7 +133,7 @@ func TestGraphQLBlockSerialization(t *testing.T) {
code: 400,
},
{
body: `{"query": "{bleh{number}}","variables": null}"`,
body: `{"query": "{bleh{number}}","variables": null}`,
want: `{"errors":[{"message":"Cannot query field \"bleh\" on type \"Query\".","locations":[{"line":1,"column":2}]}]}`,
code: 400,
},
@ -175,6 +176,35 @@ func TestGraphQLBlockSerialization(t *testing.T) {
}
}
func TestGraphQLHTTPBodyLimit(t *testing.T) {
tests := []struct {
name string
body string
}{
{
name: "should reject oversized request body if query field exceeds limit",
body: `{"query":"` + strings.Repeat("a", maxRequestBodySize) + `"}`,
},
{
name: "should reject oversized request body if trailing data exceeds limit",
body: `{"query":"{block{number}}"}` + strings.Repeat(" ", maxRequestBodySize),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/graphql", strings.NewReader(test.body))
w := httptest.NewRecorder()
handler{}.ServeHTTP(w, req)
if w.Code != http.StatusRequestEntityTooLarge {
t.Fatalf("got status %d, want %d", w.Code, http.StatusRequestEntityTooLarge)
}
})
}
}
func TestGraphQLBlockSerializationEIP2718(t *testing.T) {
// Account for signing txes
var (

View file

@ -19,6 +19,8 @@ package graphql
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"strconv"
"sync"
@ -35,18 +37,31 @@ import (
// maxQueryDepth limits the maximum field nesting depth allowed in GraphQL queries.
const maxQueryDepth = 20
// maxRequestBodySize limits the size of incoming GraphQL request bodies.
const maxRequestBodySize = 5 * 1024 * 1024
type handler struct {
Schema *graphql.Schema
}
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodySize)
var params struct {
Query string `json:"query"`
OperationName string `json:"operationName"`
Variables map[string]interface{} `json:"variables"`
}
if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
dec := json.NewDecoder(r.Body)
if err := dec.Decode(&params); err != nil {
writeRequestError(w, err)
return
}
if err := dec.Decode(&struct{}{}); err != io.EOF {
if err == nil {
err = errors.New("unexpected content after JSON value")
}
writeRequestError(w, err)
return
}
@ -108,6 +123,15 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
})
}
func writeRequestError(w http.ResponseWriter, err error) {
var maxBytesErr *http.MaxBytesError
if errors.As(err, &maxBytesErr) {
http.Error(w, err.Error(), http.StatusRequestEntityTooLarge)
return
}
http.Error(w, err.Error(), http.StatusBadRequest)
}
// New constructs a new GraphQL service instance.
func New(stack *node.Node, backend ethapi.Backend, filterSystem *filters.FilterSystem, cors, vhosts []string) error {
_, err := newHandler(stack, backend, filterSystem, cors, vhosts)