Skip to content

Keep every cheque for a sale, so one that is not the customer's cannot hold it - #49

Open
todortodorovic wants to merge 1 commit into
mainfrom
fix/cheque-intake
Open

todortodorovic wants to merge 1 commit into
mainfrom
fix/cheque-intake

Conversation

@todortodorovic

Copy link
Copy Markdown
Collaborator

Why

Anyone who has seen the QR can post a cheque for it, and a cheque says nothing about who sent it. Both security audits found several ways a stranger's cheque could take over or freeze a sale:

  • Amount above 10000. normalizeAmount threw out of the statement handler. On TrUAPI that ended the subscription and started a re-subscribe loop (audit 2, Medium).
  • First matching cheque wins. Everything that arrived while its claim ran was dropped. A cheque the host refuses therefore cost the customer's own cheque its turn for good (both audits).
  • No way out of a stuck claim. A cheque whose coins never come keeps the host in detecting for up to an hour. There was no Cancel while claiming.
  • Logs. Junk statements logged a stranger's values verbatim, and each one wrote two lines. That allowed forged lines, and enough junk pushed the claim's own lines out of Report Issue.
  • Smaller issues:
    • NFC could advertise a deeplink the screen hid.
    • A connectivity probe that returned late could arm a QR for an old amount.
    • /terminal?amount= skipped the coin limit and the pre-flight.

What

Cheque intake (new lib/payments/coinage/cheque-intake.ts, used by use-coinage-payment.ts):

  • Statement checks never throw. An over-limit amount is an amount mismatch.
  • Every matching cheque is kept, by its coins, and claimed one at a time. A registration stays live at the host whatever the terminal does, so two that may both credit are never run for one sale.
    • The next cheque goes only when the host's last word on the current registration is "nothing credited".
    • claimCoins now reports which registration it follows (onFollow), so each cheque tracks its accepted registrations.
    • While one of them may still credit, other cheques wait and only Retry runs it again.
    • On SourceBusy, the claim follows the registration the host last accepted for those coins.
  • Limits:
    • A failed cheque re-posted runs again on its own at most twice.
    • A sale makes at most 12 registrations on its own; after that only Retry registers, and Retry stays offered.
    • The book holds 16 cheques. Failed ones make room; a cheque that may still credit is never evicted.
  • Logs. Skipped statements are logged a few per reason and counted after that. A stranger's values are quoted, stripped of control characters and cut short.
  • Smaller fixes:
    • Pending-claim writes run one after another.
    • The statement subscription is re-armed after a host drop even while a claim runs.
    • The claim host is set up before the QR goes out.

Leaving a sale with a live claim:

  • Once a claim has waited 40 s for its coins (longer than one 30 s host detection pass), the merchant may cancel.
  • A sale that ends before its claim settles (cancelled, or the screen left) hands its pending claim to the new claim-handoff.ts. That follows it in the background in this session, so a claim that lands is recorded now, not at the next launch.
  • resumePendingClaims follows all of a record's registrations at once, so one that never settles cannot hold up one that credited.

QR screen (app/(shell)/terminal/page.tsx):

  • QR and NFC. The QR never comes back once a cheque arrived. The QR, its data-qr-value and NFC follow one flag.
  • Error state:
    • Retry shows only when there is a cheque to claim.
    • "retry N" counts the claims of the cheque being claimed.
    • A failure is recorded in telemetry once per sale.
  • Stale probe. A pre-flight that returns after Cancel or a newer Charge no longer arms its QR.
  • /terminal?amount= goes through startPayment like the keypad, with the limit and the pre-flight:
    • If the pre-flight fails, the amount stays on the keypad, and Charge on it charges the exact URL amount.
    • A tip larger than the sale is dropped.
    • A different sale drops the URL's tip and items.

Not covered

These need sender authentication in the protocol:

  • A stranger's cheque whose coins never come still holds the sale until the merchant cancels (after 40 s).
  • A cheque with a tiny real coin settles the sale as partial, as on main.

Also left for the claim-lifecycle PR: Cancel after a non-final claim error still drops the sale. Its claim is now followed in the background, though.

Testing

  • Unit. New tests/cheque-intake.test.ts, plus additions to coinage-claim and pending-claims. 240 unit tests pass; tsc and lint are clean.
  • e2e. New e2e/payments/coins-cheque-intake.spec.ts, plus an over-limit case in coins-cheque-filter. All 140 e2e tests pass. They cover:
    • a refused cheque handing over to the next;
    • a stuck claim holding the sale, Cancel, and the claim recorded in the background when it lands;
    • a possibly-live failure blocking other cheques until Retry follows it;
    • a junk flood;
    • ?amount= over the limit and through the pre-flight.
  • Review. Three rounds of independent review, with two-skeptic verification of each finding, shaped the design. The first version moved to the next cheque after a stall, which could charge twice.
  • Still to do: a phone check on a nightly, together with Recognise TrUAPI hosts that inject only the frame transport #48.

…t hold it

Anyone who has seen the QR can post a cheque for it, and a cheque says
nothing about who sent it. The terminal took the first cheque that matched
and dropped everything that arrived while its claim ran, so a cheque whose
coins the host refuses cost the customer's cheque its turn for good. A
cheque whose coins never come kept the host waiting for up to an hour, with
no way for the merchant to leave the sale.

The statement handling moves to lib/payments/coinage/cheque-intake.ts:

- A cheque above the cheque limit is an amount mismatch. normalizeAmount
  used to throw there, out of the statement handler, which on TrUAPI ended
  the subscription.
- Every matching cheque is kept, by its coins, and they are claimed one at
  a time. A registration stays live at the host whatever the terminal does,
  so two that may both credit are never run for one sale. Each cheque
  tracks its registrations the host accepted (claimCoins now reports which
  one it follows) and clears one only on the host's last word about that
  registration. While any may still credit, the other cheques wait and only
  Retry runs it again. A full book never evicts such a cheque.
- On SourceBusy, claimCoins follows the registration the host last
  accepted for these coins.
- A failed cheque re-posted runs again on its own at most twice. One sale
  makes at most 12 registrations on its own; after that, only Retry
  registers, and Retry stays offered while cheques wait. When the book is full, failed cheques make room.
- Skipped statements are logged a few per reason, and the rest are counted
  across pages. A stranger's values are quoted, stripped of control
  characters and cut short, so junk can neither forge log lines nor push
  the claim's lines out of Report Issue.
- Pending-claim writes run one after another.
- The statement subscription is re-armed after a host drop even while a
  claim runs.

A claim that has waited 40 s for its coins (longer than one 30 s host
detection pass) lets the merchant cancel. A sale that ends before its claim
settles, cancelled or left, hands its pending claim to
lib/payments/coinage/claim-handoff.ts. That follows it in the background in
this session, so a claim that lands is recorded now, not at the next
launch.

Pending claims are resumed by following all of a record's registrations at
once, so one that waits for coins that never come cannot hold up one that
credited.

The QR screen:

- The QR never comes back once a cheque arrived. The QR, its test attribute
  and NFC follow one flag.
- The claim host is set up before the QR goes out.
- Retry shows only when there is a cheque to claim again.
- "retry N" counts the claims of the cheque being claimed.
- A failed claim is recorded in telemetry once per sale.

/terminal?amount= is charged through startPayment like the keypad, with
the connectivity pre-flight:

- An amount above the limit lands on the keypad with the limit warning.
- If the pre-flight fails, the amount goes on the keypad, and Charge on it
  charges the URL's exact amount, sub-cent tip included.
- A tip larger than the sale is dropped.
- A different sale drops the URL's tip and items.

A pre-flight that returns after Cancel or a newer Charge no longer arms its
QR.
@github-actions

Copy link
Copy Markdown

pr49-terminal.paseo · logs

https://pr49-terminal.paseo.li

This branch was successfully deployed

1 active deployment
preview — 64ba598b Deployed Nov 22, 2026 by todortodorovic via deploy #88
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant