Skip to content

Commit

Permalink
confi: add MaxRequestHeaderBytes RPC configuration option
Browse files Browse the repository at this point in the history
A part of #3131, follow the notion of neo-project/neo-modules#827,
but don't restrict request line size due to golang/go#15494.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
  • Loading branch information
AnnaShaleva committed Nov 23, 2023
1 parent 802d8d2 commit d511f6e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
3 changes: 3 additions & 0 deletions docs/node-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ RPC:
MaxFindStoragePageSize: 50
MaxNEP11Tokens: 100
MaxRequestBodyBytes: 5242880
MaxRequestHeaderBytes: 1048576
MaxWebSocketClients: 64
SessionEnabled: false
SessionExpirationTime: 15
Expand Down Expand Up @@ -228,6 +229,8 @@ where:
`getnep11balances` call.
- `MaxRequestBodyBytes` - the maximum allowed HTTP request body size in bytes
(5MB by default).
- `MaxRequestHeaderBytes` - the maximum allowed HTTP request header size in bytes
(1MB by default).
- `MaxWebSocketClients` - the maximum simultaneous websocket client connection
number (64 by default). Attempts to establish additional connections will
lead to websocket handshake failures. Use "-1" to disable websocket
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"bytes"
"fmt"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -33,6 +34,9 @@ const (
// DefaultMaxRequestBodyBytes is the default maximum allowed size of HTTP
// request body in bytes.
DefaultMaxRequestBodyBytes = 5 * 1024 * 1024
// DefaultMaxRequestHeaderBytes is the maximum permitted size of the headers
// in an HTTP request.
DefaultMaxRequestHeaderBytes = http.DefaultMaxHeaderBytes
)

// Version is the version of the node, set at the build time.
Expand Down
1 change: 1 addition & 0 deletions pkg/config/rpc_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type (
MaxFindStorageResultItems int `yaml:"MaxFindStoragePageSize"`
MaxNEP11Tokens int `yaml:"MaxNEP11Tokens"`
MaxRequestBodyBytes int `yaml:"MaxRequestBodyBytes"`
MaxRequestHeaderBytes int `yaml:"MaxRequestHeaderBytes"`
MaxWebSocketClients int `yaml:"MaxWebSocketClients"`
SessionEnabled bool `yaml:"SessionEnabled"`
SessionExpirationTime int `yaml:"SessionExpirationTime"`
Expand Down
45 changes: 26 additions & 19 deletions pkg/services/rpcsrv/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,25 +267,6 @@ var rpcWsHandlers = map[string]func(*Server, params.Params, *subscriber) (any, *
// untyped nil or non-nil structure implementing OracleHandler interface.
func New(chain Ledger, conf config.RPC, coreServer *network.Server,
orc OracleHandler, log *zap.Logger, errChan chan<- error) Server {
addrs := conf.Addresses
httpServers := make([]*http.Server, len(addrs))
for i, addr := range addrs {
httpServers[i] = &http.Server{
Addr: addr,
}
}

var tlsServers []*http.Server
if cfg := conf.TLSConfig; cfg.Enabled {
addrs := cfg.Addresses
tlsServers = make([]*http.Server, len(addrs))
for i, addr := range addrs {
tlsServers[i] = &http.Server{
Addr: addr,
}
}
}

protoCfg := chain.GetConfig().ProtocolConfiguration
if conf.SessionEnabled {
if conf.SessionExpirationTime <= 0 {
Expand Down Expand Up @@ -317,6 +298,10 @@ func New(chain Ledger, conf config.RPC, coreServer *network.Server,
conf.MaxRequestBodyBytes = config.DefaultMaxRequestBodyBytes
log.Info("MaxRequestBodyBytes is not set or wong, setting default value", zap.Int("MaxRequestBodyBytes", config.DefaultMaxRequestBodyBytes))
}
if conf.MaxRequestHeaderBytes <= 0 {
conf.MaxRequestHeaderBytes = config.DefaultMaxRequestHeaderBytes
log.Info("MaxRequestHeaderBytes is not set or wong, setting default value", zap.Int("MaxRequestHeaderBytes", config.DefaultMaxRequestHeaderBytes))
}
if conf.MaxWebSocketClients == 0 {
conf.MaxWebSocketClients = defaultMaxWebSocketClients
log.Info("MaxWebSocketClients is not set or wrong, setting default value", zap.Int("MaxWebSocketClients", defaultMaxWebSocketClients))
Expand All @@ -329,6 +314,28 @@ func New(chain Ledger, conf config.RPC, coreServer *network.Server,
if conf.EnableCORSWorkaround {
wsOriginChecker = func(_ *http.Request) bool { return true }
}

addrs := conf.Addresses
httpServers := make([]*http.Server, len(addrs))
for i, addr := range addrs {
httpServers[i] = &http.Server{
Addr: addr,
MaxHeaderBytes: conf.MaxRequestHeaderBytes,
}
}

var tlsServers []*http.Server
if cfg := conf.TLSConfig; cfg.Enabled {
addrs := cfg.Addresses
tlsServers = make([]*http.Server, len(addrs))
for i, addr := range addrs {
tlsServers[i] = &http.Server{
Addr: addr,
MaxHeaderBytes: conf.MaxRequestHeaderBytes,
}
}

Check warning on line 336 in pkg/services/rpcsrv/server.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/rpcsrv/server.go#L329-L336

Added lines #L329 - L336 were not covered by tests
}

return Server{
http: httpServers,
https: tlsServers,
Expand Down

0 comments on commit d511f6e

Please sign in to comment.