Skip to content

btcpayserver: create the admin account before BTCPay opens its port - #278

Merged
prusnak merged 2 commits into
bitcoinix:masterfrom
ostermayer:btcpay-first-admin-registration
Nov 18, 2026
Merged

prusnak merged 2 commits into
bitcoinix:masterfrom
ostermayer:btcpay-first-admin-registration

Conversation

@ostermayer

@ostermayer ostermayer commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #131.

BTCPay Server makes the first account that registers the server admin. That account can
use the internal Lightning node and install plugins. bitcoinix started BTCPay with no
admin, so until the operator registered, any local user, or a service that can reach the
port, could register first and take it. The window also covers the onion service and any
LAN or WireGuard address.

BTCPay 2.4.7 has no setting that closes it:

  • There's no option, environment variable or CLI flag to create the first admin or to
    require a token or an invite for it.
  • disable-registration (default true) locks registration only once the first admin
    exists.
  • Turning LockSubscription on before that blocks the web form, but not
    POST /api/v1/users. While no admin exists, BTCPay treats an anonymous caller of the
    Greenfield API as an admin and lets it create one. The regtest test used exactly this
    request until now.

At the network level, loopback can't tell local users apart. IPAddressAllow filters by
address. netns-isolation lets every process in the host namespace reach the service
namespaces through the bridge. A UID owner match in the firewall would need an allow list
of every client (the operator's SSH tunnel, Tor, a reverse proxy) for both iptables and
nftables. BTCPay binds only to an address and port, so a Unix socket would mean a new
listener setup and would break the onion service and existing tunnels.

So this PR creates the admin before BTCPay opens its port:

  • A new oneshot unit btcpayserver-admin runs before each start of btcpayserver. If the
    database has an admin, it exits.
  • Otherwise it starts BTCPay in a private network namespace (PrivateNetwork), bound to
    127.0.0.1, which no other process can reach. It creates admin@localhost through the
    Greenfield API with the password in the new secret btcpayserver-admin-password, then
    stops BTCPay. BTCPay then locks registration.
  • btcpayserver requires the unit, so it doesn't start when the admin can't be created.
  • The secret belongs to services.btcpayserver.user and .group, so a custom user or
    group works with setup-secrets.
  • The setup instance's API URL follows services.btcpayserver.rootpath, with / and a
    trailing / treated like BTCPay does.
  • The BTCPay config is now a function of the bind address, because BTCPay binds to every
    bind in its config file and on its command line.
  • Nodes that already have an admin keep it and don't run the setup instance.

Nodes that ran without an admin before this change may already have an admin that isn't
the operator's. The docs say how to check the admins in Server Settings > Users. An admin
that isn't yours could have installed plugins that run as the btcpayserver user and
use the Lightning node, so the docs say to treat the node as compromised: stop BTCPay
Server, move the funds to a wallet this node doesn't control and set up the node again.
They give no in-place recovery steps.

Docs: a new "Connect to BTCPay Server" section with "Check the admin accounts" in
docs/services.md, the 0.2.0 upgrade notes and release notes, the example config and
the BTCPay recipe in dev/dev-features.sh. The old warning in examples/configuration.nix
about creating the admin before enabling the onion service is gone, since the admin now
exists first.

The 0.2.0 notice is for every node with BTCPay. Nodes without an admin now sign in with
the generated account instead of registering. Nodes with an admin should check that it's
theirs. Setups that supply their own secrets must add the new one.

Testing

The btcpayserver VM test now checks that:

  • btcpayserver-admin runs with PrivateNetwork.
  • admin@localhost with the generated password is a ServerAdmin.
  • An anonymous POST /api/v1/users gets 401, with and without isAdministrator.
  • /register redirects.

Before this change, the anonymous request created the first admin. The regtest test
relied on it. That test now signs in as the generated admin for its Greenfield request.
BTCPay accepts Basic auth only in an account's first five minutes, so a test helper
resets the account's creation time before each request.

New eval tests in test/configuration.nix:

  • With a custom user and group, the secret's owner is that user and group, and both
    exist.
  • The health URL of the setup instance for rootpath null, /, btcpay, /btcpay and
    btcpay/.

Results:

  • regtest VM scenario: pass.
  • netnsRegtest VM scenario: pass on an earlier revision of this branch, without the
    last fixes. That run checked the bind handling in a service namespace: the config
    file's bind is the namespace address, so the setup instance gets its own config with
    bind=127.0.0.1. An earlier version passed --bind=127.0.0.1 on the command line and
    failed in both scenarios.
  • The shellcheck-services build in the VM scenarios covers the new unit script.
  • checks.x86_64-linux.module-config and checks.x86_64-linux.test-runner: pass.

Not tested: the mainnet scenarios, a running node with rootpath, and a rerun of
btcpayserver-admin on a node that already has an admin.

Models

Claude Opus 5.5 wrote the change. Codex GPT-5.6 Sol, Codex GPT-6 Astra (two passes plus rechecks), Kimi K3.1 Pro (two passes), DeepSeek V4.1 Flash 1017 and GLM 5.3 reviewed it. Claude Opus 5.5 rebased it onto #224 and #283.

@github-actions github-actions Bot added area: ci .github/, test/, dev/ area: docs docs/, README, examples/, SECURITY.md, book.toml, assets area: modules modules/ and pkgs/lib.nix type: feature New capability or service labels Nov 16, 2026

@viberaven viberaven left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 4dbf53430da5.

No actionable findings. Checked the bootstrap's private network namespace, service ordering, password handling, existing-admin query, and registration response handling against the pinned BTCPay source.

Local module-config, test-runner, and mdBook builds pass. I also ran an additional NixOS VM test on this head with rootpath = "/payments", a custom BTCPay user and group, and regtest bitcoind. It verified that first startup creates exactly one server admin, the prefixed health endpoint works, rerunning btcpayserver-admin with an existing admin succeeds without adding another admin, and BTCPay starts successfully afterward. The VM test passed. I inspected the PR's anonymous-registration assertions and green CI results; I did not rerun the entire standard scenario matrix.

Review performed with Codex (GPT-6 Astra).

@github-actions github-actions Bot added the status: rebase needed Merge conflicts need to be resolved label Nov 16, 2026
@ostermayer
ostermayer force-pushed the btcpay-first-admin-registration branch from 4dbf534 to abe7644 Compare November 18, 2026 00:19
BTCPay Server makes the first account that registers the server admin,
which can use the internal Lightning node and install plugins. bitcoinix
started BTCPay without an admin, so until the operator registered, any
local user or a service that can reach the port could register first.
Neither BTCPay's web form nor its Greenfield API needs a token for that
first account, and `disable-registration` only applies once it exists.

The new unit btcpayserver-admin runs before each start of btcpayserver.
When the database has no admin, it starts BTCPay in a private network
namespace, which no other process can reach, creates the admin
`admin@localhost` through the Greenfield API with the password in the
new secret btcpayserver-admin-password, and stops BTCPay. btcpayserver
requires the unit, so it doesn't open its port before an admin exists.
BTCPay then closes registration. Nodes that already have an admin keep
it.

Fixes bitcoinix#131.

Assisted-by: Claude Code (Claude Opus 5.5)
@ostermayer
ostermayer force-pushed the btcpay-first-admin-registration branch from abe7644 to 25cf58b Compare November 18, 2026 01:01
@github-actions github-actions Bot added status: rebase needed Merge conflicts need to be resolved and removed status: rebase needed Merge conflicts need to be resolved labels Nov 18, 2026
@prusnak prusnak added this to the v0.2.0 milestone Nov 18, 2026
@github-actions github-actions Bot removed the status: rebase needed Merge conflicts need to be resolved label Nov 18, 2026
@prusnak
prusnak force-pushed the btcpay-first-admin-registration branch from 8b5343e to 25cf58b Compare November 18, 2026 14:26
@github-actions github-actions Bot added the status: rebase needed Merge conflicts need to be resolved label Nov 18, 2026
@github-actions github-actions Bot removed the status: rebase needed Merge conflicts need to be resolved label Nov 18, 2026
@prusnak
prusnak merged commit 211033b into bitcoinix:master Nov 18, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ci .github/, test/, dev/ area: docs docs/, README, examples/, SECURITY.md, book.toml, assets area: modules modules/ and pkgs/lib.nix type: feature New capability or service

Projects

None yet

Development

Successfully merging this pull request may close these issues.

btcpayserver: until the operator registers, any local user can become the server admin and reach the internal Lightning node

3 participants