Validate Electrum headers and corroborate the draw's block (B-28)

A single hostile Electrum server, or a MITM on the one active
connection, could fabricate the block header the draw's entropy comes
from and so pick the winner of every round: headers were accepted with
no proof-of-work check and no link to the previous tip.

app/rounds/draw.py adds header_meets_its_own_target (rejects a header
whose hash doesn't satisfy the difficulty target it claims) and
header_prev_hash. electrum/listener.py's _apply_header now rejects a
header failing either check by raising HeaderValidationError, which
ends the session the same way a dropped connection would so the
listener rotates to the next configured server.

ElectrumListener gains corroborate_header: before the draw uses a
block, it's independently checked against the other configured servers
and needs a majority to agree. rounds/scheduler.py's
_wait_for_next_block now calls this and, on failure, logs why and
waits for a further block instead of ever using an uncorroborated
header.

Certificate/hostname verification stays disabled, so this doesn't
cover an attacker able to MITM every configured server at once -
BUGS.md notes that as not covered.

Suite grows from 151 to 165 tests. BUGS.md moves B-28 to Previously
fixed.
This commit is contained in:
davide committed 2026-07-27 10:07:21 +02:00
1 parent 7224ca0e66
commit 0ce0562fd7
7 files changed
+463 -63

No files matched your search

+12 -40
View File
@@ -1,10 +1,11 @@
# Known bugs
A second full-codebase audit on 2026-07-27 found **25 further issues** (4 critical, 6 high,
7 medium, 8 low), listed below as B-25 … B-49. B-25, B-26 and B-27 are fixed (see "Previously
fixed" below); the other 22 are open. The 139-test suite was green at the time of the audit, so
none of these were caught by existing coverage — every fix lands with a regression test (the
three fixes so far brought the suite from 139 to 151).
7 medium, 8 low), listed below as B-29 … B-49. B-25 through B-28 are fixed (see "Previously
fixed" below) — no Critical-severity finding remains open; the other 21 are High/Medium/Low.
The 139-test suite was green at the time of the audit, so none of these were caught by existing
coverage — every fix lands with a regression test (the four fixes so far brought the suite from
139 to 165).
The recurring pattern across the open findings is worth stating once: the code is rigorous
about the failure modes that have actually been hit, and silent about the ones that have not.
@@ -18,31 +19,6 @@ single-process assumptions, no user-facing history, etc.), see "Known gaps / TOD
---
## Critical
### B-28 — A hostile Electrum server (or a MITM) can choose the winner
`electrum/listener.py:167-186` accepts any header whose `height >= tip_height`: no
proof-of-work check, no linkage to the previous block hash. That header is the **sole source
of entropy for the draw** (`scheduler.py:129`).
In parallel, `electrum/client.py:104-106` sets `check_hostname = False` and
`verify_mode = CERT_NONE`. The comment justifies this with "the protocol's trust model is
server consensus" — but there is no consensus here: one server at a time, rotated over a list
of arbitrary third parties. So a hostile server, or anyone able to MITM a connection that
validates no certificate, can fabricate a header and thereby decide who wins every round.
**Proposed fix, in order of value.** (1) Validate headers before accepting them: check the
PoW against the claimed target and that `prev_block` matches the current tip; reject anything
that fails. (2) Do not trust one server for the draw — fetch the header for
`draw_block_height` from *several* endpoints in the rotation and require agreement before
using it as the seed. (3) Pin certificates (or verify hostnames) for the configured servers
rather than disabling verification wholesale. Longer term this is the argument for replacing
the v1 draw algorithm — CLAUDE.md already calls it a replaceable component — with a scheme
that does not depend on a single unauthenticated data source.
---
## High
### B-29 — `detect_external_spends` is irreversible and trusts a single response
@@ -321,17 +297,13 @@ already does.
## Previously fixed
B-25 (the payout had no two-phase write, unlike bets and withdrawals — a crash or rejected
broadcast could leave money on-chain with no DB record, or leave the round wedged with no way
to retry safely), B-26 (a transient failure or a process restart at payout time wedged the
round in `paying_out` forever, with most failure paths logging nothing) and B-27 (every RBF
bump reset the reconciler's own abandon clock, so a repeatedly-bumped-but-never-mined
transaction was never abandoned) are fixed as of 2026-07-27. Together B-25 and B-26 make
`paying_out` fully recoverable — a payout retry is now safe (persisted before broadcast,
guarded against double-spend) and automatic (the scheduler retries a stuck round on its own,
throttled, with every failure audit-logged with a reason, including across a process restart).
See git history (commits `f13f685`, `50a43ae`, `933760e`) for the fix-by-fix breakdown; the
regression suite grew from 139 to 151 tests over the three.
- **B-25** — the payout had no two-phase write, unlike bets and withdrawals
- **B-26** — a payout failure or a process restart could wedge a round in `paying_out` forever
- **B-27** — an RBF bump reset the reconciler's own abandon clock, so a repeatedly-bumped tx was never abandoned
- **B-28** — a hostile Electrum server (or a MITM) could single-handedly pick the round's winner
See git history for the fix-by-fix breakdown (commits `f13f685`, `50a43ae`, `933760e`, and the
B-28 fix). Suite grew from 139 to 165 tests over the four.
A full-codebase audit on 2026-07-26 (commit `d4e0974`) found 24 bugs across every Python
module under `app/`, both static frontends, and the Docker/Caddy deployment — 5 critical,