Skip to content

feat(gateway): role authentication through a roles/v1 extension - #874

Open
earakely-scale wants to merge 2 commits into
mainfrom
edgararakelyan/gateway-roles-v1
Open

earakely-scale wants to merge 2 commits into
mainfrom
edgararakelyan/gateway-roles-v1

Conversation

@earakely-scale

@earakely-scale earakely-scale commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

What and why

Today a caller can act as any role on an env gateway by naming it in the AgentEnv-Role header: on /step, the MCP endpoint, /svc, /website and /browser. So a client that can reach the gateway can read and write as another persona.

This adds role authentication to the gateway. A role declared with token auth answers only to a member's agent token (sent as AgentEnv-Agent-Token) or the env control token. Undeclared roles and roles declared with none auth work exactly as today, so nothing changes until someone declares a token role.

This is the gateway half. Deploy-side support (minting agent tokens, declaring roles and adding members) comes in follow-up framework PRs.

Behaviour

  • roles/v1 extension: control-token only, and advertised on the card only by a gateway that has a control token.
    • POST /roles {role, auth: {type: none|token}}, GET /roles, DELETE /roles {role}.
    • POST /roles/members {role, agent_token} returns {member_id, auth}; DELETE /roles/members {role, member_id}.
    • Create is idempotent, and a conflicting create gets 409 with the existing auth. Both DELETEs are idempotent.
    • The store keeps token digests only. GET /roles returns member ids, never a token or digest.
  • Resolution: once per HTTP and WebSocket request.
    • The env control token is never refused; with no role header it resolves to default.
    • A token with no role header resolves to the agent's only role, else default.
    • An unrecognised token counts as no token.
    • Role names match case-insensitively, so a protected brooke@… can't be reached as BROOKE@….
  • Refusals (403, {ok: false, error: "refused: …"}): only on routes that act as a role.
    • These are the MCP path, /step, /svc/*, /website/* (WebSocket included) and /browser/*.
    • MCP sessions are rechecked on every tool call, so protecting a role or removing a member applies to sessions already open.
    • /state, the card, /trajectory, /changes, /clock/*, /triggers/*, /notifications/*, /user_chat/*, /tools/* and /roles never take a role.
    • A missing control token on a control route still answers 401.
  • Startup roles: GATEWAY_ROLES is a JSON list of {role, auth: {type}}. These come back after a restart; runtime members don't.
  • Computers: COMPUTER_TOKEN_DIGESTS (name=sha256hex,…) at startup. When the variable is present, even empty, AgentEnv-Computer counts only with a matching AgentEnv-Computer-Token (compared with hmac.compare_digest). Without it, an unauthenticated computer header is honoured only while the driver's role is open.
  • Header stripping: AgentEnv-Agent-Token, AgentEnv-Computer and AgentEnv-Computer-Token are stripped before anything is proxied. /svc and /website get the resolved role stamped.
  • Trajectory: every event that names a role records role_auth: agent_token, control_token, computer_token, header or default. /changes actors carry it too.
  • Secrets: no token or digest reaches logs, error bodies, state or trajectories. An unknown token is logged once by digest prefix.

Notes for reviewers

  • Trigger events: role_auth is not added inside the provoking dict of trigger_fired events. That would change the triggers wire shape that existing tests and consumers pin. The provoking call's own tool_call / http_request event already records it.
  • Case-insensitive matching: only auth decisions are case-insensitive. Role rules, notifications and triggers still match the header's spelling, as on main.
  • Restarts: runtime roles and members live in gateway memory, so a restart drops them, while startup roles come back. This is documented in AGENTS.md.
  • SQL console: /svc/<SQL console> is not a role-acting route.

How it was tested

  • Unit: pytest tst/unit/ packages/agentenv-protocol/tests/ -n auto.
    • On this branch: 18362 passed, 5 failed (macOS environment tests: computer_exec_test system commands and test_disk_recipe's Windows install).
    • On main: 18209 passed, 6 failed (the same five plus a clock-panel timing test). So there are no new failures.
    • Gateway subset: 1478 passed. ruff check is clean, and check_plugin_api.py --base origin/main reports no break.
  • Integration (local compose stack, gateway image built from this branch):
    • tst/integration/env/gateway/gateway_role_auth_test.py: 2 passed. A persona made token-auth refuses a header switch (in any case), a member token plays it, an open role still works, and an MCP client with a member token lists and calls tools.
    • tst/integration/computer/computer_triggers_test.py::test_a_computers_browser_requests_take_the_role_driving_it_now: 1 passed.

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; the previous recording issue is fixed and no new blocking issue was found.

Summary

Adds gateway role authentication through roles/v1, with token membership checks and authentication details in recorded events.

  • Token-protected roles require a member token or the environment control token.
  • Configured computer tokens verify which computer is acting for a role.
  • Role records show how the gateway established each caller’s identity.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Incoming request] --> B[Resolve role and credentials]
  B --> C{Role-acting route?}
  C -->|No| E[Serve route]
  C -->|Yes| D{Role allowed?}
  D -->|No| F[Refuse request]
  D -->|Yes| E
  E --> G[MCP tool call]
  G --> H[Recheck current membership]
  H --> I[Refresh recorded authentication source]
  I --> J[Run tool with temporary caller]
Loading

Reviews (2) · Last reviewed commit: "fix(gateway): a session whose token join..." · Reviewed by Greptile

A caller could act as any role by naming it in the AgentEnv-Role header.
The gateway now resolves each request's role once and refuses a role that
was declared with token auth unless the caller presents a member's agent
token (or the env control token).

- roles/v1 (control-token only, advertised only on a gateway with a control
  token): POST/GET/DELETE /roles and POST/DELETE /roles/members, fixed paths
  with the role in the JSON body. The store keeps token digests only.
- Resolution on every HTTP and WebSocket request; refusals (403) only on
  role-acting routes: the MCP path (rechecked on every tool call), /step,
  /svc/*, /website/* and /browser/*. Role names match case-insensitively.
- Startup roles from GATEWAY_ROLES; computer token digests from
  COMPUTER_TOKEN_DIGESTS, with AgentEnv-Computer honoured only alongside
  that computer's token when the variable is present.
- Identity headers are stripped before anything is proxied, and every
  trajectory event that names a role records how it was established
  (role_auth).
- No token or digest reaches logs, errors, state or trajectories.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale
earakely-scale requested a review from a team as a code owner November 22, 2026 08:03
Comment thread src/agent_env/env/gateway/gateway.py Resolved
…t_token

An MCP session keeps the role, source and agent token digest its initialize
resolved to. A session opened on an open role with a token that was not yet
a member kept source "header", so once the role was protected and the token
added, its tool calls passed the membership check but still recorded
role_auth "header".

Each tools/call now derives the source from how it is authorized at that
call (Roles.source_now) and binds it for the call: agent_token while the
token is a member, header once the membership it was established by has
ended on an open role, and the control and computer tokens unchanged. Only
the digest is consulted; no token is stored.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant