fix: Mercado Pago's signed notifications verify with a pasted secret and close formats
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 2m43s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 54s

Production refused every notification, all current-format (data.id, type),
signed and fresh, though the event and the secret looked right. The secret is
now trimmed (a space or line break pasted into the stack changes every HMAC),
and a few close variants of the signed text are tried besides the documented
one (without the request id, the id's own case, the id from the body); each
still needs the secret. The one that matched, and whether the secret was
trimmed, go to the audit log so the real format can be settled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-10-02 15:43:34 -03:00
parent ca7cf7f701
commit 0218f119aa
3 changed files with 56 additions and 12 deletions

View File

@@ -62,6 +62,24 @@ class MercadoPagoTests(unittest.TestCase):
'x-request-id': 'req-1'}, b'{}', {'data.id': '123456'}))
self.assertFalse(self.mp.verify({}, b'{}', {'data.id': '123456'}))
def test_a_secret_pasted_with_spaces_still_verifies(self):
mp = MercadoPagoPayment('TEST-token', SECRET + ' \n', 'https://dtf.example/api/payments/webhook',
transport=httpx.MockTransport(lambda r: httpx.Response(200)), clock=lambda: NOW)
headers = {'x-signature': signature('123456', 'req-1', NOW), 'x-request-id': 'req-1'}
self.assertTrue(mp.verify(headers, b'{}', {'data.id': '123456'}))
self.assertTrue(mp.secret_was_trimmed)
self.assertEqual(mp.last_signature, 'documentado')
def test_close_variants_of_the_signed_text_are_named(self):
# Signed without the request id, though the header came.
headers = {'x-signature': signature('123456', None, NOW), 'x-request-id': 'req-1'}
self.assertTrue(self.mp.verify(headers, b'{}', {'data.id': '123456'}))
self.assertEqual(self.mp.last_signature, 'sem request-id')
# Still the secret's: another secret matches no variant.
forged = {'x-signature': signature('123456', None, NOW, 'other'), 'x-request-id': 'req-1'}
self.assertFalse(self.mp.verify(forged, b'{}', {'data.id': '123456'}))
self.assertIsNone(self.mp.last_signature)
def test_absent_values_are_left_out_and_alphanumeric_ids_lowercased(self):
self.assertTrue(self.mp.verify({'x-signature': signature('abc123', None, NOW)}, b'{}',
{'data.id': 'ABC123'}))