Skip to content

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

Closed

Description

@ibetitsmike

Filed by Xum (an AI coding agent) on behalf of @ibetitsmike. Part of #311. Line numbers are at 607a95b.

Summary

The fork's dbauthz wrapper authorizes almost every fork query by first reading the row's chat with GetChatByID (and for wake-up, workflow run and workflow step queries, the wake-up, run or step row as well). It does this for every caller, including the chatd system subject, which holds create, read, update and delete on every chat. For chatd the pre-reads can never deny, so they are pure overhead, and chatd is the caller on every hot path: the wake-up worker, the generation step, the workflow journal.

This is the cheapest fix with the widest effect. It removes 6 of the 12 statements of every idle wake-up poll and 3 of the 4 statements of every workflow step transition.

Evidence

The helper and its two wrappers:

The chatd subject can act on every chat, so the object-level check is always true for it:

  • dbauthz.go:807-837: role chatd has site-wide ResourceChat: {create, read, update, delete}.

Some fork queries already authorize at type level with no read, which shows the pattern is accepted:

  • fork_forkdb.go:52-57 ClaimDueChatWakeups: authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceChat).

Hot callers that pay the pre-reads as chatd or system-restricted:

Caller Fork query Statements issued Needed
Wake-up worker, every poll of every waiting wake-up (fork_wait_for_events.go:379) ListChatWakeupRunKeys 3 1
Wake-up worker, every poll (fork_wakeups.go:375-376) FinishChatWakeupClaim 3 1
Wake-up worker, every poll (fork_wait_for_events.go:621-629) ListChildChatIDsWithActiveEventsWakeups, ListPendingChatDescendantQuestionsByRoot 2 each 1 each
Generation step, every step (fork_pinned_skills.go:64, fork_context.go:115, fork_agent_tool_policy.go:238) GetChatPinnedSkillsByChatID, GetChatTodosByChatID, GetChatSubagentByChatID 2 each 1 each
Workflow journal, every step transition UpdateWorkflowStep 4 1
Workflow journal, every run transition (fork_forkdb.go:853-863) UpdateWorkflowRunStatus 3 1

GetChatByID is not a bare primary key read: it selects from the chats_expanded view.

The HTTP handlers pay too. The route middleware has already loaded and authorized the chat, and the wrapper loads it again per query (for example the workflow run detail endpoint reads the run row three times per request).

Proposed design

  1. In forkQuerier, add one helper:

    // actsOnAllChats reports whether the subject holds action on the chat
    // type itself, so reading one chat to authorize it cannot change the
    // outcome.
    func (f *forkQuerier) actsOnAllChats(ctx context.Context, action policy.Action) bool {
        return f.q.authorizeContext(ctx, action, rbac.ResourceChat) == nil
    }

    authorizeChat, authorizeChatWakeup and the workflow run and step wrappers return early when it is true. This is the same type-level check ClaimDueChatWakeups already uses. It runs in memory, so it costs no statement. A subject that fails it (every ordinary user, and any subject whose API key scope lists specific resource IDs) takes today's object-level path unchanged, so the change can only skip a read whose answer was already "allowed".

  2. For HTTP callers, stop paying the second read by passing what the handler already has. Change the fork query params that take a bare chat_id on user-facing read paths to carry the loaded database.Chat in the wrapper call (a forkdb wrapper method variant such as ListChatWakeupsOfChat(ctx, chat) that authorizes the given object and then queries). Keep the SQL unchanged.

  3. Do not add a cache. The fix is to not ask.

Acceptance criteria

  • A dbauthz test proves that, with AsChatd and with AsSystemRestricted (both hold site-wide chat permissions), each of the fork methods listed above issues exactly one store call (use the existing dbmock recorder), and that a member without access to the chat is still denied with no change in error.
  • The negative control for that test is restoring the pre-read: the store-call count assertion fails.
  • A wake-up worker test (TestWakeup*) counts store calls for one idle poll of one events wake-up and asserts the new total.
  • No change to RBAC policy files and no new subject.

Notes

Activity

  1. added
    enhancement
    performance
    on Nov 14, 2026
  2. ibetitsmike commented on Nov 14, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    Delegated to a workstream agent on branch mike/issue-292-*. A PR follows.

  3. ibetitsmike commented on Nov 14, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    Implemented in #320 (branch mike/issue-292-dbauthz-chatd-fastpath, head da33d4993a). The fork dbauthz wrappers skip the chat/wake-up/workflow pre-reads for the chatd and system-restricted subject types, gated on subject type like authorizeProvisionerJob. Review round-trips with Codex and an independent authorization review ended with no open findings; fork-ci is green on the head.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions