Repository navigation
Conversation
UpdateAutomation now returns the row it read under the lock before the change. The PATCH handler and the manage_automations tool record it as the audit before-image, so a concurrent change between their read and the lock is not attributed to them (CODAGT-1273, CODAGT-1226 item 3). The doc comments also state that a prompt edit keeps already queued messages. Signed-off-by: Thomas Kosiewski <tk@coder.com>
…s nothing Also document in dbauthz that the chat automation update and secret rotation queries authorize only RBAC update, and that the owner-only rule lives in the chatd services (CODAGT-1226 items 1 and 2). Signed-off-by: Thomas Kosiewski <tk@coder.com>
HTML escaping turns each <, >, &, U+2028, and U+2029 into six bytes, so a body under the 256 KiB request limit could add a much larger message. PublishAutomationWebhook now refuses escaped event data above 256 KiB before admission, and the events endpoint returns 413 with the escaped size. A refused delivery queues nothing and does not consume a single-use webhook (CODAGT-1226 item 6). The docs also state that a prompt edit keeps already queued messages (item 5). Signed-off-by: Thomas Kosiewski <tk@coder.com>
Signed-off-by: Thomas Kosiewski <tk@coder.com>
…utomation The function doc comment already states that a prompt edit keeps the queue generation and leaves queued messages unchanged. Signed-off-by: Thomas Kosiewski <tk@coder.com>
The branch made the PATCH handler record the row read under the update lock as the audit Old, but only the manage_automations tool path had a test for it. A store hook renames the automation between the handler's read and the lock; the disable's audit diff must not contain the rename. The test fails when the handler records its pre-read row.
…tions-mgmt-hardening
Docs previewCheck off each page once it's been reviewed. If a page changes in a later push, its checkbox clears automatically so it gets a fresh look. Pages not yet wired into the docs navigation aren't listed here. |
|
@codex review |
Codex Review SummaryThis 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. |
|
@codex security review |
|
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". |
🛡️ Codex Security ReviewSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
|
Superseded: this work is consolidated into the single Automations branch automations-bcpb. Closing on the owner's request. |
Hardens the chat automations management API, following the security-review nits in CODAGT-1226 and the audit follow-up in CODAGT-1273.
Changes
chatd.UpdateAutomationnow returns(old, updated, err), whereoldis the row it read under the row lock. The PATCH handler and themanage_automationstool (update,enable,disable) recordoldas the auditOld. Before, a concurrent owner edit between the caller's read and the lock showed up in the diff of the wrong action. The tool change is limited to the two audit call sites.json.HTMLEscapeturns each<,>,&, U+2028 and U+2029 inside a JSON string into a six-byte\usequence, so a 256 KiB body could become about 1.5 MB of message text. The escaped event data is now capped at 256 KiB, the same as the request body limit (maxChatRequestBodyBytes). A larger event gets413 Event data too large.with the escaped size in the detail, before admission: nothing is queued or created, and a single-use webhook is not consumed.UpdateAutomationdoc comment, the PATCH swagger description anddocs/ai-coder/agents/automations.mdnow say that messages already queued keep the prompt they were sent with, and that disabling or deleting the automation removes them.UpdateChatAutomationByIDandUpdateChatAutomationWebhookSecretByIDnow state that they authorize only RBAC update, and that the owner-only rule lives in chatdUpdateAutomationandRotateAutomationSecret, so callers must go through those services.DeleteAutomationdeletes the row first, so a denied delete changes nothing. This PR adds the missing partial-change test: a read + update role gets 403 on DELETE, and the automation stays enabled with the same queue generation and update time.DeploymentValues.Validaterejects--chat-max-automations-per-ownerbelow 1 at startup, covered byTestDeploymentValues_Validate_ChatLimits. No change here.Tests
TestManageAutomationsToolAuditsLockedRow(CODAGT-1273 acceptance) andTestChatAutomations/UpdateAuditsLockedRow: the owner renames the automation between the caller's read and the lock; the audit diff has the changed field and notname. Both fail when the pre-read row is audited.TestPublishAutomationWebhookEventSize(boundary) andTestChatAutomationEvents/EscapedEventTooLarge(413, no side effects, single-use not consumed).TestChatAutomationsDeleteWithoutUpdateextension (denied delete changes nothing).Remote dogfood UAT through Coder Agents passed: escaped-size boundary (262144 accepted, 262145 refused), single-use not consumed by a 413, a PATCHed prompt leaves the queued input on the old prompt and disable removes it, and
coder serverrefuses--chat-max-automations-per-ownerof -1 and 0.Closes CODAGT-1226
Closes CODAGT-1273
Part of CODAGT-1135
📋 Implementation Plan
Plan: CODAGT-1226 + CODAGT-1273, automation management hardening (lane L2)
Contract: Linear CODAGT-1226 (6 items) and CODAGT-1273, plus owner decisions. Base origin/main (fcbf45c). Keep the manage_automations tool change minimal: lane L7 edits coderd/x/chatd/automations_tool.go and lane L1 edits other functions in coderd/x/chatd/automations.go. Do not touch site/.
Item 1: DELETE needs update permission (CODAGT-1226 #1)
Already fixed on main by #30117 (squash 5c1662e):
DeleteAutomationdeletes the row first (ActionDelete), then cleans queued rows as chatd. A 403 on delete therefore changes nothing.enterprise/coderd/exp_chat_automations_test.goTestChatAutomationsDeleteWithoutUpdate(or a sibling test): a member with a custom org role of read + update onchat_automation(no delete) gets an error on DELETE; the stored row afterwards still exists withEnabledtrue and unchangedQueueGeneration(andUpdatedAt).Items 3 + CODAGT-1273: audit
Oldfrom the locked row (one fix)func (p *Server) UpdateAutomation(...) (database.ChatAutomation, error)in coderd/x/chatd/automations.go to return(old, updated database.ChatAutomation, err error), whereoldis the row as read byGetChatAutomationsByIDsForUpdateunder the lock (before the change). Update its doc comment: callers recordoldas the audit before-image.patchChatAutomation: after a successful update setaReq.Old = old(keep the pre-read row asOldonly for failures).disablecase andmanageAutomationsUpdateaudit witholdinstead of the pre-readrow. Change nothing else in that file.grep -rn '\.UpdateAutomation(' --include=*_test.go).automationReadHookstore wrapper and the fixture pattern ofTestManageAutomationsToolConcurrentRetarget, rename the automation through the service as the owner between the tool's read and the service lock, then run toolupdate(prompt change) andenable(on a disabled automation). Assert the tool succeeded, one audit entry was written, and its diff does not containname(the locked row already had the new name) but contains the changed field (enabled; prompt is an ActionSecret field, so assert the key exists only if the diff records secret keys; otherwise assert onenabled/updated_at). Check howaudit.MockAuditorexposes the diff (AuditLog.DiffJSON) first. Negative control: the test must fail if the tool audits the pre-read row (state this in a comment, verify by temporarily reverting once locally).Item 2: owner-only rule lives only in chatd (CODAGT-1226 #2)
UpdateChatAutomationByIDandUpdateChatAutomationWebhookSecretByID: these authorize only RBAC update, which org admins and site owners also hold; the rule that only the owner can change an automation (other than disabling) or rotate its secret lives in chatdUpdateAutomationandRotateAutomationSecret, so every caller (HTTP handlers, the manage_automations tool, run_now) must go through those services. The schedule scanner writes only throughAdvanceChatAutomationScheduleCursor. Verified: no other non-test callers of either query exist.Item 4: negative automation cap (CODAGT-1226 #4)
(*DeploymentValues).Validate: rejectAI.Chat.MaxAutomationsPerOwner < 1with an error naming--chat-max-automations-per-owner(style of the existing chat hook errors). cli/server.go already callsvals.Validate()at startup.defaultDeploymentValues(t): 0 and -1 fail with the flag name in the error, 1 passes, the default passes.Item 5: prompt edit keeps queued inputs (owner decision: keep behavior, document it)
No behavior change. A prompt edit does not start a new queue generation, so messages already queued keep the prompt they were sent with; disabling (or deleting) the automation removes them.
UpdateAutomationdoc comment and at thereq.Promptbranch.@DescriptionofpatchChatAutomation(add one if missing) states it. Do not change codersdk struct comments (they regenerate site/src/api/typesGenerated.ts; site/ is out of scope for this lane).make gen(or the narrow apidoc target) if swagger output changes; commit generated files.Item 6: cap the escaped webhook body (owner decision)
Cap: the escaped event data must be at most 256 KiB, the same as the raw request body limit (
maxChatRequestBodyBytesin coderd/exp_chats.go). This keeps the message a webhook produces no larger than what the endpoint reads.MaxAutomationEventDataBytes = 256 * 1024(comment: equals coderd's request body limit),ErrAutomationEventTooLargeplusAutomationEventTooLargeError{Size, Max int}wrapping it (pattern:AutomationQueueShareFullError). Split escaping intoescapeAutomationEventData(body []byte) []byte(json.HTMLEscape).PublishAutomationWebhookescapes once after the JSON check and returns the typed error beforepublishAutomationwhen the escaped size exceeds the cap, so nothing is admitted, queued, created, or consumed.automationEventText(name, escaped)takes the escaped data; update TestAutomationEventText accordingly and add a size boundary unit test (exactly the cap passes the check, one byte more fails).writeChatAutomationEventError: map it to 413 "Event data too large." with a Detail giving the escaped size and the max, and saying that<,>,&(and U+2028/U+2029) count as six bytes each. Update the swagger@Description/ body param text ofpostChatAutomationEvent.InvalidBody(or a new subtest): a valid JSON body well under 256 KiB raw whose escaped form exceeds 256 KiB (for example"+ 50,000<+") returns 413 with the escaping detail, adds no message to the target chat, and does not consume a single-use webhook.413response row, and the Limits table row.Validation (targeted; CI is the gate)
go build ./...for touched packages,go vet.umask 022 && go test ./coderd/x/chatd/ -run 'Automation|ManageAutomations' -count=1,go test ./coderd/ -run 'ChatAutomation' -count=1,go test ./enterprise/coderd/ -run 'ChatAutomations' -count=1,go test ./codersdk/ -run 'DeploymentValues_Validate' -count=1,go test ./coderd/database/dbauthz/ -run 'ChatAutomation' -count=1.make gendrift check for apidoc;make lint/goor golangci-lint on touched packages;pnpm run format-docs/lint for the docs file if available.Acceptance criteria
update/enableand HTTP PATCH audit entries use the locked pre-update row as Old (CODAGT-1273 test passes and fails when reverted).coder serverrefuses--chat-max-automations-per-owner< 1 at startup (Validate test).Dogfooding (remote UAT via Coder Agents)
Build the branch in a dev deployment with the chat-automations experiment enabled for the test user and model config dd70d5aa-01bc-4ef6-a2db-70a4cd8cc876 ("Opus 5.5 (Bedrock)").
{"x":"<" * 50000}(about 50 KB raw, about 300 KB escaped): expect 413 with the escaping detail; the chat gets no message. POST a small body: 202 and the message arrives.coder serverwith--chat-max-automations-per-owner=-1(or the env var): startup fails with the flag named in the error; with 0 likewise.Evidence: terminal transcripts / screenshots under .mux-uat/round-/.
Lane rules (copied from lane_rules.md)
type(scope): message, scope a real path containing every changed file (likely no scope orcoderd... use a scope that contains codersdk, coderd, enterprise, docs: no scope). Body: "Closes CODAGT-1226", "Closes CODAGT-1273", "Part of CODAGT-1135", xum footer.Generated with
xum• Model:anthropic:claude-opus-5-5• Thinking:high