// CONTENTS Part 09 · Security and safety

// PART 09

Authentication and authorization

Chapter 38 / 56 3 min read English Part 09 of 14

Authentication Flow

User registration

  1. A registration request reaches the directory service (Zitadel).
  2. The directory creates the user account and returns the user id.
  3. botserver creates the local user reference (the id the platform uses everywhere else).
  4. A session is established; subsequent requests present a token.

User login

  1. The client is sent to the login surface (port 5000 — the only domain that serves login/signup).
  2. Credentials are validated by the directory; cloud login additionally verifies Zitadel v2 sessions (or the dev admin-credentials fallback with a matching password) before any token is issued.
  3. A JWT is issued — sub is the directory user id, HMAC-signed with the SaaS secret (persisted in directory_config.json so tokens survive restarts).
  4. The client presents the token; the chat page captures ?token= from a login redirect into localStorage and sends it on the socket.

Accepted Credentials (and what is not a credential)

The auth middleware accepts exactly three identity sources:

Source Verified how
Authorization: Bearer <JWT> HS256 signature + exp (SaaS secret or local JWT key)
Authorization: Bearer <gb_…> session token Session cache / login_sessions row
Cookie: <session> Same session store

X-User-ID is not an identity (#2122). The header is caller-supplied, so the middleware only logs it for correlation and never resolves it into a user; X-Bot-ID narrows scope, it does not authenticate. Internal service calls (chat api.exec, VIBE RUN) present a short-lived signed assertion instead, and CSRF is skipped only for that verified service identity — never because a header happens to be non-empty.

Reverse proxy: strip X-User-ID, X-Internal-Token and X-Forwarded-Host from external requests at the edge. The OS panel derives loopback from the real Host header only, so a forwarded-host override cannot open it.

WebSocket identity

The chat socket resolves its own identity in ws/handler.rs, in order: a token (cookie gb-access-token, then the access_token/token query field) resolved by anonymous_auth::resolve_token_user; then the user_id query value (the id /api/auth gave the page); then a new guest (guest_<id>@anonymous.local). Each handshake logs which of the three it used — when a turn runs as a guest, every per-user lookup does too, so check that log line before debugging “missing” data.

Anonymous access

src/main_module/routes/anonymous_auth.rs defines the paths that need no token: the WebSocket route, health, the public catalog and channel webhooks. Everything else is denied. Webhook routes pair the anonymous path with their own platform credential (see Webhooks and delivery).

CSRF Signing Secret (#2141)

The double-submit CSRF cookie is only as strong as the HMAC secret behind it. There is no built-in default: a public constant would let anyone who has read the repository mint a valid csrf_token/X-CSRF-Token pair.

Resolution order, evaluated once at boot (botsecurity-auth/src/csrf/secret.rs):

Source When Notes
CSRF_SECRET (env) first choice required for multi-instance deployments
Vault secret/gbo/system/security csrf_secret when the env var is absent canonical for a deployed node
ephemeral random development only 32 bytes per process, never persisted
refuse to start production without env/Vault fail closed

Rotation: write the new value (≥ 32 bytes, e.g. openssl rand -base64 48), restart botserver — the boot read is the rotation boundary — and run every instance on the same new secret before draining old ones.

Authorization in one line

Authentication answers who; authorization is the RBAC layer (roles, groups, admin_only command gating) described in RBAC and secrets.

Found a mistake? Open an issue · All books

// SEARCH ESC
Pagefind · 236 pages · 4 books Enter to open ↑ ↓ to move