Skip to content

Security: LSUDOKO/Bunch

Security

SECURITY.md

Security

Status: internal testing and internal code review only. Bunch has not had an independent third-party audit. Use small amounts. The contracts are per-bunch and immutable: a bunch always settles with the code that created it.

Reporting a vulnerability

Please report privately through a GitHub security advisory: https://github.com/LSUDOKO/Bunch/security/advisories/new

Include the bunch link (or its parameters), the transaction you built, and what you expected. Do not open a public issue for a bug that can move money. If the advisory form is not available, open an issue that says only "security contact needed" and we will reach out.

Security architecture

  • Two covenants hold every satoshi. BunchState holds the minting NFT (the bunch's state thread) and the shop's bonus pool. One Seat UTXO per buyer holds that buyer's payment and an immutable seat NFT. Source: contracts/BunchState.cash, contracts/Seat.cash (CashScript 0.14.0, pinned).
  • No backend holds keys or money. The web app is static files. One stateless Cloudflare Pages Function (functions/bcmr/[cat].ts) serves BCMR metadata; it has no keys and no storage.
  • The bunch spec lives in the URL fragment. The app rebuilds both contract addresses from it and reads state from Electrum. Display metadata in the link is untrusted and validated (src/core/spec.ts).
  • Wallets sign; the app broadcasts. Sign requests use broadcast: false; the app broadcasts over its own Electrum connection on the network it is set to (src/core/wallet.ts, src/core/network.ts).
  • Keyless settlement. complete, refund, escape and reclaim need no signature. Any open page may broadcast them. Duplicate broadcasts fail harmlessly.
  • Authority by NFT spend. WizardConnect has no message signing. leave requires spending the buyer's receipt NFT; merchantAccept requires spending the shop key NFT.

Assets

Asset Where it lives Who can move it
Buyer payment (price per seat) Seat UTXO complete (to shop), refund / leave / escape (to the buyer's stored address), merchantAccept (to shop)
Bonus pool BunchState UTXO value refund (bonus to early seats, rest to shop), complete / merchantAccept / reclaim (to shop)
State thread (minting NFT) BunchState UTXO Only BunchState functions; burns on settlement
Receipt NFT (52‖seatNo) Buyer's wallet Buyer; spending it authorises leave
Shop key NFT (4d) Shop's wallet Shop; spending it authorises merchantAccept
Platform fee Fee address output on complete / merchantAccept Fixed by the contract

Trust assumptions

  • BCH consensus and the CashScript compiler (cashc 0.14.0) behave as documented.
  • Electrum servers report UTXOs honestly. A lying server can show wrong state or hide a bunch, but it cannot move money; the contracts decide that. The app refuses to offer Join when the chain does not match the link.
  • The wallet signs only the inputs listed in inputPaths and leaves contract inputs' unlocking bytecode as built.
  • The shop delivers the goods. The covenant guarantees payment rules, not delivery (see Remaining risks).
  • The person who shares a link chose its parameters. The app validates them, but a buyer should read the price, group size and deadline before joining.

Attack surfaces

  1. Contract functions: join, complete, refund, leave, merchantAccept, reclaim, and Seat.withState, Seat.escape.
  2. Crafted bunch links: arbitrary parameters and metadata in the URL fragment.
  3. Forged tokens: NFTs from another category parked at the seat or state address.
  4. Keyless broadcasts: anyone can build complete, refund, escape, reclaim and choose the miner fee.
  5. Wallet signing over WizardConnect (relay transport, sign request contents).
  6. Chain reads: dust sent to contract addresses, indexer gaps, network mismatch.

Threat model

From docs/03_CONTRACTS.md; ✅ means a VM test in test/lifecycle.test.mjs proves the attack fails inside the contract.

Threat Mitigation Tested
Seat theft: drag a victim's seat into your own join or leave Join/leave loops reject extra seat inputs and bunch tokens ✅
Early completion below target count >= target and inputs.length == count + 1 ✅
Refund before the deadline tx.time >= deadline ✅
Refund redirected to an attacker Output i must equal seat i's stored locking bytecode ✅
Shorting an early seat's bonus Output must equal value + bonusEach for seatNo < earlyK ✅
Join underpays or skips the counter outputs[1].value == price; exact next state commitment ✅
Leaving with someone else's seat Receipt seatNo must match the seat ✅
Escape or reclaim too early tx.time >= graceEnd ✅
Fake state UTXO from another category Every function checks tokenCategory == bunchCategory + 0x02 ✅
Forged receipt (52‖victimSeatNo in another category) used to kick a buyer out leave requires the receipt's category to equal bunchCategory ✅ (found by mutation testing)
Fake seat (own-category NFT at the seat address) to claim a bonus or desync the counter Every seat loop checks tokenCategory == bunchCategory ✅ (found by mutation testing)
Forged shop key to force an early accept Key category must equal bunchCategory ✅ (found by mutation testing)
Refund that leaves a seat out but pays its output slot to an attacker inputs.length == count + 1 ✅ (found by mutation testing)
Broadcaster shifts money between listed outputs Exact fee and refund outputs (Finding F1) ✅
Shop accepts after the deadline to skip the bonus Early rebates on merchantAccept (Decision D1) ✅
Fee griefing on keyless paths Miner fee capped by maxFee; boundary tests at exactly maxFee and one sat more ✅
Token smuggling or NFT leakage tokenCategory == 0x on every payout and change output; fixed output counts ✅
Two state inputs sharing outputs activeInputIndex == 0; only one minting NFT exists Covered by other rules (see MUTATION_REPORT.md)
Late joins after the deadline Cannot be blocked by the contract (CashScript proves only "after"). The UI hides Join; late seats refund in the same batch; if late joins fill the bunch it completes UI only
Bonus farming (sybil early seats in a doomed bunch) Bonus is early seats only, seat numbers never reused; merchantAccept turns farmers into paying customers Design
Shop self-fills to dodge the bonus Shop pays itself minus the fee and still owes goods to real buyers; the UI labels seats bought by the shop's address UI only
Shop does not deliver Out of the covenant's scope Not mitigated (roadmap: redeem-to-release)
App disappears escape and reclaim after graceEnd ✅

Methodology

Method What it does Command Result
VM attack tests 20 valid flows and 111 single-change attacks on the real BCH VM (MockNetworkProvider). Each attack must fail inside a contract; a rejection for dust, fee rate or locktime finality is flagged BAD npm run test:vm 131 pass
Mutation testing Each require becomes require(true); comparisons, counters and branches are flipped. That function's tests must fail npm run mutate 82/95 rule and 33/35 operator mutants killed; 0 unexplained survivors (MUTATION_REPORT.md, scripts/mutation-notes.json)
Metamorphic tests 8 relations, e.g. join-order invariance, refund conservation, target and deadline boundaries, no second settlement npm run test:unit Pass (test/metamorphic.test.ts)
Property tests fast-check random lifecycles with invariants checked after every step npm run test:unit Pass (test/property.test.ts; 12 runs by default, PROPERTY_RUNS to raise)
Builder tests The app's own transaction builders on the real VM, up to 50-seat settlements npm run test:unit Pass (test/builders.test.ts)
On-chain runs join, complete, leave, refund and merchantAccept on testnet3 (mainnet consensus rules); join and keyless complete on mainnet. escape and reclaim are VM- and builder-tested only npm run rehearse:chipnet (REHEARSE_NET=testnet3) docs/EVIDENCE.md
Code review An independent review pass over the app code. It was not a third-party audit Fixes R3, R4, R6, R7 below (44aea07)

The 13 surviving rule mutants and 2 operator mutants are rules that another rule already enforces (defence in depth), for example activeInputIndex == 0 when input 0 must already hold the only minting NFT. Each reason is in the last column of MUTATION_REPORT.md.

Findings

Severity is our own assessment; the source docs do not assign severities.

ID Finding Severity Status Commit
F1 Payout checks used >= with maxFee slack, so the broadcaster of a keyless tx could move up to maxFee sats between listed outputs (shop leftover to a buyer in refund; shop to fee output in complete / merchantAccept). Nobody outside the listed outputs could receive anything Low Fixed: fee output == max(fee, 1000), each refund output == owed; 1-sat overpay and maxFee boundary tests 4cd1e4f
D1 merchantAccept returned the whole pool to the shop. Since the contract cannot enforce "before the deadline", a shop could accept after the deadline and early joiners would lose their promised bonus Medium Fixed: accept pays bonusEach to every early seat and requires count < target 4cd1e4f
M1 Mutation testing exposed 8 missing negative tests: forged receipts from another category, fake seats claiming a bonus, forged shop keys, a refund that orphans a seat. The contracts already blocked them Informational Fixed: tests added see MUTATION_REPORT.md history
A1 Untrusted genesis. The covenants trust every token of the bunch's category. A shop that built its bunch outside this app could keep a second minting NFT and later move any seat through Seat.withState, or pre-mint fake seats and receipts High (protocol trust assumption; not reachable for bunches created by this app) Fixed client-side without changing the contracts: the page finds the creation transaction and offers Join only if it made exactly one minting NFT (at the state address, zero seats), at most one shop key and no other tokens of the category. src/core/genesis.ts, test/genesis.test.ts (real mainnet genesis + 4 forged ones). Wallets that join without this page do not get the check
R1 Crafted links: a genesis built outside the app could start at count = target − 1 with no seats and trap the next buyer's money High (client) Fixed: Join is offered only when the chain has one live seat per counted seat and the pool is at least poolFor(spec) (src/ui/pages/BunchPage.tsx). This is an app check, not a contract rule 5202679
R2 A crafted link could set a very long grace period, keeping money in a broken bunch for a long time Medium (client) Fixed: validateSpec caps the grace period at 30 days (4,320 blocks for height deadlines) 5202679
R3 Pickup check accepted vouchers it should not: refunded or open bunches and foreign-category receipts Medium Fixed: a voucher is valid only for a completed bunch of this shop; vout validated 44aea07
R4 Settlement classification could show "It filled!" for something that was not a settlement (for example a join seen during an indexer blip) Medium Fixed: unknown transactions return unknown; the page keeps its last good state 44aea07
R5 A dust send to the state address could hide the settlement and break voucher checks Low Fixed: the outcome is found by walking the address history newest-first to the first real settlement 5202679
R6 Untrusted link metadata could crash the page Low Fixed: solo price, https photo links and text fields are validated 44aea07
R7 Wallet waits could hang forever (cancel, disconnect, pairing that never starts) Low Fixed: waiters always settle, 5-minute timeout 44aea07
R8 A copy edit to the BCMR registry could silently change what mainnet genesis transactions commit to Low Fixed: hash snapshot test for a fixed category 5202679
R9 After a rejected settlement broadcast, the settler retried every few seconds Low Fixed: wait for the next block 0b72f2d

Remaining risks

  • CashScript can only prove "after". The contract cannot block a join after the deadline. The UI hides Join, late seats are refunded in the same batch, and late joins that fill the bunch complete it, which only helps buyers.
  • One state thread. Each bunch has one state UTXO, so only one join can spend it at a time. Concurrent joiners conflict; the UI rebuilds and asks again. Lanes (roadmap) would remove this.
  • Median-time-past lag. Time deadlines use MTP, about an hour behind the wall clock, so refunds open about an hour after a time deadline. Demo bunches use block-height deadlines.
  • maxFee on keyless paths. Whoever broadcasts complete, refund, escape or reclaim chooses the miner fee, up to maxFee. That much can go to miners instead of the shop (or, on escape, the buyer). It cannot go to anyone else.
  • Platform fee floor. The contract rounds the fee up to 1,000 sats, even when feeBps is 0. Small bunches pay a larger share; the create form validates a minimum price.
  • Shop non-delivery is outside the covenant. Buyers trust the shop to hand over the goods after completion.
  • Client-side guards (R1, R2) protect users of this app. A different client could still build a join into a malformed bunch.
  • Wallet behaviour. Only Selene has signed on mainnet, and only a join. A wallet that alters contract inputs' unlocking bytecode would fail to sign; it cannot steal funds, because the contracts check every output.
  • Dependencies. @wizardconnect/* packages are young (0.2.x). All versions are pinned (docs/VERSIONS.md).

There aren't any published security advisories