Merge pull request #3107 from nspcc-dev/default-iter-items-conut

rpcsrv: enforce default config values on server creation if malformed value specified
This commit is contained in:
Roman Khimov 2023-08-29 15:06:58 +03:00 committed by GitHub
commit 0b67fa9bca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 10 deletions

View file

@ -17,11 +17,18 @@ const (
// UserAgentFormat is a formatted string used to generate user agent string.
UserAgentFormat = UserAgentWrapper + UserAgentPrefix + "%s" + UserAgentWrapper
// DefaultMaxIteratorResultItems is the default upper bound of traversed
// iterator items per JSON-RPC response.
// iterator items per JSON-RPC response. It covers both session-based and
// naive iterators.
DefaultMaxIteratorResultItems = 100
// DefaultMaxFindResultItems is the default maximum number of resulting
// contract states items that can be retrieved by `findstates` JSON-RPC handler.
DefaultMaxFindResultItems = 100
// DefaultMaxFindStorageResultItems is the default maximum number of resulting
// contract storage items that can be retrieved by `findstorge` JSON-RPC handler.
DefaultMaxFindStorageResultItems = 50
// DefaultMaxNEP11Tokens is the default maximum number of resulting NEP11 tokens
// that can be traversed by `getnep11balances` JSON-RPC handler.
DefaultMaxNEP11Tokens = 100
)
// Version is the version of the node, set at the build time.
@ -73,12 +80,6 @@ func LoadFile(configPath string) (Config, error) {
PingInterval: 30 * time.Second,
PingTimeout: 90 * time.Second,
},
RPC: RPC{
MaxIteratorResultItems: DefaultMaxIteratorResultItems,
MaxFindResultItems: 100,
MaxFindStorageResultItems: DefaultMaxFindStorageResultItems,
MaxNEP11Tokens: 100,
},
},
}

View file

@ -295,6 +295,22 @@ func New(chain Ledger, conf config.RPC, coreServer *network.Server,
log.Info("SessionPoolSize is not set or wrong, setting default value", zap.Int("SessionPoolSize", defaultSessionPoolSize))
}
}
if conf.MaxIteratorResultItems <= 0 {
conf.MaxIteratorResultItems = config.DefaultMaxIteratorResultItems
log.Info("MaxIteratorResultItems is not set or wrong, setting default value", zap.Int("MaxIteratorResultItems", config.DefaultMaxIteratorResultItems))
}
if conf.MaxFindResultItems <= 0 {
conf.MaxFindResultItems = config.DefaultMaxFindResultItems
log.Info("MaxFindResultItems is not set or wrong, setting default value", zap.Int("MaxFindResultItems", config.DefaultMaxFindResultItems))
}
if conf.MaxFindStorageResultItems <= 0 {
conf.MaxFindStorageResultItems = config.DefaultMaxFindStorageResultItems
log.Info("MaxFindStorageResultItems is not set or wrong, setting default value", zap.Int("MaxFindStorageResultItems", config.DefaultMaxFindStorageResultItems))
}
if conf.MaxNEP11Tokens <= 0 {
conf.MaxNEP11Tokens = config.DefaultMaxNEP11Tokens
log.Info("MaxNEP11Tokens is not set or wrong, setting default value", zap.Int("MaxNEP11Tokens", config.DefaultMaxNEP11Tokens))
}
if conf.MaxWebSocketClients == 0 {
conf.MaxWebSocketClients = defaultMaxWebSocketClients
log.Info("MaxWebSocketClients is not set or wrong, setting default value", zap.Int("MaxWebSocketClients", defaultMaxWebSocketClients))
@ -2483,7 +2499,7 @@ func (s *Server) traverseIterator(reqParams params.Params) (any, *neorpc.Error)
return nil, neorpc.NewInvalidParamsError("invalid iterator items count: not an int32")
}
if count > s.config.MaxIteratorResultItems {
return nil, neorpc.NewInvalidParamsError(fmt.Sprintf("iterator items count is out of range (%d at max)", s.config.MaxIteratorResultItems))
return nil, neorpc.NewInvalidParamsError(fmt.Sprintf("iterator items count (%d) is out of range (%d at max)", count, s.config.MaxIteratorResultItems))
}
s.sessionsLock.Lock()

View file

@ -2973,9 +2973,9 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []
})
t.Run("count is out of range", func(t *testing.T) {
sID, iID := prepareIteratorSession(t)
rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "traverseiterator", "params": ["%s", "%s", %d]}"`, sID.String(), iID.String(), rpcSrv.config.MaxIteratorResultItems+1)
rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "traverseiterator", "params": ["%s", "%s", %d]}"`, sID.String(), iID.String(), config.DefaultMaxIteratorResultItems+1)
body := doRPCCall(rpc, httpSrv.URL, t)
checkErrGetResult(t, body, true, neorpc.InvalidParamsCode, fmt.Sprintf("iterator items count is out of range (%d at max)", rpcSrv.config.MaxIteratorResultItems))
checkErrGetResult(t, body, true, neorpc.InvalidParamsCode, fmt.Sprintf("iterator items count (%d) is out of range (%d at max)", config.DefaultMaxIteratorResultItems+1, config.DefaultMaxIteratorResultItems))
})
t.Run("unknown session", func(t *testing.T) {
_, iID := prepareIteratorSession(t)