Skip to content

perf(coderd/database/dbauthz): skip chat pre-reads for site-wide chat subjects - #320

Merged
ibetitsmike merged 8 commits into
mainfrom
mike/issue-292-dbauthz-chatd-fastpath
Nov 14, 2026
Merged

ibetitsmike merged 8 commits into
mainfrom
mike/issue-292-dbauthz-chatd-fastpath

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Closes #292 (part of the perf plan in #311).

What this does

The fork dbauthz wrappers in coderd/database/dbauthz/fork_forkdb.go currently 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 actsOnAllChats returns true only when the actor's subject type is rbac.SubjectTypeChatd or rbac.SubjectTypeSystemRestricted, the same pattern authorizeProvisionerJob uses for system-restricted callers. Checking the type rather than running a quiet Authorize probe means no extra rego evaluation and no extra entry in coderd_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.

  • Authorization results are identical for every subject. Both gated subject types carry site-level chat create/read/update/delete with ScopeAll in 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.
  • TestForkActsOnAllChatsMatchesRoles guards 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. Dropping ResourceChat from the chatd role fails this test, so a future role change cannot let the fast path grant silently.
  • All f.q.GetChatByID-based pre-reads were converted to authorizeChat so they share the fast path. GetChatWakeupByIDForUpdate, FinishChatWakeupClaim, InsertChatWakeupRun, ListChatWakeupRunKeys and UpdateChatWakeupStatus use authorizeChatWakeupID, which skips all reads including the wake-up row fetch when the fast path applies.
  • Workflow helpers (GetWorkflowRun, GetWorkflowStep, GetWorkflowStepsByRun, InsertWorkflowStep, UpdateWorkflowRunStatus, UpdateWorkflowStep, FailOpenWorkflowStepsOfRun) skip their authorization pre-read when the fast path passes. GetWorkflowRun and GetWorkflowStep still perform their own row fetch, so their sql.ErrNoRows semantics are unchanged. UpdateWorkflowStep keeps its GetWorkflowStep read even on the fast path because the UPDATE is a :exec query: 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 in coderd/x/chatd (fork_wait_for_events.go, fork_wakeups.go) treat sql.ErrNoRows as claim takeover / not found. On the fast path a nonexistent wake-up ID now reaches the underlying query's own WHERE clause and still yields sql.ErrNoRows; on the slow path the pre-read produces it as before.
  • UpdateWorkflowRunStatus on a missing run: previously the GetWorkflowRun pre-read returned sql.ErrNoRows; now the UPDATE simply affects 0 rows with no error. The chatd journal treats updateRunStatus as best effort (it only logs failures), so this is tolerated. GetWorkflowRun/GetWorkflowStep keep their exact previous semantics because the fast path still executes their own fetch.
  • SetChatWakeupStatus in fork_wakeup_controls.go maps sql.ErrNoRows/NotAuthorizedError to a 404 via httpapi.Is404Error; on the fast path a missing wakeup surfaces as sql.ErrNoRows from the update, which still matches.

No SQL query, chatd logic or upstream dbauthz behavior is changed.

Tests

TestForkQuerierChatdSkipsChatPreReads in fork_forkdb_test.go drives 17 fork methods through counting dbmock wrappers and asserts, for both the chatd subject and the system-restricted subject, that no GetChatByID/GetChatWakeupByID/GetWorkflowRun/GetWorkflowStep pre-read happens (the row-fetch methods themselves assert exactly their own single call, and UpdateWorkflowStep asserts its one kept GetWorkflowStep). A member-denied subtest 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 with IsNotAuthorizedError. With the fast path disabled, 52 subtests fail; with it enabled, all pass. TestForkActsOnAllChatsMatchesRoles pins the role invariant behind the gate as described above.

  • flock ~/.fork-go.lock go test -count=1 ./coderd/database/dbauthz/... — ok
  • flock ~/.fork-go.lock go test -count=1 ./coderd/x/chatd/... — ok
  • flock ~/.fork-go.lock golangci-lint run --fix=false ./coderd/database/dbauthz/... — 0 issues

… 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.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.
@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T12:58:55.669810Z df6850d Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread coderd/database/dbauthz/fork_forkdb.go Resolved
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.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

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.

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.
@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread coderd/database/dbauthz/fork_forkdb.go Resolved
… 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.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

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.

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.
@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: c89d61e164

ℹ️ 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".

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

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.

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.
@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: da33d4993a

ℹ️ 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".

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.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.
@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: df6850da93

ℹ️ 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".

@ibetitsmike
ibetitsmike merged commit ba26490 into main Nov 14, 2026
10 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-292-dbauthz-chatd-fastpath branch November 14, 2026 13:12
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged as ba26490 (squash), 2026-11-14 13:12Z.

Gates on head df6850da939ecdad6e982ba361adf546210117de:

My review:

  • I read the full fork_forkdb.go diff. actsOnAllChats trusts only SubjectTypeChatd and SubjectTypeSystemRestricted. Both subjects are built only in dbauthz.go, with site-wide chat create, read, update and delete and ScopeAll. Every other subject takes the unchanged read-then-authorize path, which matches upstream GetChatByID (fetch with ActionRead).
  • One error change on the fast path: UpdateWorkflowRunStatus on a missing run now affects 0 rows instead of returning sql.ErrNoRows. Its chatd caller only logs failures.

Tests I ran on the exact head:

  • go test -count=1 -v -run '(TestForkActsOnAllChatsMatchesRoles|TestForkQuerierChatdSkipsChatPreReads|TestForkQuerier)$' ./coderd/database/dbauthz/: all pass.
  • Negative controls in a throwaway worktree: with ResourceChat removed from the chatd role, TestForkActsOnAllChatsMatchesRoles fails ("chatd must keep read on chats site-wide, or actsOnAllChats grants silently"). With the fast path disabled, TestForkQuerierChatdSkipsChatPreReads fails (54 failures).
  • The child ran go test ./coderd/database/dbauthz/... and ./coderd/x/chatd/... (ok) and golangci-lint (0 issues).

Not exercised:

  • No live measurement of the statement count on the daily server. The reduction is shown by the counting test only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dbauthz: fork queries re-read the chat to authorize chatd, which may act on every chat

1 participant