Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ADR 0009 — User gateway: sessions, per-request ptok, trust boundary

Status: accepted · 2026-10-20

Context

People use WYRM through a browser or app, not with an Ed25519 key or a client certificate (wiki “Користувачі — gateway чи mTLS”). Links already authorize every ~set/~act by ptok (spec 04). The gateway must turn a password login into such tokens without becoming a master key for the whole broker, and without leaking long-lived credentials into URLs or logs.

Decision

  • Hosting. crates/wyrm-gateway runs inside wyrmd when wyrmd.json5 has a gateway section (GatewaySection: listen, tls_cert/tls_key, session_ttl_s clamped to 60 s … 7 d, admin_user default admin). TLS (rustls) is required off loopback, checked in both Config::from_json5 and Gateway::open. State lives in data_dir/gateway/: hmac.key, users/ (wyrm-state WAL + snapshot, ADR 0003) and gateway.key (Ed25519, 0600). The Gateway survives broker rebuilds; its HTTP/WS server is per broker (ADR 0006) and talks to Zenoh through the in-process session.
  • API. Canonical CBOR bodies and frames: POST /api/login, GET /api/me, POST /api/ws-ticket, GET /api/ws, and user management under /api/users for users with config on wy/<site>/sys/**.
  • Users. argon2id PHC hashes (hashed before the WAL sees the operation), passwords 8–1024 bytes, names [A-Za-z0-9._-]{1,64}. Unknown names run a dummy verify of the same cost. At most 4 argon2 runs at once.
  • Sessions. Stateless token base64url(0x01 ‖ cbor{sub, roles, exp, iat, jti, gen} ‖ HMAC-SHA256); the MAC key is derived from hmac.key (HMAC(key, "wyrm-gateway/session/v1")), never the key itself. Default lifetime 8 h. On every request the token is resolved against the store: the user must exist, be enabled and have the same gen; grants come from the user’s roles now, not from the token.
  • Generation counter is store-wide. UserDb.next_gen only grows and survives RemoveUser. Creation, password change and disable take a new value, so they end older sessions, including those of an earlier user with the same name: a re-created name never revives a session.
  • Per-request ptok. Gateway::tokens_for signs one token per request with gateway.key: subject user:<name>, the user’s grants and max permission, scoped to the request key, 5 min lifetime. The gateway checks grants itself before sending (list/meta/children ≥ list, subscribe ≥ read on part of the pattern, set ≥ write, invoke ≥ read with the action level left to the responder) and drops samples and children the user may not see. Refusals are audited as Denied.
  • Trust boundary. Links (ControlConfig.link_keys) and the flow engine trust the gateway key. Amended 2026-10-22 (owner decision): the admin trusts it too (ControlConfig.control_keys), so the control plane (sys/**: link install, start/stop, update, secrets, quarantine, profiles) is reachable from the app. The token of every request carries the user’s grants, so only roles with config on wy/<site>/sys/** (the default admin role) pass; viewer, operator and engineer get permission_denied. Each action is audited with user:<name>. Test: gateway_in_wyrmd. Gateway user management is local to the gateway store, not an admin operation.
  • WebSocket tickets. POST /api/ws-ticket (bearer session) returns a random 128-bit ticket, single use, 30 s; GET /api/ws?ticket=… spends it and re-resolves the session. The long-lived session never appears in a URL. At most 8 live tickets per user; a new one replaces that user’s oldest, so one user cannot crowd out the others. Authorization: Bearer is also accepted for non-browser clients. Open sockets re-read the user every 15 s and close at session expiry.
  • Role grants at runtime (2026-10-22, owner decision, as DSA updatePermissions/getPermissions). sys/~act/update_permissions {role, path, level} (config) adds a grant, changes its level, or at level none removes it; a new role starts empty. The stored list of a role (users store Op::SetRole, SCHEMA 3) replaces the configured role of the same name. A new grant goes before the first grant of that role whose key expression includes it, so a narrower grant is not shadowed (first match wins). A change that leaves no enabled user with config on wy/<site>/sys/** is refused. Sessions see the change on their next request (grants are resolved per request). sys/~act/get_permissions (config) lists the role grants and the generated router ACL, one row per subject and rule. Every change is audited (update_permissions). Tests: grant_edits_keep_narrow_grants_first, role_grants_change_persist_and_keep_an_admin, gateway_in_wyrmd.
  • First admin. At start, if WYRM_GATEWAY_ADMIN_PASSWORD is set and admin_user does not exist, that user is created with the admin role. The variable is removed from the process environment right after reading, and the supervisor starts link processes with a cleared environment (env_clear plus RUST_LOG, RUST_BACKTRACE, TZ, LANG, LC_ALL, PATH). The password never comes from wyrmd.json5.
  • Login rate limit. At most 5 failed logins per (user, ip) and 20 per ip over all user names in a sliding 60 s window; a success clears the (user, ip) record; at 10 000 records expired ones go first, then the one with the oldest last failure.
  • Audit. AuditEvents (LoginOk, LoginFailed{reason}, UserChanged, Denied) go into the admin journal through Control::record: logins and refusals with subject user:<name>, user changes with the acting user’s name as subject. They never carry passwords or tokens.

Consequences

  • A stolen gateway key gives write/config on devices, flows and (since the 2026-10-22 amendment) the control plane of this site, not on other sites (their links do not trust it). The gateway runs inside wyrmd with the same data_dir, so its key is as exposed as the broker itself.
  • Role changes take effect on the next request without a new login; sessions can only be ended by password change, disable or removal (no logout list).
  • The default roles grant nothing on sys/** except the admin role’s config, which governs gateway user management and the control plane.
  • Flow deploys through the WebSocket get their proofs from the gateway (ADR 0008): for invoke wy/<site>/dataflows deploy, the gateway replaces any client proofs with one token per command target, signed with that user’s grants. A user without rights on a target gets no proof, and the engine refuses the deploy.
  • Spraying from many addresses is bounded only by the argon2 cost (4 concurrent hashes).
  • A broker rebuild (ADR 0006) closes all WebSockets; sessions and tickets stay valid.

Alternatives

  • Users with client certificates (mTLS) and an operator CN each. Rejected for people: certificate handling in browsers and phones; kept for engineering tools (operators).
  • The admin trusts the gateway key. First rejected (a web-facing process would hold the control plane); adopted on 2026-10-22 by owner decision, with the role grants as the limit.
  • Server-side session table. Rejected: the store-wide gen gives revocation without per-session state.
  • Session token in the WebSocket URL. Rejected: URLs end up in proxy and browser logs; replaced by single-use tickets.