Merge branch 'fix/grpc-dos-hardening'

This commit is contained in:
2026-11-08 11:22:49 -06:00
+57 -15
View File
@@ -12,6 +12,8 @@ import (
"github.com/btcsuite/btcd/rpcclient"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"git.hush.is/hush/lightwalletd/common"
"git.hush.is/hush/lightwalletd/walletrpc"
@@ -68,6 +70,18 @@ func (s *SqlStreamer) GetAddressTxids(addressBlockFilter *walletrpc.TransparentA
return nil
}
// Reject a missing range before dereferencing it (same nil-panic class as
// GetBlockRange) and bound the span.
if addressBlockFilter.Range == nil || addressBlockFilter.Range.Start == nil || addressBlockFilter.Range.End == nil {
return status.Error(codes.InvalidArgument, "GetAddressTxids: missing range (Start and End are required)")
}
if addressBlockFilter.Range.End.Height < addressBlockFilter.Range.Start.Height {
return status.Error(codes.InvalidArgument, "GetAddressTxids: end height is below start height")
}
if addressBlockFilter.Range.End.Height-addressBlockFilter.Range.Start.Height+1 > maxBlockRangeSpan {
return status.Errorf(codes.InvalidArgument, "GetAddressTxids: span exceeds the maximum of %d blocks", maxBlockRangeSpan)
}
params := make([]json.RawMessage, 1)
st := "{\"addresses\": [\"" + addressBlockFilter.Address + "\"]," +
"\"start\": " + strconv.FormatUint(addressBlockFilter.Range.Start.Height, 10) +
@@ -180,7 +194,25 @@ func (s *SqlStreamer) GetBlock(ctx context.Context, id *walletrpc.BlockID) (*wal
}
// maxBlockRangeSpan bounds how many blocks one GetBlockRange / GetAddressTxids
// request may walk. Honest wallet syncs are far below this; the cap stops an
// attacker-sized request from driving one backend RPC per uncached height.
const maxBlockRangeSpan = 10000
func (s *SqlStreamer) GetBlockRange(span *walletrpc.BlockRange, resp walletrpc.CompactTxStreamer_GetBlockRangeServer) error {
// Reject a missing range before dereferencing it. An empty BlockRange
// (nil Start/End) would otherwise nil-panic and take the whole process
// down, since grpc-go does not recover handler panics per call.
if span == nil || span.Start == nil || span.End == nil {
return status.Error(codes.InvalidArgument, "GetBlockRange: missing range (Start and End are required)")
}
start, end := int(span.Start.Height), int(span.End.Height)
if end < start {
return status.Error(codes.InvalidArgument, "GetBlockRange: end height is below start height")
}
if end-start+1 > maxBlockRangeSpan {
return status.Errorf(codes.InvalidArgument, "GetBlockRange: span of %d blocks exceeds the maximum of %d", end-start+1, maxBlockRangeSpan)
}
s.log.WithFields(logrus.Fields{
"start": span.Start.Height,
"end": span.End.Height,
@@ -189,7 +221,7 @@ func (s *SqlStreamer) GetBlockRange(span *walletrpc.BlockRange, resp walletrpc.C
blockChan := make(chan walletrpc.CompactBlock)
errChan := make(chan error)
go common.GetBlockRange(s.client, s.cache, blockChan, errChan, int(span.Start.Height), int(span.End.Height))
go common.GetBlockRange(s.client, s.cache, blockChan, errChan, start, end)
blockCount := 0
for {
@@ -246,17 +278,20 @@ func (s *SqlStreamer) GetTransaction(ctx context.Context, txf *walletrpc.TxFilte
result, rpcErr := s.client.RawRequest("getrawtransaction", params)
var err error
var errCode int64
// For some reason, the error responses are not JSON
if rpcErr != nil {
s.log.Errorf("Got error: %s", rpcErr.Error())
errParts := strings.SplitN(rpcErr.Error(), ":", 2)
errCode, err = strconv.ParseInt(errParts[0], 10, 32)
//Check to see if we are requesting a height the dragonxd doesn't have yet
if err == nil && errCode == -8 {
return nil, err
parsedCode, parseErr := strconv.ParseInt(errParts[0], 10, 32)
// -5 (RPC_INVALID_ADDRESS_OR_KEY, "No information available about transaction")
// and -8 (a height the node doesn't have yet) both mean the transaction is not
// available -> NotFound, so a client can tell it apart from a server failure.
// Return a real gRPC status instead of the parsed-away (nil) error, which would
// otherwise marshal a typed nil and surface to the client as Internal.
if parseErr == nil && (parsedCode == -5 || parsedCode == -8) {
return nil, status.Error(codes.NotFound, "transaction not found")
}
return nil, err
return nil, status.Errorf(codes.Internal, "backend RPC error: %s", rpcErr.Error())
}
var txhex string
@@ -281,12 +316,16 @@ func (s *SqlStreamer) GetTransaction(ctx context.Context, txf *walletrpc.TxFilte
if rpcErr != nil {
s.log.Errorf("Got error: %s", rpcErr.Error())
errParts := strings.SplitN(rpcErr.Error(), ":", 2)
errCode, err = strconv.ParseInt(errParts[0], 10, 32)
//Check to see if we are requesting a height the dragonxd doesn't have yet
if err == nil && errCode == -8 {
return nil, err
parsedCode, parseErr := strconv.ParseInt(errParts[0], 10, 32)
// -5 (RPC_INVALID_ADDRESS_OR_KEY, "No information available about transaction")
// and -8 (a height the node doesn't have yet) both mean the transaction is not
// available -> NotFound, so a client can tell it apart from a server failure.
// Return a real gRPC status instead of the parsed-away (nil) error, which would
// otherwise marshal a typed nil and surface to the client as Internal.
if parseErr == nil && (parsedCode == -5 || parsedCode == -8) {
return nil, status.Error(codes.NotFound, "transaction not found")
}
return nil, err
return nil, status.Errorf(codes.Internal, "backend RPC error: %s", rpcErr.Error())
}
var txinfo interface{}
err = json.Unmarshal(result, &txinfo)
@@ -317,12 +356,15 @@ func (s *SqlStreamer) GetTransaction(ctx context.Context, txf *walletrpc.TxFilte
return &walletrpc.RawTransaction{Data: txBytes, Height: uint64(txHeight)}, nil
}
if txf.Block.Hash != nil {
// txf.Block may be nil (an empty TxFilter); guard it before dereferencing, or a single
// empty request panics the server (grpc-go v1.24.0 has no handler recover).
if txf.Block != nil && txf.Block.Hash != nil {
s.log.Error("Can't GetTransaction with a blockhash+num. Please call GetTransaction with txid")
return nil, errors.New("Can't GetTransaction with a blockhash+num. Please call GetTransaction with txid")
return nil, status.Error(codes.InvalidArgument, "Can't GetTransaction with a blockhash+num. Please call GetTransaction with txid")
}
return &walletrpc.RawTransaction{Data: txBytes, Height: uint64(txHeight)}, nil
// Neither a txid (Hash) nor a usable block filter was supplied.
return nil, status.Error(codes.InvalidArgument, "GetTransaction: a txid (Hash) is required")
}
// GetLightdInfo gets the LightWalletD (this server) info