mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-27 18:29:26 +00:00
graphql: limit request body size
This commit is contained in:
parent
d3edc58ef7
commit
335c8086dc
2 changed files with 57 additions and 3 deletions
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -132,7 +133,7 @@ func TestGraphQLBlockSerialization(t *testing.T) {
|
||||||
code: 400,
|
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}]}]}`,
|
want: `{"errors":[{"message":"Cannot query field \"bleh\" on type \"Query\".","locations":[{"line":1,"column":2}]}]}`,
|
||||||
code: 400,
|
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) {
|
func TestGraphQLBlockSerializationEIP2718(t *testing.T) {
|
||||||
// Account for signing txes
|
// Account for signing txes
|
||||||
var (
|
var (
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,8 @@ package graphql
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
@ -35,18 +37,31 @@ import (
|
||||||
// maxQueryDepth limits the maximum field nesting depth allowed in GraphQL queries.
|
// maxQueryDepth limits the maximum field nesting depth allowed in GraphQL queries.
|
||||||
const maxQueryDepth = 20
|
const maxQueryDepth = 20
|
||||||
|
|
||||||
|
// maxRequestBodySize limits the size of incoming GraphQL request bodies.
|
||||||
|
const maxRequestBodySize = 5 * 1024 * 1024
|
||||||
|
|
||||||
type handler struct {
|
type handler struct {
|
||||||
Schema *graphql.Schema
|
Schema *graphql.Schema
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodySize)
|
||||||
|
|
||||||
var params struct {
|
var params struct {
|
||||||
Query string `json:"query"`
|
Query string `json:"query"`
|
||||||
OperationName string `json:"operationName"`
|
OperationName string `json:"operationName"`
|
||||||
Variables map[string]interface{} `json:"variables"`
|
Variables map[string]interface{} `json:"variables"`
|
||||||
}
|
}
|
||||||
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
|
dec := json.NewDecoder(r.Body)
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
if err := dec.Decode(¶ms); 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
|
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.
|
// New constructs a new GraphQL service instance.
|
||||||
func New(stack *node.Node, backend ethapi.Backend, filterSystem *filters.FilterSystem, cors, vhosts []string) error {
|
func New(stack *node.Node, backend ethapi.Backend, filterSystem *filters.FilterSystem, cors, vhosts []string) error {
|
||||||
_, err := newHandler(stack, backend, filterSystem, cors, vhosts)
|
_, err := newHandler(stack, backend, filterSystem, cors, vhosts)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue