Files
gabrielvidalandClaude Opus 5.5 36528b2953 fix(plugins): leftovers of the plugin migration — read-only plugin reads, conversation.started, a clean OpenAPI dump
- `GET /api/plugins/{pid}/actions/{action}?payload=` runs an action the
  manifest marks `readOnly`; a read-only account (the screenshot bot) may only
  use safe methods, so its plugin frames send actions as GETs and plugin pages
  (Plans, Approvals, Forms) show their data instead of "method not allowed".
- Core now fires the `conversation.started` hub event it already accepted from
  manifests (prompt ≤ 4,000 chars, projects, worker, owner, origin) — Plans
  links the run its *Implement* opens.
- The plugin conversation bridge's rows carry `worker` ("local" = the lab), so
  Approvals can refuse requests from another machine again.
- The catch-all `/proxy/tools/plugins/{pid}/proxy/{path}` is out of the OpenAPI
  spec (six methods, one operation id): `frontend/openapi.json` is a full
  re-dump again and `src/generated/` is regenerated from it.
- `projects.create` and the `createProjects` switch are retired (New project is
  the `new-project` plugin's grant); the carry-over reads the old permission
  off the raw record. Mocks and the roles catalog dump follow.
- The worker outbox mirror loses the `frm` answer branch and the `secrets`
  hook the retired form engine used; its docstring no longer names /api/forms.

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

116 lines
4.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""What a person may do: their role, plus their overrides.
``effective(user)`` = the role's permissions ∪ ``allow`` − ``deny`` (the
overrides only ever name person-tier permissions) ∪ the core ones. This is the
one place a ``users.json`` record turns into permissions; the gate, the
handlers and the run's scope all read it through ``Caller.perms``.
The switches the Access tab had before roles (``cli``, ``mcp``,
``scratchBash``, ``codeMode``, ``computers.enabled``) are permissions now
(``createProjects`` is gone: New project is the ``new-project`` plugin's
managed grant). :data:`SWITCHES` maps them: :func:`migrate` turns a record's
old switches into overrides once, and :func:`set_switch` lets the API keep
accepting them (an old client, a test) — always as an override relative to
the person's role, never as a stored boolean.
"""
from __future__ import annotations
from authz import catalog as cat
from authz import roles
# users.json switch → (permission, its value when the key was unset)
SWITCHES: dict[str, tuple[str, bool]] = {
"cli": ("cli.use", False),
"mcp": ("mcp.connect", False),
"scratchBash": ("runs.scratch.bash", True),
"codeMode": ("runs.codemode", True),
}
COMPUTERS_PERM = "computers.pair"
# Switches with no permission left: dropped from a record as it migrates.
RETIRED_SWITCHES = ("createProjects",)
MARK = "authz" # users.json: the record's switches became overrides
def role_of(user: dict | None) -> str:
rid = (user or {}).get("role")
return rid if roles.store().assignable(rid) else roles.DEFAULT_ROLE
def effective(user: dict | None) -> frozenset[str]:
"""A person's permissions. No record is a bare guest (the role's
defaults); a record the store hasn't migrated yet (a hand-made dict, an
old copy) is read through its old switches."""
user = user or {}
if not user.get(MARK) and any(k in user for k in (*SWITCHES, "computers")):
user = dict(user, computers=dict(user.get("computers") or {}))
migrate(user)
base = roles.store().perms(role_of(user))
allow = set(user.get("allow") or ()) & cat.PERSON_PERMS
deny = set(user.get("deny") or ()) & cat.PERSON_PERMS - cat.CORE
return frozenset((base | allow) - deny)
def has(user: dict | None, perm: str) -> bool:
return perm in effective(user)
def overrides_for(role_perms: frozenset[str], want: dict[str, bool],
allow=(), deny=()) -> tuple[list[str], list[str]]:
"""``allow`` / ``deny`` after setting each ``perm: on`` of ``want``,
relative to ``role_perms``: an override only where it differs."""
a, d = set(allow or ()), set(deny or ())
for perm, on in want.items():
a.discard(perm)
d.discard(perm)
if on and perm not in role_perms:
a.add(perm)
elif not on and perm in role_perms:
d.add(perm)
return cat.clean(a, cat.PERSON_PERMS), cat.clean(d, cat.PERSON_PERMS - cat.CORE)
def set_switch(user: dict, want: dict[str, bool]) -> dict:
"""The ``allow`` / ``deny`` fields that make ``want`` (perm → on) true
for ``user`` under their current role."""
allow, deny = overrides_for(roles.store().perms(role_of(user)), want,
user.get("allow"), user.get("deny"))
return {"allow": allow, "deny": deny}
def legacy_switches(user: dict) -> dict[str, bool]:
"""A pre-roles record's switches, as perm → on (unset keys at their old
defaults)."""
out = {}
for key, (perm, default) in SWITCHES.items():
v = user.get(key)
out[perm] = default if v is None else bool(v)
out[COMPUTERS_PERM] = bool((user.get("computers") or {}).get("enabled"))
return out
def migrate(user: dict) -> bool:
"""Turn a pre-roles record's switches into a role + overrides, in place,
so that its effective permissions are exactly what the switches allowed.
False when it was done already."""
if user.get(MARK):
return False
if not roles.store().assignable(user.get("role")):
user["role"] = roles.DEFAULT_ROLE
allow, deny = overrides_for(roles.store().perms(user["role"]),
legacy_switches(user), user.get("allow"), user.get("deny"))
user["allow"], user["deny"] = allow, deny
for key in (*SWITCHES, *RETIRED_SWITCHES):
user.pop(key, None)
if isinstance(user.get("computers"), dict):
user["computers"] = {k: v for k, v in user["computers"].items() if k != "enabled"}
user[MARK] = 1
return True
def switches_view(user: dict) -> dict:
"""The old switches, read off the permissions — what the admin page and
``/api/me`` still show."""
perms = effective(user)
return {key: perm in perms for key, (perm, _) in SWITCHES.items()}