Files
gabrielvidalandClaude Opus 5.5 c7da0f5008 feat(authz): roles, scoped credentials and one permission table for every route
- authz/catalog.py: 42 named permissions in groups and tiers (person, admin,
  host); authz/roles.py: built-in Operator (the owner), Admin (assignable with
  team mode), Member, Guest, Viewer + custom clones, in roles.json
- a person = role + allow/deny overrides (authz/policy.py); users.json's old
  switches (cli, mcp, createProjects, scratchBash, codeMode,
  computers.enabled) migrate once to exactly what they allowed, backup kept
- authz/scopes.py: what a credential acting for someone keeps (cli,
  worker.read|run, bot, phone) — a scope only narrows
- authz/routes.py: (method, path) -> permission for all 310 /api routes; the
  gate asks Caller.allows() for every caller kind, replacing GUEST_ROUTES,
  CLI_ROUTES and the worker READ/RUN_ROUTES regex lists
- golden route x caller matrix (authz/matrix.golden.txt, test_authz_matrix)
- the claude bot reads the admin app minus people/security/the machine; the
  phone key reads its unread notifications only
- loopback without the host token is logged (loopback_tokenless), refusals
  are perm_denied lines; the hub's own MCP calls send the token
- /api/roles CRUD, /api/me and /api/users carry role/allow/deny/perms
- worker-bound scheduled items and conversation-meta respect the binding

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 17:59:05 +01:00

71 lines
3.0 KiB
Python

"""
A guest's ``ai-agent`` CLI: who a brokered call is, and what it may reach.
A guest's run can't call the hub itself — loopback is the owner, and the
guard blocks the CLI and the hub's port in a scoped run. Instead the guard
(sidecar ``guard.py``) *brokers* a plain ``ai-agent …`` command: it runs the
CLI outside the session with ``AIA_AS_SESSION=<the run's session id>``, and
the CLI sends that as :data:`AS_SESSION_HEADER`. The gate (``core/auth.py``)
then answers the call as the guest that session's live run was launched for
(``meta.runAs``) — never as the owner:
* the header only ever **downgrades**: it is read on a call that is already
the owner (a trusted peer), so naming a session can't win anything a
loopback caller didn't already have;
* the guest must be active, guests not locked, and their ``cli`` switch on
(Settings → Users → <guest> → Access);
* the guest comes through the ``cli`` credential scope (authz/scopes.py) —
narrower than their browser: no settings, devices, sharing, uploads or
answering forms — and the handlers filter by ownership exactly as for their
browser, so the CLI sees their own conversations and granted projects and
nothing else.
"""
from __future__ import annotations
import dataclasses
import re
from core.i18n import tr
from users.caller import OWNER, Caller, guest_from_record
from users.deliver import GUEST_PUBLIC_URL
AS_SESSION_HEADER = "x-aia-as-session"
# The links the CLI prints for a guest open their app, not the owner's.
VIEW_URL_HEADER = "x-aia-view-url"
GUEST_VIEW_URL = GUEST_PUBLIC_URL
_SID_RE = re.compile(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
class Refused(Exception):
def __init__(self, status: int, detail):
super().__init__(str(detail))
self.status = status
self.detail = detail
def session_caller(deps, sid: str) -> Caller:
"""The caller a brokered CLI call for session ``sid`` is: the guest its
run was launched for, else (an owner run) the owner. :class:`Refused`
when the session is unknown or its guest may not use the CLI."""
sid = (sid or "").strip()
if not _SID_RE.match(sid):
raise Refused(400, "bad session id")
meta = deps.meta_store.peek(sid) or {}
if not meta:
raise Refused(403, "unknown session")
uid = meta.get("runAs") or meta.get("owner")
if not uid or uid == OWNER.id:
return OWNER
u = deps.users.active(uid)
if not u:
raise Refused(403, "this guest is disabled")
guest = guest_from_record(u)
if deps.security_state.guests_locked():
raise Refused(403, {"message": tr(guest.lang, "guest access is paused for now"),
"kind": "guests_locked"})
if not guest.cli:
raise Refused(403, {"message": tr(guest.lang, "the ai-agent CLI isn't open to you — "
"the owner can switch it on"),
"kind": "guest_cli"})
return dataclasses.replace(guest, via_session=sid)