View Issue Details
| ID | Project | Category | View Status | Date Submitted | Last Update |
|---|---|---|---|---|---|
| 0009457 | Taler | merchant backend | public | 2025-01-13 16:37 | 2026-11-22 09:37 |
| Reporter | fefe | ||||
| Priority | normal | Severity | tweak | ||
| Status | implementing | ||||
| Product Version | git (master) | ||||
| Target Version | post-1.0 | ||||
| Summary | 0009457: scary comment about double payment | ||||
| Description | This is in batch_deposit_transaction (taler-merchant-httpd_post-orders-ID-pay.c): ``` 930 /* NOTE: We might want to check if the order was fully paid concurrently 931 by some other wallet here, and if so, issue an auto-refund. Right now, 932 it is possible to over-pay if two wallets literally make a concurrent 933 payment, as the earlier check for 'paid' is not in the same transaction 934 scope as this 'insert' operation. */ ``` Has this been fixed elsewhere? That sounds like a pretty bad race condition that could be triggered, for example, if there is a network outage, a customer tries to pay again, and then when the network comes back up both transactions are retried automatically. | ||||
| Tags | No tags attached. | ||||
| Well, I think the comment is more scary than reality. Not because it is false, but because of the context: - Before a wallet can pay, it needs to "claim" the contract (using the /claim endpoint). - Only one wallet can claim an order, only that wallet gets the contract terms. - Regular wallets would never attempt to pay an order for which they didn't get the contract terms (would be like sending money without knowing how much or for what). I didn't check if we validate that *this* wallet did the claim before in this endpoint, but at least in regular use we can be pretty sure wallets wouldn't pay contracts they didn't claim (and thus don't know the terms). So the only "practical" situation where this may still matter is if a wallet is backed up, restored to another device, filled with different coins (some _other_ transactions are made so that different coins are chosen), and then the user does a concurrent payment on both devices and manages to get the race. VERY, very unlikely. That said, the FIXME is correct and we probably should eventually do something about it. | |
| The formalization of the Taler protocols models this race. A payment that arrives for an order already paid must be refused (`ALREADY_PAID`); if its coins were deposited because two payments raced, the merchant must refund them in full, as `post-orders-ORDER_ID-pay.rst` already documents (`ALREADY_PAID` with refund signatures). Without that refund the model reaches a state in which a wallet stays charged for an order that another payment paid: the over-payment described in the FIXME in `taler-merchant-httpd_post-orders-ORDER_ID-pay.c` is possible, and the excess is not returned to the wallet. Proposed fix: within the transaction that records the deposits, check whether the order is paid; if so, refund the new deposits and answer `ALREADY_PAID`. Found by: the formalization of the Taler protocols; the rule violated is D10 in `spec/deposit.md`: the merchant marks an order paid once, and refunds in full the coins of a payment that raced with the one that paid it, so no wallet is charged twice for one order. | |
| Proposed fix on branch dev/oec/fix-9457 (merchant, on master 07b3040b): - At the start of the payment transaction, check_paid_concurrently(): if the order is marked paid and the deposits of coins not in this payment, less their refunds, cover the price, this payment raced with the one that paid. It is then refunded in full per coin (TALER_MERCHANTDB_insert_refund_by_coin, rtransaction 0, as for an abort), committed, and answered with 409 ALREADY_PAID and refund signatures for its coins. Nothing else of it (tokens, money pots, inventory) is recorded. - A replay of the same payment while it is the one that paid is unaffected (its coins are among those that pay). - phase_contract_paid: coins refunded in full no longer count as paying, so a replay of the refunded payment gets ALREADY_PAID with refunds instead of an idempotent success; the refund-signing loop is shared (reply_already_paid). test_merchant_api_{rsa,cs} and the harness payment, payment-multiple, payment-abort, payment-transient, pay-paid, payment-claim, refund, refund-incremental, merchant-refund-api and payment-forgettable pass. The race itself needs two concurrent /pay requests and has no test. | |
| https://git-www.taler.net/merchant.git/log/?h=dev/oec/fix-9457 | |
| Date Modified | Username | Field | Change |
|---|---|---|---|
| 2025-01-13 16:37 | fefe | New Issue | |
| 2025-01-13 16:37 | fefe | Status | new => assigned |
| 2025-01-13 16:37 | fefe | Locked by | => Christian Grothoff |
| 2025-01-13 22:00 | Christian Grothoff | Note Added: 0023976 | |
| 2025-01-13 22:01 | Christian Grothoff | Locked by | Christian Grothoff => |
| 2025-01-13 22:01 | Christian Grothoff | Severity | minor => tweak |
| 2025-01-13 22:01 | Christian Grothoff | Status | assigned => implementing |
| 2025-01-13 22:01 | Christian Grothoff | Product Version | => git (master) |
| 2025-01-13 22:01 | Christian Grothoff | Target Version | => post-1.0 |
| 2025-03-22 13:09 | Christian Grothoff | Category | mechant backend => merchant backend |
| 2026-11-21 10:29 | bot-oec | Note Added: 0030664 | |
| 2026-11-22 00:29 | bot-oec | Note Added: 0030751 | |
| 2026-11-22 09:37 | bot-oec | Note Added: 0030753 |