From ca7cf7f701039ac92d4d743961b712eee1c33860 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Fri, 2 Oct 2026 15:18:51 -0300 Subject: [PATCH] chore: say what a refused payment notification looked like "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 --- app/api/payments.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/api/payments.py b/app/api/payments.py index 8787ace..4eb8f76 100644 --- a/app/api/payments.py +++ b/app/api/payments.py @@ -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=,v1=), 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: