security-review-fixes #1

Merged
jorijnvdgraaf merged 5 commits from security-review-fixes into master 2026-11-26 10:09:20 +00:00

2026-11-26

deploy: the bank callback's controls are in the Caddy example
All checks were successful
Deploy / build-deploy (push) Successful in 7m4s
The README says a bank callback is not signed and that three controls
in Caddy "all have to stay in place": a source-IP allowlist, a path of
which only the non-secret prefix is in the web server's config, and
log_skip. Caddyfile.example had none of them. A host set up from it
proxied the callback to the backend from anyone and wrote the secret
path to the access log, which feeds the public analytics page.

The example now has the block: /hooks/bunq/* answers 403 to anything
outside the bank's range, is kept out of the log, and is proxied. It
adapts with Caddy 2.11.7 and sorts ahead of the catch-all. Both proxy
blocks carry a 64 KiB request_body cap, the same as the backend's.

catcrafts-analytics drops /hooks lines from the public tier as well, so
the public page does not rest on that one directive.

The README no longer says the backend binds loopback, because it does
not: ListenerHTTP1 takes a port and no address and binds every IPv4
interface, while the server logs 127.0.0.1 (Crafter.Network issue 11
asks for the address). The firewall on 8081 is what the per-peer limit,
the callback's allowlist and the request-size bound rest on, with the
two commands that check it.

It also documents what the earlier commits changed: the claims file
and what to do with a payment that named two orders, the rule that a
reference names one order, a payment being counted once whatever the
credits file holds, and the ledger being held in memory.

The server's copies of the Caddyfile and of the analytics script are
installed by hand and are not changed by a deploy.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
2026-11-26 11:05:35 +01:00
bank: a payment is spent once
A payer could place several orders and send one transfer naming all of
them. Each order looked for its own reference, found it, and saw the
full amount, so every order settled on one payment: three EUR 5 orders
on a single EUR 5 credit, reproduced. The rail logged "settle this one
by hand" and settled each of them. The README called it a known
limitation; it was a way to pay once for several orders.

The rail now records which payment settled which order, by the bank's
id, in <credits>.claims, before it reports the order paid, and a claimed
payment is left out of every other order's sum. The record cannot live
in the credits file, which the pull replaces every hour. It is read back
at start. If it cannot be written the rail settles nothing and says why;
if it is there and cannot be read the rail does not start. A claims
file that ends mid-line gets its line break before the next claim, or
that claim would be lost at the next start and the payment free again.

It does not rest on what a description looks like, because the payer
writes it. The "also quotes another reference" flag stays as a pointer
in the log and decides nothing: a name followed by a date has the same
shape. A payer who really sends one transfer for two orders gets the
first settled and the other by hand, with one line in the journal.

One payment is also counted once however many lines of the credits file
carry its id. Two lines are not meant to happen and were reproduced all
the same (one callback delivered on two connections at once passed the
id check twice), and summed they settle an order on half its price. The
callback's look-then-append is now under a lock as well.

ShouldMatchBankTransfers drives the matcher and the rail itself across
a restart, a torn claims file, an unreadable one and a duplicated line;
ShouldSettleBankTransfers shows over HTTP that one of three orders
settles.

Found in the security review of 2026-11-26; the torn claims file and
the duplicate line by the adversarial review of the first fix.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
2026-11-26 11:05:35 +01:00
orders: the fold is kept in memory, and a reference names one order
The ledger was folded from disk on every call: every order page, every
/financials request, every reconciler sweep, and every two seconds for
the mailer. Each event found its order by walking the orders so far, so
a fold was quadratic. A ledger of 40 000 orders took 11.9 s to fold,
anyone can add orders, and /financials is public and uncached.

Orders are now found through an index, which makes a fold one pass
(0.33 s for the same ledger), and the fold is held in memory. Every
call stats the file, and a different size or modification time means
fold it again, so an append by the CLI from another process and an edit
by hand are seen on the next call without a restart. What this process
appends itself is applied to the held fold by the same function that
folds a line from disk, provided the file grew by exactly what was
written; if it grew by more, another writer's line is in there and the
file is folded again. With 40 000 orders /financials takes about 1 ms
and an order page 0.3 ms. /financials sums from the held fold through
LedgerSales and no longer copies every order to do it.

The file stays the ledger and the held fold a reading of it. It costs 3
to 4 KB of memory an order, and it cannot see a change that leaves both
the size and the modification time as they were.

One thing the writer does differently. A file that ends mid-line, after
a crash during a write, now gets its line break before the next event.
The next event used to be written onto the end of the broken line and
was skipped with it, so a crash cost two events, not one.

The reference a payer quotes is the first six hex digits of the token:
24 bits, where two orders in a few thousand share one often enough to
plan for, and a transfer quoting it would pay for both. Checkout now
mints its token through NewUniqueOrderToken, which reads every
reference the ledger has ever held, lapsed and cancelled orders
included, and holds the reference while the checkout is in flight.

ShouldFoldTheOrderLedger compares the held fold with a fresh one after
appends of every kind, an outside append, a same-size edit, a replaced
file and a torn tail. It hands colliding draws to the mint, since the
real generator collides once in sixteen million. And it races one
outside line against a stream of this process's own appends, which
fails within a few rounds when the size check is removed.

Found in the security review of 2026-11-26.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
2026-11-26 11:05:35 +01:00
http: bodies end at 64 KiB, and the checkout limit counts per IPv6 /48
Two limits that were not limiting.

The listener's default body limit is 512 MiB and nothing set another,
with no cap on connections: one 64 MiB POST took the server from 7 MB
to 72 MB before the handler's own 16 KiB check answered 413. The limit
is now 64 KiB. A request that announces more in Content-Length is
answered 400 from its headers, with none of its body read: a 64 MiB
upload is refused after 786 KB and the server's peak memory does not
move.

That is a limit on the decoded body, and the comment says so. A chunked
request sent straight to the port can pad its chunk framing and still
make one connection hold hundreds of megabytes; the parser that allows
it is Crafter.Network's. Behind Caddy an oversized body is refused by
request_body before it gets here, so this is one more thing that rests
on port 8081 being private.

The per-peer checkout limit was kept per address. Over IPv6 one
subscriber holds a whole prefix, a /56 or a /48, and can submit from a
fresh address every time, so the per-peer cap did nothing and the
global cap was the only limit an IPv6 client met. The key is now the
IPv4 address or the IPv6 /48 (RateLimitKeyFor). An IPv4-mapped address
is keyed as the IPv4 client it is, and something that does not read as
an address is keyed as written, so a peer is never dropped to "no peer"
by its spelling. On a /56 ISP 256 neighbouring households share one
budget of six submissions per ten minutes; that was weighed and taken.

ShouldGuardRequestProvenance pins the key. ShouldServeRoutes drives it
over HTTP: six addresses of one /48 are accepted, a seventh is refused
with 429, the next /48 is not; and a body past 64 KiB is refused with
the server still up afterwards.

Found in the security review of 2026-11-26.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
2026-11-26 11:05:35 +01:00
checkout: an address field is one line, and the invoice prints it as text
The invoice is clearsigned, and its "Billed and shipped to" block is the
buyer's own words: name, street, postal code and city went into it as
typed. A name with a line break in it could write lines of its own into
a document the shop signs. One with no line break could still open a
heading ("# CREDIT NOTE") or, together with the street and the city, a
table, because each of those fields starts a line.

Checkout now refuses those four fields when they hold a line break or a
control character: the C0 and C1 controls, U+2028 and U+2029, and the
bidirectional embedding, override and isolate controls, which make a
line display in another order than it is stored. A tab is the
exception. A single-line input drops line breaks from a paste and keeps
tabs, so "Hoofdstraat<TAB>12" is a real buyer's address, and it is
stored with the space the tab stood for. Form::SingleLine is the one
definition of what counts.

The invoice builder applies the same function to whatever the record
holds, since a record can be older than the check or written by hand,
and prints the block indented four spaces. That is Markdown's literal
block: nothing in it is read as markup, and it reads the same as plain
text. The item in the amounts table is held to its cell too, because a
colour the catalogue no longer lists is printed as the ledger has it,
and checkout no longer stores a posted colour for a product that has no
variants.

ShouldBuildInvoices checks the property on the document (every line of
the block is blank or indented) rather than on particular strings, and
ShouldValidateForms pins the edges of each character range.

Found in the security review of 2026-11-26; the Markdown half by the
adversarial review of the first fix. No priced product is listed, so
none of it was reachable yet.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
2026-11-26 11:05:35 +01:00