Repository navigation
fix(security): Nexi redirect MAC mandatory and constant-time (release 0.1.1) - #9
Open
nb-camelot wants to merge 1 commit into
Open
nb-camelot wants to merge 1 commit into
nb-camelot wants to merge 1 commit into
Conversation
…t time (#3) handle_redirect checked the MAC only when a non-empty one was present, so ?codTrans=X&esito=OK completed a payment without the MAC key. A missing or empty mac now fails ("MAC missing"), a wrong one fails ("MAC verification failed"), and the comparison uses hmac.compare_digest on bytes. Release 0.1.1. Fixes #3 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3
Change
NexiProvider.handle_redirect(awesome_python_checkout/providers/nexi.py) now requires the outcome MAC and compares it in constant time:mac→status="failed",payment_id=codTrans,raw={"error": "MAC missing"};mac→status="failed",raw={"error": "MAC verification failed"};hmac.compare_digest(received.lower().encode(), expected.encode()). It compares bytes, so a non-ASCIImacfails cleanly instead of raisingTypeError.nexi.pyhas no!=on MAC values left.esitois read only after the MAC checks.CheckoutConfigurator.handle_redirectis unchanged; it now emitspayment.failedfor these outcomes, neverpayment.completed.Compatibility:
esito=OKwithout a valid MAC goes fromcompletedtofailed. Non-OK outcomes stayfailed; onlyrawchanges. Whether every genuine Nexi return, includingurl_back, carriesmacstill has to be confirmed against the Nexi XPay documentation, as the issue notes.Tests (per acceptance criteria)
tests/test_providers.py, all using a MAC built withhashlibthrough the new_nexi_signed_outcomehelper:test_nexi_handle_redirect_missing_mac_rejectedandtest_nexi_handle_redirect_empty_mac_rejected:failed,payment_id == "ORD-1",raw == {"error": "MAC missing"}.test_nexi_handle_redirect_non_ascii_mac_rejected: a 40-char"é"MAC givesMAC verification failed, with no exception.test_nexi_handle_redirect_uppercase_mac_accepted: an uppercase hex MAC withesito=OKgivescompleted.test_nexi_handle_redirect_failed_esitonow sends a valid MAC, so it covers theesitopath. It assertsfailed,payment_idandraw["esito"] == "KO".test_nexi_handle_redirect_bad_macalso assertsraw == {"error": "MAC verification failed"}.tests/test_configurator.py:test_handle_redirect_nexi_missing_mac_fires_failed_eventuses a realNexiProvider. It asserts nopayment.completedand exactly onepayment.failedwith{"provider": "nexi", "error": {"error": "MAC missing"}}.Docs and release
### NexiConfig: the redirect MAC is required and checked in constant time.## [0.1.1] - 2026-09-29,### Security.pyproject.tomlversion is0.1.1. PyPI publishing (publish.yml) runs when a GitHub release is published, so merging alone publishes nothing.Mirror on awesome-node-checkout
The criteria ask for a mirror issue. It isn't needed:
NexiProvider.executePaymenton node-checkoutmain(aae0277, 1.3.0) already rejects a missing MAC (if (!mac)→'MAC missing',src/providers/nexi/nexi.provider.ts:189) and usescrypto.timingSafeEqual.Out of scope
The response-MAC field list differs between the ports: 7 fields in
_compute_response_machere, 4 in Node. Which one Nexi uses has to be confirmed on the Nexi XPay documentation, so it is left unchanged.Commands run (Python 3.11 venv,
pip install -e ".[dev]")main:43 passed, 1 warning.python -m pytest tests/ -qgives48 passed, 1 warning. The warning is the pre-existing Starlettehttpxdeprecation fromfastapi.testclient.awesome_python_checkout/frommainand the new tests kept:3 failed, 45 passed. The failing tests aremissing_mac,empty_macand the configurator event test.python -m build:Successfully built awesome_python_checkout-0.1.1.tar.gz and awesome_python_checkout-0.1.1-py3-none-any.whl.🤖 Generated with Claude Code