What changed, and why it matters
This commit adds safety checks to LNbits' NWC (Nostr Wallet Connect) wallet code. It now verifies that invoice lookups return the expected payment hash, and that Nostr events come from the expected service public key and have valid signatures. These changes close paths where a malicious relay or third party could return fake invoice data or impersonate the wallet service.
Review the rest of the NWC wallet code for additional places where lookup_invoice results or Nostr events are consumed without origin/integrity checks. Add regression tests for mismatched payment_hash and forged-pubkey events. Consider whether raising a generic Exception is sufficient or if a more specific exception/logging is warranted.
Security signals we found
Missing cryptographic origin check on Nostr events before patch
Missing payment_hash integrity check on lookup_invoice responses before patch
Addition of pubkey verification against expected service key
Addition of authors filter on response subscription
Addition of payment_hash equality checks on invoice lookups
Evidence from the diff
The patch hardens NWCWallet and NWCConnection in lnbits/wallets/nwc.py. Two invoice lookup sites now raise if the returned payment_hash does not match the requested checking_id, preventing a relay/service from substituting a different invoice’s status. The event handler now rejects events whose pubkey differs from self.service_pubkey_hex, and the response subscription filters events by authors=[self.service_pubkey_hex], ensuring responses are only accepted from the intended NWC service and not arbitrary relays/impersonators.
Changed components
lnbits/wallets/nwc.pyNWCWallet.get_payment_statusNWCWallet.get_invoice_statusNWCConnection.handle_eventNWCConnection.send_requestInspect captured patch +8 / −2
diff --git a/lnbits/wallets/nwc.py b/lnbits/wallets/nwc.py
index c73ba66..a6f1f82 100644
--- a/lnbits/wallets/nwc.py
+++ b/lnbits/wallets/nwc.py
@@ -88,6 +88,8 @@ class NWCWallet(Wallet):
payment_data = await self.conn.call(
"lookup_invoice", {"payment_hash": payment["checking_id"]}
)
+ if payment_data.get("payment_hash") != payment["checking_id"]:
+ raise Exception("Mismatched payment hash")
settled = (
"settled_at" in payment_data
and payment_data["settled_at"]
@@ -264,6 +266,8 @@ class NWCWallet(Wallet):
payment_data = await self.conn.call(
"lookup_invoice", {"payment_hash": checking_id}
)
+ if payment_data.get("payment_hash") != checking_id:
+ raise Exception("Mismatched payment hash")
settled = payment_data.get("settled_at", None) and payment_data.get(
"preimage", None
)
@@ -520,8 +524,9 @@ class NWCConnection:
"""
sub_id = cast(str, msg[1])
event = cast(dict, msg[2])
- if not verify_event(event): # Ensure the event is valid (do not trust relays)
- raise Exception("Invalid event signature")
+ # Ensure the event is valid (do not trust relays)
+ if not verify_event(event) or event.get("pubkey") != self.service_pubkey_hex:
+ raise Exception("Invalid event")
tags = event["tags"]
if event["kind"] == 13194: # An info event
# info events are handled specially,
@@ -687,6 +692,7 @@ class NWCConnection:
"#p": [self.account_public_key_hex],
"#e": [event["id"]],
"since": event["created_at"],
+ "authors": [self.service_pubkey_hex],
}
sub_id = self._get_new_subid()
# register a future to receive the response asynchronously
Why this scored 70/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.