chore: say what a refused payment notification looked like
All checks were successful
Build and deploy / Validate source (push) Successful in 10s
Build and deploy / Integration suite on a real stack (push) Successful in 3m8s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 57s

"payment_webhook_rejected reason=signature" did not tell an unsigned old-style
IPN notice (topic/id, which Mercado Pago may send alongside the signed webhook
when a notification_url is given) from a real signature mismatch. The audit now
records the query parameter names, source_news, whether the signature and
request-id headers came, and the signature's age; never values or secrets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-10-02 15:18:51 -03:00
parent 67825a983d
commit ca7cf7f701

View File

@@ -33,7 +33,14 @@ async def webhook(request: Request):
query = dict(request.query_params)
if not payment.verify(request.headers, body, query):
audit('payment_webhook_rejected', ip=client_ip(request), reason='signature')
# What arrived, never what it carried: which query parameters (Mercado
# Pago's current webhook sends data.id/type, its old IPN topic/id and
# often no signature), whether the signature and request id headers came,
# and how old the signature's timestamp is.
audit('payment_webhook_rejected', ip=client_ip(request), reason='signature',
params=','.join(sorted(query))[:200], source=query.get('source_news', '')[:20],
signed='x-signature' in request.headers, request_id='x-request-id' in request.headers,
age=signature_age(request.headers.get('x-signature', '')))
raise HTTPException(403, 'Invalid signature')
event = payment.parse(body, query)
@@ -61,6 +68,14 @@ def event_provider():
return payment.name
def signature_age(signature):
"""Seconds since the signature's ts (ts=<unix>,v1=<hex>), or '' without one."""
import time
parts = dict(part.strip().split('=', 1) for part in signature.split(',') if '=' in part)
ts = parts.get('ts', '')
return int(time.time() - int(ts[:10])) if ts[:10].isdigit() else ''
def provider_reason(response):
"""A short, loggable reason from a refused provider call."""
try: