Elffuss BraidHub

kikocis/utopia public

262af8b3828a · 5.8 KiBSourceRawPermalink

0014 · Identity from the person, scope from the token

The problem

Exposing Utopia's tools over MCP raises one question before transport: as whom does the client connect? Neither existing credential fits. The JWT follows a person but is stateless, expires in seven days and cannot be revoked — a client configured in a file on someone else's machine would be reconfigured weekly, and a lost laptop waits for expiry. sources.ingest_token follows a source, is stored in plaintext, never expires and can only push documents in.

Decisions

  1. The token acts as the person. Effective permission = the person's role ∩ the token's scope. A token can only narrow, never widen: a viewer's token ticked write stays read-only. Existing guards (require_kb(kb_id, Role::Viewer), access::kb_role) still receive a User; the audit trail shows a person; deactivating the person kills their tokens; several bases need no extra keys.

  2. Scope still narrows separately, because of the confused deputy. An MCP client is someone else's agent under someone else's system prompt, reading untrusted content from the base — a document saying "run this SQL" or "remember X" may be obeyed with the person's full permissions, and Utopia knows nothing about that client's prompt or its other servers. Full permission means read-only SQL against every production database mounted in every base the person can reach (query_data) and writes to the ledger (remember), all hanging off a string in claude_desktop_config.json. So the default token is read-only and limited to one base; writing must be ticked.

  3. This token is hashed while ingest_token is not. 0002_ingest.sql reasoned that once the database is lost the documents are lost anyway, so hashing buys nothing — true for a token that can only push documents in. A personal token reads production databases outside Utopia through query_data, and that warehouse should not fall with Utopia's database. Different blast radius, different storage. SHA-256 rather than argon2: a high-entropy string does not fear brute force, and a per-row salt would defeat the UNIQUE index on token_hash.

  4. Shape. personal_tokens(id, user_id, name, token_prefix, token_hash, scope, kb_ids, expires_at, last_used_at, revoked_at, created_at): scope defaults to read; kb_ids NULL means every base the person can reach; expires_at NULL means never, but the UI defaults to 90 days and "never" is an explicit choice; last_used_at answers "is this still in use" before revoking. The prefix utp_pat_… against ingest's utp_ tells the kinds apart in logs and config files. user_id cascades on delete, unlike the bare foreign key on audit_events.actor_id: the ledger must outlive the user, the keys must not. Revocation writes revoked_at rather than deleting the row: the revocation is a trace.

  5. Every tool call checks scope. The lesson of 0014_data_source_grants: list filtering only guards what is visible, the mount endpoint is called by id, so the guard sits on both sides. Rather than a handshake check followed by trust for the connection's lifetime, every POST re-runs authentication (revocation and expiry in the SQL WHERE), covers() and require_kb; a token revoked mid-session fails on its next call. Each call writes an audit row (mcp.tool_called, target = the token), so attribution is real.

Dead ends

Revisions

Open questions