Document M8 in AGENTS.md and README

This commit is contained in:
raucao committed 2026-11-23 23:43:17 +04:00
1 parent 623ef35bac
commit 8a3e3f3481
2 files changed
+125 -59

No files matched your search

+111 -56
View File
@@ -12,8 +12,10 @@ The full plan lives at `~/.opencode/plan/regent.md`.
- **The NIP‑46 responder is ours.** Do not add a NIP‑46 signer/bunker dependency,
and do not shell out to `nak`. `nak` is a *test instrument* only.
- **No Flatpak.** Distribution is AUR + Gitea releases + Nostr events.
- **Never write the nsec to disk in the clear.** Keys go in the Secret Service
(oo7), one item per key.
- **Never write an identity `nsec` to disk in the clear.** Identity keys are
NIP-49 (`ncryptsec`) in `keys.json`, mode 0600, under one passphrase. The
**device key is the one deliberate exception** — plaintext in the same file
(see "The key vault (M8)"). Nothing else is.
- **Split keys**: `device` (NIP‑46 transport + response signing, rotatable) and
`user` (the nsec — signs events, nip04/44 only). The user key must never be
used for relay traffic.
@@ -27,13 +29,13 @@ src/headless.rs headless mode + CLI pairing (no window)
src/window.rs main window: view switcher over the pages
src/config.rs GSettings wrapper (+ REGENT_RELAYS override)
src/runtime.rs tokio runtime + the bridge back to the GTK main loop
src/state.rs shared state: key store, app registry, activity log, change channel
src/state.rs shared state: key vault, app registry, activity log, change channel
src/signer/mod.rs the NIP-46 responder
src/signer/approval.rs asking the user (timeout ⇒ deny)
src/signer/approval.rs asking the user (timeout ⇒ deny), incl. unlocking
src/signer/policy.rs the auto-approval rate limit
src/store/apps.rs the registry of approved applications + their permissions
src/store/audit.rs the activity log (metadata only, mode 0600)
src/store/keys.rs identity + device keys; listing without reading secrets
src/store/keys.rs the NIP-49 key vault (identity keys + the device key)
src/store/profiles.rs cached kind-0 display names (read-only)
src/ui/approval.rs approval dialogs
src/ui/apps_view.rs the apps page (permission table + revoke)
@@ -152,14 +154,13 @@ data/ desktop, metainfo, gschema, icons, resources (ui/*.blp, sty
- The registry is keyed by **client pubkey only** (unique per connection). Audit
entries carry `identity: Option<String>`; pre-M7 entries have none and are
shown under every scope (M7b).
- The keyring/API split: `KeyStore::list_user_keys()` (no secrets),
`user_key(pubkey)` (on demand), `SignerConfig.keys: IdentityKeys`
(`Fixed`/`Many` for tests, `Store(Arc<KeyStore>)` in production). The
responder loads the key **after** the approval, just before signing.
- One-time migrations run in this order: migrate the keyring item → resolve the
current identity (setting valid, else first key) → bind legacy apps
(`bind_legacy`) → only then serve. `AppState::refresh_identities` does the
latter two at startup and after any key change.
- The vault/API split: `KeyStore::list_user_keys()` (metadata only, works while
locked), `user_key(pubkey)` (only while unlocked), `SignerConfig.keys:
IdentityKeys` (`Fixed`/`Many` for tests, `Store(Arc<KeyStore>)` in production).
The responder opens the vault **after** the approval, just before signing.
- `AppState::refresh_identities` resolves the current identity (setting valid,
else first key) and binds legacy apps (`bind_legacy`) at startup and after any
key change. Pre-M8 this also migrated a keyring item; that is gone.
- Keys page: list, **Add/Import**, per-key **Rename / Copy / Export / Remove**,
and **Use for new pairings**. Removing a key clears it as current, revokes the
apps bound to it (the dialog names them), and deletes the item.
@@ -216,12 +217,19 @@ data/ desktop, metainfo, gschema, icons, resources (ui/*.blp, sty
possession by receiving our response. A new client still needs the secret.
- **One instance only.** Two signers on one device key fight over `apps.json`.
The GUI owns the D-Bus name `org.kosmos.raucao.Regent`; headless checks
`NameHasOwner` and exits "Regent is already running". Residual race: two
headless runs started at once, or the GUI starting after headless, are not
caught — a lock or D-Bus activation would close that.
`NameHasOwner` and exits "Regent is already running". With **no session bus**
(a server, an SSH box) the GUI cannot run either, so headless falls back to an
exclusive `flock` on `regent.lock` — which also catches two headless runs
started at once. The kernel drops the lock on exit, so it cannot go stale.
- Errors are loud: `--headless`/`--pair` print a message and exit non-zero for a
bad URI, no keyring, no identity key, or a relay that refused the connect
response.
bad URI, an unreadable vault, no identity key, or a relay that refused the
connect response.
- **Unlocking for an unattended run**: `--passphrase-file <path>` (a small
regular file, mode 0400/0600 — never argv or the environment), or a systemd
credential at `$CREDENTIALS_DIRECTORY/regent-passphrase`, which wins when
present. It is read once, wrapped in `Zeroizing`, and never logged. Without it
a locked vault simply denies the sensitive methods. `--pair` needs no identity
secret and works while locked.
- **In-app pairing** (Signer page → "Pair with a client…"): paste the
`nostrconnect://` string, then the *same* approval dialog used for responder
questions (`ui::approval::present_question`). On allow, the client's relays are
@@ -244,26 +252,67 @@ data/ desktop, metainfo, gschema, icons, resources (ui/*.blp, sty
`apps.json` is the clean follow-up.
- **Still deferred:** D-Bus activation (a `.service` file + `DBusActivatable`).
## Keys
## The key vault (M8)
- **Any number of identity keys** (`nsec`s) live in the system keyring; they sign
events and do nip04/nip44. Identity items are addressed by
`application=org.kosmos.raucao.Regent`, `kind=user`, `pubkey=<hex>`; the item
**label is the user-facing name**. Listing uses attributes/labels only — a
secret is read **on demand**, when a request actually needs it, never all at
startup (the exposure window stays small, and a locked keyring fails closed).
- **One device key** (`kind=device`), generated on first use, used for NIP-46
transport (encrypting/decrypting requests, signing responses). It is what
relays and requesting apps see and can be rotated without touching any
identity.
- A legacy single `kind=user` item (no `pubkey` attribute) is migrated in place
on connect; its secret is read exactly once for that.
- **Never write the `nsec` to a file**, log it, or put it in argv/env. The
plaintext strings we handle are wrapped in `zeroize::Zeroizing`. Verified: on
drop, `nostr::SecretKey` calls secp256k1's `non_secure_erase()`, which is a
volatile overwrite — *not* a guarantee (hence the name; copies can survive in
registers or on the stack, and there is no `ZeroizeOnDrop` contract). Treat
in-memory exposure as a real risk and keep the window small.
- **One file, `keys.json`**, at `glib::user_data_dir()/org.kosmos.raucao.Regent/`
(mode **0600**, atomic temp+rename via `write_private`, a corrupt file moved to
`keys.json.corrupt`). It holds `version`, an optional plaintext `device` nsec,
and `keys: [{pubkey, name, ncryptsec}]`. There is **no Secret Service and no
`oo7`** dependency: the login keyring was never at-rest protection (unlocked at
login for the whole session, keyed off the login password).
- **Identity keys are NIP-49 `ncryptsec`** (`log_n = 16`, rust-nostr's
`MAX_LOG_N = 18` caps a tampered entry), all under **one global passphrase**.
The `pubkey` is stored beside the ciphertext so `list_user_keys` and the
signer's `get_public_key` work **while locked**.
- **The device key is deliberately plaintext** in the same file. It is rotatable
and low value (a leak exposes future request plaintexts and allows response
spoofing to paired clients, but forges no identity signature), and it must stay
available so a locked Regent still receives requests, records them and can ask
to unlock. It is the *only* exception to the "never in the clear" rule.
- **Cache the keys, never the passphrase.** Unlocking decrypts every entry into
`HashMap<PublicKey, Keys>`; the passphrase is used and dropped (`Zeroizing`).
Add/import/change ask for it again. Caching the passphrase instead would be
strictly worse: with `keys.json` readable on disk a memory dump holding it
decrypts everything, it is the *reusable* secret (exports, other clients,
other machines, backups), it enables offline scrypt attack on every backed-up
copy, and it pays a synchronous scrypt (~70 ms and 64 MiB at `log_n` 16) on
every request — a 100-event burst would be ~7 s of one core.
- **`add_user_key` verifies the passphrase** against an existing entry before
writing, so a typo cannot quietly create a second passphrase that opens only
the new key. `change_passphrase` needs only the *new* one (we hold the keys)
and re-encrypts every entry. `rename`/`forget` need no passphrase at all.
- **Auto-lock is a hard timeout since the unlock** (`lock-timeout`, default
900 s, 0 = never), checked by a 30 s `glib` timer in `state.rs`, plus "Lock
now". It is deliberately **not** an idle timer that request traffic resets — a
chatty client would hold the vault open forever. Headless never auto-locks
(there is nobody to re-prompt). Dropping the cache triggers rust-nostr's
`non_secure_erase`, a best-effort volatile overwrite with **no contract**; the
docs must not pretend it is a guarantee.
- **The vault gate follows the policy gate, and changes nothing about it:**
`sign_event`/`nip04|44_encrypt|decrypt` are decided first (a stored permission,
or a dialog), and only then — if the vault is locked — does `Prompt::Unlock` go
over the same approval channel. That order matters: a client that is unapproved
or explicitly declined never burns a passphrase prompt. A second concurrent
request sees "already unlocked"; a timeout, a missing UI or a refusal
**denies**. `connect`, `get_public_key`, `get_relays`, `ping`,
`get_permissions`, `logout` and `switch_relays` never touch the vault and keep
working while locked.
- **Audit fidelity:** `authorize()` opens the vault **before** `record()`, so a
request the vault refused is logged as `locked` and never as `silent`/`allowed`
— the log must not claim an answer that was never produced. Never reintroduce
a `keys.get()` that runs after the record.
- UI: a header lock/unlock button (`app.lock`, `<primary>l`) reflecting state; the
Keys page owns the passphrase ("Change…"), the auto-lock `ComboRow` and "Lock
now", and its add/import dialogs carry the passphrase field (with a
confirmation only when that call *sets* the passphrase). Export needs the vault
open first (`with_unlocked`). The Signer page shows the lock state.
- **Never log, print or put the passphrase in argv/env.** The passphrase is
`Zeroizing` where it crosses the vault and the headless path, and is dropped as
soon as the unlock lands; the GTK dialog fields hand it over as a plain
`String`, so this is hygiene, not a blanket guarantee. Verified: on drop,
`nostr::SecretKey` calls secp256k1's `non_secure_erase()`, a volatile overwrite
— *not* a guarantee (copies can survive in registers or on the stack, and there
is no `ZeroizeOnDrop` contract). Treat in-memory exposure as a real risk.
- Everything async goes through `runtime::spawn`, which runs the future on tokio
and applies the result on the main loop. GTK objects are not `Send`, so a
closure passed to `MainContext::invoke` cannot capture widgets — that is why
@@ -283,12 +332,19 @@ UI lives in `data/resources/ui/*.blp` (Blueprint) and is compiled to `.ui` by
- `Cargo.lock` is committed; builds use `--locked`.
- All GTK calls happen on the main context. Anything arriving from a tokio task
(relay, signer) must be marshalled with `glib::MainContext::invoke`.
- Approval prompts must **time out to Deny**, never to allow.
- Approval prompts must **time out to Deny**, never to allow. The same holds for
the unlock prompt: a timeout, a missing UI or a refusal leaves the vault
**locked**.
- The **vault gate runs before `record()`** in `authorize()`. Anything that reads
an identity key must open the vault first, so the activity log never claims a
`silent` or `allowed` answer for a request that was refused.
- Event content and client-supplied metadata are attacker-controlled text. Show
them only as **plain, selectable labels** (`use_markup(false)`), never as Pango
markup — a client must not be able to forge formatting that looks like Regent's
own UI. The approval dialog deliberately has **no default response**, so Enter
cannot approve; Escape (and the timeout) deny.
cannot approve; Escape (and the timeout) deny. The *passphrase* dialogs are the
one exception: they carry a default response because the user is typing the
secret themselves, and an empty submit simply fails and asks again.
- Set `wrap_mode(gtk::pango::WrapMode::WordChar)` on labels that show client
text. The default `Word` mode will not break a long token (an `npub`, a URL),
so the label's **minimum** width becomes the whole token and the dialog grows
@@ -380,30 +436,29 @@ the UI is tested. `build-aux/deploy-dev.sh` runs on the build host,
The audit has its own tests: a content canary never reaches the log,
unapproved requests are not logged, names are bounded, and `record` trims an
oversized file while keeping it 0600. All run on a plain `cargo test`.
- The **vault** has its own fast tests (no Secret Service needed any more):
add/list/unlock/lock, a wrong passphrase refused both for unlocking and for
adding a key, rename/remove without a passphrase, `keys.json` is 0600 and holds
ciphertext for identity keys, a corrupt file is quarantined, re-import
replaces, and export round-trips. Two responder tests cover the gate end to
end: a locked vault that the user opens signs, and one the user does not open
denies with `locked` in the log (never `allowed-silently`) — plus a responder
test that a silent UI never unlocks.
- `a_request_on_an_added_relay_is_answered_there` (ignored, needs `nak`) starts
the signer on one relay, adds a second, and drives a request/response through
the added one — the check that a dynamically added client relay carries both
directions. It uses a local second relay even when `REGENT_TEST_RELAY` is set.
- End to end (keyring → responder → app), in a throwaway HOME:
- End to end (vault → responder → app), in a throwaway HOME:
```sh
dbus-run-session -- build-aux/acceptance.sh
```
Checks the keyring round-trip, a `nak` loopback against the responder, that a
seeded identity key makes the app start the signer, that the app created a
device key, and that headless `--pair` completes a client-initiated pairing
(a `nak req --stream` subscriber must be listening, or `nak serve` mutes the
response). Needs `nak`, `secret-tool`, `gnome-keyring-daemon`,
`gtk4-broadwayd`; skips nothing, so run it where those exist.
- Key store against a real (throwaway) Secret Service:
```sh
dbus-run-session -- bash -c '
export HOME=$(mktemp -d) && mkdir -p "$HOME/.local/share/keyrings"
eval "$(printf test | gnome-keyring-daemon --unlock --components=secrets 2>/dev/null)"
CARGO_TARGET_DIR=_build/cargo-target cargo test --release -- --ignored --nocapture'
```
Note `--daemonize` breaks this: it starts a daemon without the password, and
every write then fails with `IsLocked("Cannot create an item in a locked
collection")`.
Seeds a NIP-49 `keys.json` (and checks it holds no plaintext identity key and
is mode 0600), runs a `nak` loopback against the responder, checks that a
seeded identity key makes the app start the signer and that the app created its
own device key in the vault, and that headless `--pair` (with
`--passphrase-file`) completes a client-initiated pairing (a `nak req --stream`
subscriber must be listening, or `nak serve` mutes the response). Needs `nak`
and `gtk4-broadwayd`; the GUI part still needs a D-Bus session.
- Loopback with no public relays and no display:
- `nak serve --port 10547` = writable in-memory relay.
- `nak --sec 'bunker://…' …` = independent NIP-46 *client*.
+14 -3
View File
@@ -7,7 +7,9 @@ it is answered.
## Status
Early development. M0 (skeleton) is in place; there is no signer yet.
Early development, but usable: NIP-46 pairing and approvals, per-app permissions,
the activity log, multiple identities, and a NIP-49 encrypted key vault with a
passphrase (unlock once, then auto-lock). Packaging and polish are still ahead.
## Building
@@ -42,7 +44,7 @@ dbus-run-session -- sh -c \
## Running without a window
Regent can run the signer with no window at all, including on a machine with no
display (it needs a D-Bus session for the keyring, but no GUI):
display and no session bus:
```sh
regent --headless
@@ -52,9 +54,18 @@ Anything not already allowed with "Allow always" is denied — with no window th
is nobody to ask, so pre-approve permissions in the GUI first.
`get_public_key`, `ping` and `get_relays` are still answered automatically.
Your identity keys are stored NIP-49 encrypted under a passphrase, and a locked
vault refuses to sign. Unlock it for an unattended run with a passphrase file
(mode `0400`/`0600`), or with a systemd credential named `regent-passphrase`:
```sh
regent --headless --passphrase-file /run/credentials/regent-passphrase
```
To pair a client without opening the GUI, pass the `nostrconnect://` string the
client shows (the client-initiated flow). Regent joins the client's relays, sends
the connect response, remembers the application, then keeps running headless:
the connect response, remembers the application, then keeps running headless.
Pairing needs no identity key, so it works while the vault is locked:
```sh
regent --pair 'nostrconnect://…'