What changed, and why it matters
This commit fixes two security issues in the LNbits Paywall extension. First, it stops the download feature from using a basic Python URL fetcher that could be tricked into reading files from the server itself (a server-side request forgery risk). It replaces that with a safer HTTP client and validates URLs first. Second, it hides secret API keys and authorization tokens from non-admin users when listing paywalls, so regular users cannot steal credentials embedded in paywall configurations.
Deploy this commit promptly. Review any existing paywall file_config URLs for malicious values and audit access logs for unauthorized downloads or paywall listing access by non-admin keys. Consider adding allow-list validation for download domains if not already enforced by SafeHttpClient.
Security signals we found
SSRF mitigation: urllib.request.urlopen replaced with SafeHttpClient and validate_url()
Credential redaction: file_config.headers hidden from non-admin paywall listings
New security regression tests for download SSRF, unpaid-download gating, and header visibility
Streaming download now uses bounded chunk size (65536) and raises on HTTP errors
Evidence from the diff
The patch addresses two vulnerabilities in views_api.py. (1) The previous _file_streamer used urllib.request.urlopen, which is vulnerable to SSRF and lacks redirect/header controls. It is replaced by lnbits.utils.safe_http.SafeHttpClient with streaming, redirect following, and status checks, plus validate_url() is called before fetching. A new test confirms file:// URLs are rejected and that unpaid downloads do not trigger URL validation or fetching. (2) The api_paywalls listing endpoint previously returned file_config.headers to any caller with an invoice key. The patch now redacts headers to {} unless wallet.key_type == KeyType.admin, while preserving the stored headers for subsequent downloads. Tests verify non-admin responses do not contain private canary values.
Changed components
views_api.pyapi_paywalls endpointapi_paywall_download_file endpoint_file_streamer helperInspect captured patch +193 / −5
### tests/test_download_security.py
@@ -0,0 +1,96 @@
+import asyncio
+import socket
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import httpx
+import pytest
+from lnbits.settings import settings
+from lnbits.utils.safe_http import SafeHttpClient
+from starlette.exceptions import HTTPException
+
+from .. import views_api as views
+
+
+@pytest.fixture
+def anyio_backend():
+ return "asyncio"
+
+
+def paywall(url):
+ return SimpleNamespace(
+ memo="download",
+ extras=SimpleNamespace(
+ type="file",
+ file_config=SimpleNamespace(url=url, headers={"X-API-KEY": "download-key"}),
+ ),
+ )
+
+
+@pytest.mark.anyio
+async def test_paid_file_url_cannot_read_server_file(monkeypatch, tmp_path):
+ canary = tmp_path / "server-key"
+ canary.write_text("private-canary")
+ monkeypatch.setattr(
+ views, "get_paywall", AsyncMock(return_value=paywall(canary.as_uri()))
+ )
+ monkeypatch.setattr(views, "_is_payment_made", AsyncMock(return_value=1000))
+ with pytest.raises(HTTPException):
+ await views.api_paywall_download_file("wall", payment_hash="paid")
+
+
+@pytest.mark.anyio
+async def test_unpaid_download_never_resolves_or_fetches(monkeypatch):
+ monkeypatch.setattr(
+ views,
+ "get_paywall",
+ AsyncMock(return_value=paywall("https://public.example/file")),
+ )
+ monkeypatch.setattr(views, "_is_payment_made", AsyncMock(return_value=0))
+ validate = AsyncMock()
+ monkeypatch.setattr(views, "validate_url", validate)
+ with pytest.raises(HTTPException):
+ await views.api_paywall_download_file("wall", payment_hash="unpaid")
+ validate.assert_not_awaited()
+
+
+@pytest.mark.anyio
+async def test_paid_download_preserves_version_headers_and_streaming(monkeypatch):
+ monkeypatch.setattr(settings, "lnbits_callback_url_rules", [])
+ monkeypatch.setattr(
+ views,
+ "get_paywall",
+ AsyncMock(return_value=paywall("https://public.example/{version}/file")),
+ )
+ monkeypatch.setattr(views, "_is_payment_made", AsyncMock(return_value=1000))
+ monkeypatch.setattr(
+ asyncio.get_running_loop(),
+ "getaddrinfo",
+ AsyncMock(
+ return_value=[
+ (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 443))
+ ]
+ ),
+ )
+ seen = []
+
+ def factory(**kwargs):
+ client = SafeHttpClient(**kwargs)
+
+ def handler(request):
+ seen.append(request)
+ return httpx.Response(200, content=b"download" * 200000)
+
+ client.client._transport = httpx.MockTransport(handler)
+ return client
+
+ monkeypatch.setattr(views, "SafeHttpClient", factory)
+ response = await views.api_paywall_download_file(
+ "wall", version="v2", payment_hash="paid"
+ )
+ chunks = [chunk async for chunk in response.body_iterator]
+ assert b"".join(chunks) == b"download" * 200000
+ assert max(map(len, chunks)) <= 65536
+ assert seen[0].url.path == "/v2/file"
+ assert seen[0].headers["X-API-KEY"] == "download-key"
+ assert response.headers["Content-Disposition"] == 'attachment; filename="download"'
### tests/test_header_security.py
@@ -0,0 +1,82 @@
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import httpx
+import pytest
+from fastapi import FastAPI
+from lnbits.core.models import KeyType
+from lnbits.decorators import require_invoice_key
+
+from .. import views_api as views
+from ..models import Paywall, PaywallExtra, PaywallFileConfig
+
+
+@pytest.fixture
+def anyio_backend():
+ return "asyncio"
+
+
+@pytest.mark.anyio
+@pytest.mark.parametrize("admin", [False, True])
+@pytest.mark.parametrize("all_wallets", [False, True])
+async def test_list_headers_only_visible_with_admin_key(
+ monkeypatch, admin, all_wallets
+):
+ stored = Paywall(
+ id="paywall",
+ wallet="wallet",
+ url="https://example.com",
+ memo="file",
+ amount=1,
+ currency="sat",
+ remembers=False,
+ extras=PaywallExtra(
+ type="file",
+ file_config=PaywallFileConfig(
+ url="https://example.com/download",
+ headers={
+ "Authorization": "Bearer private-canary",
+ "X-API-Key": "secret-canary",
+ },
+ ),
+ ),
+ )
+ other = stored.model_copy(
+ update={"id": "other", "wallet": "other-wallet"}, deep=True
+ )
+ rows = [stored, other] if all_wallets else [stored]
+ listing = AsyncMock(return_value=rows)
+ monkeypatch.setattr(views, "get_paywalls", listing)
+ monkeypatch.setattr(
+ views,
+ "get_user",
+ AsyncMock(return_value=SimpleNamespace(wallet_ids=["wallet", "other-wallet"])),
+ )
+ wallet_info = SimpleNamespace(
+ key_type=KeyType.admin if admin else KeyType.invoice,
+ wallet=SimpleNamespace(id="wallet", user="owner"),
+ )
+ app = FastAPI()
+ app.include_router(views.paywall_api_router)
+ app.dependency_overrides[require_invoice_key] = lambda: wallet_info
+ async with httpx.AsyncClient(
+ transport=httpx.ASGITransport(app=app), base_url="http://test"
+ ) as client:
+ response = await client.get(
+ "/api/v1/paywalls", params={"all_wallets": all_wallets}
+ )
+ assert response.status_code == 200
+ listing.assert_awaited_once_with(
+ ["wallet", "other-wallet"] if all_wallets else ["wallet"]
+ )
+ for row in response.json():
+ assert row["extras"]["file_config"]["headers"] == (
+ stored.extras.file_config.headers if admin else {}
+ )
+ if not admin:
+ assert "private-canary" not in response.text
+ assert "secret-canary" not in response.text
+ # Redaction must never strip credentials needed by subsequent downloads.
+ assert all(
+ row.extras.file_config.headers["X-API-Key"] == "secret-canary" for row in rows
+ )
### views_api.py
@@ -1,7 +1,6 @@
import json
from asyncio import Queue
from http import HTTPStatus
-from urllib import request
from fastapi import (
APIRouter,
@@ -15,7 +14,7 @@
from fastapi.exceptions import HTTPException
from fastapi.responses import StreamingResponse
from lnbits.core.crud import get_standalone_payment, get_user
-from lnbits.core.models import CreateInvoice, WalletTypeInfo
+from lnbits.core.models import CreateInvoice, KeyType, WalletTypeInfo
from lnbits.core.services import (
check_transaction_status,
create_invoice,
@@ -26,6 +25,7 @@
require_invoice_key,
)
from lnbits.utils.exchange_rates import fiat_amount_as_satoshis
+from lnbits.utils.safe_http import SafeHttpClient, validate_url
from loguru import logger
from .crud import (
@@ -52,7 +52,13 @@ async def api_paywalls(
user = await get_user(wallet.wallet.user)
wallet_ids = user.wallet_ids if user else []
- return await get_paywalls(wallet_ids)
+ paywalls = await get_paywalls(wallet_ids)
+ if wallet.key_type != KeyType.admin:
+ paywalls = [paywall.model_copy(deep=True) for paywall in paywalls]
+ for paywall in paywalls:
+ if paywall.extras and paywall.extras.file_config:
+ paywall.extras.file_config.headers = {}
+ return paywalls
@paywall_api_router.post("/api/v1/paywalls")
@@ -246,6 +252,7 @@ async def api_paywall_download_file(
headers = {"Content-Disposition": f'attachment; filename="{paywall.memo}"'}
if version:
file_config.url = file_config.url.format(version=version)
+ await validate_url(file_config.url)
return StreamingResponse(
content=_file_streamer(file_config.url, file_config.headers),
headers=headers,
@@ -295,8 +302,11 @@ async def api_paywall_check_file(paywall_id: str, payment_hash: str | None = Non
async def _file_streamer(url, headers):
- with request.urlopen(request.Request(url=url, headers=headers)) as dl_file:
- yield dl_file.read()
+ async with SafeHttpClient(follow_redirects=True) as client:
+ async with client.stream("GET", url, headers=headers) as response:
+ response.raise_for_status()
+ async for chunk in response.aiter_bytes(chunk_size=65536):
+ yield chunk
async def _create_paywall_invoice(paywall: Paywall, amount: int):Why this scored 76/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.