Repository navigation
perf(coderd/database/dbauthz): skip chat pre-reads for site-wide chat subjects - #320
Conversation
… subjects The fork wrappers authorized chat-owned fork queries by reading the row's chat (or the wake-up, run or step row) first. A subject that already holds site-wide chat permission, like chatd, paid those reads on every hot path although the object-level check could never deny it. Add forkQuerier.actsOnAllChats, a quiet in-memory type-level authorize on rbac.ResourceChat. When it passes, authorizeChat and the wake-up, workflow run and step wrappers skip their pre-reads; when it fails, the read-then- authorize path is unchanged. Regular users still get the pre-read and are still denied on another user's chat.
…t/ws-292-dbauthz-chatd-fast-path-0a33e3ff
…ugh authorizeChat
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d3609e4f5f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Review found that the quiet type-level Authorize probe records an authorization-denial metric for every non-site-wide caller whose object-level check then succeeds. Gate the fast path on the chatd and system-restricted subject types instead, like authorizeProvisionerJob does, so no extra authz evaluation is recorded. Revert the main-suite asserts to the chat objects since the owner subject walks the pre-read path again.
Fixed in 22995f0: the fast path is now gated on the subject type (chatd and system-restricted) instead of a quiet type-level Authorize probe, matching the authorizeProvisionerJob pattern. No extra authz evaluation is recorded for non-site-wide callers, so denial telemetry is unchanged. The owner subject walks the pre-read path again and the main-suite asserts the chat object as before. Tests: dbauthz and chatd suites pass, golangci-lint 0 issues. |
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 22995f048d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
… path UpdateWorkflowStep is a :exec query, so with the pre-read skipped a step deleted between the caller's read and the write would vanish silently and the journal would report success on a lost write. Keep GetWorkflowStep unconditionally and skip only the run read on the fast path.
Fixed in c89d61e: UpdateWorkflowStep keeps its GetWorkflowStep read even on the fast path. The UPDATE is a :exec query, so without the pre-read a step deleted between the caller's read and the write would vanish silently and the journal would report success on a lost write. Only the run read (authorization-only) is skipped now. dbauthz suite and lint pass. |
|
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…ember-denied workflow cases
Restored the GetWorkflowRun count assertion for the GetWorkflowRun and GetWorkflowStepsByRun member-denied cases that an earlier refactor dropped, in da33d49. Test-only; dbauthz suite and lint pass. |
|
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
actsOnAllChats trusts the subject type instead of evaluating policy, so a future role change that removes chat permission from the chatd or system-restricted role would let the fast path grant silently. Pin the invariant with a real authorizer: both subjects must keep read and update on a chat object, and a member must not. Dropping the chatd chat grant fails this test.
|
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Merged as ba26490 (squash), 2026-11-14 13:12Z. Gates on head
My review:
Tests I ran on the exact head:
Not exercised:
|
Closes #292 (part of the perf plan in #311).
What this does
The fork dbauthz wrappers in
coderd/database/dbauthz/fork_forkdb.gocurrently pre-read rows to authorize chat, wake-up and workflow-run access:GetChatByID,GetChatWakeupByID,GetWorkflowRun,GetWorkflowStep. For a subject that already holds site-wide chat permission (the chatd subject and the system-restricted subject chatd runs under), those pre-reads can never deny, so they are wasted database round trips on every call.This PR adds a fast path: before fetching the row, the wrapper checks the subject type directly. The new helper
actsOnAllChatsreturns true only when the actor's subject type isrbac.SubjectTypeChatdorrbac.SubjectTypeSystemRestricted, the same patternauthorizeProvisionerJobuses for system-restricted callers. Checking the type rather than running a quietAuthorizeprobe means no extra rego evaluation and no extra entry incoderd_authz_authorize_duration_seconds{allowed="false"}for callers that then succeed on the object-level check. Shared helpers (authorizeChat,authorizeChatWakeup,authorizeChatWakeupID) apply the gate; when it fails, the existing pre-read path runs unchanged.ScopeAllin their roles; every other subject, including ACL-granted users and ID-restricted scopes, walks the unchanged pre-read path. The gate admits fewer subjects than a permission probe would, never more.TestForkActsOnAllChatsMatchesRolesguards the invariant that the fast path depends on: with a real authorizer it asserts the chatd and system-restricted subjects still hold read and update on a chat object owned by a random user in a random org, and that a member subject is denied. DroppingResourceChatfrom the chatd role fails this test, so a future role change cannot let the fast path grant silently.f.q.GetChatByID-based pre-reads were converted toauthorizeChatso they share the fast path.GetChatWakeupByIDForUpdate,FinishChatWakeupClaim,InsertChatWakeupRun,ListChatWakeupRunKeysandUpdateChatWakeupStatususeauthorizeChatWakeupID, which skips all reads including the wake-up row fetch when the fast path applies.GetWorkflowRun,GetWorkflowStep,GetWorkflowStepsByRun,InsertWorkflowStep,UpdateWorkflowRunStatus,UpdateWorkflowStep,FailOpenWorkflowStepsOfRun) skip their authorization pre-read when the fast path passes.GetWorkflowRunandGetWorkflowStepstill perform their own row fetch, so theirsql.ErrNoRowssemantics are unchanged.UpdateWorkflowStepkeeps itsGetWorkflowStepread even on the fast path because the UPDATE is a:execquery: without the pre-read, a step deleted between the caller's read and the write would vanish silently and the journal would report success on a lost write.Error semantics on missing rows
For site-wide subjects, only the denial behavior of the pre-read was ever relevant; the fast path does not change what callers see for missing rows, with two best-effort cases in the chatd path:
FinishChatWakeupClaim/InsertChatWakeupRun/ wake-up lookups: callers incoderd/x/chatd(fork_wait_for_events.go,fork_wakeups.go) treatsql.ErrNoRowsas claim takeover / not found. On the fast path a nonexistent wake-up ID now reaches the underlying query's own WHERE clause and still yieldssql.ErrNoRows; on the slow path the pre-read produces it as before.UpdateWorkflowRunStatuson a missing run: previously theGetWorkflowRunpre-read returnedsql.ErrNoRows; now the UPDATE simply affects 0 rows with no error. The chatd journal treatsupdateRunStatusas best effort (it only logs failures), so this is tolerated.GetWorkflowRun/GetWorkflowStepkeep their exact previous semantics because the fast path still executes their own fetch.SetChatWakeupStatusinfork_wakeup_controls.gomapssql.ErrNoRows/NotAuthorizedErrorto a 404 viahttpapi.Is404Error; on the fast path a missing wakeup surfaces assql.ErrNoRowsfrom the update, which still matches.No SQL query, chatd logic or upstream dbauthz behavior is changed.
Tests
TestForkQuerierChatdSkipsChatPreReadsinfork_forkdb_test.godrives 17 fork methods through counting dbmock wrappers and asserts, for both the chatd subject and the system-restricted subject, that noGetChatByID/GetChatWakeupByID/GetWorkflowRun/GetWorkflowSteppre-read happens (the row-fetch methods themselves assert exactly their own single call, andUpdateWorkflowStepasserts its one keptGetWorkflowStep). Amember-deniedsubtest per method, using a real authorizer with a member subject that has no chat access, asserts the exact pre-read counts on the slow path and that the call is denied withIsNotAuthorizedError. With the fast path disabled, 52 subtests fail; with it enabled, all pass.TestForkActsOnAllChatsMatchesRolespins the role invariant behind the gate as described above.flock ~/.fork-go.lock go test -count=1 ./coderd/database/dbauthz/...— okflock ~/.fork-go.lock go test -count=1 ./coderd/x/chatd/...— okflock ~/.fork-go.lock golangci-lint run --fix=false ./coderd/database/dbauthz/...— 0 issues