bank: the hourly pull asks nostrod, and holds no bunq key

tools/pull-and-ship-credits.sh ran this repo's own bunq client
(`catcrafts-server --pull-credits`) with the API key from .env: a key
that can initiate payments, where every program run in the checkout
could read it. It now asks nostrod (Nostro) for the account's
transactions over the system bus and turns the answer into the same
credit lines. nostrod lets a program look without a password and lets
nothing pay without one, so the pull holds no credential at all.

- Each run ships the last 90 days (CREDITS_DAYS), the longest window an
  order can wait in, in place of an accumulating local copy fed by a
  reader that only ever saw the newest 50 payments.
- --dry-run prints what would be shipped and ships nothing.
- deploy/README.md: how the pull works now, the polkit rule a timer
  needs to look without a login session, and what the old reader is
  still kept for (tools/register-bunq-webhook.sh, until `nostro
  callbacks` of Nostro 1.1.0 has been seen to list the shop's webhook).

Checked on 2026-11-21 against the production account, through nostrod:
a dry run gave, for each of the 24 incoming payments the old client had
written, a line byte-identical to the old client's. A real run, with
the ship to the server, has not been made, and the timer is installed
nowhere yet.

No C++ changes: the two edits to sources are to comments.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Jorijn van der Graaf 2026-11-25 14:21:48 +01:00
commit e0d3181840
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
5 changed files with 354 additions and 160 deletions

View file

@ -383,19 +383,15 @@ matching credit appears on the account. The order page is the payment page.
file is an empty list, not an error, since a shop that has taken no
transfers yet has no file.
#### Filling the credits file: the bunq reader
#### Filling the credits file
`--pull-credits` reads the bunq account once and appends anything new to the
credits file, deduplicated by bunq's own payment id, then prints how many
arrived. Every pull re-reads an overlapping window, so that dedupe is what
stops a credit being counted twice and settling an order nobody paid twice
for. It appends rather than rewrites, so lines added by hand survive.
```sh
BUNQ_API_KEY=… TRANSFER_IBAN=NL.. \
catcrafts-server --pull-credits --orders /var/lib/catcrafts/orders.jsonl
# -> pulled 2 new credit(s) into /var/lib/catcrafts/orders.jsonl.transfer-credits.jsonl
```
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
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.
#### How settlement actually reaches the shop
@ -406,7 +402,11 @@ 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.
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.
**bunq does not sign these notifications** — verified against doc.bunq.com: no
HMAC, no server signature, and certificate pinning authenticates us to bunq
@ -437,71 +437,139 @@ 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.
**The key is IP-restricted, and that decides the shape.** Verified the hard way
on 2026-08-20: the bunq API key permits one address (the owner's home
connection), and **bunq enforces that on every request, not only at device
registration**. Registering the server's address in the device's
`permitted_ips` does 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"`. So `BUNQ_API_KEY` on the web host cannot
work unless the key's allowlist is widened in the bunq app.
**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).
That constraint happens to enforce the right design, so it is now the supported
one: **`tools/pull-and-ship-credits.sh`**, run by a systemd USER timer on the
machine bunq permits (`deploy/catcrafts-credits.{service,timer}`, install
instructions in the service file). It pulls, keeps an accumulating local copy,
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. The script refuses to overwrite
the remote with an empty file, so an upstream parse failure leaves the server
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.
Latency, so nobody wonders: five minutes for the timer plus the reconciler's own
60 s cadence, so a fresh order confirms within about six minutes of the money
landing. Orders more than two hours old back off to a ten-minute poll, so an
older one can take that long.
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
for an older credit. nostrod keeps the account's whole history and answers for
any window, so a machine that was off for weeks leaves no hole in what it
ships.
Setting it up, once, on that machine: install Nostro (its README), connect the
account with `nostro connectors add bunq`, and check that `nostro accounts`
lists the IBAN in `TRANSFER_IBAN`. `tools/pull-and-ship-credits.sh --dry-run`
then prints the lines it would ship, and ships nothing.
**Who may look.** As nostrod ships, a program may look without a password only
in an active local session: while you are logged in at that machine. A user
timer counts as part of that session, so the pull works while you are logged
in, and is refused with nobody logged in or over SSH (the script says so when
it happens). To let it run regardless, an administrator adds a polkit rule:
```js
// /etc/polkit-1/rules.d/50-catcrafts-credits.rules
polkit.addRule(function(action, subject) {
if (action.id == "net.catcrafts.Nostro1.View" && subject.user == "YOUR_USER") {
return polkit.Result.YES;
}
});
```
That lets every program running as that user look at the accounts, session or
not, and polkit cannot tell this script from any other of them. It lets none of
them pay: paying and changing stay behind the password. With the rule in place,
`loginctl enable-linger` makes the timer run with nobody logged in at all.
**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"`.
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
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.
Two operational notes. The timer only runs while that machine is awake, so
`Persistent=true` makes it pull once on waking rather than silently skipping
every window it missed. And if the home address changes, both the key's
allowlist (in the bunq app) and settlement stop working until it is updated —
the failure mode is silent from the shop's side, so the credits file's mtime is
worth glancing at.
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
failing `catcrafts-credits` unit is worth noticing.
**Read this before deciding where to put the 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. That gives two deployment shapes, and they
are not equivalent:
#### The old reader: `--pull-credits`
1. **Key off the public host (intended).** Run `--pull-credits` on a trusted
machine on a timer, and ship the credits file to the server (rsync, scp,
whatever). The server settles orders while holding **no credential that can
move a cent**, so even a full compromise of the web host cannot spend the
account. This is the same rule the EURC rail follows by never holding a
wallet key.
2. **Key on the server (simpler, strictly worse).** Set `BUNQ_API_KEY` in
`payments.env` and the rail reads the bank itself, no file shipping. The
startup log prints a warning saying exactly what has been traded away.
`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.
Either way, set **`BUNQ_PERMITTED_IPS`** to the egress address of whatever
machine 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.
The session in the context file expires, so refresh it right before using the
script, on a machine the key permits:
Other bunq settings: `BUNQ_STATE` (default `<credits>.bunq-context.json`) holds
the generated RSA keypair and the session tokens at 0600 — **back it up or at
least know that 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.
```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
```
Rate limits worth respecting: bunq caps reads at roughly **3 GET per 3
seconds** per method. One pull is one request, and the rail additionally shares
a single read across all orders in a sweep, so neither path is anywhere near
the cap. What WOULD hit it is retrying the onboarding calls in a loop; the
**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
@ -650,8 +718,8 @@ a fact about the rails rather than a difference in standard.
**`ShouldSettleBankTransfers` needs nothing and always runs.** A self-hosted
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
`--pull-credits` fills in production. It covers the whole lifecycle: the order
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`.