Skip to content

fix: authorize agent conversation reads in the router gate - #743

Merged
pikann merged 4 commits into
masterfrom
fix/634-agent-read-conversation
Nov 13, 2026
Merged

pikann merged 4 commits into
masterfrom
fix/634-agent-read-conversation

Conversation

@pikann

@pikann pikann commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #634

Problem

GET /agents/me/conversations/:id (the read_conversation MCP tool) decided access in the service from the current conversation named by an X-Conversation-ID header: its project, actor and chat-session member. The agent's own identity, which the API key already proves, was not what the decision rested on. Consequences:

  • A Remote Agent (ACP bridge) runs its own MCP server with no PACA_CONVERSATION_ID, so it sends no header and every cross-conversation read returned 404.
  • A task-assigned run has no chat session, so only project-shared conversations were reachable, and only through that header-derived context.
  • The permission check lived in the service, not the router.

Change

  • Router gate. /agents/me/conversations/{id} and .../events now require conversations:read, judged on the roles of the agent the API key proves, over the conversation's own resource: project/<P>/conversation/<C>, or agent/<A> for a global chat. New ConversationResource resolver (middleware/resource_lookup.go) and ConversationByID guard (router/guards.go). A conversation that does not exist resolves to a resource no policy names, so it is refused exactly like one the caller may not read.
  • Service. GetConversationForAgent(conversationID, callerAgentID) no longer takes currentConversationID and does no authorization (the authorizer, WithAuthorizer and the header parsing are removed). It keeps only the data rules the gate cannot see: a deleted conversation is not found, and an owner-private chat is never returned (see below).
  • New IAM attribute conversation.agent_id (schema, attribute loader, scope columns, docs), so a role can be limited to some conversations, for example an agent's own, with a condition.
  • Wired Deps.Conversations (the agent repository) in bootstrap.

Behavior changes

  • An agent without conversations:read now gets 403 on every read, including its own conversations. Roles that should read their own history need the permission, optionally limited with conversation.agent_id (project conversations only; a global chat is authorized as agent/<id>).
  • Owner-private chats are never readable through the agent API, the agent's own included. One agent chats with many members, and nothing the agent sends proves the human it is acting for owns a given chat, so allowing own owner-private reads would let member M1 steer the agent into reading M2's chat (raised in review). Task-assigned runs and other project-shared conversations are readable with the grant, which is the Planner -> Remote Agent result case from [Bug] MCP read_conversation returns 404 for valid current and cross-agent conversation IDs #634.
  • The X-Conversation-ID header is removed from the MCP client and agent-runner. PACA_CONVERSATION_ID stays: it still feeds X-Paca-Conversation-Id and the Paca-Run: commit trailer.

Tests

  • Unit: service data rules, handler plumbing, router coverage and guard tests (both routes are now gated), attribute schema.
  • E2E (apps/e2e/tests/projects/agent-conversation-read.spec.ts, 5 scenarios, run against a rebuilt e2e stack): no grant -> 403; own task run with grant -> 200; owner-private chats (own and a peer's) -> 404; a task-assigned run of another agent -> 200 with the grant, 403 without; a role limited by conversation.agent_id -> own run 200, peer run 403.
  • Not covered: the DB-backed TestIAMAttributeLoaders case for conversation.agent_id is skipped without a database; the e2e condition scenario exercises it instead.

- Updated the GetConversationForAgent method to remove the currentConversationID parameter, simplifying the function signature and its usage across the codebase.
- Removed the currentConversationIDFromHeader function as it is no longer needed.
- Adjusted the ConversationResource middleware to handle conversation lookups by ID, ensuring proper authorization based on the conversation's project or agent context.
- Updated route guards to include conversation authorization, ensuring that conversation access is properly gated.
- Modified tests to reflect the changes in method signatures and removed unnecessary checks related to the current conversation ID.
- Ensured that all relevant routes and handlers are updated to maintain functionality while adhering to the new structure.
@pikann pikann changed the title feat: refactor conversation handling in agent API fix: authorize agent conversation reads in the router gate Nov 13, 2026

@pullfrog pullfrog Bot 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.

Caution

This re-opens the confused-deputy leak that the removed authorizeAgentConversationRead existed to prevent: an agent with conversations:read can now read a different human's owner-private chat whenever that chat belongs to the same agent. Every shipped project role already grants conversations:read project-wide, so the project case needs no extra configuration to be exploited. See the inline comment on agent_service.go.

Reviewed changes

  • Router gate — GET /agents/me/conversations/{conversationId} and its /events sibling now require conversations:read; the new ConversationResource resolves the target to project/<P>/conversation/<C> or agent/<A>, and both routes moved from the open-route list to the IAM resource map.
  • Service — GetConversationForAgent drops currentConversationID, the authorizer and WithAuthorizer, keeping only the deleted-conversation and cross-agent owner-private rules.
  • New IAM attribute — conversation.agent_id added to the schema, the Postgres attribute loader, the scope columns and three authorization docs.
  • Wiring and tests — Deps.Conversations backed by the agent repository in bootstrap; guard, route-coverage, handler and service tests updated.
  • E2E — new feature file plus a five-scenario spec covering no-grant, own read, peer owner-private, task-assigned run, and a conversation.agent_id-conditioned role.

ℹ️ Global agents on the default role lose even their own-conversation reads

Global agents are created with the default USER role (bootstrap/seed_roles.go:74-82), whose policy grants only users:read, and no seed or migration adds conversations:read for them. Because this PR now requires the permission for every read, including the agent's own current conversation, every existing global agent on the default role will start returning 403 for read_conversation after upgrade until an admin creates and attaches a role. The PR description states the rule change, but the repo carries no upgrade note or default-role adjustment for it.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot 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.

Important

Denying every owner-private read closes the leak from the previous review, but it also denies the run's own current conversation, which two shipped consumers read without ever naming a chat the model picked. Details inline on agent_service.go; the rest of the delta (header removal, docs, e2e rework) looks right.

Reviewed changes

  • Denied all owner-private reads — GetConversationForAgent now returns 404 for every owner_private conversation, the caller's own included, closing the same-agent / different-human leak raised in the prior review.
  • Removed the soft-deleted-but-running exemption — a soft-deleted conversation is always not found, and the test state the delete path could not produce is gone.
  • Retired the dead X-Conversation-ID plumbing — the MCP conversation client no longer sends the header, the PacaConfig.conversationId doc and the agent-runner comment no longer claim it authorizes reads, and PACA_CONVERSATION_ID stays for X-Paca-Conversation-Id and the commit trailer.
  • Clarified conversation.agent_id — both authorization docs now scope the condition to project conversations and note that global chats are authorized as agent/<id> with no conversation attributes.
  • Reworked the E2E scenarios around task runs — the own-read and conversation.agent_id cases assign tasks, and a new case asserts the agent's own chat returns 404.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot 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.

Important

The owner-mention rewiring is right for ephemeral sandboxes, but PACA_OWNER_MEMBER_ID is conversation-scoped and is not one of the keys static environments reconcile on later attaches, so a reused static environment keeps a stale (or missing) owner. Details inline.

Reviewed changes

  • Rewired ask_user_question's default owner mention to a PACA_OWNER_MEMBER_ID supplied by agent-runner instead of reading the current (owner-private) conversation.
  • Forwarded trigger.ActorMemberID as that env var in the built-in Paca MCP server's environment, with a unit test pinning both presence and absence.
  • Corrected the read_conversation description to state that chats, including the current and attached ones, cannot be read.
  • Dropped the stale PACA_CONVERSATION_ID-authorization comment from executor_test.go.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot 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.

✅ No new issues found.

Reviewed changes

  • Reconciled PACA_OWNER_MEMBER_ID on static environments — added the key to pacaInfraEnvKeys in both sandbox/docker and sandbox/k8s, with a test in each package, closing the stale/missing-owner gap raised in the previous review. Both ensureEnvironmentInfraEnv implementations already delete a key whose cfg.Env value is empty, so actor-less triggers clear a stale owner.
  • Removed the now-dead getCurrentConversation() — PacaAPIConversationClient only keeps getConversation, which read_conversation still uses.
  • Tidied the mention helper — renamed to conversationOwnerMention and refreshed its doc comment and the related test names to match the PACA_OWNER_MEMBER_ID source.

I ran go test ./internal/sandbox/... ./internal/executor/ in services/agent-runner and the ask-tools/conversation-client vitest files in apps/mcp; all pass.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pikann
pikann merged commit 5d8c3f5 into master Nov 13, 2026
12 checks passed
@pikann
pikann deleted the fix/634-agent-read-conversation branch November 13, 2026 18:29
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.

[Bug] MCP read_conversation returns 404 for valid current and cross-agent conversation IDs

1 participant