Repository navigation
fix(coderd): list endpoints return 500 for API keys with ID-scoped allow lists - #32145
Conversation
…low lists The SQL authorization filters for workspaces and templates matched the resource ID against an unqualified "id" column. When an API key scope has an ID-scoped allow list, the compiled filter references that column, and the workspace and template list queries join other tables with an "id" column. Postgres rejects the query with 'column reference "id" is ambiguous', so the list endpoints return 500. Qualify the column as workspaces.id in WorkspaceConverter and t.id in TemplateConverter. Add a Postgres-backed regression test that lists workspaces and templates with an ID-scoped allow list. Fixes CORE-780
|
All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
|
/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, no findings, COMMENT. Review Finding inventoryFindings
Contested and acknowledgedNone. Round logRound 1Full panel (round 1). Netero: no findings. Law not run (74 effective additions). Panel: ging-go, knuckle, kurapika, ryosuke (domain specialists: Go, Go/SQL boundary, RBAC); pariston, mafuuu (core); bisky, gon, leorio, komugi, razor, mafu-san (pool); kite (wildcard). Multi-domain target 9-12 because the change is RBAC authorization SQL; 12 plus wildcard. Law analysisNot run. Effective additions 74. About deep-reviewCRF = Coder Review Finding (P0-P4, Nit, Note)
|
There was a problem hiding this comment.
This PR qualifies the id matcher in WorkspaceConverter and TemplateConverter, so the workspace and template list queries no longer fail with column reference "id" is ambiguous for API keys with ID-scoped allow lists. No P0, P1, or P2 findings.
Lower-severity findings are omitted as requested. One of them affects the merged commit: the title says "list endpoints", but the audit log and connection log list endpoints still return this 500 (first two items below), so a title scoped to workspaces and templates keeps the changelog accurate.
Out of scope (needs a ticket or explicit acceptance by a human):
coderd/rbac/regosql/configs.go:130:AuditLogConverteremits unqualifiedid.GetAuditLogsOffsetandCountAuditLogsreturnpq: column reference "id" is ambiguousfor an API key whose allow list holds anaudit_log:<uuid>or*:<uuid>entry (reproduced), so the audit log list endpoint returns 500 for that key.coderd/rbac/regosql/configs.go:144:ConnectionLogConverteremits unqualifiedid.GetConnectionLogsOffsetandCountConnectionLogsreturn the same error for aconnection_log:<uuid>or*:<uuid>entry (reproduced), so the connection log list endpoint returns 500 for that key.coderd/rbac/regosql/configs.go:5:resourceIDMatcher()emits unqualifiedid, and no test executes the compiled filters of the converters that still use it. A query that later joins a table with anidcolumn returns the same 500; if the target table has noidcolumn, the filter compares against the joined table's ID with no error. The decision log defers this to a follow-up with no linked ticket, so this needs a human decision.coderd/rbac/regosql/configs.go:17:userOwnerMatcher()emits unqualifiedowner_idandWorkspaceConverterstill uses it. A join that adds a table with anowner_idcolumn produces the same ambiguity error.
🤖 This review was automatically generated with Coder Agents.
BobbyHo
left a comment
There was a problem hiding this comment.
Nice fix! One small thing I noticed: the same issue seems to affect the audit log and connection log lists, as AuditLogConverter and ConnectionLogConverter still use the unqualified resourceIDMatcher(). Not a blocker, but I wonder if it might be worth addressing those here as well, or perhaps in a follow-up PR.
i think it's a small enough change so i can roll it into this PR! |
…ters AuditLogConverter and ConnectionLogConverter matched the resource ID against an unqualified "id" column. The audit log and connection log list and count queries join users, organizations and other tables that also have an "id" column, so an ID-scoped allow list makes the compiled filter ambiguous. Qualify the column as audit_logs.id and connection_logs.id. Replace the Postgres-backed regression test with string-matching cases in TestRegoQueries for the workspace, template, audit log and connection log converters. Each case fails without the qualified column. Refs CORE-780
|
I have read the CLA Document and I hereby sign the CLA |
|
/coder-agents-review Please limit the review to P0, P1, and P2 issues only. |
BobbyHo
left a comment
There was a problem hiding this comment.
Thanks for adding the audit log and connection log fixes!
Just one small question: I noticed that the database test (allowlist_test.go) was removed in the latest commit. I was curious if there was a particular reason for removing it. I thought it was useful to have a test that actually runs the queries, since the new string-based tests might not catch future changes to aliases or joins.
I wonder if there might still be some value in keeping both, but happy to go with your approach. Not a blocker at all!
…st Postgres Restore the Postgres-backed TestAllowListIDScopedListEndpoints and extend it to the audit log and connection log list queries. The string-matching cases in TestRegoQueries check the generated SQL, but only running the queries catches a later alias or join change that makes the qualified column invalid or ambiguous again. Each subtest fails with 'column reference "id" is ambiguous' without the qualified ID matchers. Refs CORE-780
|
@BobbyHo i accidentally removed that test file when i updated the PR to include fixes for |
BobbyHo
left a comment
There was a problem hiding this comment.
lgtm, Thanks for addressing my comments.
Note
This PR was generated by Coder Agents on behalf of @ksrinija.
Ticket Link
Fixes CORE-780
Description
List endpoints returned 500 with
pq: column reference "id" is ambiguousfor 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 sharedresourceIDMatcher(), which emits an unqualifiedid. Their list and count queries join other tables that also have anidcolumn. 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.idandconnection_logs.id. This follows the existing pattern used bychatResourceIDMatcher()(chats_expanded.id) andChatModelConfigConverter(cmc.id).Decision log
resourceIDMatcher(). Other converters share the helper and query different tables and aliases, so no single prefix inside it is correct for all of them.UserConverter,MCPServerConfigConverterandOrganizationSkillConverterfilter single-table queries.AIBridgeInterceptionConverter's queries either read one table or join subqueries that do not expose anidcolumn.NoACLConverterandDefaultVariableConverterare only used in tests. This was checked against the query SQL, not run against Postgres.AIBridgeInterceptionConvertermust keep a bareid. Its queries refer to the table both asaibridge_interceptionsand by the aliasai, so qualifying it with either name would break the others.resourceIDMatcher()to take a column argument so each converter names its column explicitly. Pending team discussion.compile_test.godocument 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.Testing Done
WorkspaceAllowList,TemplateAllowList,AuditLogAllowListandConnectionLogAllowListtoTestRegoQueriesincoderd/rbac/regosql/compile_test.go. Each compiles an ID-scoped allow list with the affected converter and checks the exact generated SQL.TestAllowListIDScopedListEndpointsincoderd/database/dbauthz/allowlist_test.go. It runsGetWorkspaces,GetTemplatesWithFilter,GetAuditLogsOffsetandGetConnectionLogsOffsetagainst 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.configs.goreverted tomain, all 4TestRegoQueriescases fail because the generated SQL contains the bareid, and all 4 Postgres subtests fail withpq: column reference "id" is ambiguous. With the fix, all pass.go test ./coderd/rbac/... ./coderd/database/dbauthz/...passes.make pre-commit, includinggolangci-lint) pass.e64c7f69c9>