Author SHA1 Message Date
DanSandClaude Opus 4.8 cbda810849 frontend: guard nil Block in GetTransaction (empty TxFilter crash)
An empty TxFilter (Hash==nil, Block==nil) fell through to 'if txf.Block.Hash'
which dereferenced a nil Block and panicked the server (grpc-go v1.24.0 has no
handler recover) -- one unauthenticated request killed the process. Guard
txf.Block before dereferencing and return InvalidArgument for a request that
carries neither a txid nor a usable block filter.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 10:39:41 -05:00
DanSandClaude Opus 4.8 6f52ff0969 frontend: harden the gRPC handlers against malformed and oversized requests
Three unauthenticated issues from a security review of the published
lightwalletd, all in frontend/service.go, now fixed and validated end to end
against a regtest dragonxd backend:

- GetBlockRange and GetAddressTxids dereferenced the request's range
  (Start/End) with no nil check, so one request with an empty BlockRange
  nil-panicked and, because grpc-go v1.24.0 does not recover handler panics,
  took the whole process down. Reject a missing range with InvalidArgument
  before touching it. (Verified: an empty-range request killed the old binary;
  the fixed binary answers InvalidArgument and stays up.)
- Neither method bounded the span, so one request could walk the entire
  uncached range -- one backend RPC per height. Reject end<start and cap the
  span at maxBlockRangeSpan (10000; honest syncs are far below it). (Verified:
  a 9,000,000-block span is now refused with InvalidArgument up front.)
- GetTransaction returned Internal ("Marshal called with nil") for a missing
  transaction: strconv.ParseInt overwrote err and both branches returned a
  typed nil. Parse into a separate variable and return a real status. The
  backend reports a missing tx as -5 ("No information available about
  transaction") and a not-yet-available height as -8; map both to NotFound,
  everything else to Internal with the backend message. (Verified: a bogus
  txid returned Internal before and returns NotFound after.)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-29 01:10:25 -05: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"
@@ -65,6 +67,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) +
@@ -138,7 +152,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,
@@ -147,7 +179,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 {
@@ -204,17 +236,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
@@ -239,12 +274,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)
@@ -256,12 +295,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