Skip to content

fix(coderd/x/gitsync): verify chat workspace before storing agent git refs - #31494

Merged
deansheather merged 2 commits into
mainfrom
dean/codagt-1420-security-verify-chat-ownership-before-agent-external-auth
Nov 9, 2026
Merged

deansheather merged 2 commits into
mainfrom
dean/codagt-1420-security-verify-chat-ownership-before-agent-external-auth

Conversation

@deansheather

@deansheather deansheather commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

When a workspace agent requests external auth, it can send a chat_id (from CODER_CHAT_ID) so the current git branch is linked to that chat. gitsync.Worker.MarkStale trusted that ID and never checked that the chat belongs to the agent's workspace. As a result, any workspace agent could set the git branch, origin, and PR shown on another user's chat, and the refresh worker would then fetch PR status with that chat owner's git token.

MarkStale now ignores the request and logs a warning unless both the requested chat and the root chat that receives the ref are bound to the calling agent's workspace. Subagent chats are created with their parent's workspace_id (coderd/x/chatd/subagent.go), so their legitimate updates still reach the root chat. The root is checked too because rebinding a chat's workspace does not rebind its existing subagent chats. Both external-auth handlers in coderd/workspaceagents.go pass the chat ID through MarkStale, so this single check covers both.

Tests:

  • TestWorker_MarkStale_WithChatID is now table-driven. A chat on the same workspace is updated. A chat on another workspace, a chat with no workspace, and a missing chat get no update and no event.
  • TestWorker_MarkStale_SubagentChatID_UsesRootChat binds the chats to real workspaces and adds two calls that must not reach the root: one from another workspace using the child's ID, and one from the old workspace through a child after the root is rebound to a new workspace.
  • TestWorker_MarkStale_StripsOriginCredentials binds its chat to the caller's workspace, since that test relied on the missing check.

Refs CODAGT-1420, SEC-856. Affects v2.32.0 onward; backports are needed for every supported release line from 2.32.

Investigation notes
  • Source of the chat ID: agent/agentproc/process.go sets CODER_CHAT_ID in the process environment, and cli/gitaskpass.go sends it as chat_id. Anyone with access to the workspace can set this value.
  • The path without a chat ID already filtered by workspace (GetChatsByWorkspaceIDs); only the chat-ID path skipped the check.
  • Open question from the issue (do child chats inherit workspace_id?): yes, CreateChatWithID in subagent.go sets WorkspaceID: parent.WorkspaceID.
  • Edge case (from review): UpdateChatWorkspaceBinding rebinds only the chat it targets, so a child can stay bound to the old workspace after its root is rebound. Checking only the requested chat let the old workspace write to the rebound root, so the root's workspace is checked too.
  • I did not add a second rejection in the handlers (optional item 3 in the issue). MarkStale is the only consumer of the chat ID, and rejecting there would change the external-auth response for git operations.

🤖 Generated by Coder Agents on behalf of @deansheather.

… refs

MarkStale trusted the chat ID sent by the workspace agent with external
auth requests. Any agent could attach a git branch and origin to another
user's chat, and the refresh worker would then query PR status with the
chat owner's git token.

Ignore the request unless the requested chat is bound to the calling
agent's workspace. The check runs on the requested chat before resolving
its root, so subagent chats (which inherit the parent's workspace) keep
updating the root chat.
@linear-code

linear-code Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

SEC-856

CODAGT-1420

@deansheather
deansheather marked this pull request as ready for review November 9, 2026 10:53
@deansheather
deansheather temporarily deployed to main November 9, 2026 10:54 — with GitHub Actions Inactive
@ethanndickson

Copy link
Copy Markdown
Member

@codex review

@ethanndickson

Copy link
Copy Markdown
Member

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T11:14:39.813234Z 6985cca Manual request
🔒 Security Review ✅ Completed 2026-11-09T11:02:57.570404Z c7ddb1a Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c7ddb1ae5e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/gitsync/worker.go Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: c7ddb1ae5e

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

Rebinding a chat's workspace does not rebind its existing subagent
chats, so a child can stay bound to the old workspace. Checking only
the requested chat let that old workspace write git refs to a root now
bound elsewhere. Verify the resolved root chat as well.
@deansheather
deansheather temporarily deployed to main November 9, 2026 11:12 — with GitHub Actions Inactive
@deansheather
deansheather temporarily deployed to main November 9, 2026 11:12 — with GitHub Actions Inactive
@deansheather

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 6985ccaac2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@deansheather
deansheather merged commit 0399d86 into main Nov 9, 2026
37 of 38 checks passed
@deansheather
deansheather deleted the dean/codagt-1420-security-verify-chat-ownership-before-agent-external-auth branch November 9, 2026 11:38
@deansheather
deansheather temporarily deployed to main November 9, 2026 11:38 — with GitHub Actions Inactive
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 9, 2026

This branch was previously deployed

1 inactive deployment
main — 6985ccaa Deployed Nov 9, 2026 by deansheather via cla #252409
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants