fix: ignore failed payments in daily withdraw limit (#4297)
What changed, and why it matters
This commit fixes a bug in LNbits where failed outgoing payments were incorrectly counted against a wallet's daily withdrawal limit. Because failed payments never actually moved money out of the wallet, counting them could wrongly block legitimate withdrawals. The fix adds a filter so only completed (successful) outgoing payments are tallied.
Review whether other payment-summation or limit-checking functions (e.g., fee totals, balance checks, admin limits) also need explicit `complete=True` or equivalent state filtering. Deploy the patch and verify the new unit tests pass.
Security signals we found
Business-logic flaw in access-control / spending limit
Incorrect state filter on financial transaction query
Denial-of-service-like effect: legitimate withdrawals blocked by stale failed payment records
Fix validated by new unit tests covering both success and failure states
Evidence from the diff
In check_wallet_daily_withdraw_limit, the call to get_payments now passes complete=True. Previously, the function summed all outgoing payments since midnight without filtering by state, so payments in FAILED, PENDING, or other non-final states could consume the configured daily withdrawal budget. A unit test confirms that a PaymentState.FAILED outgoing payment does not trigger the limit, while a PaymentState.SUCCESS one does.
Changed components
lnbits/core/services/payments.pycheck_wallet_daily_withdraw_limit functiondaily withdrawal limit enforcementInspect captured patch +54 / −0
### lnbits/core/services/payments.py
@@ -654,6 +654,7 @@ async def check_wallet_daily_withdraw_limit(
while settings.lnbits_running:
payments = await get_payments(
since=since,
+ complete=True,
outgoing=True,
wallet_id=wallet_id,
limit=page_size,
### tests/unit/test_services_wallet_limit.py
@@ -1,5 +1,10 @@
+from uuid import uuid4
+
import pytest
+from lnbits.core.crud import create_payment, create_wallet
+from lnbits.core.models import CreatePayment, PaymentState
+from lnbits.core.services import create_user_account
from lnbits.core.services.payments import check_wallet_daily_withdraw_limit
from lnbits.settings import Settings
@@ -34,3 +39,51 @@ async def test_no_wallet_spend_allowed(settings: Settings):
await check_wallet_daily_withdraw_limit(
conn=None, wallet_id="333333", amount_msat=0
)
+
+
+async def _create_outgoing_payment(wallet_id: str, status: PaymentState):
+ checking_id = f"checking_{uuid4().hex[:8]}"
+ await create_payment(
+ checking_id=checking_id,
+ data=CreatePayment(
+ wallet_id=wallet_id,
+ payment_hash=uuid4().hex,
+ bolt11=f"bolt11-{checking_id}",
+ amount_msat=-5_000,
+ memo="",
+ ),
+ status=status,
+ )
+
+
+@pytest.mark.anyio
+async def test_failed_payments_do_not_count_towards_limit(app, settings: Settings):
+ user = await create_user_account()
+ wallet = await create_wallet(user_id=user.id)
+ await _create_outgoing_payment(wallet.id, PaymentState.FAILED)
+ settings.lnbits_wallet_limit_daily_max_withdraw = 5
+ try:
+ result = await check_wallet_daily_withdraw_limit(
+ conn=None, wallet_id=wallet.id, amount_msat=1_000
+ )
+ finally:
+ settings.lnbits_wallet_limit_daily_max_withdraw = 0
+
+ assert result is None, "Failed payments should not use up the limit."
+
+
+@pytest.mark.anyio
+async def test_successful_payments_count_towards_limit(app, settings: Settings):
+ user = await create_user_account()
+ wallet = await create_wallet(user_id=user.id)
+ await _create_outgoing_payment(wallet.id, PaymentState.SUCCESS)
+ settings.lnbits_wallet_limit_daily_max_withdraw = 5
+ try:
+ with pytest.raises(
+ ValueError, match="Daily withdrawal limit of 5 sats reached."
+ ):
+ await check_wallet_daily_withdraw_limit(
+ conn=None, wallet_id=wallet.id, amount_msat=1_000
+ )
+ finally:
+ settings.lnbits_wallet_limit_daily_max_withdraw = 0Why this scored 52/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.