diff --git a/internal/lnurl/lnurl.go b/internal/lnurl/lnurl.go index 5689601a..bb9b07d1 100644 --- a/internal/lnurl/lnurl.go +++ b/internal/lnurl/lnurl.go @@ -127,21 +127,17 @@ func (w Lnurl) Handle(writer http.ResponseWriter, request *http.Request) { zapEventQuery := request.FormValue("nostr") var zapEvent nostr.Event if len(zapEventQuery) > 0 { - err = json.Unmarshal([]byte(zapEventQuery), &zapEvent) - if err != nil { - log.Errorf("[handleLnUrl] Couldn't parse nostr event: %v", err) - } else { - valid, err := zapEvent.CheckSignature() - if !valid || err != nil { - log.Errorf("[handleLnUrl] Nostr NIP-57 zap event signature invalid: %v", err) - return - } - if len(zapEvent.Tags) == 0 || zapEvent.Tags.GetFirst([]string{"p"}) == nil { - // zapEvent.Tags.GetFirst([]string{"e"}) == nil { - log.Errorf("[handleLnUrl] Nostr NIP-57 zap event validation error") - return - } - + // A request that does not decode cleanly can still carry a pubkey, tags + // and sig (the library keeps the fields it read before the error), so + // it must never reach the zap path unverified. + if err = json.Unmarshal([]byte(zapEventQuery), &zapEvent); err != nil { + api.NotFoundHandler(writer, fmt.Errorf("[handleLnUrl] Couldn't parse nostr event: %v", err)) + return + } + valid, err := zapEvent.CheckSignature() + if !valid || err != nil { + api.NotFoundHandler(writer, fmt.Errorf("[handleLnUrl] Nostr NIP-57 zap event signature invalid: %v", err)) + return } } @@ -293,6 +289,22 @@ func (w Lnurl) serveLNURLpSecond(username string, amount_msat int64, comment str } else { user = user2 } + // A zap request is only honoured if it names this user (NIP-57, appendix D). + // The receipt is signed with the bot's key and is valid for any p tag, so + // the recipient must be bound to the paid user here. + if zapEvent.Sig != "" { + recipient := "" + if user.Settings != nil { + recipient = user.Settings.Nostr.PubKey + } + if err := validateZapRequest(zapEvent, amount_msat, recipient); err != nil { + return &lnurl.LNURLPayValues{ + LNURLResponse: lnurl.LNURLResponse{ + Status: api.StatusError, + Reason: "Invalid zap request."}, + }, fmt.Errorf("[serveLNURLpSecond] %v", err) + } + } // user is ok now create invoice // set wallet lnbits client diff --git a/internal/lnurl/nip57.go b/internal/lnurl/nip57.go index ef8d9895..83627bf9 100644 --- a/internal/lnurl/nip57.go +++ b/internal/lnurl/nip57.go @@ -3,7 +3,11 @@ package lnurl import ( "crypto/sha256" "encoding/hex" + "fmt" + "strconv" "time" + + "github.com/nbd-wtf/go-nostr" ) type Tag []string @@ -26,3 +30,37 @@ func (w Lnurl) Nip57DescriptionHash(zapEventSerialized string) string { hashString := hex.EncodeToString(hash[:]) return hashString } + +// validateZapRequest checks a NIP-57 zap request against the payment it is +// for: kind 9734, exactly one p tag naming the recipient, at most one e tag, +// and an amount tag (if present) equal to the amount of the payment. +// recipientPubkey is the nostr pubkey the paid user registered with the bot; +// without one the request cannot be bound to the user and is refused. +func validateZapRequest(ev nostr.Event, amountMsat int64, recipientPubkey string) error { + if ev.Kind != 9734 { + return fmt.Errorf("zap request has kind %d", ev.Kind) + } + var pTags, eTags int + for _, t := range ev.Tags { + if len(t) < 2 { + continue + } + switch t[0] { + case "p": + pTags++ + if recipientPubkey == "" || t[1] != recipientPubkey { + return fmt.Errorf("zap request p tag does not name the paid user") + } + case "e": + eTags++ + case "amount": + if v, err := strconv.ParseInt(t[1], 10, 64); err != nil || v != amountMsat { + return fmt.Errorf("zap request amount %q does not match %d msat", t[1], amountMsat) + } + } + } + if pTags != 1 || eTags > 1 { + return fmt.Errorf("zap request has %d p tags and %d e tags", pTags, eTags) + } + return nil +} diff --git a/internal/lnurl/nip57_test.go b/internal/lnurl/nip57_test.go new file mode 100644 index 00000000..680b079f --- /dev/null +++ b/internal/lnurl/nip57_test.go @@ -0,0 +1,37 @@ +package lnurl + +import ( + "testing" + + "github.com/nbd-wtf/go-nostr" +) + +func zap(kind int, tags ...nostr.Tag) nostr.Event { + return nostr.Event{Kind: kind, Tags: nostr.Tags(tags)} +} + +func TestValidateZapRequest(t *testing.T) { + const me, other = "aa", "bb" + p := func(k string) nostr.Tag { return nostr.Tag{"p", k} } + for _, c := range []struct { + name string + ev nostr.Event + rcpt string + ok bool + }{ + {"valid", zap(9734, p(me), nostr.Tag{"e", "n1"}, nostr.Tag{"amount", "1000"}), me, true}, + {"no amount tag", zap(9734, p(me)), me, true}, + {"other recipient", zap(9734, p(other)), me, false}, + {"user has no pubkey", zap(9734, p(me)), "", false}, + {"wrong kind", zap(1, p(me)), me, false}, + {"two p tags", zap(9734, p(me), p(me)), me, false}, + {"no p tag", zap(9734), me, false}, + {"two e tags", zap(9734, p(me), nostr.Tag{"e", "1"}, nostr.Tag{"e", "2"}), me, false}, + {"amount mismatch", zap(9734, p(me), nostr.Tag{"amount", "100000000"}), me, false}, + {"amount not a number", zap(9734, p(me), nostr.Tag{"amount", "x"}), me, false}, + } { + if err := validateZapRequest(c.ev, 1000, c.rcpt); (err == nil) != c.ok { + t.Errorf("%s: err=%v, want ok=%v", c.name, err, c.ok) + } + } +}