Skip to content

routerrpc: reject payment to invoice that don't have payment secret or blinded paths #9752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/release-notes/release-notes-0.20.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ circuit. The indices are only available for forwarding events saved after v0.20.
`channel_update` message and handle it explicitly throughout the code base
instead of extracting it from the TLV stream at various call-sites.

* [Require invoices to include a payment address or blinded paths](https://github.com/lightningnetwork/lnd/pull/9752)
to comply with updated BOLT 11 specifications before sending payments.

## Testing

## Database
Expand All @@ -126,6 +129,7 @@ circuit. The indices are only available for forwarding events saved after v0.20.

* Abdulkbk
* Elle Mouton
* Erick Cestari
* Funyug
* Mohamed Awnallah
* Pins
22 changes: 18 additions & 4 deletions lnrpc/routerrpc/router_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/btcsuite/btcd/wire"
sphinx "github.com/lightningnetwork/lightning-onion"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/feature"
"github.com/lightningnetwork/lnd/fn/v2"
"github.com/lightningnetwork/lnd/htlcswitch"
Expand Down Expand Up @@ -118,6 +119,10 @@ type RouterBackend struct {
// ShouldSetExpEndorsement returns a boolean indicating whether the
// experimental endorsement bit should be set.
ShouldSetExpEndorsement func() bool

// Clock is the clock used to validate payment requests expiry.
// It is useful for testing.
Clock clock.Clock
}

// MissionControl defines the mission control dependencies of routerrpc.
Expand Down Expand Up @@ -980,11 +985,20 @@ func (r *RouterBackend) extractIntentFromSendRequest(
}

// Next, we'll ensure that this payreq hasn't already expired.
err = ValidatePayReqExpiry(payReq)
err = ValidatePayReqExpiry(r.Clock, payReq)
if err != nil {
return nil, err
}

// An invoice must include either a payment address or
// blinded paths.
if payReq.PaymentAddr.IsNone() &&
len(payReq.BlindedPaymentPaths) == 0 {

return nil, errors.New("payment request must contain " +
"either a payment address or blinded paths")
}

// If the amount was not included in the invoice, then we let
// the payer specify the amount of satoshis they wish to send.
// We override the amount to pay with the amount provided from
Expand All @@ -1001,7 +1015,7 @@ func (r *RouterBackend) extractIntentFromSendRequest(
if reqAmt != 0 {
return nil, errors.New("amount must not be " +
"specified when paying a non-zero " +
" amount invoice")
"amount invoice")
}

payIntent.Amount = *payReq.MilliSat
Expand Down Expand Up @@ -1370,10 +1384,10 @@ func UnmarshalFeatures(

// ValidatePayReqExpiry checks if the passed payment request has expired. In
// the case it has expired, an error will be returned.
func ValidatePayReqExpiry(payReq *zpay32.Invoice) error {
func ValidatePayReqExpiry(clock clock.Clock, payReq *zpay32.Invoice) error {
expiry := payReq.Expiry()
validUntil := payReq.Timestamp.Add(expiry)
if time.Now().After(validUntil) {
if clock.Now().After(validUntil) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

niiiiice!

return fmt.Errorf("invoice expired. Valid until %v", validUntil)
}

Expand Down
30 changes: 30 additions & 0 deletions lnrpc/routerrpc/router_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"context"
"encoding/hex"
"testing"
"time"

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/lightningnetwork/lnd/lnmock"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record"
Expand Down Expand Up @@ -503,12 +505,22 @@ func TestExtractIntentFromSendRequest(t *testing.T) {
"g6aykds4ydvf2x9lpngqcfux3hv8qlraan9v3s9296r5w5eh959yzadgh5ck" +
"gjydgyfxdpumxtuk3p3caugmlqpz5necs"

const paymentReqMissingAddr = "lnbcrt100p1p70xwfzpp5qqqsyqcyq5rqwzqfq" +
"qqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdpl2pkx2ctnv5sxxmmwwd5kge" +
"tjypeh2ursdae8g6twvus8g6rfwvs8qun0dfjkxaqnp4q0n326hr8v9zprg8" +
"gsvezcch06gfaqqhde2aj730yg0durunfhv669qypqqqz3uu8wnr7883qzxr" +
"566nuhled49fx6e6q0jn06w6gpgyznwzxwf8xdmye87kpx0y8lqtcgwywsau" +
"0jkm66evelkw7cggwlegp4anv3cq62wusm"

destNodeBytes, err := hex.DecodeString(destKey)
require.NoError(t, err)

target, err := route.NewVertexFromBytes(destNodeBytes)
require.NoError(t, err)

mockClock := &lnmock.MockClock{}
mockClock.On("Now").Return(time.Date(2025, 3, 1, 13, 0, 0, 0, time.UTC))

testCases := []extractIntentTestCase{
{
name: "Time preference out of range",
Expand Down Expand Up @@ -706,6 +718,7 @@ func TestExtractIntentFromSendRequest(t *testing.T) {
return false
},
ActiveNetParams: &chaincfg.RegressionNetParams,
Clock: mockClock,
},
sendReq: &SendPaymentRequest{
Amt: int64(paymentAmount),
Expand All @@ -714,6 +727,23 @@ func TestExtractIntentFromSendRequest(t *testing.T) {
valid: false,
expectedErrorMsg: "invoice expired.",
},
{
name: "Invoice missing payment address",
backend: &RouterBackend{
ShouldSetExpEndorsement: func() bool {
return false
},
ActiveNetParams: &chaincfg.RegressionNetParams,
MaxTotalTimelock: 1000,
Clock: mockClock,
},
sendReq: &SendPaymentRequest{
PaymentRequest: paymentReqMissingAddr,
},
valid: false,
expectedErrorMsg: "payment request must contain " +
"either a payment address or blinded paths",
},
{
name: "Invalid dest vertex length",
backend: &RouterBackend{
Expand Down
5 changes: 4 additions & 1 deletion rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ func (r *rpcServer) addDeps(s *server, macService *macaroons.Service,

routerBackend := &routerrpc.RouterBackend{
SelfNode: selfNode.PubKeyBytes,
Clock: clock.NewDefaultClock(),
FetchChannelCapacity: func(chanID uint64) (btcutil.Amount,
error) {

Expand Down Expand Up @@ -5609,7 +5610,9 @@ func (r *rpcServer) extractPaymentIntent(rpcPayReq *rpcPaymentRequest) (rpcPayme
}

// Next, we'll ensure that this payreq hasn't already expired.
err = routerrpc.ValidatePayReqExpiry(payReq)
err = routerrpc.ValidatePayReqExpiry(
r.routerBackend.Clock, payReq,
)
if err != nil {
return payIntent, err
}
Expand Down
Loading