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>
128 lines
3.2 KiB
Go
128 lines
3.2 KiB
Go
package workspacesdk
|
|
|
|
import (
|
|
"io"
|
|
"math"
|
|
neturl "net/url"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAgentAPIPath(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("encodes reserved query characters", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
path := "/tmp/a&b ?#%c.md"
|
|
got := agentAPIPath("/api/v0/resolve-path", neturl.Values{
|
|
"path": []string{path},
|
|
})
|
|
|
|
parsed, err := neturl.Parse(got)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "/api/v0/resolve-path", parsed.Path)
|
|
require.Equal(t, path, parsed.Query().Get("path"))
|
|
})
|
|
|
|
t.Run("preserves all query values", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
got := agentAPIPath("/api/v0/read-file-lines", neturl.Values{
|
|
"path": []string{"/tmp/plan v1#.md"},
|
|
"offset": []string{"10"},
|
|
"limit": []string{"20"},
|
|
"max_file_size": []string{"30"},
|
|
"max_line_bytes": []string{"40"},
|
|
"max_response_lines": []string{"50"},
|
|
"max_response_bytes": []string{"60"},
|
|
})
|
|
|
|
parsed, err := neturl.Parse(got)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "/api/v0/read-file-lines", parsed.Path)
|
|
require.Equal(t, "/tmp/plan v1#.md", parsed.Query().Get("path"))
|
|
require.Equal(t, "10", parsed.Query().Get("offset"))
|
|
require.Equal(t, "20", parsed.Query().Get("limit"))
|
|
require.Equal(t, "30", parsed.Query().Get("max_file_size"))
|
|
require.Equal(t, "40", parsed.Query().Get("max_line_bytes"))
|
|
require.Equal(t, "50", parsed.Query().Get("max_response_lines"))
|
|
require.Equal(t, "60", parsed.Query().Get("max_response_bytes"))
|
|
})
|
|
|
|
t.Run("debug logs zero after", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
got := debugLogsPath(time.Time{})
|
|
require.Equal(t, "/debug/logs", got)
|
|
})
|
|
|
|
t.Run("debug logs after", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
after := time.Date(2026, 5, 18, 12, 34, 56, 789, time.FixedZone("test", -7*60*60))
|
|
got := debugLogsPath(after)
|
|
parsed, err := neturl.Parse(got)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "/debug/logs", parsed.Path)
|
|
require.Equal(t, after.UTC().Format(time.RFC3339Nano), parsed.Query().Get("after"))
|
|
})
|
|
}
|
|
|
|
// countingReader serves filler bytes after a prefix, up to size bytes,
|
|
// and records how many bytes the consumer pulled.
|
|
type countingReader struct {
|
|
prefix string
|
|
size int64
|
|
read int64
|
|
}
|
|
|
|
func (r *countingReader) Read(p []byte) (int, error) {
|
|
if r.read >= r.size {
|
|
return 0, io.EOF
|
|
}
|
|
n := 0
|
|
for n < len(p) && r.read < r.size {
|
|
if int(r.read) < len(r.prefix) {
|
|
p[n] = r.prefix[r.read]
|
|
} else {
|
|
p[n] = 'a'
|
|
}
|
|
n++
|
|
r.read++
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
func TestLimitedResponseBody(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
limit int64
|
|
size int64
|
|
wantErr bool
|
|
}{
|
|
{name: "AtLimit", limit: 16, size: 16},
|
|
{name: "OverLimit", limit: 16, size: 1 << 20, wantErr: true},
|
|
{name: "MaxLimit", limit: math.MaxInt64, size: 16},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
src := &countingReader{size: tc.size}
|
|
body := newLimitedResponseBody(io.NopCloser(src), tc.limit)
|
|
got, err := io.ReadAll(body)
|
|
if tc.wantErr {
|
|
require.ErrorContains(t, err, "workspace agent response exceeds 16 bytes")
|
|
require.LessOrEqual(t, src.read, tc.limit+1)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
require.Len(t, got, 16)
|
|
})
|
|
}
|
|
}
|