Summary
A broken access control flaw in Coder's workspace app provisioning logic allows a privileged template author or external provisioner operator to reassign app agents across workspaces.
By abusing how UpsertWorkspaceApp and insertAgentApp handle app IDs without validating workspace ownership, an attacker can perform unauthorized cross-workspace modifications during Terraform-based environment creation.
Affected context
This vulnerability occurs in Coder deployments that use Terraform-based remote development environment provisioning and rely on UpsertWorkspaceApp and insertAgentApp to manage workspace applications.
It is relevant when:
- Workspaces are created or updated via provisioners using
CompleteJobflows - Template authors or external provisioner operators have elevated configuration or operational access
- The system permits app IDs to be supplied in job completion payloads without strict ownership checks
Exploit preconditions
- Attack surface: Internal service APIs involved in workspace provisioning (
CompleteJob,UpsertWorkspaceApp,insertAgentApp) - Required access: Elevated permissions as a template author or external provisioner operator with the ability to influence
CompleteJobpayloads - Trigger point: Submission of a crafted
CompleteJobpayload that references an app ID from a different workspace, causingUpsertWorkspaceAppto overwrite theagent_idfield - Why exploitation is possible: The authorization layer runs under
dbauthz.AsProvisionerd, and the logic does not verify that an app ID belongs to the workspace being built, enabling cross-workspace agent reassignment on primary-key conflicts
Why this happens
The underlying bug stems from insufficient object-level authorization and ownership validation in workspace app management routines.
Specifically, insertAgentApp trusts the app ID supplied in the provisioner's CompleteJob payload, and UpsertWorkspaceApp overwrites the existing app's agent_id on a primary-key conflict without confirming the app is part of the current workspace.
This behavior maps to CWE-639: Insecure Direct Object Reference, where internal identifiers are accepted and acted upon without validating that the caller is authorized for the referenced resource.
Potential impact
- Cross-workspace reassignment of app agents that may affect execution context or connectivity
- Unauthorized modification of another workspace's configuration and associated runtime behavior
- Potential indirect access to data or services reachable via the reassigned agent
- Disruption of targeted developers' environments and reliability of remote development workflows
General mitigation guidance
Mitigation generally focuses on enforcing strict resource ownership checks and hardening authorization boundaries around provisioning flows.
Recommended defensive patterns include:
- Validating that any app or agent IDs supplied by provisioners belong to the workspace being constructed before performing upsert operations
- Implementing object-level authorization checks in application logic instead of relying solely on broad role-based contexts like
AsProvisionerd - Treating internal identifiers from external systems as untrusted and confirming referential integrity against the workspace scope
- Limiting the effective privileges of template authors and provisioner operators to the workspaces they are intended to manage
- Designing provisioning APIs to avoid side effects on resources outside the current workspace or tenant
How teams detect issues like this
Detection can rely on both runtime and code-level analysis.
Common approaches include:
- Reviewing provisioning and upsert code paths for missing ownership validation when handling IDs such as workspace apps, agents, or similar resources
- Adding logging and monitoring for
CompleteJoboperations that reference app IDs not originally associated with the workspace being built - Implementing database-level or application-level integrity checks that flag cross-workspace references or unexpected changes to
agent_idfields - Using static analysis or automated code review tools to identify direct object references that lack associated authorization or workspace scope checks

