Skip to content

fix(security): X-Source-Agent is authoritative only for a principal that can prove it names itself (abilityai/trinity-enterprise#614) - #2788

Merged
vybe merged 2 commits into
devfrom
feature/614-source-agent-attribution
Sep 14, 2026
Merged

vybe merged 2 commits into
devfrom
feature/614-source-agent-attribution

Conversation

@webmixgamer

@webmixgamer webmixgamer commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • X-Source-Agent is a raw client header, and its only check (SELF-EXEC-001) was gated on current_user.agent_name — so it fired only for the agent-scoped keys that cannot lie anyway, never for a human — and its single caller was /task, leaving /chat and /fan-out unchecked. A permitted user could record their own action as performed by any agent they named, with their identity dropped and the hash chain certifying the row, and could forge an AGENT_COLLABORATION activity plus a WebSocket edge on an agent they cannot access.
  • This is the third occurrence of the class (bug: 'Continue as Chat' (EXEC-023) broken or unreliable across use cases — test and validate the full matrix #1672 gated a resume IDOR on the header's absence; ent#265 chose a provenance arm on its presence), so the repair is the gate, not a fourth endpoint patch: one helper in the refactor(auth): consolidate duplicated auth/dependency patterns behind factories (INV-8, split from #654) #1310 imperative family, every reader routed through it, and a static guard so a route added tomorrow inherits the rule.
  • The header is now honoured only when the principal can prove it names itself: an agent-scoped key naming its own agent, or the EVT-001 loopback carrying a source the backend itself derived. Every other principal gets a named 403.

Changes

The gate — dependencies.resolve_source_agent (allowlist over identity, so a scope invented later is refused by construction; the falsy check precedes any getattr, and the getattr default is the unprivileged one).

The six readers — /chat, /task and /fan-out rebind the parameter so the raw value is unreachable downstream. loops, reminders and the manual schedule trigger resolve first and then prefer the validated key, because the MCP client sends no header on those paths (a header-only resolution would have wiped the origin of every agent-created reminder). The resolve call is never placed behind an or — that short-circuit skips the check for an agent principal and silently ignores a mismatched header, which is the failure mode this fix exists to end.

The event loopback — proves itself with a scope=event_loopback claim in its SECRET_KEY-signed JWT (narrower than INTERNAL_API_SECRET, which the scheduler and MCP server also hold), surfaced as User.vouched_source_agent and fenced in get_current_user to POST /api/agents/{name}/task; it was previously an unrestricted five-minute admin bearer. It vouches only for agent-originated events — emit_event writes a human caller's username into the event's source, and emit_event_for_agent is gated on read access, so neither may be certified as an agent.

Audit rows — shape unchanged, and the agent branch gains the key owner's actor_email plus, on the fan-out replay row, the presented credential. An agent row now joins back to a person, which the issue's walkthrough called out as missing.

MCP server — tools/schedules.ts gates the header on scope === "agent" like tools/chat.ts; it was the one tool sending it for system-scoped keys too.

Behaviour changes

Before After
Non-agent principal sends the header Silently trusted
Agent key names a sibling Trusted on 5 routes
Agent-started loop source_agent_name NULL
Human-emitted event wakes a subscriber Subscriber's execution attributed to a phantom agent
System key triggers a schedule source_agent_name from the header

Deploy note: the backend refusal and the MCP tool change ship together in one compose build. A partial update that moves the backend alone would 403 trigger_agent_schedule from a system-scoped key until the MCP image is rebuilt.

Test Plan

  • New tests pass: cd tests && pytest unit/test_ent614_source_agent_attribution.py -v — 92 tests: the helper matrix over eight principal kinds, the three dispatch routes end-to-end, the three attribution-only routes, the loopback JWT round-trip and its route fence, trigger_subscription's vouching in both directions, and the AST guard with self-tests that judge it in both directions.
  • Existing tests unaffected: full unit island 15707 passed, 6 failed — all six are this clone's recorded environmental failures (one mounted-submodule guard, five order-dependent in an unrelated file that pass in isolation), none in a touched file.
  • Manual verification on a live instance: admin token + forged header → 403; same call without the header → 200; agent key naming itself → 200; agent key naming a sibling → 403. The resulting audit rows show actor_type=agent with the owner's email on the agent branch, and actor_type=user on the human branch.

Docs

architecture/security.md (the rule), architecture/execution.md (the loopback), the IA-01 trust-boundary note (its one false-positive source is closed), five feature flows, AGENT_NETWORK_DEMO.md (its curl recipe taught the exact forbidden shape), and a learnings.md entry for the class.

Journey Impact: none — trust-boundary fix on existing endpoints; J10 drives agent-scoped keys and is unaffected.

Fixes abilityai/trinity-enterprise#614

Reviewer note on that reference: it is deliberately fully qualified, and the bare same-repo form must not be substituted for it. Public abilityai/trinity#614 is an unrelated merged frontend refactor, and issue-status-on-merge.yml matches a closing keyword followed directly by a plain number — so the unqualified spelling would stamp status-in-dev on that item while leaving the real issue stranded. The qualified form deliberately does not match that pattern. Since both the closing keyword and the promotion workflow are same-repo only, this PR neither closes nor relabels the private issue: set status-in-dev on it by hand after merge, and close it at the release cut.

🤖 Generated with Claude Code

webmixgamer and others added 2 commits September 14, 2026 16:31
…hat can prove it names itself (Abilityai/trinity-enterprise#614)

`X-Source-Agent` is a raw client header that decides `triggered_by='agent'`, the
AGENT_COLLABORATION activity and its WebSocket edge, the execution row's
`source_agent_name`, and the actor on three SEC-001 audit sites. Its only check —
SELF-EXEC-001 — was gated on `current_user.agent_name`, so it fired only for the
agent-scoped keys that cannot lie anyway and never for a human, and its single
caller was `/task`, leaving `/chat` and `/fan-out` unchecked. A permitted user
could therefore record their own action as performed by any agent they named,
with their identity dropped and the hash chain certifying the row, and could
forge collaboration activity on an agent they cannot access.

Third occurrence of this class (#1672, ent#265), so the repair is the gate rather
than a fourth endpoint patch:

- `dependencies.resolve_source_agent` honours the header only for an agent-scoped
  key naming its own agent or the event loopback's backend-vouched source, and
  refuses every other principal with a named 403. Allowlist over identity, so a
  scope invented later is refused by construction.
- All six readers route through it. `/chat`, `/task` and `/fan-out` rebind the
  parameter so the raw value is unreachable downstream; loops, reminders and the
  manual schedule trigger resolve first and then prefer the validated key, since
  the MCP client sends no header on those paths. Resolving is never placed behind
  an `or` — that short-circuit would silently ignore a mismatched header.
- The EVT-001 loopback proves itself with a `scope=event_loopback` claim in its
  SECRET_KEY-signed JWT, surfaced as `User.vouched_source_agent` and fenced to
  `POST /api/agents/{name}/task`; it was an unrestricted five-minute admin bearer.
  It vouches only for agent-originated events: `emit_event` writes a human's
  username into the event's source, and `emit_event_for_agent` is gated on read
  access, so neither may be certified as an agent.
- Audit rows keep their shape and the agent branch gains the key owner's email
  plus, on fan-out replay, the presented credential — an agent row now joins back
  to a person.
- The MCP schedules tool gates the header on agent scope like the chat tool.

Behaviour changes: a non-agent principal sending the header now gets a 403 where
it was silently trusted; an agent key naming a sibling gets a 403 on five routes;
agent-started loops now record their origin; a human-emitted event no longer
attributes its subscriber's execution to a phantom agent.

Verified on a live instance: the reported exploit returns 403, the same call
without the header is unchanged, an agent key naming itself is honoured, a
sibling name is refused, and the resulting audit rows carry the agent together
with its owner's email.

Docs: the rule in security.md, the loopback in execution.md, IA-01's trust-boundary
note closed, five feature flows, the demo recipe that taught the forbidden shape,
and a learnings entry for the class.

Refs Abilityai/trinity-enterprise#614

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Resolves the PR's conflict. One real conflict, in `docs/memory/learnings.md`:
both sides appended a 2026-09-14 entry at the tail of the append-only ledger, so
both are kept, dev's first since it landed first.

`security.md`, `models.py` and `tests/registry.json` auto-merged and were
verified rather than assumed: dev's `models.py` hunk adds fields to a different
model, `registry.json` still parses with both entries and no duplicate, and the
ent#614 paragraph in `security.md` is intact. No migrations in the incoming
range, so the merge applies no schema change (backup taken regardless).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@webmixgamer
webmixgamer requested a review from dolho September 14, 2026 16:32

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validated via /validate-pr (Lane C) and /cso --diff: zero findings at the gate, 404 touched tests pass locally at head, all 25 CI checks green. One latent appendix item (loopback principal's actor_email on the agent audit branch is the admin user, unreachable today because the loopback sends no Idempotency-Key) — follow-up, not a blocker. Report: docs/security-reports/cso-diff-2026-09-14-1702-source-agent-attribution.md

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.

2 participants