Skip to content
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
23 changes: 17 additions & 6 deletions accessman.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,23 @@ func (a *accessMan) newPendingCloseChan(remotePub *btcec.PublicKey) error {

peerMapKey := string(remotePub.SerializeCompressed())

// Fetch the peer's access status from peerScores.
status, found := a.peerScores[peerMapKey]
if !found {
acsmLog.ErrorS(ctx, "Peer score not found", ErrNoPeerScore)
// An offline peer can still have pending channels after its live
// score has been removed. Update its count without assigning a slot.
status, connected := a.peerScores[peerMapKey]
if !connected {
info, ok := a.peerChanInfo[peerMapKey]
if !ok || info.PendingOpenCount == 0 {
return ErrNoPendingPeerInfo
}

return ErrNoPeerScore
info.PendingOpenCount--
if info.PendingOpenCount == 0 && !info.HasOpenOrClosedChan {
delete(a.peerChanInfo, peerMapKey)
} else {
a.peerChanInfo[peerMapKey] = info
}

return nil
}

switch status.state {
Expand All @@ -314,7 +325,7 @@ func (a *accessMan) newPendingCloseChan(remotePub *btcec.PublicKey) error {
// If this peer is temporary, we need to check if it will
// revert to a restricted-access peer.
peerCount, found := a.peerChanInfo[peerMapKey]
if !found {
if !found || peerCount.PendingOpenCount == 0 {
acsmLog.ErrorS(ctx, "Pending peer info not found",
ErrNoPendingPeerInfo)

Expand Down
45 changes: 45 additions & 0 deletions accessman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,51 @@ import (
"github.com/stretchr/testify/require"
)

// TestAccessManPendingCloseOfflinePeer checks that a pending channel close
// still decrements the peer's pending count when the peer disconnected while
// the channel was pending, so its access score was removed.
func TestAccessManPendingCloseOfflinePeer(t *testing.T) {
t.Parallel()

peerPriv, err := btcec.NewPrivateKey()
require.NoError(t, err)
peerKey := peerPriv.PubKey()
peerKeySer := string(peerKey.SerializeCompressed())

initPerms := func() (map[string]channeldb.ChanCount, error) {
return map[string]channeldb.ChanCount{
peerKeySer: {
HasOpenOrClosedChan: false,
PendingOpenCount: 1,
},
}, nil
}

a, err := newAccessMan(&accessManConfig{
initAccessPerms: initPerms,
shouldDisconnect: func(*btcec.PublicKey) (bool, error) {
return false, nil
},
maxRestrictedSlots: 1,
})
require.NoError(t, err)

a.addPeerAccess(peerKey, peerStatusTemporary, true)

// The peer disconnects while its channel is pending; removePeerAccess
// deletes its score but keeps the pending count.
a.removePeerAccess(t.Context(), peerKeySer)

// The pending channel now times out. The close must still be processed
// rather than failing with ErrNoPeerScore.
require.NoError(t, a.newPendingCloseChan(peerKey))

// The pending count must be gone so a reconnect cannot reuse it.
require.NotContains(t, a.peerChanInfo, peerKeySer)
require.NotContains(t, a.peerScores, peerKeySer)
require.Zero(t, a.numRestricted)
}

// assertInboundConnection asserts that we're able to accept an inbound
// connection successfully without any access permissions being violated.
func assertInboundConnection(t *testing.T, a *accessMan,
Expand Down
88 changes: 78 additions & 10 deletions bolt12/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"math/bits"
"slices"
"time"
"unicode/utf8"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg/v2"
Expand Down Expand Up @@ -288,12 +287,12 @@ func validateInvoiceErrorWrite(ie *InvoiceError) error {

// The spec's "explanatory string" is a type-5 UTF-8 message, so an
// empty or non-UTF-8 blob carries no explanation and fails the
// requirement even though IsSome passes. checkUTF8 mirrors the
// requirement even though IsSome passes. checkUTF8ForWrite mirrors the
// treatment every other BOLT 12 UTF-8 field receives.
if len(ie.Error.ValOpt().UnwrapOr(nil)) == 0 {
return ErrEmptyError
}
if err := checkUTF8(ie.Error, "error"); err != nil {
if err := checkUTF8ForWrite(ie.Error, "error"); err != nil {
return err
}

Expand Down Expand Up @@ -539,8 +538,18 @@ func validateInvoiceRequestWrite(ir *InvoiceRequest) error {
// NOT CHECKED HERE: the bits are the caller's own, and the reader
// rejects unknown even bits using the known bits passed to it.

// check UTF-8 constraints and BIP 353
err := checkUTF8(ir.InvreqPayerNote, "invreq_payer_note")
// Check UTF-8 constraints and BIP 353.
if err := checkUTF8ForWrite(
ir.OfferDescription, "offer_description",
); err != nil {
return err
}
if err := checkUTF8ForWrite(
ir.OfferIssuer, "offer_issuer",
); err != nil {
return err
}
err := checkUTF8ForWrite(ir.InvreqPayerNote, "invreq_payer_note")
if err != nil {
return err
}
Expand Down Expand Up @@ -783,6 +792,20 @@ func ValidateInvoiceRequestRead(ir *InvoiceRequest,
return err
}

if err := checkUTF8(
ir.OfferDescription, "offer_description",
); err != nil {
return err
}
if err := checkUTF8(ir.OfferIssuer, "offer_issuer"); err != nil {
return err
}
if err := checkUTF8(
ir.InvreqPayerNote, "invreq_payer_note",
); err != nil {
return err
}

// - MUST reject the invoice request if signature is not correct as
// detailed in Signature Calculation using the invreq_payer_id.
return verifyInvoiceRequest(ir)
Expand Down Expand Up @@ -1269,17 +1292,19 @@ func validateOfferWrite(o *Offer) error {

// Defense in depth: the reader rejects a non-UTF-8 text field, so the
// writer does not emit one.
if err := checkUTF8(o.OfferCurrency, "offer_currency"); err != nil {
if err := checkUTF8ForWrite(
o.OfferCurrency, "offer_currency",
); err != nil {
return err
}

if err := checkUTF8(
if err := checkUTF8ForWrite(
o.OfferDescription, "offer_description",
); err != nil {
return err
}

if err := checkUTF8(o.OfferIssuer, "offer_issuer"); err != nil {
if err := checkUTF8ForWrite(o.OfferIssuer, "offer_issuer"); err != nil {
return err
}

Expand Down Expand Up @@ -1359,12 +1384,25 @@ func checkAmountPositive[T tlv.TlvType](
})
}

// checkUTF8 validates that a blob field contains valid UTF-8.
// checkUTF8ForWrite validates a locally authored BOLT 1 text field.
func checkUTF8ForWrite[T tlv.TlvType](opt tlv.OptionalRecordT[T, tlv.Blob],
name string) error {

return fn.MapOptionZ(opt.ValOpt(), func(data tlv.Blob) error {
if !lnwire.ValidBoltTextForWrite(data) {
return fmt.Errorf("%w: %s", ErrInvalidUTF8, name)
}

return nil
})
}

// checkUTF8 validates that a blob field contains BOLT 1-valid UTF-8 text.
func checkUTF8[T tlv.TlvType](opt tlv.OptionalRecordT[T, tlv.Blob],
name string) error {

return fn.MapOptionZ(opt.ValOpt(), func(data tlv.Blob) error {
if !utf8.Valid(data) {
if !lnwire.ValidBoltText(data) {
return fmt.Errorf("%w: %s", ErrInvalidUTF8, name)
}

Expand Down Expand Up @@ -1574,6 +1612,22 @@ func validateInvoiceWrite(inv *Invoice) error {
return err
}

if err := checkUTF8ForWrite(
inv.OfferDescription, "offer_description",
); err != nil {
return err
}
if err := checkUTF8ForWrite(
inv.OfferIssuer, "offer_issuer",
); err != nil {
return err
}
if err := checkUTF8ForWrite(
inv.InvreqPayerNote, "invreq_payer_note",
); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -1987,6 +2041,20 @@ func validateInvoiceRead(inv *Invoice, activeChain [32]byte,
// validateInvoiceAgainstRequest; the fallback ignore rules by
// usableFallbackAddresses.

if err := checkUTF8(
inv.OfferDescription, "offer_description",
); err != nil {
return err
}
if err := checkUTF8(inv.OfferIssuer, "offer_issuer"); err != nil {
return err
}
if err := checkUTF8(
inv.InvreqPayerNote, "invreq_payer_note",
); err != nil {
return err
}

// - MUST reject the invoice if signature is not a valid signature using
// invoice_node_id as described in Signature Calculation.
return verifyInvoice(inv)
Expand Down
Loading
Loading