Skip to content

feat: encrypt the webhook secret of a chat automation with dbcrypt - #32076

Open
ThomasK33 wants to merge 33 commits into
tk/automations-schemafrom
tk/automations-dbcrypt
Open

ThomasK33 wants to merge 33 commits into
tk/automations-schemafrom
tk/automations-dbcrypt

Conversation

@ThomasK33

@ThomasK33 ThomasK33 commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Part of CODAGT-1135.

Stacked on #32067. Review only this diff. It merges after #32067.

Why

Chat automations store a webhook secret in chat_automations.webhook_secret. dbcrypt encrypts only in the methods it overrides, so a writer without an override would store the secret in plaintext and nothing would encrypt it later. This PR makes the database layer encrypt the secret before any code writes one. As the RFC says, dbcrypt encrypts the column when the deployment sets --external-token-encryption-keys. Without keys the column holds plaintext, as it does for external auth tokens today.

What changed

  • InsertChatAutomation encrypts the secret and sets webhook_secret_key_id. GetChatAutomationByID decrypts it. The list query still returns no secret.
  • dbcrypt rotate, decrypt and delete cover the column. Two new queries serve only these commands (system actor in dbauthz). delete blanks the secret to the empty string and keeps the row, because a webhook row must hold a secret.
  • A test fails with the method name when a database.Store method takes or returns a WebhookSecret, or its query names the column, and dbCrypt has no override of that name. A later update or rotate query cannot forget its override.
  • The "malformed encrypted field" decrypt error no longer quotes the stored value. A plaintext secret under a key ID would have ended up in the error text. This is its own commit and touches every encrypted column.
  • One line in the docs list of encrypted columns, and one line in the dbcrypt delete confirmation prompt that names the blanked webhook secrets.

Scope

No handler, API type or writer of the secret. A NULL or empty secret is not encrypted: it keeps a NULL key ID and rotation skips it, like AI provider keys.

Tradeoffs

  • rotate and decrypt handle a user's automations in the existing per-user transaction. If coderd rewrites a secret at the same time, the transaction fails and the command stops with an error. It never writes a stale secret back. A rerun works. The commands never change webhook_secret_version.
  • dbcrypt delete leaves webhook automations enabled with an empty secret. A request must never verify against an empty secret, so the webhook route has to reject it.
  • A replica that was already running without keys when the first key appeared stores new secrets in plaintext with a NULL key ID, as for every dbcrypt column. It cannot read a secret that a keyed replica wrote and returns a decrypt error. A replica that starts without keys while a key is active refuses to start. The plaintext rows stay plaintext until every replica has the keys and dbcrypt rotate has run.

Live check

On a local Enterprise build with keys set, a webhook row inserted through the dbcrypt store left no plaintext in the raw column. dbcrypt rotate, decrypt and delete then ran against that database and ended as intended. On a database that a running coderd has used, rotate stops at the key revoke because of crypto_keys rows. That gap is older than this PR (issue 25381).


Generated with xum • Model: anthropic:claude-sonnet-5-5 • Thinking: xhigh

…otation needs

GetChatAutomationsWithWebhookSecretByOwnerID lists the rows that hold a
webhook secret, and UpdateEncryptedChatAutomationWebhookSecret rewrites only
the secret and its key ID. Both are system-only in dbauthz, like the chat MCP
server pair, and nothing outside the dbcrypt commands calls them.
…tion

InsertChatAutomation encrypts the secret and sets its key ID, and
GetChatAutomationByID decrypts it. dbcrypt encrypts only in the methods it
overrides, so a test now fails with the method name when a database.Store
method takes or returns a WebhookSecret and dbCrypt does not override it.
… and delete

Rotate and Decrypt handle a user's chat automations in the same transaction
as the user's other encrypted rows, so a concurrent change of a secret fails
the transaction and never writes a stale secret back. Delete blanks the
secret to the empty string instead of removing the row, because a webhook row
must hold a secret.
…field error

A row with a key ID and data that is not base64 made the decrypt error quote
the stored value. For a column that is plaintext by mistake, such as a webhook
secret, that put the secret into an error message.
…hooks

chatd will run the user_prompt_submit hooks when a turn starts from a
message that has prompt_hooks_pending set. This adds the queries that
step needs.

ResolveChatMessagePromptHooks clears the flag and either rewrites the
content or soft-deletes the message. InsertChatMessages and
InsertChatQueuedMessageWithCreator carry the flag, and
UpdateChatQueuedMessageContent clears it because an edit runs the hooks
on the new content. Both soft deletes clear the flag, so a deleted row
never blocks the down migration. ClearChatMessagesPromptHooks serves a
context reset.

_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-sonnet-5-5` • Thinking: `xhigh`_
…solvePromptHooks

A message keeps its prompt_hooks_pending flag when it moves from the
queue into history, so chatd can run the hooks when the message starts a
turn. ResolvePromptHooks is the transition that clears the flag and
rewrites or soft-deletes the message. It runs from R0, R1 and R1P and
does not change the status.

A flagged row must be a user message visible to the user and the model,
because chatd looks for flagged rows in the history the user sees.
ClearContext clears the flags of the rows it hides, since those messages
never start a turn.

_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-sonnet-5-5` • Thinking: `xhigh`_
…flagged message

When a generation step finds a message with prompt_hooks_pending set, it
runs the user_prompt_submit hooks first, outside any transaction, and
one short transaction applies the result and clears the flag. A rewrite
replaces the content, a denial deletes the message and fails the chat
with kind hook_denied in the same transaction, and a failure that is not
a denial fails the chat and keeps the message. With hooks off the flag is
cleared and the message stays as saved. If a worker dies before the
transaction commits, the flag is still set and the next worker runs the
hooks again.

Nothing sets the flag yet, so no existing chat is affected. A manual
title no longer sends a message that still waits for its hooks to a
model.

_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-sonnet-5-5` • Thinking: `xhigh`_
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-sonnet-5-5` • Thinking: `xhigh`_
…repeat a behavior

The history query skips deleted rows, so a flagged message is never
deleted. Each removed test is covered by a test that a mutant of the
product code turns red alone.
…de guard

The guard now also finds a secret inside a named field (sqlc.embed) and a
query whose SQL names webhook_secret, so a bare-column query or a parameter
with another name cannot skip its override. A corrupt secret stops rotate and
decrypt with a decrypt error, and a key ID cannot name an empty secret.
…very prompt

Reading the prompt query for the title and summary generators sent a
flagged message to a model before its hooks ran. The query now skips it.
Also drop the content version input and the context boundary skip that no
path can reach, and describe the transition in the state diagram.
…utomations-dbcrypt

Drop A1's fail-closed InsertChatAutomation stub and its test: the real override
and TestChatAutomations replace them.
@linear-code

linear-code Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

CODAGT-1135

CODAGT-1569

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 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-13T15:55:28.401480Z e542056 Manual request
🔒 Security Review ✅ Completed 2026-11-13T15:57:36.285408Z e542056 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 274ef72d24

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

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 274ef72d24

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex security review

@ThomasK33
ThomasK33 changed the base branch from tk/automations-schema to tk/automations-prompt-hooks November 13, 2026 07:57
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 357d3a2999

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

@ThomasK33
ThomasK33 temporarily deployed to docs-preview November 13, 2026 07:58 — with GitHub Actions Inactive
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Docs preview

Links open this pull request's preview at https://pr-32076.docs.cdr-preview.dev, which updates with each push.

Check 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.

Preview sites for each built commit (2)

Every built commit keeps its own preview, newest first. Previews aren't indexed by search engines and are deleted when this pull request is merged or closed.

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 357d3a2999

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

- TestChatAutomations/KeysSet: drop the InTx twin of each case. The tx store is the same dbCrypt type, and every Rotate and Decrypt test already runs through InTx.
- TestChatAutomations/KeysSet: drop the NotEqual that requireEncryptedEquals already implies.
- TestChatAutomations/KeylessReplica: delete. KeysUnset, TestEncryptDecryptField/NoKeys, /MissingKey and KeysSet/EmptySecret own each branch it ran.
- TestChatAutomations/DecryptErr: keep one case. TestChatAutomationsCorruptSecret and TestEncryptDecryptField/MissingKey own the corrupt data and missing key branches.
- TestWebhookSecretMethodsAreOverridden: drop the list loop, which TestChatAutomationQueries/ListRowHasNoSecret owns, and the self-check on a type the test built.
- TestRotateChatAutomations: drop the read-back through a cipher B store, since the raw key ID and decrypted value asserts already cover it.
- TestDeleteChatAutomations: drop the CHECK recheck UPDATE, since Delete's own UPDATE must pass the CHECK. Replace the xmin loop with a value check on the plaintext row.
- TestDecryptChatAutomations: drop the xmin loop. Decrypt needs coderd stopped, so a no-op rewrite has no observable effect.
ThomasK33 added a commit that referenced this pull request Nov 13, 2026
…st uses them

- enterprise/dbcrypt: the InsertChatAutomation webhook secret refusal and TestChatAutomationWebhookSecret go to #32076, which encrypts the secret instead.
- coderd/database/migrations: the organization_id checks of enforce_chat_automation_organization, the dump.sql lines and the organization-change cases of TestChatAutomationOrganizationTrigger go to the PR that adds an organization move.
- coderd/database/dbauthz: the chatd update grant on chat_automation and its direct Authorize(Update) assert go to the PR that adds the run-recording queries.
- coderd/database/dbgen: chatAutomationGenCtx goes away. dbgen.ChatAutomation uses genCtx and the dbauthz test seeds its automations through rawDB.
The previous commit deleted TestChatAutomations/KeylessReplica. It was the only test that reads a pre-existing plaintext webhook secret through GetChatAutomationByID on a keyed store. A deployment that created a webhook automation before it enabled encryption stores that row as plaintext with a NULL key ID, so a broken read would fail every by-ID read of it until a rotate.

Restore the case unchanged. It also covers a keyless replica that reads a sealed row, which must return DecryptFailedError and not the ciphertext.

Each break below turns only this case red in `go test ./enterprise/dbcrypt/ -run TestChatAutomations`:

- GetChatAutomationByID decrypts every non-empty secret under the primary key: fails at dbcrypt_internal_test.go:2499.
- GetChatAutomationByID skips decryption when no cipher is loaded: fails at :2505.
- decryptChatAutomation treats a key ID with no cipher as plaintext: fails at :2505.

---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-sonnet-5-5` • Thinking: `xhigh`_
…utomations-dbcrypt

# Conflicts:
#	enterprise/dbcrypt/dbcrypt_internal_test.go
@ThomasK33
ThomasK33 changed the base branch from tk/automations-prompt-hooks to tk/automations-schema November 13, 2026 15:52
@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

@ThomasK33

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: e5420566fa

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

@ThomasK33
ThomasK33 temporarily deployed to docs-preview November 13, 2026 15:56 — with GitHub Actions Inactive
@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: e5420566fa

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@ThomasK33
ThomasK33 added this pull request to stack #32184 November 13, 2026 17:08

This branch was previously deployed

1 inactive deployment
docs-preview — e5420566 Deployed Nov 13, 2026 by ThomasK33 via deploy #152
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.

1 participant