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>
This commit is contained in:
Jorijn van der Graaf 2026-11-26 11:05:35 +01:00
commit 5de9107d31
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
3 changed files with 191 additions and 21 deletions

View file

@ -54,6 +54,32 @@ Crafter.Network's `ListenerHTTP1` rather than its HTTP/3 `ListenerHTTP` for one
concrete reason: **Caddy cannot `reverse_proxy` to an h3 upstream.** Port 8081
must never be exposed directly.
**The backend does not enforce that itself, and its startup line is wrong
about it.** `ListenerHTTP1` takes a port and no address, and binds every IPv4
interface (`0.0.0.0`), while the server logs `listening on 127.0.0.1`. Until
the listener can be given `127.0.0.1`
([Crafter.Network issue #11](https://forgejo.catcrafts.net/Catcrafts/Crafter.Network/issues/11)),
the host firewall is the only thing keeping the port private. Three things
rest on it:
- the per-peer checkout limit, which trusts the address Caddy puts in
`X-Forwarded-For`;
- the bank callback's source-IP allowlist, which lives in Caddy and is simply
skipped by anyone who can reach the backend directly;
- the bound on what one request can make the server hold. The backend's own
64 KiB limit is on the decoded body. A chunked request sent straight to the
port can pad its chunk framing and have one connection hold hundreds of
megabytes for 64 KiB of body (reproduced in review, 2026-11-26). Caddy's
`request_body` cap is what refuses an oversized request before it gets
there, and the parser itself is Crafter.Network's to fix.
So check it after any firewall change:
```sh
ss -ltn 'sport = :8081' # on the box: shows 0.0.0.0:8081
curl -m 5 http://<this host>:8081/api/healthz # from anywhere else: must time out
```
## One-time host setup
```sh
@ -393,8 +419,19 @@ callback, which the server receives itself, and an hourly pull on a trusted
machine, which asks nostrod and ships the result. The next section has both.
Every line carries the bank's own id for the payment. The callback skips an id
the file already holds, and the pull replaces the file with one line per
payment, so a payment that both deliver is counted once and cannot settle an
order nobody paid twice for.
payment, so a payment that both deliver is meant to be in the file once. The
rail does not depend on that: it counts an id once however many lines carry
it, because two lines can still happen (a callback landing while the pull is
replacing the file, an address registered twice at the bank), and summed they
would settle an order on half its price. When it sees a duplicate it says so
in the journal, once per order:
```
transfer: CC-… is quoted by a payment the credits file lists 1 time(s) too many. …
```
Seen regularly, that means the callback address is registered more than once:
`nostro callbacks --connector NAME` lists what is, and there should be one.
The server knows no bank. What a bank posts is read by `ReadCallback`, and how
a payment arrived is said by `PaymentMethodOf`, both of the module
@ -440,12 +477,19 @@ listing shows what is registered whoever set it. The registration in place on
**A callback is not signed.** bunq's are not: verified against doc.bunq.com,
there is no HMAC and no server signature, and certificate pinning
authenticates us to bunq rather than bunq to us. So the endpoint's
authentication is the controls around it, all of which have to stay in place:
authentication is the controls around it, all of which have to stay in place.
The first three are one block of `deploy/Caddyfile.example`, "the bank's
callback": merge it with the rest of that file. A Caddyfile without it still
proxies the callback to the backend, from anyone, and logs the secret path.
1. **Source-IP allowlist** for `185.40.108.0/22` in the Caddyfile. This is the
strong one: forging requires completing a TLS handshake from inside that
range. bunq warns the range may change; if it does, callbacks 403 and
settlement stops — it fails closed.
settlement stops — it fails closed. It is only as strong as Caddy being
the one way in: a request that reaches port 8081 directly never meets this
rule, and the backend's listener does not bind loopback on its own (see
"Why the backend speaks plaintext HTTP/1.1"), so the firewall on that port
is part of this control.
2. **A secret path.** Caddy matches only the non-secret prefix `/hooks/bunq/*`,
so the secret lives solely in `BANK_CALLBACK_PATH` in `payments.env` and not
in the web server config. The backend compares it in constant time, and a
@ -453,7 +497,13 @@ authentication is the controls around it, all of which have to stay in place:
405 — a different answer there would confirm a guessed path.
3. **`log_skip` on that prefix.** Without it the secret would be written to the
access log and then ingested into the GoAccess database and shown in its
top-URLs panel, i.e. leaked to anyone who opens `/analytics/`.
top-URLs panel, i.e. leaked to anyone who opens `/analytics/`. The
analytics script drops `/hooks` lines from the public tier as well
(`CENSOR_RE`), so the public page does not rest on this one directive; the
raw log and the private tier do, which is why it stays. If it was ever
missing while callbacks arrived, the path is in the retained logs: set a
new `BANK_CALLBACK_PATH`, register it with `nostro callbacks`, and rebuild
the public analytics DB (the procedure is at the top of the script).
4. **The account.** A bank posts to whatever address any of its customers
registered. Someone with an account at bunq could register this address for
an account of their own and pay themselves, and the callback that follows
@ -576,8 +626,9 @@ The file source stays regardless — it is how the suites drive real settlement
with no network, and it is the manual escape hatch for settling a transfer by
hand. `--mark-paid` remains the other one.
Two matching properties worth knowing, both pinned in
`ShouldMatchBankTransfers`:
Four matching properties worth knowing. The first three are pinned in
`ShouldMatchBankTransfers` (the third over HTTP as well, in
`ShouldSettleBankTransfers`), the fourth in `ShouldFoldTheOrderLedger`:
* **The reference matches in whatever form the payer typed it.** Both sides
are reduced to upper-case alphanumerics and the match is on the reference
@ -591,11 +642,50 @@ Two matching properties worth knowing, both pinned in
Outgoing amounts are ignored, so a refund quoting the reference cannot pay
for the order it refunded.
One known limitation, logged rather than solved: a single transfer quoting
**two** order references cannot be attributed by a per-order matcher, and both
orders would settle on the same money. The rail detects the shape and logs
"settle this one by hand"; at this volume that is proportionate, but do not
assume the matcher is total.
* **A payment is spent once.** A transfer that settled one order pays for no
other, however many references its description quotes. Before the rail
reports an order paid it writes which payment settled it, by the bank's id,
to `<credits>.claims` (default
`/var/lib/catcrafts/orders.jsonl.transfer-credits.jsonl.claims`), and every
later match leaves a claimed payment out. It cannot live in the credits
file, which the pull replaces every hour. This used to be a "known
limitation": the rail only logged a transfer that quoted two orders and
settled both. It was a way to pay once for several orders (place three,
send one transfer naming all three; found in review on 2026-11-26), and
what a description looks like is no defence, because the payer writes it.
* **A reference names one order.** The reference is 24 bits of the order
token, short on purpose because a person types it, and two orders sharing
one would both be paid by a transfer quoting it. Checkout therefore mints
its token against every reference the ledger has ever held, lapsed and
cancelled orders included: their payments are still in the bank's history.
What the first of those means in practice. A payer who really does send ONE
transfer for two orders gets the first of them settled; the other keeps
waiting, and the journal says so once per order and per run of the server:
```
transfer: CC-… is quoted by 1 payment(s) whose money already settled another order, …
```
Settle that one with `--mark-paid` after looking at the amount. The amount is
not split across orders automatically; at this volume a human finishing the
rare case is proportionate. A settlement on a payment that *looks* as if it
names a second order is logged too, as a pointer, and decides nothing: a name
followed by a date has the same shape as a reference.
The claims file is small and append-only, like the ledger. Keep it with the
ledger backup. Without it nothing is lost that the bank does not also have,
but a payment that named several orders could then settle a second one of
them. If it cannot be written (a full disk, wrong ownership after a restore)
the rail settles nothing and says why, rather than settle on money it cannot
mark as spent. If it is there and cannot be READ at startup, the bank rail
does not start at all, for the same reason: the journal names the file, and
checkout offers only the other rail until it is fixed and the server
restarted.
How far "written before the order is reported paid" goes: it holds across a
crash or a restart of the server at any moment. Neither this file nor the
ledger is fsynced, so a power cut can keep one line and lose the other.
### The EURC rail
@ -991,6 +1081,23 @@ Each appends a status event to the log — nothing is ever rewritten, so the
file remains its own audit trail. Deleting personal data on request is an edit
of the fields the seven-year fiscal retention does not cover.
Neither needs a restart. The running server holds the ledger folded in memory
(an order page or `/financials` is a lookup, not a read of the whole history)
and folds the file again whenever its size or modification time has changed,
which a CLI transition and an edit in any editor both do. The first request
after one takes the length of a fold: between a third and two thirds of a
second for 40 000 orders, in two measurements. The file is the ledger and the
server's copy is a reading of it; memory for that copy is 3 to 4 KB an order.
Two changes it cannot see, both narrow. One leaves BOTH the size and the
modification time as they were: a same-length edit by a tool that then puts
the old time back (`touch -r`, a sync tool patching in place). The other is a
same-length edit that lands in the few microseconds in which the server is
itself appending a line, because the server takes the file's new time as its
own doing. A person with an editor will not hit that one; a script rewriting
fields in a loop could. Nothing here does either, and a restart reads the
file afresh.
## The open financials page
`/financials` publishes running totals only: sales, donations, and expenses
@ -1034,11 +1141,13 @@ reports, each with its own persistent DB and ingest ledger:
anonymized at ingest (last octet zeroed before anything reaches its DB),
no HOSTS or full-URL REFERRERS panels, and log lines matching `CENSOR_RE`
in the script never enter its DB at all — the public tier cannot leak
what it never ingested. `CENSOR_RE` covers `/api` **and `/order`**: an
order token is the entire capability to read that buyer's status page and
their invoice (name, street, postal code, city), so publishing the path
publishes the buyer. Keep secrets out of URL *paths* regardless — query
strings are already stripped, paths are not.
what it never ingested. `CENSOR_RE` covers `/api`, **`/order`** and
`/hooks`: an order token is the entire capability to read that buyer's
status page and their invoice (name, street, postal code, city), so
publishing the path publishes the buyer, and `/hooks/bunq/<secret>` is the
bank callback's shared secret (Caddy's `log_skip` already keeps it out of
the log; this is the second layer). Keep secrets out of URL *paths*
regardless — query strings are already stripped, paths are not.
- `https://catcrafts.net/analytics/private/` — **uncensored** (basic auth,
hash in the Caddyfile): full IPs, all panels.