Files
coder/coderd
Spike Curtis 8475a1defb refactor(coderd): enforce workspace template authz in singleWorkspaceData (#29665)
Part of the lite `codersdk.Workspace` work
([GRU-82](https://linear.app/codercom/issue/GRU-82),
[RFC](https://app.notion.com/p/coderhq/Lite-Workspace-API-Requests-3aad579be5928060b5e7cc65c539717f)).

`api.workspaceData` omits templates the actor cannot read and leaves
each caller to check `len(data.templates) == 0` and fail the request
itself. Now that `workspaceData` receives the `wsrelated.Config`, it
knows whether the template was requested, so the authorization check can
live closer to the data.

This adds `singleWorkspaceData`, a thin wrapper for single-workspace
fetches that returns `errWorkspaceTemplateUnauthorized` when the
template was requested but is absent (i.e. unreadable). The five
single-workspace callers route through it and map that error to the
response their endpoint requires:

- get workspace by ID → `403`
- get workspace by owner/name → `404` (preserves existence concealment)
- create/put workspace → `403`
- watch workspace (SSE) → forbidden error event

The batch `workspaceData` keeps its RBAC-filtering behavior so the
**list** endpoint can still skip workspaces whose template the actor
cannot read, rather than failing the whole request. Field selection
(`Config`) and authorization policy are kept separate: the strict check
is a property of the single-workspace wrapper, not of `All()`.

<details>
<summary>Design note</summary>

The check tests membership by the workspace's `TemplateID` rather than
only `len(templates) == 0`, and only real query failures (not RBAC
omissions) propagate as generic errors, so a genuine DB error is never
mistaken for an authorization denial.
</details>

### Testing

- New `TestSingleWorkspaceData` (mocked `database.Store`): template
readable, template requested-but-unauthorized (sentinel), template not
requested (no error), and a real query error (not the sentinel).
- Existing `TestWorkspace`, `TestWorkspaceByOwnerAndName`,
`TestWorkspaceWatcher`, and `TestWorkspaceIncludeRelated` pass
unchanged.

---

*Generated by Coder Agents on behalf of @spikecurtis.*
2026-09-21 21:06:06 +02:00
..