Skip to content

refactor(codersdk): use ReadBodyAsJSON in typed endpoints - #27857

Merged
dylanhuff-at-coder merged 1 commit into
mainfrom
dylan/plat-378-codersdk-read-body-as-json
Aug 5, 2026
Merged

refactor(codersdk): use ReadBodyAsJSON in typed endpoints#27857
dylanhuff-at-coder merged 1 commit into
mainfrom
dylan/plat-378-codersdk-read-body-as-json

Conversation

@dylanhuff-at-coder

@dylanhuff-at-coder dylanhuff-at-coder commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

This PR migrates 224 typed JSON response sites across 46 files to codersdk.ReadBodyAsJSON, so invalid 2xx bodies return structured errors while preserving URL credential redaction.

It intentionally excludes agent-direct HTTP, Azure IMDS, UseNumber, and chat paths; stacked on #27804, with chat and lint follow-ups in #27858 and #27859. Refs #27044.

Reviewed and updated by Coder Agents on behalf of @dylanhuff-at-coder.

@linear-code

linear-code Bot commented Aug 4, 2026

Copy link
Copy Markdown

PLAT-378

dylanhuff-at-coder commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

@dylanhuff-at-coder
dylanhuff-at-coder force-pushed the dylan/plat-378-codersdk-read-body-as-json branch from a08a60d to 0f4d0e0 Compare August 5, 2026 17:52
@dylanhuff-at-coder
dylanhuff-at-coder marked this pull request as draft August 5, 2026 18:28
@dylanhuff-at-coder
dylanhuff-at-coder marked this pull request as ready for review August 5, 2026 19:15

@BobbyHo BobbyHo 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.

lgtm

@dylanhuff-at-coder
dylanhuff-at-coder merged commit 76ae643 into main Aug 5, 2026
38 checks passed
@dylanhuff-at-coder
dylanhuff-at-coder deleted the dylan/plat-378-codersdk-read-body-as-json branch August 5, 2026 20:22
dylanhuff-at-coder added a commit that referenced this pull request Aug 5, 2026
Migrate chat endpoint response decoding to `ReadBodyAsJSON` and
consolidate `ReadBodyAsError` construction through `newResponseError`,
so empty-body and non-JSON errors consistently include the request
method and URL.

Stacked on #27857, with the lint rule following in #27859. Refs #27044.

Reviewed and updated by Coder Agents on behalf of @dylanhuff-at-coder.
dylanhuff-at-coder added a commit that referenced this pull request Aug 6, 2026
Add a ruleguard rule forbidding direct
`json.NewDecoder(res.Body).Decode(...)` on `*http.Response` in codersdk
packages, so new typed endpoints use `codersdk.ReadBodyAsJSON` and keep
returning structured errors for non-JSON bodies. The rule matches both
the chained call form and decoders assigned to a variable first.

Intentional raw-body paths carry documented `//nolint:gocritic`
exceptions: the 16 agent-direct HTTP decodes in
`workspacesdk/agentconn.go` route through a single `decodeAgentJSON`
helper (agent-direct over tailnet, so `ReadBodyAsJSON`'s reverse
proxy/SSO error guidance does not apply), and the Azure IMDS
attested-document decode in `agentsdk/azure.go` keeps an inline
exception.

The two `UseNumber` decoders in `licenses.go` are migrated to a new
`codersdk.ReadBodyAsJSONUseNumber`, so `coder licenses add/list` also
return structured errors for non-JSON bodies instead of `invalid
character '<' looking for beginning of value`.

Note for local verification: golangci-lint caches results, so run
`golangci-lint cache clean` after modifying `scripts/rules.go` or the
rule may silently not fire.

Final PR of the stack on #27804, #27857, and #27858. Refs #27044.


Stack plan

Inventory (full-tree audit): 280 migratable call sites across 47 files;
17 excluded (16 agent-direct HTTP sites in `workspacesdk/agentconn.go`,
1 Azure IMDS decode in `agentsdk/azure.go`).

1. **#27857** `refactor(codersdk): use ReadBodyAsJSON in typed
endpoints`: mechanical migration of all sites except `chats.go` (224
sites, 46 files).
2. **#27858** `refactor(codersdk): use shared error helpers in chat
endpoints`: migrate the 56 `chats.go` sites and consolidate the
duplicated `readRawBodyAsError`/`newResponseError` helpers onto the
shared `client.go` error path, with regression tests for the 409
usage-limit flow.
3. **#27859** `chore: forbid direct response body JSON decode in
codersdk`: ruleguard rule with documented exceptions for the intentional
raw-body paths, plus `ReadBodyAsJSONUseNumber` for the `licenses.go`
decoders.



Reviewed and updated by Coder Agents on behalf of @dylanhuff-at-coder.
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.

2 participants