Skip to content

Commit

Permalink
Set a cap on the length of subscription queries. (#8349)
Browse files Browse the repository at this point in the history
A manual backport of #7263.

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.

Co-authored-by: M. J. Fromberger <fromberger@interchain.io>
  • Loading branch information
tnasu and M. J. Fromberger committed Jul 13, 2023
1 parent 8163143 commit 424fe2d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions rpc/core/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (
rpctypes "github.com/Finschia/ostracon/rpc/jsonrpc/types"
)

const (
// 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.
// More: https://docs.tendermint.com/master/rpc/#/Websocket/subscribe
func Subscribe(ctx *rpctypes.Context, query string) (*ctypes.ResultSubscribe, error) {
Expand All @@ -21,6 +27,8 @@ func Subscribe(ctx *rpctypes.Context, query string) (*ctypes.ResultSubscribe, er
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)
Expand Down
2 changes: 2 additions & 0 deletions rpc/core/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func TxSearch(
// if index is disabled, return error
if _, ok := env.TxIndexer.(*null.TxIndex); ok {
return nil, errors.New("transaction indexing is disabled")
} else if len(query) > maxQueryLength {
return nil, errors.New("maximum query length exceeded")
}

q, err := tmquery.New(query)
Expand Down

0 comments on commit 424fe2d

Please sign in to comment.