Repository navigation
fix(coderd): list endpoints return 500 for API keys with ID-scoped allow lists (#32145) - #32399
Conversation
|
@ksrinija I've merged all other backports but this one has conflicts that need to be fixed first. Could you please take a look and let me know once it's ready for review? Thank you. |
…low lists (#32145) > [!NOTE] > This PR was generated by Coder Agents on behalf of @ksrinija. Fixes [CORE-780](https://linear.app/codercom/issue/CORE-780/bug-list-endpoints-return-500-column-reference-id-is-ambiguous-for-api) List endpoints returned 500 with `pq: column reference "id" is ambiguous` for API keys whose scope has an ID-scoped allow list (for example `--allow template:<uuid>`). The SQL authorization filters for workspaces, templates, audit logs and connection logs used the shared `resourceIDMatcher()`, which emits an unqualified `id`. Their list and count queries join other tables that also have an `id` column. The ID check is only generated for specific IDs, so wildcard allow lists (`template:*`) and tokens without allow lists were not affected. This PR qualifies the column in each affected converter in `coderd/rbac/regosql/configs.go`: `workspaces.id`, `t.id`, `audit_logs.id` and `connection_logs.id`. This follows the existing pattern used by `chatResourceIDMatcher()` (`chats_expanded.id`) and `ChatModelConfigConverter` (`cmc.id`). <details> <summary>Decision log</summary> - **Inline qualified matchers instead of changing `resourceIDMatcher()`.** Other converters share the helper and query different tables and aliases, so no single prefix inside it is correct for all of them. - **Remaining converters checked; none affected.** `UserConverter`, `MCPServerConfigConverter` and `OrganizationSkillConverter` filter single-table queries. `AIBridgeInterceptionConverter`'s queries either read one table or join subqueries that do not expose an `id` column. `NoACLConverter` and `DefaultVariableConverter` are only used in tests. This was checked against the query SQL, not run against Postgres. - **`AIBridgeInterceptionConverter` must keep a bare `id`.** Its queries refer to the table both as `aibridge_interceptions` and by the alias `ai`, so qualifying it with either name would break the others. - **Possible follow-up, not in scope:** change `resourceIDMatcher()` to take a column argument so each converter names its column explicitly. Pending team discussion. - **Tests:** string-match cases in `compile_test.go` document the exact SQL each converter generates, and need no database fixtures. A Postgres-backed test (`TestAllowListIDScopedListEndpoints`) also runs all 4 affected queries, so a later alias or join change that breaks the qualified column is caught. Both were kept after review discussion. </details> - **SQL generation tests:** added `WorkspaceAllowList`, `TemplateAllowList`, `AuditLogAllowList` and `ConnectionLogAllowList` to `TestRegoQueries` in `coderd/rbac/regosql/compile_test.go`. Each compiles an ID-scoped allow list with the affected converter and checks the exact generated SQL. - **Postgres regression test:** added `TestAllowListIDScopedListEndpoints` in `coderd/database/dbauthz/allowlist_test.go`. It runs `GetWorkspaces`, `GetTemplatesWithFilter`, `GetAuditLogsOffset` and `GetConnectionLogsOffset` against Postgres with the real RBAC authorizer and an allow list naming one row of each type. Each subtest checks that the query succeeds and returns only the allow-listed row, out of two. This catches future alias or join changes that the string tests cannot. - **Confirmed both tests catch the bug:** with `configs.go` reverted to `main`, all 4 `TestRegoQueries` cases fail because the generated SQL contains the bare `id`, and all 4 Postgres subtests fail with `pq: column reference "id" is ambiguous`. With the fix, all pass. - **Existing tests:** `go test ./coderd/rbac/... ./coderd/database/dbauthz/...` passes. - **Lint:** the repo's pre-commit checks (`make pre-commit`, including `golangci-lint`) pass. - **CI:** <fill in once checks finish on `e64c7f69c9`> (cherry picked from commit 05d8c8d)
50b99ab to
2e0fc49
Compare
|
/coder-agents-review Please limit the review to P0, P1, and P2 issues only. |
|
Chat: Review posted | View chat Review history
deep-review v0.13.0 | Round 1 | Last posted: Round 1, 1 findings (1 P2), COMMENT. Review Finding inventoryFindings
Drop/downgrade gate notes:
Contested and acknowledgedLaw analysisNot run. Effective additions 145 (threshold 1000). Round logRound 1Netero: no findings. Full panel (round 1): ging-go, kurapika, knuckle, ryosuke, pariston, mafuuu, bisky, gon, leorio, kite, razor, wildcard knov (multi-domain size target, authorization code). Requester asked to limit review to P0-P2. 1 P2 posted; 2 P3, 1 P4, 3 Nit not posted; 1 OOS. Reviewed against f781efe..2e0fc49. About deep-reviewCRF = Coder Review Finding (P0-P4, Nit, Note)
|
There was a problem hiding this comment.
This backport qualifies the ID column in the workspace, template, audit log, and connection log SQL authorization converters, and matches #32145 except for the 2.29 slogtest import path. Limited to P0-P2 as requested: 1 P2.
Out of scope (needs a ticket or explicit acceptance by a human):
coderd/rbac/regosql/configs.go:5:resourceIDMatcher()still emits a bareidforUserConverter,NoACLConverter, andDefaultVariableConverter, so any authorized query using them that joins another table with anidcolumn returns the same 500 under an ID-scoped allow list.
coderd/rbac/regosql/configs.go:83
P2 [CRF-1] On release/2.29, AIBridgeInterceptionConverter still uses the unqualified resourceIDMatcher(), so GET /api/v2/aibridge/interceptions still returns 500 for API keys with an aibridge_interception:<uuid> allow list entry. (Ryosuke, Leorio, Kite)
On this branch, ListAIBridgeInterceptions (coderd/database/queries/aibridge.sql:128-129) joins visible_users, which has its own id column, and a dbauthz test with that allow list returns pq: column reference "id" is ambiguous. The #32145 description says this converter is unaffected, which holds on main because main no longer has this query, but not on 2.29. Both 2.29 AI Bridge queries read the unaliased aibridge_interceptions table, so sqltypes.StringVarMatcher("aibridge_interceptions.id :: text", []string{"input", "object", "id"}) fixes both; add an AI Bridge subtest to TestAllowListIDScopedListEndpoints and a case to TestRegoQueries.
🤖
🤖 This review was automatically generated with Coder Agents.
Backport of #32145
Original PR: #32145 — fix(coderd): list endpoints return 500 for API keys with ID-scoped allow lists
Merge commit: 05d8c8d
Requested by: @ksrinija
Warning
The automatic cherry-pick had conflicts.
Please resolve manually by cherry-picking the original merge commit: