Skip to content

fix(coderd): list endpoints return 500 for API keys with ID-scoped allow lists (#32145) - #32399

Merged
denisra merged 1 commit into
release/2.29from
backport/32145-to-2.29
Nov 24, 2026
Merged

denisra merged 1 commit into
release/2.29from
backport/32145-to-2.29

Conversation

@coder-backporter-app

Copy link
Copy Markdown

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:

git fetch origin backport/32145-to-2.29
git checkout backport/32145-to-2.29
git reset --hard origin/release/2.29
git cherry-pick -x -m1 05d8c8d4f8489532c39d15b4f2ee218d36fe8800
# resolve conflicts, then push

@coder-backporter-app coder-backporter-app Bot added the backport/v2.29 Backport PR targeting release/2.29 label Nov 18, 2026
@coder-backporter-app coder-backporter-app Bot added the backport/v2.29 Backport PR targeting release/2.29 label Nov 18, 2026
@denisra

denisra commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

@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)
@ksrinija
ksrinija force-pushed the backport/32145-to-2.29 branch from 50b99ab to 2e0fc49 Compare November 24, 2026 15:54
@ksrinija
ksrinija requested a review from Emyrk as a code owner November 24, 2026 15:54
@ksrinija

Copy link
Copy Markdown
Collaborator

/coder-agents-review Please limit the review to P0, P1, and P2 issues only.

@coder-agents-review

coder-agents-review Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Chat: Review posted | View chat
Requested: 2026-11-24 16:26 UTC by @ksrinija
Spend: $8.60 / $100.00

Review history
  • R1 (2026-11-24): 12 reviewers, 1 P2, COMMENT. Review

deep-review v0.13.0 | Round 1 | f781efe..2e0fc49

Last posted: Round 1, 1 findings (1 P2), COMMENT. Review

Finding inventory

Findings

# Sev Status Location Summary Round Reviewer Posted
CRF-1 P2 Open configs.go:83 AIBridgeInterceptionConverter still emits bare id; ListAIBridgeInterceptions joins visible_users on 2.29 and returns 500 under ID-scoped allow list R1 Ryosuke P2, Leorio P2, Kite P2 (Netero, Kurapika, Knuckle, Pariston, Mafuuu, Bisky, Razor listed as OOS) Yes
CRF-2 P3 Dropped by orchestrator (below requester's P0-P2 limit) allowlist_test.go:65 CountAuditLogs, CountConnectionLogs, GetWorkspaceBuildParametersByBuildIDs not covered under allow list R1 Bisky P3, Kurapika Note, Kite Note No
CRF-3 Nit Dropped by orchestrator (below requester's P0-P2 limit; comment accurate and identical to upstream) allowlist_test.go:20 Test doc comment restates name and understates filtering assertions R1 Gon P2 No
CRF-4 Nit Dropped by orchestrator (below requester's P0-P2 limit; comment accurate and identical to upstream) allowlist_test.go:29 Fixture comment narrates assertions R1 Gon P2 No
CRF-5 Nit Dropped by orchestrator (below requester's P0-P2 limit; name matches upstream) allowlist_test.go:23 Test name says Endpoints but calls store methods R1 Gon No
CRF-6 P3 Dropped by orchestrator (below requester's P0-P2 limit) PR description Title keeps "(conflicts)" and body is only the bot conflict warning R1 Leorio No
CRF-7 P4 Dropped by orchestrator (below requester's P0-P2 limit) commit message Cherry-picked body cites main-only converters and an unfilled CI placeholder R1 Leorio No
CRF-8 OOS Out of scope (body) configs.go:5 resourceIDMatcher() still emits bare id for UserConverter, NoACLConverter, DefaultVariableConverter R1 Ryosuke Note, Pariston No

Drop/downgrade gate notes:

  • CRF-1 scope: keep-in-scope argument wins. The fix changes configs.go, a file the PR changes, to meet the PR's stated goal (list endpoints must not 500 under ID-scoped allow lists) on the branch it targets. Orchestrator verified -- name: ListAIBridgeInterceptions :many is absent from origin/main aibridge.sql and present on release/2.29 with JOIN visible_users, so the OOS reviewers who said "main has the same problem" were wrong about main. aibridge_interception is a valid allow-list type (rbac.NewAllowListElement checks policy.RBACPermissions).
  • CRF-2 keep-argument for P2: GET /api/v2/audit and the connection log endpoint call the count query too, so a later SQL change could reintroduce the 500 with no failing test. Kept at P3 because all three queries pass at HEAD (verified by Bisky, Knuckle, Kite, Ryosuke) and this is a coverage gap only.
  • CRF-3/CRF-4 keep-argument for P2: a comment that understates the assertions could lead a maintainer to remove the row-count checks. Downgraded because both comments are accurate, byte-identical to upstream fix(coderd): list endpoints return 500 for API keys with ID-scoped allow lists #32145, and changing them diverges the backport from main for no behavior change.
  • Razor OOS claim that upstream fix(coderd): list endpoints return 500 for API keys with ID-scoped allow lists #32145 is also incomplete: disproved for AI Bridge (query absent on main).

Contested and acknowledged

Law analysis

Not run. Effective additions 145 (threshold 1000).

Round log

Round 1

Netero: 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-review

CRF = Coder Review Finding (P0-P4, Nit, Note)

Reviewer Focus
Bisky tests
Chopper ops/errors
Churn-guard change verification
Ging language modernization
Gon naming
Hisoka edge cases
Killua perf
Kite change integrity
Knov contracts
Knuckle SQL
Komugi flake/determinism
Kurapika security
Law decomposition
Leorio docs
Luffy product
Mafu-san process
Mafuuu contracts
Melody dispatch/pairing
Meruem structural
Nami frontend
Netero mechanical checks
Pariston premise testing
Pen-botter product gaps
Razor verification
Robin duplication
Ryosuke Go arch
Takumi concurrency
Zoro shape

🤖 Managed by Coder Agents.

@coder-agents-review coder-agents-review Bot 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.

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 bare id for UserConverter, NoACLConverter, and DefaultVariableConverter, so any authorized query using them that joins another table with an id column 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.

@ksrinija ksrinija changed the title fix(coderd): list endpoints return 500 for API keys with ID-scoped allow lists (#32145) (conflicts) fix(coderd): list endpoints return 500 for API keys with ID-scoped allow lists (#32145) Nov 24, 2026
@denisra
denisra merged commit caea470 into release/2.29 Nov 24, 2026
34 checks passed
@denisra
denisra deleted the backport/32145-to-2.29 branch November 24, 2026 16:58

This branch had an error being deployed

1 failed deployment
docs-preview — 2e0fc49c Deployed Nov 24, 2026 by denisra via cleanup #429
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/v2.29 Backport PR targeting release/2.29

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants