bank: Nostro reads the bank's callback, and the shop's bunq code is gone
All checks were successful
Deploy / build-deploy (push) Successful in 7m20s

The shadow run of 36a635f had Nostro's ReadCallback read a real
callback beside our own parser on 2026-11-25, for order CC-BCD155: the
same id, amount and reference, on the shop's own account. So the reader
takes over, and what this repo carried of bunq goes: the callback
parser, the payment-type table, the API client with its RSA signing,
`--pull-credits`, the mode that read the bank from the server with an
API key, and tools/register-bunq-webhook.sh.

The server now knows no bank. Catcrafts.Server-BankCallback.cpp is the
one unit that imports Nostro.Money: ReadBankCallback has a body read
and says what it comes to, CreditMethodFor says how a payment arrived.
Which bank it is is configuration: --banks= in project.cpp for what the
build can read, BANK_CALLBACK_BANK on the server for what arrives.

What changes for a running shop:

- The endpoint is configured with BANK_CALLBACK_PATH and
  BANK_CALLBACK_BANK; BUNQ_WEBHOOK_PATH is no longer read. Without the
  bank's name, or without TRANSFER_IBAN, the endpoint stays off and the
  startup log says so: one that answered 200 and credited nothing would
  look like working. Production's payments.env got both names before
  this was pushed.
- Only a payment on TRANSFER_IBAN is credited. A bank posts to whatever
  address any of its customers registered, so a callback about someone
  else's account can be the bank's own. Until now a payment in a
  callback was credited whichever account it was on.
- `via` says `internal` for a payment from another account at the bank
  itself, where it said `bunq`, and `card` where it would have said
  `MASTERCARD`. A kind without a word is still passed on as the bank
  wrote it, now only where that is a plain word.
- Every callback leaves one line in the log, `bank callback: credited
  ...` or `bank callback: nothing credited: <why>`, with nothing of the
  body in it beyond the credit.
- A line of the credits file may carry the bank's kind and the bank
  (`"bank":"bunq","kind":"EBA_SCT/SCT"`) in place of `method`, put into
  the ledger's words when the file is read. The pull script ships those
  and takes the bank's name from nostrod, so it holds no table of a
  bank's words either.

Also gone, with their last user: ParseAmountToMinor, PullCreditsInto
and the explicit -lcrypto (Crafter.Network names what it needs).

ShouldReadBankCallbacks replaces ShouldParseBunqPayments and
ShouldCompareCallbackReaders: what a callback comes to for each shape,
each method, the account and the sign, and that no note repeats
anything a body carried. ShouldReceiveBankCallbacks posts to the real
endpoint: an order settles from a callback, one from another account
quoting the same order does not pay for it, and a server without the
bank's name has no endpoint.

Built from scratch and run in a copy of this tree with crafter-build
6.4.1: the server and the wasm bundle build, and every suite passes but
ShouldServePostPages, which fails there for want of mirrored post media
and passed in CI on the commit before this. A dry run of the pull
script against nostrod gave 19 credits with their kinds.

Not tried: a real callback through the new handler, and `nostro
callbacks` against the registration that is in place, which was made
with the script this removes. deploy/README.md has the new setup.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Jorijn van der Graaf 2026-11-25 15:08:42 +01:00
commit 33ce8f45d8
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
24 changed files with 1218 additions and 2385 deletions

View file

@ -379,84 +379,146 @@ matching credit appears on the account. The order page is the payment page.
with `--mark-paid`. The server logs exactly this when it lapses one.
- `TRANSFER_CREDITS` (default `<orders>.transfer-credits.jsonl`) — where the
rail reads incoming credits from. One JSON object per line:
`{"id":"…","reference":"…","amount_minor":2500,"method":"sepa"}`. A missing
file is an empty list, not an error, since a shop that has taken no
transfers yet has no file.
`{"id":"…","reference":"…","amount_minor":2500,"method":"sepa"}`. In place of
`method` a line may carry the bank's own word for the payment and the bank
it is of (`"bank":"bunq","kind":"EBA_SCT/SCT"`), which the server puts into
the `via` words itself: that is what the hourly pull ships. A missing file
is an empty list, not an error, since a shop that has taken no transfers yet
has no file.
#### Filling the credits file
Two things write it, and neither puts a bank key on the web host: the bunq
webhook, which the server receives itself, and an hourly pull on a trusted
Two things write it, and neither puts a bank key on the web host: the bank's
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 bunq's own payment id. The webhook 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.
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.
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
`Nostro.Money` of [Nostro](https://forgejo.catcrafts.net/Catcrafts/Nostro),
which `project.cpp` builds in at the commit of a Nostro release. That module
holds no credential. Which bank it is is configuration, in two places:
`--banks=` in `project.cpp`, for what the build can read, and
`BANK_CALLBACK_BANK` on the server, for what arrives.
#### How settlement actually reaches the shop
Two paths, and the split is deliberate.
**Primary: the bunq webhook.** bunq pushes a `MUTATION` notification when money
lands; the backend decodes the payment and appends it to the credits file, and
the reconciler settles from there. Normally seconds end to end. Registered with
`tools/register-bunq-webhook.sh` from a machine bunq's key permits (`--show`
lists what is registered, with the secret redacted). **That call REPLACES the
whole filter set**, so look before overwriting. The script needs an
authenticated bunq session for it, and takes that from this repo's own bunq
client: see "The old reader" below. Since its 1.1.0 Nostro lists and changes
callbacks itself (`nostro callbacks`, through nostrod), which is what replaces
this script.
**Primary: the bank's callback.** The bank posts a notification when money
lands; the backend has the payment read, appends it to the credits file, and
the reconciler settles from there. Normally seconds end to end. In
`payments.env`:
**bunq does not sign these notifications** — verified against doc.bunq.com: no
HMAC, no server signature, and certificate pinning authenticates us to bunq
rather than bunq to us. So the endpoint's authentication is three transport-level
controls, all of which have to stay in place:
- `BANK_CALLBACK_PATH=/hooks/bunq/<long random>`: the secret address the bank
posts to. Unset means the endpoint does not exist.
- `BANK_CALLBACK_BANK=bunq`: whose callbacks arrive there, as a Nostro
connector names the bank. Without it the endpoint stays off, and the startup
log says so: an endpoint that answered the bank and credited nothing would
look like working.
- `TRANSFER_IBAN`, above, is also the account a payment has to be on.
The startup log confirms it with `bank callback: enabled on a secret path`.
Where the bank posts is registered at the bank, with Nostro (1.1.0 or later),
on the machine where nostrod holds the connection:
```sh
nostro callbacks --connector NAME # what is registered, each address whole
nostro callbacks add --connector NAME # asks for the https address at a prompt
nostro callbacks remove --connector NAME # asks for the address, as listed
```
Each asks for your password, the listing too: the address is a secret. The
listing shows what is registered whoever set it. The registration in place on
2026-11-25 was made with a script this repo carried until then, and
`nostro callbacks` had not been run against it yet.
**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:
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.
2. **A secret path.** Caddy matches only the non-secret prefix `/hooks/bunq/*`,
so the secret lives solely in `BUNQ_WEBHOOK_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
non-POST to it falls through to an ordinary 404 rather than a distinctive
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/`.
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
would be bunq's own, from bunq's range. So only a payment on
`TRANSFER_IBAN` is credited.
The handler deliberately does the least it can: decode one payment, append it,
answer 200. It makes no settlement decision, so a forged callback can at worst
manufacture a credit line — it cannot bypass the reference match or the
covering-amount rule, and nothing ships without a human either way.
The handler deliberately does the least it can: have one payment read, append
it, answer 200. It makes no settlement decision, so a forged callback can at
worst manufacture a credit line — it cannot bypass the reference match or the
covering-amount rule, and nothing ships without a human either way. And a
credit line that never was a payment is gone at the next pull, which replaces
the file with what the bank says is on the account.
**Backstop: the hourly pull.** Not optional. bunq retries a callback about six
times and then drops it permanently, so a payment arriving while the backend
restarts — every deploy is a restart — is lost to the webhook with no error
anywhere. `deploy/catcrafts-credits.timer` runs `pull-and-ship-credits.sh`
hourly to turn that from a missing donation into an hour of delay.
Every callback leaves one line in the journal, with nothing of the payer in it:
**The pull holds no key.** A bunq API key can **initiate payments**, and bunq
offers no read-only scope, so there is no such thing as a key that can only
read. Since 2026-11-21 the pull therefore holds none. The bank connection lives
in [nostrod](https://forgejo.catcrafts.net/Catcrafts/Nostro) on a trusted
machine, under a system user of its own, and
**`tools/pull-and-ship-credits.sh`** asks it over the system bus for the
account's recent transactions (`Transactions` on `net.catcrafts.Nostro1`, the
interface Nostro publishes for other programs). nostrod lets a program look
without a password and lets nothing pay or change anything without one, which
is the read-only scope bunq lacks. A systemd USER timer runs the script
(`deploy/catcrafts-credits.{service,timer}`, install instructions in the
service file).
```
bank callback: credited 25.00 via sepa (id …)
bank callback: nothing credited: the payment was credited before, or …
bank callback: nothing credited: the payment is not money coming in
bank callback: nothing credited: the payment is on another account than the shop's
bank callback: nothing credited: the body was refused: <the reader's sentence>
```
`the body was refused` where a payment was expected is the line to act on: the
bank sent a shape Nostro's reader does not take. The pull still settles that
payment, the sentence names the check that failed, and the fix is Nostro's.
**How a payment arrived** is the ledger's `via`: `sepa` (a credit transfer),
`ideal`, `card`, `directdebit`, `swift`, and `internal` for a payment from
another account at the bank itself. A kind the table has no word for is not
guessed at and shows as the bank's own word: `ADYEN` is a bunq.me payment link,
which either a card or iDEAL may have paid. That column is what tells you
whether an order is safe to ship: a credit transfer is final, a card payment
can be reversed for months. Orders settled before 2026-11-25 say `bunq` where
newer ones say `internal`, and would have said `MASTERCARD` for `card`.
**Backstop: the hourly pull.** Not optional. A bank retries a callback a few
times (bunq: five, a minute apart) and then drops it permanently, so a payment
arriving while the backend restarts — every deploy is a restart — is lost to
the callback with no error anywhere. `deploy/catcrafts-credits.timer` runs
`pull-and-ship-credits.sh` hourly to turn that from a missing donation into an
hour of delay. It is also the one path that asks the bank with the bank's own
authentication.
**The pull holds no key.** A key that can read a bank account can as a rule
also pay from it (a bunq API key can, and bunq offers no read-only scope), so
the pull holds none. The bank connection lives in
[nostrod](https://forgejo.catcrafts.net/Catcrafts/Nostro) on a trusted machine,
under a system user of its own, and **`tools/pull-and-ship-credits.sh`** asks it
over the system bus for the account's recent transactions (`Transactions` on
`net.catcrafts.Nostro1`, the interface Nostro publishes for other programs).
nostrod lets a program look without a password and lets nothing pay or change
anything without one, which is the read-only scope the bank lacks. A systemd
USER timer runs the script (`deploy/catcrafts-credits.{service,timer}`, install
instructions in the service file).
The script turns nostrod's answer into credit lines and ships **incoming
credits only** to the file the rail reads. Outgoing lines are supplier payments
and card spending: the matcher ignores negative amounts, so shipping them would
put the business's outgoing payment history on a public-facing host for no
settlement benefit. It ships nothing when the read failed, and it never
overwrites the remote with an empty file, so a failure leaves the server
settling from the last good copy rather than from nothing.
settlement benefit. A line carries the payment's kind as the bank wrote it and
the bank's name as nostrod has it, and the server puts that into the `via`
words above, so the script knows no bank either. It ships nothing when the read
failed, and it never overwrites the remote with an empty file, so a failure
leaves the server settling from the last good copy rather than from nothing.
Each run ships the last 90 days (`CREDITS_DAYS`). That is the longest
`TRANSFER_WINDOW_HOURS` the server accepts, so no order can still be waiting
@ -492,13 +554,13 @@ them pay: paying and changing stay behind the password. With the rule in place,
**Why the server does not read the bank itself.** Besides the key policy above,
a standard bunq API key works only from the address it was registered at, and
**bunq enforces that on every request, not only at device registration**.
Verified the hard way on 2026-08-20, with this repo's own reader: registering
the server's address in the device's `permitted_ips` did not help (the device
registered fine from home with both addresses listed), and every read from the
server still came back `"Incorrect API key or IP address"`.
Verified the hard way on 2026-08-20, with the reader this repo had then:
registering the server's address in the device's `permitted_ips` did not help
(the device registered fine from home with both addresses listed), and every
read from the server still came back `"Incorrect API key or IP address"`.
Latency, so nobody wonders: through the webhook a fresh order confirms within
the reconciler's own 60 s cadence of the money landing. A payment the webhook
Latency, so nobody wonders: through the callback a fresh order confirms within
the reconciler's own 60 s cadence of the money landing. A payment the callback
lost waits for the next hourly pull on top of that. Orders more than two hours
old back off to a ten-minute poll, so an older one can take that long.
@ -507,110 +569,9 @@ Two operational notes. The timer only runs while that machine is awake, so
every window it missed. And when the connection in nostrod stops working
(`nostro connectors` says whether it does: a revoked key, or an IP-restricted
key after the home address changed), the pull fails and its unit says so, but
nothing tells the shop. Settlement then rests on the webhook alone, so a
nothing tells the shop. Settlement then rests on the callback alone, so a
failing `catcrafts-credits` unit is worth noticing.
#### The callback shadow (temporary, since 2026-11-25)
The webhook's parser (`ParseBunqCallback`) is to be replaced by Nostro's
`ReadCallback`, so that this repo stops carrying bunq's wire format. That
reader is stricter than ours and had never been run on a callback bunq really
sent, while ours has settled real payments since 2026-08. So for now both run:
ours decides exactly as before, and Nostro's reads the same body afterwards and
logs what it made of it. It cannot change the answer or the credit, and it
turns on by itself wherever `BUNQ_WEBHOOK_PATH` is set.
Read the result on the server, after any payment in or out of the account:
```sh
journalctl -u catcrafts-server | grep 'bank callback shadow'
```
- `AGREE in method=sepa account=ours event=transactions`: both readers read
the same id, amount and reference. This line on a real incoming transfer is
what the test is for.
- `AGREE … method ours=MASTERCARD nostro=card …`: still agreement. Only the
name for how the money arrived differs, which is expected for card payments.
- `account=other`: the payment is on another account than `TRANSFER_IBAN`. Our
parser never looked at that, and the replacement must.
- `DIFFER nostro refused what ours reads as a payment: <sentence>`: Nostro's
reader would have dropped a payment ours credited. The sentence names the
check that failed, and that is a fix for Nostro, not for this repo.
- `DIFFER id=… amount=… reference=…`: both read a payment and not the same one.
The line carries no payment data: no id, no amount, no name and no reference.
`ShouldCompareCallbackReaders` pins the verdicts and
`ShouldReceiveBankCallbacks` the endpoint, including that a payment Nostro's
reader refuses is credited all the same.
When real payments read `AGREE`, the reader takes over, and the shadow goes
together with `ParseBunqCallback`
(`server/implementations/Catcrafts.Server-CallbackShadow.cpp` and its call in
`Catcrafts.Server-Http.cpp`). Nostro's module is built in through
`project.cpp`, named at the commit of its 1.1.0 release.
#### The old reader: `--pull-credits`
`catcrafts-server --pull-credits` is this repo's own bunq client. It onboards
with the API key, keeps its context in a file, reads the account's newest
payments and appends them to a credits file, deduplicated by bunq's payment id.
It was the pull until 2026-11-21. Nothing runs it on a schedule any more, and
it is still here for one job: `tools/register-bunq-webhook.sh` needs a bunq
session to list or replace the notification filter, and takes it from this
reader's context file. Nostro does the same with no key in this checkout since
its 1.1.0 (`nostro callbacks`). Once that command has been seen to list the
shop's webhook, the reader, its key in `.env` and that script can all go.
The session in the context file expires, so refresh it right before using the
script, on a machine the key permits:
```sh
BUNQ_API_KEY=… TRANSFER_IBAN=NL.. \
bin/Catcrafts.Server-*/catcrafts-server --pull-credits \
--orders ~/.cache/catcrafts/orders.jsonl
# -> pulled 2 new credit(s) into ~/.cache/catcrafts/orders.jsonl.transfer-credits.jsonl
```
**The webhook was registered with this reader's key.** bunq's documentation
does not say whether a registration outlives the key that made it, so do not
revoke that key before `nostro callbacks` lists the webhook through the
connection nostrod holds, and look again after revoking it: if it is gone,
`nostro callbacks add` sets it again. Otherwise settlement may quietly fall
back to the hourly pull alone.
What the reader costs is the reason it is no longer the pull. Its key
(`BUNQ_KEY` in the repo-root `.env`) can **initiate payments**, and every
program run in that checkout can read it. Two things follow for as long as it
exists:
1. **Never put it on the server.** Setting `BUNQ_API_KEY` in `payments.env`
makes the rail run this same reader in-process instead of reading the
credits file. That puts a credential that can spend the account on a public
host, the startup log prints a warning saying exactly that, and with an
IP-restricted key it does not even work (see above). The server settles
orders while holding **no credential that can move a cent**, the same rule
the EURC rail follows by never holding a wallet key.
2. **Pin it.** `BUNQ_PERMITTED_IPS` is the egress address of the machine that
holds the key. bunq registers it once, at device-server time, and it is the
only thing standing between a leaked key and someone spending the balance.
The default is `*`, which works and is announced loudly. Changing it later
means deleting the context file and re-onboarding.
Its other settings: `BUNQ_STATE` (default `<credits>.bunq-context.json`) holds
the generated RSA keypair and the session tokens at 0600. **Deleting it forces
a re-onboard**, and bunq allows as few as ten setup calls PER DAY, so do not
delete it casually. If the key can see more than one active account,
`TRANSFER_IBAN` picks which; without it, a key seeing several accounts is a
refusal rather than a guess, because guessing means reconciling the shop
against its savings.
Rate limits worth respecting, whoever reads: bunq caps reads at roughly **3 GET
per 3 seconds** per method. One pull is a handful of requests (nostrod waits a
429 out by itself), and the rail shares a single read of the credits file
across all orders in a sweep, so neither path is anywhere near the cap. What
WOULD hit it is retrying the old reader's onboarding calls in a loop; its
context file is what prevents that.
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.
@ -758,10 +719,18 @@ a fact about the rails rather than a difference in standard.
rail has no provider to authenticate to, so there is nothing to call and no
secret to hold. The suite drives the REAL transfer rail — not a stand-in — and
plays the part of the bank by writing a credits line, the same file the
webhook and the hourly pull fill in production. It covers the whole lifecycle: the order
page's account details, the reference in all the mangled forms a payer might
type, partial payments accumulating, an outgoing amount being ignored, and the
reconciler flipping the order to paid with the right `via`.
bank's callback and the hourly pull fill in production. It covers the whole
lifecycle: the order page's account details, the reference in all the mangled
forms a payer might type, partial payments accumulating, an outgoing amount
being ignored, and the reconciler flipping the order to paid with the right
`via`.
**`ShouldReceiveBankCallbacks` is its counterpart for the callback endpoint**,
and needs nothing either: it plays the bank by posting to the endpoint. An
order settles from a callback, a payment on another account or one the reader
refuses credits nothing, and the server's log repeats nothing a callback
carried. What the reader makes of each shape is pinned in
`ShouldReadBankCallbacks`.
**`ShouldSettleEurcOnTestnet` is the one live-counterparty suite**: a €1
donation paid with 1 real testnet EURC on Ethereum Sepolia, settled by the same

View file

@ -1,10 +1,9 @@
# Read the bank and hand the incoming credits to the server.
#
# A USER unit, on the machine where nostrod holds the bunq connection, NOT on
# the web host. The web host cannot call bunq at all (the key is IP-restricted
# at the key level and bunq enforces it on every request), and it should not be
# able to: a bunq key can initiate payments, because bunq offers no read-only
# scope. So the server only ever reads a file.
# A USER unit, on the machine where nostrod holds the bank connection, NOT on
# the web host. The web host holds no key to the bank, and should not: a key
# that can read the account can as a rule also pay from it. So the server only
# ever reads a file.
#
# This unit holds no key either. tools/pull-and-ship-credits.sh asks nostrod
# (https://forgejo.catcrafts.net/Catcrafts/Nostro) over the system bus, and

View file

@ -1,12 +1,12 @@
# Hourly, because this is now the BACKSTOP rather than the primary path.
#
# Since the bunq webhook landed, a payment normally settles within seconds: bunq
# pushes it, the handler writes the credit, the reconciler picks it up. This
# timer exists for the case the webhook cannot cover — bunq retries a callback
# roughly six times over a few minutes and then DROPS IT FOREVER, so a payment
# that arrives while the backend is restarting (every deploy is a restart) is
# lost to the webhook with no error anywhere. An hourly pull turns that from a
# silently missing donation into an hour of delay.
# A payment normally settles within seconds: the bank posts a callback, the
# handler writes the credit, the reconciler picks it up. This timer exists for
# the case the callback cannot cover — a bank retries a callback a few times
# over a few minutes and then DROPS IT FOREVER, so a payment that arrives while
# the backend is restarting (every deploy is a restart) is lost to the callback
# with no error anywhere. An hourly pull turns that from a silently missing
# donation into an hour of delay.
#
# It was five minutes while the pull WAS the settlement path. Hourly is the
# right cadence for a safety net, and it keeps the noise down.
@ -14,7 +14,7 @@
# Persistent=true so a laptop that was asleep pulls once on waking instead of
# silently skipping every window it missed.
[Unit]
Description=Backstop pull of bunq credits for catcrafts.net (hourly)
Description=Backstop pull of bank credits for catcrafts.net (hourly)
[Timer]
OnBootSec=2min