What changed, and why it matters
This change fixes a bug where LNbits would leave a payment stuck in 'pending' status when the underlying LND lightning node rejected the payment because it was a self-payment (paying your own invoice). The fix tells LNbits to recognize LND's 'self-payments not allowed' error as a pre-dispatch failure, so it marks the payment as failed and refunds the user's balance instead of leaving it hanging.
Deploy the patch. Review other wallet backends for similar unhandled pre-dispatch error messages. Monitor for any stuck pending payments that may need manual reconciliation on affected nodes.
Security signals we found
State inconsistency bug: payment status not synchronized with backend failure
Self-payment rejection not handled as a terminal failure
Regression test added for security/state correctness
No input validation bypass or cryptographic weakness in diff
Evidence from the diff
The commit adds ‘self-payments not allowed’ to the _PRE_DISPATCH_PAYMENT_ERROR_MESSAGES tuple in both lndgrpc.py and lndrest.py wallet backends. This causes LNbits to classify LND self-payment rejections as pre-dispatch failures, marking the payment failed rather than pending. It also adds regression tests verifying that a same-node invoice rejected by LND returns HTTP 520 with status ‘failed’, the payment record is marked failed, and the wallet balance is unchanged.
Changed components
lnbits/wallets/lndgrpc.pylnbits/wallets/lndrest.pytests/regtest/test_real_invoice.pytests/unit/test_wallet_payment_ambiguity.pyInspect captured patch +62 / −0
diff --git a/lnbits/wallets/lndgrpc.py b/lnbits/wallets/lndgrpc.py
index 2641de2..aa101b5 100644
--- a/lnbits/wallets/lndgrpc.py
+++ b/lnbits/wallets/lndgrpc.py
@@ -394,6 +394,7 @@ _PRE_DISPATCH_PAYMENT_ERROR_CODES = {
_PRE_DISPATCH_PAYMENT_ERROR_MESSAGES = (
"invoice not for current active network",
"invoice expired",
+ "self-payments not allowed",
)
diff --git a/lnbits/wallets/lndrest.py b/lnbits/wallets/lndrest.py
index bec06bf..8becd03 100644
--- a/lnbits/wallets/lndrest.py
+++ b/lnbits/wallets/lndrest.py
@@ -30,6 +30,7 @@ _PRE_DISPATCH_PAYMENT_ERROR_CODES = {3, 7, 16}
_PRE_DISPATCH_PAYMENT_ERROR_MESSAGES = (
"invoice not for current active network",
"invoice expired",
+ "self-payments not allowed",
)
diff --git a/tests/regtest/test_real_invoice.py b/tests/regtest/test_real_invoice.py
index 602f2e3..0a05661 100644
--- a/tests/regtest/test_real_invoice.py
+++ b/tests/regtest/test_real_invoice.py
@@ -19,6 +19,7 @@ from lnbits.core.services.payments import (
)
from lnbits.core.services.users import create_user_account
from lnbits.exceptions import PaymentError
+from lnbits.settings import Settings
from lnbits.task_manager import task_manager
from lnbits.wallets import get_funding_source
@@ -106,6 +107,63 @@ async def test_pay_real_invoice_noroute(
assert payment["status"] == "failed"
+@pytest.mark.anyio
+@pytest.mark.skipif(
+ get_funding_source().__class__.__name__ not in ("LndRestWallet", "LndWallet"),
+ reason="LND self-payment regression requires an LND funding source",
+)
+async def test_pay_real_invoice_self_payment_is_failed(
+ client,
+ from_wallet,
+ adminkey_headers_from,
+ inkey_headers_from,
+ settings: Settings,
+ monkeypatch: pytest.MonkeyPatch,
+):
+ """A same-node invoice rejected by LND must not stay pending (#4240)."""
+ monkeypatch.setattr(settings, "lnd_rest_allow_self_payment", False)
+ monkeypatch.setattr(settings, "lnd_grpc_allow_self_payment", False)
+
+ # Create directly on the funding source so LNbits cannot pay it internally.
+ funding_source = get_funding_source()
+ invoice = await funding_source.create_invoice(amount=100, memo="regression #4240")
+ assert invoice.ok, invoice.error_message
+ assert invoice.payment_request
+ payment_hash = bolt11.decode(invoice.payment_request).payment_hash
+ assert await get_standalone_payment(payment_hash) is None
+
+ wallet_before = await get_wallet(from_wallet.id)
+ assert wallet_before
+
+ response = await client.post(
+ "/api/v1/payments",
+ json={"out": True, "bolt11": invoice.payment_request},
+ headers=adminkey_headers_from,
+ )
+ assert response.status_code == 520, response.text
+ assert response.json()["status"] == "failed"
+
+ payment = await get_standalone_payment(payment_hash, wallet_id=from_wallet.id)
+ assert payment
+ assert payment.is_out
+ assert payment.failed
+
+ response = await client.get(
+ f"/api/v1/payments/{payment_hash}", headers=inkey_headers_from
+ )
+ assert response.status_code == 200, response.text
+ assert response.json()["paid"] is False
+ assert response.json()["status"] == "failed"
+
+ payment_after = await get_standalone_payment(payment_hash, wallet_id=from_wallet.id)
+ assert payment_after
+ assert payment_after.failed
+
+ wallet_after = await get_wallet(from_wallet.id)
+ assert wallet_after
+ assert wallet_after.balance_msat == wallet_before.balance_msat
+
+
@pytest.mark.anyio
@pytest.mark.skipif(is_fake, reason="this only works in regtest")
async def test_pay_real_invoice_mainnet(
diff --git a/tests/unit/test_wallet_payment_ambiguity.py b/tests/unit/test_wallet_payment_ambiguity.py
index 9c23d32..001a99d 100644
--- a/tests/unit/test_wallet_payment_ambiguity.py
+++ b/tests/unit/test_wallet_payment_ambiguity.py
@@ -164,6 +164,7 @@ async def test_lndrest_unknown_payment_state_is_pending(
False,
),
({"code": 2, "message": "invoice expired"}, False),
+ ({"code": 2, "message": "self-payments not allowed"}, False),
({"code": 3, "message": "invalid payment request"}, False),
({"code": 2, "message": "payment stream interrupted"}, None),
({"code": 14, "message": "transport unavailable"}, None),
@@ -200,6 +201,7 @@ async def test_lndrest_only_pre_dispatch_rpc_errors_are_failed(
False,
),
(grpc.StatusCode.UNKNOWN, "invoice expired", False),
+ (grpc.StatusCode.UNKNOWN, "self-payments not allowed", False),
(grpc.StatusCode.INVALID_ARGUMENT, "invalid payment request", False),
(grpc.StatusCode.PERMISSION_DENIED, "permission denied", False),
(grpc.StatusCode.UNAUTHENTICATED, "invalid macaroon", False),
Why this scored 50/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.