security-review-fixes #1

Merged
jorijnvdgraaf merged 5 commits from security-review-fixes into master 2026-11-26 10:09:20 +00:00
3 changed files with 191 additions and 21 deletions
Showing only changes of commit 5de9107d31 - Show all commits

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>
Jorijn van der Graaf 2026-11-26 11:05:35 +01:00
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB

View file

@ -169,6 +169,45 @@ catcrafts.net {
file_server file_server
} }
# ── the bank's callback ──────────────────────────────────────────────
#
# The bank posts here when money lands (deploy/README.md, "How settlement
# actually reaches the shop"). A callback is NOT signed, so what makes one
# believable is the controls around the endpoint, and three of the four
# are this block. None of it is optional once BANK_CALLBACK_PATH is set:
#
# remote_ip only the range the bank publishes for its callbacks reaches
# the backend. Anyone else is answered 403 here, whatever
# follows the prefix, so the answer says nothing about
# whether a guessed path was the right one. When the bank
# moves its range, callbacks 403 and settlement falls back to
# the hourly pull: it fails closed.
# the path only the NON-secret prefix is written here. The secret
# segment is BANK_CALLBACK_PATH in payments.env and nowhere
# else, which is what lets this file be public.
# log_skip the request line carries the secret, and the access log
# below feeds the PUBLIC analytics report. Without this line
# the path is published within the hour. (The analytics
# script drops /hooks lines from the public tier as well, so
# the report does not rest on this directive alone.)
#
# remote_ip is the address of the connection itself, which is the right
# thing to match while this Caddy faces the internet directly. Behind
# another proxy every connection is that proxy's: match client_ip there,
# with trusted_proxies set, or this allowlist admits everyone or no one.
#
# All of it assumes this block is the only way to the backend. See the
# note on port 8081 above the catch-all below.
log_skip /hooks/bunq/*
handle /hooks/bunq/* {
@not_the_bank not remote_ip 185.40.108.0/22
respond @not_the_bank 403
request_body {
max_size 64KiB
}
reverse_proxy 127.0.0.1:8081
}
# ── everything else: server-rendered ───────────────────────────────── # ── everything else: server-rendered ─────────────────────────────────
# #
# Pages, /feed.xml, /sitemap.xml and later /api/*. The backend sets its own # Pages, /feed.xml, /sitemap.xml and later /api/*. The backend sets its own
@ -178,15 +217,29 @@ catcrafts.net {
# possible: reverse_proxy APPENDS the real client address to # possible: reverse_proxy APPENDS the real client address to
# X-Forwarded-For, and the backend reads the rightmost entry (see # X-Forwarded-For, and the backend reads the rightmost entry (see
# ClientAddressFromForwarded — the leftmost is whatever the client claimed). # ClientAddressFromForwarded — the leftmost is whatever the client claimed).
# That is only sound while nothing but Caddy can reach 8081, which is why # That is only sound while nothing but Caddy can reach 8081, and the same
# the backend binds loopback and why the warning at the top of this file # goes for the bank callback's allowlist above. THE BACKEND DOES NOT
# says not to expose the port. # ENFORCE THAT ITSELF: Crafter.Network's ListenerHTTP1 takes a port and no
# address and binds every IPv4 interface, whatever the server's startup
# line says (Crafter.Network issue #11 asks for the address). Until it can
# be given 127.0.0.1 the host firewall is what keeps the port private, so
# check it: `ss -ltn 'sport = :8081'` on the box shows 0.0.0.0, and
# `curl -m 5 http://<this host>:8081/api/healthz` from anywhere else must
# time out.
#
# request_body is the same 64 KiB the backend sets on its own listener
# (kMaxRequestBodyBytes): nothing proxied here is larger than a checkout
# form or a bank's callback, and refusing more at the edge means an
# oversized upload never crosses to the backend at all.
# #
# Caddy's own rate_limit directive is a third-party module and is NOT in a # Caddy's own rate_limit directive is a third-party module and is NOT in a
# standard build — adding it here without rebuilding Caddy stops the server # standard build — adding it here without rebuilding Caddy stops the server
# from starting. If volume ever justifies limiting at the edge, build Caddy # from starting. If volume ever justifies limiting at the edge, build Caddy
# with github.com/mholt/caddy-ratelimit first. # with github.com/mholt/caddy-ratelimit first.
handle { handle {
request_body {
max_size 64KiB
}
reverse_proxy 127.0.0.1:8081 { reverse_proxy 127.0.0.1:8081 {
health_uri /api/healthz health_uri /api/healthz
} }

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 concrete reason: **Caddy cannot `reverse_proxy` to an h3 upstream.** Port 8081
must never be exposed directly. 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 ## One-time host setup
```sh ```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. 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 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 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 payment, so a payment that both deliver is meant to be in the file once. The
order nobody paid twice for. 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 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 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, **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 there is no HMAC and no server signature, and certificate pinning
authenticates us to bunq rather than bunq to us. So the endpoint's 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 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 strong one: forging requires completing a TLS handshake from inside that
range. bunq warns the range may change; if it does, callbacks 403 and 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/*`, 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 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 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. 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 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 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 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 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 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 with no network, and it is the manual escape hatch for settling a transfer by
hand. `--mark-paid` remains the other one. hand. `--mark-paid` remains the other one.
Two matching properties worth knowing, both pinned in Four matching properties worth knowing. The first three are pinned in
`ShouldMatchBankTransfers`: `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 * **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 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 Outgoing amounts are ignored, so a refund quoting the reference cannot pay
for the order it refunded. for the order it refunded.
One known limitation, logged rather than solved: a single transfer quoting * **A payment is spent once.** A transfer that settled one order pays for no
**two** order references cannot be attributed by a per-order matcher, and both other, however many references its description quotes. Before the rail
orders would settle on the same money. The rail detects the shape and logs reports an order paid it writes which payment settled it, by the bank's id,
"settle this one by hand"; at this volume that is proportionate, but do not to `<credits>.claims` (default
assume the matcher is total. `/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 ### 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 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. 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 ## The open financials page
`/financials` publishes running totals only: sales, donations, and expenses `/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), anonymized at ingest (last octet zeroed before anything reaches its DB),
no HOSTS or full-URL REFERRERS panels, and log lines matching `CENSOR_RE` 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 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 what it never ingested. `CENSOR_RE` covers `/api`, **`/order`** and
order token is the entire capability to read that buyer's status page and `/hooks`: an order token is the entire capability to read that buyer's
their invoice (name, street, postal code, city), so publishing the path status page and their invoice (name, street, postal code, city), so
publishes the buyer. Keep secrets out of URL *paths* regardless — query publishing the path publishes the buyer, and `/hooks/bunq/<secret>` is the
strings are already stripped, paths are not. 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, - `https://catcrafts.net/analytics/private/` — **uncensored** (basic auth,
hash in the Caddyfile): full IPs, all panels. hash in the Caddyfile): full IPs, all panels.

View file

@ -37,11 +37,19 @@ OUT_PRIVATE=/var/www/analytics-private/index.html
# PATH), so an uncensored public tier would have published a harvestable index # PATH), so an uncensored public tier would have published a harvestable index
# of every buyer's address within an hour of the first sale. # of every buyer's address within an hour of the first sale.
# #
# /hooks is here as the second of two layers. /hooks/bunq/<secret> is where the
# bank posts its callbacks, and that path is the endpoint's shared secret. The
# Caddyfile keeps it out of the access log altogether (log_skip on the
# prefix), so normally there is no such line to drop. But that is one
# directive in a file that is merged by hand, and the cost of it going missing
# would be the secret on a public page: with this, the public tier does not
# depend on it. The raw log and the private tier still do, so log_skip stays.
#
# Filters apply at INGEST, so this must be correct BEFORE the shop opens. # Filters apply at INGEST, so this must be correct BEFORE the shop opens.
# Should an order URL ever reach the public DB, changing this line is not # Should an order URL ever reach the public DB, changing this line is not
# enough — follow the re-filter procedure at the top of this file to rebuild # enough — follow the re-filter procedure at the top of this file to rebuild
# from the retained raw logs. # from the retained raw logs.
CENSOR_RE='"uri":"/(api|order)' CENSOR_RE='"uri":"/(api|order|hooks)'
# Serialize runs: a manual run racing the hourly timer once ingested the same # Serialize runs: a manual run racing the hourly timer once ingested the same
# rotated log twice (both processes passed the ledger check before either # rotated log twice (both processes passed the ledger check before either