mirror of
https://github.com/Such-Software/xmrcheckout.git
synced 2026-12-01 12:14:11 +01:00
fix(webhooks): refuse internal webhook targets and stop following redirects
Merchant-supplied webhook URLs were posted to without any host check, redirects were followed, and the raw request error was stored and returned to the caller. With open registration, anyone could make the service send requests to the wallet RPC, the database, the daemon or a cloud metadata address, and read back what the error revealed. Every delivery path now goes through webhook_targets.post_webhook. It accepts only http and https URLs without embedded credentials, requires every resolved address to be public, and never follows redirects (a 3xx is a failed delivery). WEBHOOK_ALLOWED_PRIVATE_HOSTS names exact hosts that may resolve privately, for receivers reached over a LAN. Stored delivery errors carry only the refusal reason or the exception class.
This commit is contained in:
+11
-38
@@ -6,20 +6,16 @@ import json
|
||||
import logging
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
from urllib.parse import urljoin
|
||||
|
||||
import requests
|
||||
from requests import RequestException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .models import BtcpayWebhook, Invoice, InvoiceTransfer
|
||||
from .security import decrypt_secret
|
||||
from .webhook_targets import WebhookTargetRefused, post_webhook
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_REDIRECT_STATUSES = {301, 302, 303, 307, 308}
|
||||
|
||||
|
||||
def dispatch_btcpay_webhooks(
|
||||
db: Session,
|
||||
user_id: str,
|
||||
@@ -61,13 +57,13 @@ def dispatch_btcpay_webhooks(
|
||||
last_response = None
|
||||
for attempt in range(3):
|
||||
try:
|
||||
last_response = _post_with_redirects(
|
||||
last_response = post_webhook(
|
||||
hook.url,
|
||||
data=body,
|
||||
headers=headers,
|
||||
timeout=10,
|
||||
)
|
||||
if last_response is not None and last_response.status_code < 500:
|
||||
if last_response.status_code < 500:
|
||||
break
|
||||
except RequestException:
|
||||
if attempt == 2:
|
||||
@@ -75,12 +71,9 @@ def dispatch_btcpay_webhooks(
|
||||
if attempt < 2:
|
||||
time.sleep(1.0 * (attempt + 1))
|
||||
if last_response is None:
|
||||
logger.warning(
|
||||
"BTCPay webhook delivery failed after retries (redirect loop)",
|
||||
extra={"webhook_id": str(hook.id), "event": event_type, "url": hook.url},
|
||||
)
|
||||
continue
|
||||
if last_response.status_code >= 400:
|
||||
# Redirects are not followed, so a 3xx is a failed delivery.
|
||||
if last_response.status_code >= 300:
|
||||
logger.warning(
|
||||
"BTCPay webhook delivered non-success status",
|
||||
extra={
|
||||
@@ -89,6 +82,12 @@ def dispatch_btcpay_webhooks(
|
||||
"http_status": last_response.status_code,
|
||||
},
|
||||
)
|
||||
except WebhookTargetRefused as exc:
|
||||
logger.warning(
|
||||
"BTCPay webhook target refused: %s",
|
||||
exc,
|
||||
extra={"webhook_id": str(hook.id), "event": event_type},
|
||||
)
|
||||
except RequestException as exc:
|
||||
logger.error(
|
||||
"BTCPay webhook delivery failed after 3 attempts: %s",
|
||||
@@ -179,29 +178,3 @@ def _after_expiration(invoice: Invoice) -> bool:
|
||||
|
||||
def _sign_payload(body: bytes, secret: str) -> str:
|
||||
return hmac.new(secret.encode("utf-8"), body, hashlib.sha256).hexdigest()
|
||||
|
||||
|
||||
def _post_with_redirects(
|
||||
url: str,
|
||||
*,
|
||||
data: bytes,
|
||||
headers: dict[str, str],
|
||||
timeout: int,
|
||||
max_redirects: int = 3,
|
||||
) -> requests.Response | None:
|
||||
current_url = url
|
||||
for _ in range(max_redirects + 1):
|
||||
response = requests.post(
|
||||
current_url,
|
||||
data=data,
|
||||
headers=headers,
|
||||
timeout=timeout,
|
||||
allow_redirects=False,
|
||||
)
|
||||
if response.status_code not in _REDIRECT_STATUSES:
|
||||
return response
|
||||
location = response.headers.get("Location")
|
||||
if not location:
|
||||
return response
|
||||
current_url = urljoin(current_url, location)
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user