What changed, and why it matters
This merge commit hardens LNbits Paywall's file-download feature against two security risks. First, it stops the server from fetching files from internal/private network addresses (so an attacker can't trick the paywall into reading secrets from itself). Second, it hides secret download headers from non-admin users when listing paywalls, preventing invoice-key holders from stealing credentials. The old code used a basic Python URL fetcher and now uses a safer HTTP client with URL validation and streamed downloads.
Review the implementation of validate_url() and SafeHttpClient in the lnbits core dependency to confirm the SSRF filter is comprehensive (covers IPv4/IPv6, DNS rebinding, redirects, and scheme restrictions). Ensure the admin-key check cannot be bypassed via dependency overrides or key_type spoofing. Consider adding tests for redirect handling and for header redaction on the single-paywall retrieval endpoint if one exists.
Security signals we found
Server-Side Request Forgery (SSRF) mitigation added via validate_url() and SafeHttpClient
Sensitive header redaction for non-admin API consumers
Switch from urllib.request.urlopen to safer, streamed HTTP client
New regression tests for download SSRF, payment gating, and header visibility
Evidence from the diff
The patch replaces urllib.request.urlopen with lnbits.utils.safe_http.SafeHttpClient plus validate_url() in api_paywall_download_file/_file_streamer. validate_url() is intended to block SSRF-style destinations (localhost, private IPs, etc.). It also redacts file_config.headers in api_paywalls unless the caller’s key_type is KeyType.admin, preventing disclosure of backend credentials to invoice-key users. New tests assert that a paid file URL cannot read a local server file, that unpaid downloads never resolve/fetch, that downloads preserve headers and stream correctly, and that headers are only visible with admin keys.
Changed components
views_api.py: api_paywalls listing endpointviews_api.py: api_paywall_download_file endpointviews_api.py: _file_streamer helpertests/test_download_security.pytests/test_header_security.pyInspect captured patch +194 / −6
### config.json
@@ -3,7 +3,7 @@
"version": "1.3.0",
"short_description": "Create paywalls for content",
"tile": "/paywall/static/image/paywall.png",
- "min_lnbits_version": "1.7.0-rc1",
+ "min_lnbits_version": "1.7.0",
"contributors": [
{
"name": "eillarra",
### 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 74/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.