Files
coder/codersdk/workspacesdk
Michael SuchaczandEthan Dickson 79e3601535 fix: bound workspace agent responses and stop relaying agent 401s (#30183)
Follow-up to the chat workspace-upload security review (ISSUE-1,
ISSUE-10).

Workspace code can impersonate the workspace agent HTTP API, so coderd
shouldn't trust what comes back from it. This PR fixes two places where
it did.

First, coderd read agent responses without a size limit. The worst case
was the `propose_plan` tool, which the model can call at any time and
which ran `io.ReadAll` on a `ReadFile` body, but `DebugLogs`,
`DebugMagicsock`, `DebugManifest`, `PrometheusMetrics` and
`StopDesktopRecording` did the same. To fix, every agent request in
`workspacesdk` now goes through one limit in `apiRequestWithOptions`: 32
MiB by default, with larger per-request limits for the few endpoints
that legitimately return more (support bundle files, debug logs, desktop
recordings, and `ReadFile` up to the requested limit). Reads past the
limit fail rather than truncate. Error bodies are capped at 64 KiB for
every status, including on the ACP client and `CancelToolCall`. Nothing
legitimate gets close to 32 MiB; the biggest is a desktop screenshot at
around 8 MB.

Second, coderd relayed agent statuses straight to the browser. The
container list and devcontainer delete/recreate handlers passed through
any agent status and response, so an agent answering 401 made the site's
`RequireAuth` interceptor sign the user out. The workspace page lists
containers automatically, so this hit anyone viewing the workspace, not
just the owner. The chat upload handler had the same problem. To fix,
these handlers now turn an agent 401, 407, or anything outside 4xx/5xx
into a 502, with the agent's message in a truncated `Detail`. A real
agent never sends any of those, so every status it does send (403, 404,
409, 500, 503) still passes through unchanged. A 404 on upload still
maps to 409 (outdated agent).

An earlier revision of this PR relayed only an allowlist of expected
statuses with a coderd-written message, but that turned real 500s and
503s into 502s and replaced the agent's own messages. There's no reason
to change the statuses a real agent sends today, so we just block the
ones that can sign the user out.

The composer upload pill still truncates the error message and never
shows `Detail`; that's left for a follow-up.

> [!NOTE]
> Opened by Xum on behalf of @ibetitsmike. Rebased by Coder Agents on
behalf of @ibetitsmike. Updated by Xum on behalf of @ethanndickson.

---------

Co-authored-by: Ethan Dickson <ethanndickson@gmail.com>
2026-11-04 12:15:55 +11:00
..