From 052b08160ad68c71763c0aea87c9acf55ca76de6 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 9 Nov 2021 19:53:44 +0100 Subject: [PATCH] Set a cap on the length of subscription queries. (backport #7263) (#7264) As a safety measure, don't allow a query string to be unreasonably long. The query filter is not especially efficient, so a query that needs more than basic detail should filter coarsely in the subscriber and refine on the client side. This affects Subscribe and TxSearch queries. (cherry picked from commit 9dc3d7f9a26f2e395311b2f1d705aa950bb25c7a) --- internal/rpc/core/events.go | 7 +++++++ internal/rpc/core/tx.go | 2 ++ 2 files changed, 9 insertions(+) diff --git a/internal/rpc/core/events.go b/internal/rpc/core/events.go index 8632e00c1..46d9ff6a7 100644 --- a/internal/rpc/core/events.go +++ b/internal/rpc/core/events.go @@ -2,6 +2,7 @@ package core import ( "context" + "errors" "fmt" "time" @@ -14,6 +15,10 @@ import ( const ( // Buffer on the Tendermint (server) side to allow some slowness in clients. subBufferSize = 100 + + // maxQueryLength is the maximum length of a query string that will be + // accepted. This is just a safety check to avoid outlandish queries. + maxQueryLength = 512 ) // Subscribe for events via WebSocket. @@ -25,6 +30,8 @@ func (env *Environment) Subscribe(ctx *rpctypes.Context, query string) (*coretyp return nil, fmt.Errorf("max_subscription_clients %d reached", env.Config.MaxSubscriptionClients) } else if env.EventBus.NumClientSubscriptions(addr) >= env.Config.MaxSubscriptionsPerClient { return nil, fmt.Errorf("max_subscriptions_per_client %d reached", env.Config.MaxSubscriptionsPerClient) + } else if len(query) > maxQueryLength { + return nil, errors.New("maximum query length exceeded") } env.Logger.Info("Subscribe to query", "remote", addr, "query", query) diff --git a/internal/rpc/core/tx.go b/internal/rpc/core/tx.go index 60c7519c0..7ba2bf90c 100644 --- a/internal/rpc/core/tx.go +++ b/internal/rpc/core/tx.go @@ -72,6 +72,8 @@ func (env *Environment) TxSearch( if !indexer.KVSinkEnabled(env.EventSinks) { return nil, fmt.Errorf("transaction searching is disabled due to no kvEventSink") + } else if len(query) > maxQueryLength { + return nil, errors.New("maximum query length exceeded") } q, err := tmquery.New(query)