From ac69ae984622e127235e13d1b532e7d4a15b1bf9 Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Sun, 23 Feb 2025 16:51:26 +0000 Subject: [PATCH] more hardening --- paranoia.py | 8 ++++++++ tasks.py | 7 +++++-- views_api.py | 7 +++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/paranoia.py b/paranoia.py index b1f905c..17829e8 100644 --- a/paranoia.py +++ b/paranoia.py @@ -144,4 +144,12 @@ def assert_valid_bolt11(invoice:str): if not ENABLE_HARDENING: return assert_printable(invoice) + + +# Check if boolean +def assert_boolean(v:bool): + if not ENABLE_HARDENING: + return + if not isinstance(v, bool): + panic("not a boolean") diff --git a/tasks.py b/tasks.py index 5091656..1cc1f1a 100644 --- a/tasks.py +++ b/tasks.py @@ -22,7 +22,7 @@ from .execution_queue import execution_queue from .models import NWCKey, TrackedSpendNWC, GetNWC from .nwcp import NWCServiceProvider from .permission import nwc_permissions -from .paranoia import assert_valid_wallet_id, assert_valid_pubkey, assert_sane_string, assert_valid_timestamp_seconds, assert_valid_expiration_seconds, assert_valid_msats, assert_valid_positive_int, assert_valid_bolt11, assert_valid_sha256 +from .paranoia import assert_valid_wallet_id, assert_valid_pubkey, assert_boolean, assert_sane_string, assert_valid_expiration_seconds, assert_valid_msats, assert_valid_positive_int, assert_valid_bolt11, assert_valid_sha256 async def _check(nwc: Optional[NWCKey], method: str) -> Optional[Dict]: @@ -202,6 +202,8 @@ async def _on_multi_pay_invoice( # hardening # assert_valid_bolt11(invoice) assert_valid_msats(amount_msats) + if invoice_id: + assert_sane_string(invoice_id) # ## # res = await _process_invoice( @@ -379,13 +381,14 @@ async def _on_list_transactions( limit = payload.get("limit", 10) offset = payload.get("offset", 0) unpaid = payload.get("unpaid", False) - tx_type = payload.get("type", None) + tx_type = payload.get("type", "") # hardening # assert_valid_positive_int(tfrom) assert_valid_positive_int(tto) assert_valid_positive_int(limit) assert_valid_positive_int(offset) + assert_boolean(unpaid) assert_sane_string(tx_type) # ## # diff --git a/views_api.py b/views_api.py index fcd755e..b7ef1ad 100644 --- a/views_api.py +++ b/views_api.py @@ -28,7 +28,7 @@ from .models import ( GetBudgetsNWC ) from .permission import nwc_permissions -from .paranoia import assert_valid_wallet_id, assert_valid_pubkey, assert_sane_string +from .paranoia import assert_valid_wallet_id, assert_valid_pubkey, assert_sane_string, assert_boolean nwcprovider_api_router = APIRouter() @@ -57,6 +57,8 @@ async def api_get_nwcs( # hardening # assert_valid_wallet_id(wallet_id) + assert_boolean(include_expired) + assert_boolean(calculate_spent_budget) # ## # wallet_nwcs = GetWalletNWC( @@ -84,13 +86,14 @@ async def api_get_nwcs( ) async def api_get_nwc( pubkey: str, - include_expired: Optional[bool] = False, + include_expired: bool = False, wallet: WalletTypeInfo = Depends(require_admin_key) ) -> NWCGetResponse: wallet_id = wallet.wallet.id # hardening # assert_valid_pubkey(pubkey) + assert_boolean(include_expired) assert_valid_wallet_id(wallet_id) # ## #