Skip to content

Commit

Permalink
Default MaxHeaderSize to 4kb (flashbots#206)
Browse files Browse the repository at this point in the history
* Set max header byte size

* remove cli flag
  • Loading branch information
avalonche authored and screwyprof committed Feb 3, 2023
1 parent 5c22e59 commit e3de075
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
2 changes: 2 additions & 0 deletions cmd/mev-boost/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
defaultRelayTimeoutMs = getEnvInt("RELAY_TIMEOUT_MS", 2000) // timeout for all the requests to the relay
defaultRelayCheck = os.Getenv("RELAY_STARTUP_CHECK") != ""
defaultGenesisForkVersion = getEnv("GENESIS_FORK_VERSION", "")
maxHeaderBytes = getEnvInt("MAX_HEADER_BYTES", 4000) // max header byte size for requests for dos prevention

// cli flags
logJSON = flag.Bool("json", defaultLogJSON, "log in JSON format instead of text")
Expand Down Expand Up @@ -102,6 +103,7 @@ func main() {
GenesisForkVersionHex: genesisForkVersionHex,
RelayRequestTimeout: relayTimeout,
RelayCheck: *relayCheck,
MaxHeaderBytes: maxHeaderBytes,
}
server, err := server.NewBoostService(opts)
if err != nil {
Expand Down
13 changes: 9 additions & 4 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type BoostServiceOpts struct {
GenesisForkVersionHex string
RelayRequestTimeout time.Duration
RelayCheck bool
MaxHeaderBytes int
}

// BoostService TODO
Expand All @@ -53,6 +54,8 @@ type BoostService struct {
srv *http.Server
relayCheck bool

maxHeaderBytes int

builderSigningDomain types.Domain
httpClient http.Client
}
Expand All @@ -69,10 +72,11 @@ func NewBoostService(opts BoostServiceOpts) (*BoostService, error) {
}

return &BoostService{
listenAddr: opts.ListenAddr,
relays: opts.Relays,
log: opts.Log.WithField("module", "service"),
relayCheck: opts.RelayCheck,
listenAddr: opts.ListenAddr,
relays: opts.Relays,
log: opts.Log.WithField("module", "service"),
relayCheck: opts.RelayCheck,
maxHeaderBytes: opts.MaxHeaderBytes,

builderSigningDomain: builderSigningDomain,
httpClient: http.Client{
Expand Down Expand Up @@ -131,6 +135,7 @@ func (m *BoostService) StartHTTPServer() error {
ReadHeaderTimeout: 0,
WriteTimeout: 0,
IdleTimeout: 0,
MaxHeaderBytes: m.maxHeaderBytes,
}

err := m.srv.ListenAndServe()
Expand Down
21 changes: 20 additions & 1 deletion server/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package server

import (
"bytes"
"context"
"encoding/json"
"fmt"
"math"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -45,6 +47,7 @@ func newTestBackend(t *testing.T, numRelays int, relayTimeout time.Duration) *te
GenesisForkVersionHex: "0x00000000",
RelayRequestTimeout: relayTimeout,
RelayCheck: true,
MaxHeaderBytes: 4000,
}
service, err := NewBoostService(opts)
require.NoError(t, err)
Expand Down Expand Up @@ -73,7 +76,7 @@ func (be *testBackend) request(t *testing.T, method string, path string, payload

func TestNewBoostServiceErrors(t *testing.T) {
t.Run("errors when no relays", func(t *testing.T) {
_, err := NewBoostService(BoostServiceOpts{testLog, ":123", []RelayEntry{}, "0x00000000", time.Second, true})
_, err := NewBoostService(BoostServiceOpts{testLog, ":123", []RelayEntry{}, "0x00000000", time.Second, true, 4000})
require.Error(t, err)
})
}
Expand Down Expand Up @@ -115,6 +118,22 @@ func TestWebserverRootHandler(t *testing.T) {
require.Equal(t, "{}\n", rr.Body.String())
}

func TestWebserverMaxHeaderSize(t *testing.T) {
backend := newTestBackend(t, 1, time.Second)
addr := "localhost:1234"
backend.boost.listenAddr = addr
go func() {
err := backend.boost.StartHTTPServer()
require.NoError(t, err)
}()
time.Sleep(time.Millisecond * 100)
path := "http://" + addr + "?" + strings.Repeat("abc", 4000) // path with characters of size over 4kb
code, err := SendHTTPRequest(context.Background(), *http.DefaultClient, http.MethodGet, path, nil, nil)
require.Error(t, err)
require.Equal(t, http.StatusRequestHeaderFieldsTooLarge, code)
backend.boost.srv.Close()
}

// Example good registerValidator payload
var payloadRegisterValidator = types.SignedValidatorRegistration{
Message: &types.RegisterValidatorRequestMessage{
Expand Down

0 comments on commit e3de075

Please sign in to comment.