User Tools

Site Tools


wiki:manara:bugs:153

#153 — No idempotency / duplicate-submission protection on contact-transaction creation — two overlapping submissions create two full, (Resolved)

← Back to Manara Bugs · View issue #153 in MantisBT


Reported by: Khaled Tarabay on 2026-07-30T14:30:32+01:00
Assigned to: Claude
Resolved by: Claude on 2026-08-01T07:26:34+01:00
Severity: major
Priority: high
Reproducibility: always
Category: General

Description: Firing two identical, concurrent POST /contacts/<id>/transactions requests (same CSRF token, same body — simulating any client capable of sending two overlapping submissions, e.g. a duplicate browser tab, a flaky-network client-side retry, or a resubmitted request) creates two independent, fully real Purchase Bill invoices instead of being deduplicated, doubling the contact's actual owed balance. On contact 73 (DH DoubleSubmit 765229, Supplier+Client), captured the real transaction-creation POST body via the network layer while recording an unrelated purchase, then built a fresh, uniquely-described $250.00 Purchase payload and fired it as two truly concurrent raw-POST requests (Promise.all, identical _csrf and body) against /contacts/73/transactions. Both returned 302 (success). A fresh reload of /billing confirms two separate, fully real invoices were created — LG-INV-195 (id 284) and LG-INV-196 (id 285), each independently Issued, each $250.00, each with its own line item and its own number — not a rejected duplicate, not a merged single row. Re-loading the contact page fresh confirms the real financial consequence: YOU OWE reads $500.00, not $250.00, for what was a single logical purchase action.

Went further to characterize the actual real-world reachability of this before rating it, since the task brief specifically warns against trusting a raw-POST result without checking whether an equivalent real-UI path is even exposed: tried reproducing the same duplication via genuine browser double-clicks on the real “Record” button, first with two independent async Playwright .click() calls in flight together, then with two fully synchronous native HTMLElement.click() calls dispatched in the same JS tick via page.evaluate. Neither reproduced a duplicate — only one POST /transactions request was ever actually sent to the server in both cases, and only one ledger row resulted. This matches ordinary browser form-submission semantics: the first click's navigation begins before a second, same-tab click can dispatch a second submit. So an accidental, ordinary same-tab double-click is incidentally safe today, but not because of any guard the application provides — the server itself imposes no idempotency check at all, as the raw concurrent-request test proves conclusively. Any client capable of producing two genuinely overlapping requests before the first completes (two tabs open to the same contact, a mobile client that retries on a slow response before the first request lands, a proxy/browser extension replaying a request, or simply two staff members racing to record the same paperwork at once) will silently double-invoice the contact with no warning, no error, and no dedup — each half of the duplicate is indistinguishable from a legitimate second purchase once created. The fix is a standard idempotency safeguard on this endpoint: either a single-use submission token embedded in the form (invalidated after first use, causing a repeat submission to no-op or show a friendly “already recorded” message) or a short server-side lock/uniqueness check keyed on contact + description + amount + timestamp window, matching the kind of protection this engagement has already seen correctly applied elsewhere (e.g. the payment-race test in tests/hunt/race_pay.ts found ordinary invoice payments are correctly serialized against concurrent overpayment — this contact-transaction endpoint needs the equivalent).

Steps to Reproduce: On any Supplier/Client contact, capture the real POST /contacts/<id>/transactions request body (kind=purchase, a content line, qty, price, a fresh CSRF token). Fire two copies of that exact same body concurrently (e.g. Promise.all of two page.request.post calls using the same session/CSRF). Both return 302. Reload /billing (or the contact's ledger) — two separate invoices exist, each with the full amount, each independently numbered. Reload the contact page — YOU OWE (or THEY OWE YOU) reflects double the real transaction amount.

Resolution: Fixed. Two overlapping POST /contacts/{id}/transactions (a resubmit, a client retry on a slow response, or a duplicate tab) each created an independent real invoice + ledger row, doubling the owed balance — the server imposed no idempotency guard (an ordinary same-tab double-click is only incidentally safe via browser nav semantics).

FIX: the add-transaction form embeds a per-render single-use submission_token (random_bytes); ContactController::addTransaction() consumes it via consumeSubmissionToken() (session-backed set, bounded to the most recent 100) — a repeat of the same token no-ops with a friendly “already recorded” message. PHP's per-session file lock serialises two concurrent same-session requests, so the check-then-mark is atomic across them (verified with an active-session harness: same token twice → true then false). An empty/absent token is allowed (nothing to dedupe).

Scope: this covers the realistic technical duplicates (retry / duplicate tab / resubmit within one session). A genuine two-DIFFERENT-staff race is a data-entry judgment call, not a false-positive-free technical dedup target, so it is deliberately not force-blocked.

Files: app/controllers/ContactController.php, app/views/contacts/show.php. No schema change.

Activity / Notes

  • 2026-08-01T07:26:30+01:00 - Claude: Fixed. Two overlapping POST /contacts/{id}/transactions (a resubmit, a client retry on a slow response, or a duplicate tab) each created an independent real invoice + ledger row, doubling the owed balance — the server imposed no idempotency guard (an ordinary same-tab double-click is only incidentally safe via browser nav semantics).

FIX: the add-transaction form embeds a per-render single-use submission_token (random_bytes); ContactController::addTransaction() consumes it via consumeSubmissionToken() (session-backed set, bounded to the most recent 100) — a repeat of the same token no-ops with a friendly “already recorded” message. PHP's per-session file lock serialises two concurrent same-session requests, so the check-then-mark is atomic across them (verified with an active-session harness: same token twice → true then false). An empty/absent token is allowed (nothing to dedupe).

Scope: this covers the realistic technical duplicates (retry / duplicate tab / resubmit within one session). A genuine two-DIFFERENT-staff race is a data-entry judgment call, not a false-positive-free technical dedup target, so it is deliberately not force-blocked.

Files: app/controllers/ContactController.php, app/views/contacts/show.php. No schema change.

Full History

Date User Field Change
2026-07-30T14:30:32+01:00 Khaled Tarabay issue-new New Issue
2026-08-01T07:26:24+01:00 Claude Assigned To ⇒ Claudine
2026-08-01T07:26:28+01:00 Claude tag-added Tag Attached: done-by-claude
2026-08-01T07:26:30+01:00 Claude note-added Note Added: 0000111
2026-08-01T07:26:34+01:00 Claude Status new ⇒ resolved

Synced automatically from MantisBT issue #153.

wiki/manara/bugs/153.txt · Last modified: by mantisbot