Skip to content

feat(server): give each program its own akou key and let an admin log in over the network - #43

Merged
GeiserX merged 21 commits into
mainfrom
feat/server-keys
Sep 26, 2026
Merged

GeiserX merged 21 commits into
mainfrom
feat/server-keys

Conversation

@GeiserX

@GeiserX GeiserX commented Sep 25, 2026

Copy link
Copy Markdown
Owner

Problem

In server mode akou has to be reachable from other programs on the network, each with its own credentials, without opening the desktop app's loopback guard to anyone who can reach the port. Before this PR there was one app token, a loopback-only listener, no way to scope or revoke access, no health check a container orchestrator can use, and no way for an admin to open the web UI from another machine. A container restart could also leave a lock behind that the new container never took.

Fix

  • Keys. akou keys create|list|revoke issues ak_ keys with a jobs or admin scope and a callback-host allowlist. Only the hash is stored (keys.json, mode 0600). The server re-reads the file when it changes, so a revoked key gets 401 on the next request. Edits run under a lock file, and revoke matches by id only.
  • Scopes. Every route declares jobs or admin. A jobs key on an admin route gets 403. A keyed typo gets 404 or 405, not "needs admin".
  • Callback hosts. A key's allowlist is checked before a callback is accepted (422 callback_not_allowed). * matches public hosts only: loopback, RFC 1918, CGNAT, link-local, unique-local and localhost must be named on the key. SERVER.md SV-K4 now says so.
  • Network binding. api.bind defaults to 127.0.0.1 in app mode and 0.0.0.0 in server mode. A non-loopback bind without server.behind_proxy exits 78 naming both settings. Only 127.0.0.1, 0.0.0.0 or :: are accepted in server mode, so the local CLI can always reach it. X-Forwarded-For is believed only from server.trusted_proxies.
  • Open routes. GET /healthz (ok, version, models_ready, queue_depth) answers 503 while models download or the recognizer loads. GET /v1/server lists presets, engines and capabilities. GET /v1/keys/me names the caller. /healthz and /v1/server need no token in either mode; DESIGN 6.3 says so.
  • Uploads. Routes marked upload: true take multipart up to server.max_upload_mb (default 512). Every other route, and the web UI on the same listener, keeps the 64 KB JSON cap.
  • Admin login. akou admin set-password (piped in; it refuses a terminal) stores an Argon2 hash, or an admin key can be pasted once. A wrong password costs 2 s, and logins run one at a time, so the delay is a real rate limit. Sessions live in the tab only and end when their key is revoked or the password changes. The cross-origin refusal sits at the login. An Origin naming server.public_host is accepted for proxies that rewrite Host.
  • Lock. The lock records pid and a per-process id and is refreshed every 10 s. In server mode a lock older than 30 s is taken; a fresher one from an earlier start exits 75 with how long until it frees, so the container restart policy brings it back. Two containers on one volume never both run.
  • One real bug found on the way: saving settings through the API rewrote config.json from memory and would have dropped a password set after start.

Beads

Bead Acceptance Evidence
akou-5an.60 SI-4 Stale own-pid lock taken; fresh refused then taken after 30 s; same-process and live-foreign locks refused; second container takes over within 40 s of SIGKILL tests/lock.test.ts (two real processes at 1/10 timings, bound stale + beat); entry-point test for exit 75. Red on today's rule, on dropping the mtime check, on a no-op heartbeat
akou-5an.2 SV-D2 DESIGN 6.3 test passes in app mode; proxy Host and browser Origin accepted in server mode tests/server-mode.e2e.test.ts; tests/security.e2e.test.ts unchanged and green
akou-5an.3 SV-D3 300 MB upload succeeds; 300 MB JSON gets 413; multipart to /calls gets 415 tests/server-mode.e2e.test.ts against a stub upload route; 64 KB cap on the web UI bodies
akou-5an.8 SV-P4 200 with no header; 503 while models are not ready Test holds a stub recognizer in loading, unavailable, ready
akou-5an.9 SV-P5 Bad bind exits naming both keys; forged XFF audited under the peer Real entry point exits 78; trusted-proxy positive control
akou-5an.15 SV-K1 Answers in both modes; presets equal section 8's table Test parses SERVER.md section 8
akou-5an.16 SV-K2 Printed key authenticates; file 0600 without the key; revoked key 401 tests/server-access.e2e.test.ts
akou-5an.17 SV-K3 Both scopes over every route against a table Walks server.routes(); a route with no row fails the test
akou-5an.18 SV-K4 archive.lan accepted, other.lan refused; * accepts both Plus 19 private or loopback URLs refused under *
akou-5an.59 SI-3 jobs key gets its name and ["jobs"]; revoked and no key 401 Both refusal paths removed together turn it red
akou-5an.47 SV-U1 Wrong password 401 after 2 s; right one shows the app; session dies with the tab tests/server-login.e2e.test.ts, browser test tests/ui/server-login.test.ts

Every rule above was proven red by a one-line mutation, then restored.

Fully done: akou-5an.2, akou-5an.8, akou-5an.9, akou-5an.15, akou-5an.16, akou-5an.47, akou-5an.59, akou-5an.60.

Partial:

  • akou-5an.3: the real POST /v1/jobs and streaming the body to disk belong to the server-jobs lane, which is stacked on this branch.
  • akou-5an.17: tested against the route table; the OpenAPI-file version waits on PG-A2.
  • akou-5an.18: calling the allowlist from POST /v1/jobs is the server-jobs lane's. The refusal name is open (below).
  • akou-5an.59: the SI-2 CI health check that shows the key's name waits on SI-2.

For the owner to check

  • Decide the refusal name: callback_not_allowed (SERVER.md, shipped here) or callback_host_not_allowed (what Telegram-Archive's design expects).
  • Two real Docker containers on one volume: the second takes over within 40 s of docker kill on the first (needs the image, SV-P1).
  • A loopback Host (127.0.0.1:<port>, localhost:<port>) is accepted in server mode even when server.public_host is set, so the CLI on the same box keeps working. That is looser than SV-D2's wording.
  • api.bind, server.enabled, behind_proxy, public_host, trusted_proxies and admin_password_hash are file-only; the API cannot write them.
  • Admin sessions have no server-side expiry beyond key revocation, password change and restart.

…container's

In a container Bun is pid 1 on every start, so a lock left on the volume by an
unclean stop named a pid that looked alive, the app refused to start and exited
0, and the restart policy saw a clean exit. The lock now records the holder's
pid and an id made once at process start, and the holder touches it every 10 s.

- A lock with this process's pid and id is refused (a second acquire).
- A live pid that is not this process is refused, as before.
- In app mode a dead pid is taken at once, as before.
- Otherwise (this pid with another id, or in server mode a pid this namespace
  cannot see) the lock is taken once its mtime is 30 s old, so a second
  container on the same volume stays out while the first runs.

The call log writer follows the same rule. Refs akou-5an.60 (SI-4).
Server mode (`server.enabled`, or AKOU_SERVER=1) is the same /v1 with a door
for programs on the network. This lays the base the job routes build on.

- Bind: the app stays on 127.0.0.1. Server mode binds `api.bind`, 0.0.0.0 by
  default, and refuses to start (exit 78, naming both keys) on an address that
  is not loopback unless `server.behind_proxy` says a proxy terminates TLS.
- Host: `server.public_host` when set, any host behind a proxy, or loopback.
  Browser headers are allowed there; the web UI's door is the admin login.
- Keys: `akou keys create|list|revoke` edit keys.json (0600, the key's SHA-256
  and the whsec_ secret) with no app running; the server reads the file again
  when it changes, so a revoked key gets 401 on its next request.
- Scopes: every route declares `open`, `jobs` or `admin`, a route that says
  nothing is admin, and the guard checks the caller once before any handler.
  A jobs key gets 403 on PATCH /v1/config; the app token keeps admin.
- Callback hosts: `requireCallbackAllowed` answers 422 callback_not_allowed for
  a host off the key's list; `*` allows any.
- Uploads: a route added with `upload: true` takes multipart up to
  `server.max_upload_mb` (512 MiB); every other route keeps 64 KB of JSON.
- `server.trusted_proxies`: only those peers' X-Forwarded-For names the
  source address; a refused key is logged under that source.
- Open routes: GET /healthz (503 while models download), GET /v1/server (mode,
  presets, engines, capabilities that turn true with their routes), and
  GET /v1/keys/me for any key.

Refs akou-5an.2, akou-5an.3, akou-5an.8, akou-5an.9, akou-5an.15,
akou-5an.16, akou-5an.17, akou-5an.18, akou-5an.59.
A page reachable over the network needs a door, and the one-time code that
opens it on loopback is minted by a CLI on the same machine. In server mode
the API's own listener now serves the page behind the proxy, and the page
opens a session with the admin password or an admin key pasted once.

- `akou admin set-password` reads the password from stdin (never argv) and
  stores an argon2id hash as `server.admin_password_hash`; the login reads it
  from the file, so it works without a restart.
- A wrong password or key gets 401 after 2 s; a jobs key cannot log in.
- The session lives in the tab's sessionStorage, never a cookie, so it dies
  with the tab and there is no CSRF; a request from another Origin is refused.
- A save over the API keeps the file's own keys as they are on disk, so a
  PATCH /v1/config no longer drops a hash written since the start.

Refs akou-5an.47 (SV-U1).
…ddresses

A key holding only '*' could point callbacks at 127.0.0.1, 10/8, link-local
and akou's own API. The owner's rule is that '*' matches public hosts only;
loopback, RFC 1918, shared, link-local and unique-local literals and localhost
must be listed on the key by name.
Mounted on the API listener, the page inherited Bun's upload-sized body
limit, so an anonymous POST /session could make akou hold up to 512 MiB.
The page now refuses a declared body over 64 KB before reading it, and reads
every body through the same capped reader as the API.
…ner comes back

After a crash or OOM kill the restarted container is pid 1 again and the
lock it left is under 30 s old. The entry point called that 'already running
(pid 1)', naming itself, and exited 0, so Docker's on-failure policy left the
service down. It now exits 75 (EX_TEMPFAIL) and says when the lock frees.
… models

models_ready meant only that the files were on disk, so while the recognizer
was still loading them, or after it failed to, /healthz said healthy and
Compose's service_healthy released clients before any job could run.
…uessing

The delay applied to each request on its own, so parallel guesses were not
slowed at all, and each ran an argon2id check with no credentials needed.
Logins now queue across the process, and a wrong one holds the queue for its
delay: one guess per 2 s, one password check at a time.
nginx sends Host: $proxy_host by default, so akou saw its own loopback
address while the browser sent Origin: https://akou.example, and every page
POST, the login included, got 403 cross_site. An Origin naming
server.public_host is now accepted too, and the SI-6 recipe says the proxy
must pass the original Host or set the public host.
Run in a terminal, akou admin set-password waited silently for end of input
and echoed the password as it was typed. It now says to pipe the password in
from a file, and reads nothing.
…r keyless request

key.refused wrote the first 7 characters of any bearer, so a client sending
another service's secret left part of it in akou's log, and every request
with no key wrote a warn line, so a scanner could grow the log without bound.
…age's dead TLS check

The CLI always dials 127.0.0.1, so a server bound to ::1, 127.0.0.2 or one
LAN address started fine and then could not be reached from its own box.
Server mode now binds 127.0.0.1, 0.0.0.0 or :: and refuses anything else at
start. With that rule in apiBind the page's own plain-HTTP refusal could
never fire, so it is gone.
An unknown path was checked as an admin route, so a valid jobs key asking for
GET /v1/jbos was told the route needs admin. It is now checked as needing any
key: no key is still 401 before any 404, and a valid key gets the 404 or 405.
At a tenth of the timings the acceptance's 40 s is 4 s, the threshold plus
one heartbeat; the test allowed 5 s.
Rule 2 says every request needs the token, GETs included, but /healthz and
GET /v1/server answer with none in app mode too; the server-mode note listed
only server-mode changes.
create and revoke each read keys.json, changed it and wrote it back with no
lock, so a create running beside a revoke could write the revoked key back.
Both now run under keys.json.lock and refuse while another edit holds it.
revoke also matched by name, so a key named like another key's id took that
key down with it; it matches the id only, as its usage says.
declare wrote 2 MiB of every large body. Past the drain cap the server
closed on unread bytes, and past Bun's own limit Bun refused at the headers
and closed at once, so the client's write hit EPIPE or ECONNRESET before the
413 arrived: 2 of 6 runs failed on the lane's first head. It now sends one
byte past the drain cap, which the server reads whole, and nothing when the
length is past Bun's limit. 12 of 12 runs pass.
…word changes

A session opened with an admin key stayed valid after the key was revoked,
and a new admin password left every session opened with the old one alive
until a restart. Each admin session now carries a check of the credential
that opened it, run on every use.
@coderabbitai

coderabbitai Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 46 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 3b8f8126-aa44-4356-b7aa-07f8c8549dab

📥 Commits

Reviewing files that changed from the base of the PR and between d85ddfe and 8d318dd.

📒 Files selected for processing (36)
  • docs/DESIGN.md
  • docs/api/openapi.json
  • docs/research/service-interface.md
  • docs/ux/SERVER.md
  • src/core/log/writer.ts
  • src/main/api/access.ts
  • src/main/api/caller.ts
  • src/main/api/guard.ts
  • src/main/api/http.ts
  • src/main/api/keys.ts
  • src/main/api/net.ts
  • src/main/api/routes/server.ts
  • src/main/api/server.ts
  • src/main/cli/args.ts
  • src/main/cli/cli.ts
  • src/main/cli/commands/server.ts
  • src/main/cli/context.ts
  • src/main/config/schema.ts
  • src/main/index.ts
  • src/main/server/presets.ts
  • src/main/window/page-server.ts
  • src/ui/index.html
  • src/ui/theme.css
  • src/ui/web.ts
  • tests/api-helpers.ts
  • tests/api.e2e.test.ts
  • tests/app.e2e.test.ts
  • tests/cli-registry.test.ts
  • tests/lock.test.ts
  • tests/log-io.test.ts
  • tests/openapi.test.ts
  • tests/scopes.test.ts
  • tests/server-access.e2e.test.ts
  • tests/server-login.e2e.test.ts
  • tests/server-mode.e2e.test.ts
  • tests/ui/server-login.test.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Comment thread src/main/api/keys.ts Dismissed
Put the keys lane's routes into the OpenAPI route table: GET /v1/server
(open) and GET /v1/keys/me (jobs) carry RouteDocs and are in
openapi.json; /healthz stays outside /v1 and the file. The guard's
upload rule reads the route's multipart body spec instead of a second
flag, an unknown path stays UNKNOWN_ROUTE (any key, so 401 before 404),
ApiApp.mode is the method the OpenAPI route calls, the keys and admin
commands carry the flag descriptions and examples the CLI registry
requires, and stdin is read through the one readStdin.
@GeiserX
GeiserX merged commit 778bc20 into main Sep 26, 2026
19 checks passed
GeiserX added a commit that referenced this pull request Sep 26, 2026
Brings in the keys (#43) and jobs (#45) lanes this branch's CI needs: /healthz,
api.bind and server.behind_proxy.

Conflicts: decode.ts takes main's version (#45's superset with maxSamples and
too_long); cli.ts keeps both serve and the server commands; args.ts keeps the
secret-flag refusal beside main's described COMMON flags; ci.yml keeps the
server job beside diarize and executor, all under ci-ok; floors add both
sides' skips; the docs keep this branch's SV-P3, SV-T1 and SI-1 rows and
main's SV-P4, SV-P5, SV-T2, SV-T3 and SI-2 to SI-4 rows.

To pass main's CLI rules: flag descriptions and examples for jobs, serve and
models pull, serve listed as local-only, and parity rows for jobs and serve.
@GeiserX
GeiserX deleted the feat/server-keys branch September 28, 2026 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants