Repository navigation
feat: add chat previews endpoint - #31285
DanielleMaywood wants to merge 1 commit into
Conversation
ibetitsmike
left a comment
There was a problem hiding this comment.
Adversarial security review — #31285 (chat previews endpoint)
Part of the stack review; the stack-level roll-up (findings S1/S2, method, verified negatives) is on #31282. Line numbers at stack head 678b80103.
Contributes to S2 (P2, unanimous; validated) — catalog is gated on chat read, which site owners (and org admins) hold for other users' chats
GetChatOpenPreviewResults authorizes via GetChatByID (coderd/database/dbauthz/dbauthz.go:3739), consistent with GetChatMessagesByChatID — so this endpoint itself leaks nothing beyond /messages. The problem is composition: a site owner (chat read and application_connect by default) who opens a victim's chat gets this catalog and the UI (#31286) iframes preview--<id>--agent--ws--<victim> in the admin's browser (validated end-to-end: site owner → 200 here, then 200 with the victim's file body through the proxy). Org admins get 200 here (titles + absolute workspace paths) but are denied at the proxy.
Suggested defence-in-depth here: return [] (or 403) when httpmw.APIKey(r).UserID != chat.OwnerID — previews are only ever renderable by the owner anyway, so non-owners gain nothing from the catalog. The primary fix (viewer-vs-owner check before building/iframing the URL) belongs in #31286.
Unverified (single reviewer) — P3
- Unbounded
jsonb_array_elementsover every tool message of the chat (coderd/database/queries/chats.sql:539): noLIMIT, no pre-filter, no index on the predicate; re-run on every refetch/invalidation. Reviewer ranEXPLAIN ANALYZEand confirmed a full expansion. Suggest a containment pre-filter (cm.content @> '[{"type":"tool-result","tool_name":"open_preview"}]') + partial GIN index, and/or a limit on rows scanned. - A
tool-resultpart without aresultkey makes the whole endpoint 500 —(part->'result')::jsonbis NULL →rows.Scanintojson.RawMessagefails (reviewer verified with a DB test). AddAND part ? 'result'. - No server-side re-validation of stored
preview_id/port/agent_nameinchatPreviewsFromResults(coderd/exp_chats_previews.go:54-66). Every reviewer agreed no poisoning path exists today (see below), but the UI interpolates these straight into a hostname (site/src/utils/portForward.ts:63; node PoC:preview_id="evil.com/#"→ hostpreview--evil.com). Cheap hardening:appurl.IsPreviewID, port range, and agent-name regex before returning; drop rows that fail.
nit
- On the authz race (chat deleted between
ExtractChatParamand the query) the handler returns500witherr.Error()inDetail, while the sibling handlers map to404. - Org admins / chat-ACL readers receive a catalog whose every iframe the owner-only proxy will reject — the response is misleading for them (folds into the S2 fix above).
Verified negatives (no action)
- Catalog poisoning is unreachable: user message create/edit accept only
ChatInputPart(text/file);postChatToolResultstakes the tool name from the pending assistant call; a dynamic tool namedopen_previewis dropped at turn build because it collides with the always-present built-in; MCP tool names are always<server>__<tool>and carrymcp_server_config_id; hooks emit text/notice/error parts only; edits soft-delete the suffix. The SQL'smcp_server_config_id IS NULL,is_error,provider_executedandvisibilityfilters are correct for the shapes chatd actually writes. - Route sits under
apiKeyMiddleware+ExtractChatParam;GetChatPreviewsclient path matches; dbauthz test assertsActionReadon the chat.
b457acc to
64a8953
Compare
afa3dfb to
c89ee3d
Compare
c89ee3d to
86b0abb
Compare
DanielleMaywood
left a comment
There was a problem hiding this comment.
This review was performed by ai-review, an automated review bot.
1 inline finding.
Coverage
Coverage: 8 of 16 files reviewed, 4 unsupported, 4 generated; 22 rules.
Candidates: 485 found, 480 outside changed lines, 0 already discussed, 5 evaluated.
Findings: 1 (1 sites, 0 pre-existing sites), 0 over budget, 0 failed.
Model: deepseek/deepseek-v4.1-flash; 4 questions, 4 answered, 0 abstained, 0 cached; 0 uncalibrated findings held back; 4426 input and 1210 output tokens.
86b0abb to
1b757b3
Compare
1b757b3 to
b6b9095
Compare
b6b9095 to
532b183
Compare
532b183 to
16c47ac
Compare
|
@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 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". |
d80e417 to
369e23c
Compare
|
@codex review
|
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. 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". |
c1d0055 to
be58ca7
Compare
DanielleMaywood
left a comment
There was a problem hiding this comment.
This review was performed by ai-review, an automated review bot.
1 inline finding.
Coverage
Coverage: 8 of 16 files reviewed, 4 unsupported, 4 generated; 53 rules.
Candidates: 4480 found, 4457 outside changed lines, 2 already discussed, 21 evaluated.
Findings: 1 (1 sites, 0 pre-existing sites), 0 over budget, 0 failed.
Model: deepseek/deepseek-v4.1-flash; 21 questions, 19 answered, 2 abstained (unsure 1, truncated 1), 5 cached; 0 uncalibrated findings held back; 21712 input and 19040 output tokens.
be58ca7 to
d598e1d
Compare
3dab4f9 to
795455b
Compare
|
/coder-agents-review |
|
Chat: Review posted | View chat Review history
deep-review v0.13.0 | Round 5 | Last posted: Round 5, 24 findings (2 P2, 8 P3, 12 P4, 2 Nit), APPROVE. Review Finding inventoryFindings
Contested and acknowledgedCRF-2 (P3, exp_chats_previews.go:71) - duplicated acceptance rules
CRF-3 (P4, exp_chats_previews_test.go:74) - no error result test row
CRF-8 cross-kind part (P2, exp_chats_previews.go:73) - url_path on file previews, path on port previews
CRF-11 (P4, exp_chats_previews.go:33) - list returned with no app hostname
CRF-12 (P4, exp_chats_previews_test.go:110) - unbound-chat assertion vacuous
CRF-13 (P4, codersdk/chats.go:1174) - REST reference lacks exactly-one rule
CRF-15 (P4, codersdk/chats.go:1182) - Path vs FilePath
CRF-16 (Nit, exp_chats_previews.go:55) - endpoint type name
CRF-26 (P3, ARCHITECTURE.md:548) - route recorded only in a hidden TODO
CRF-29 (P4, exp_chats_previews.go:73) - dropped rows not logged
Round logRound 1Netero first pass: 2 P3, 1 P4, 2 Note, 1 OOS. No P0, panel proceeds. Full panel (round 1). Specialists: ging-go (.go), knuckle (query), kurapika (endpoint, auth, values used to build hostnames), ryosuke (Go API), melody (tool result JSON crosses producer/consumer boundary), killua (per-request scan). Core: pariston, mafuuu. Pool: bisky (tests), hisoka (input handling), gon, leorio (once-per-PR floor). Wildcard: meruem (random). 13 reviewers: multi-domain security-sensitive target 9-12 plus wildcard. ging-ts and nami not selected: the only .ts change is generated typesGenerated.ts with a new type no code imports yet. Cross-check:
Result: 2 P2, 4 P3, 7 P4, 2 Nit, 2 Note (body), 5 OOS, 1 dropped. Reviewed against 2ce038a..8a8f704. Round 2Churn guard PROCEED: 8 addressed, 7 contested (plus the CRF-8 cross-kind part), 0 silent. Branch rebased onto new base c2b194d; head 96c31cb. Inventory validation: CRF-19 (agent ID counter) root cause fixed in #31282 ( Fix round, not restructure: no files added, renamed, or moved; the only new exported symbol ( Netero (reused R1 agent for the CRF-2/CRF-3 dispute): no new findings; accepted CRF-2 and CRF-3 defenses. Panel (13): dispute check-in with reused R1 agents for gon, bisky, ryosuke, leorio, kurapika. New agents for raisers of round 1 open findings whose code changed: pariston, mafuuu, hisoka, meruem, melody (CRF-7), knuckle (CRF-9). ging-go: .go files changed. Wildcard: chopper (random from Chopper, Kite, Zoro, Knov, Razor). Cross-check:
Result: new 3 P3, 3 P4, 1 Note (body), 1 OOS; CRF-14 re-raised at P3. Reviewed against c2b194d..96c31cb. Round 3Churn guard PROCEED: 5 addressed, 2 contested, 0 silent. Head c975cf9 on base 31acf45 (rebased). Fix round, not restructure: no files added, renamed, or moved; no exported signature changes; goal unchanged; effective additions +311 vs +298. Netero (new agent; no contested Netero findings): no findings; verified CRF-1, CRF-24, CRF-25, CRF-28 by mutation. Panel (10): dispute check-in with reused agents for gon (R1 agent), hisoka, melody (CRF-26), chopper (CRF-29), all R2 agents. New agents: leorio, mafuuu (CRF-14), bisky (CRF-24, CRF-25), knuckle (query changed, CRF-28), ging-go (.go changed). Wildcard: meruem (random from Meruem, Kite, Zoro, Knov, Razor). Gon and Leorio present for comment changes. Cross-check:
Result: 3 P4 new; all prior findings closed or verified. Reviewed against 31acf45..c975cf9. Round 4Churn guard PROCEED: 3 addressed, 0 contested, 0 silent. Head 6f65124 on base 9554451 (rebased). Since round 3 the PR's own changes are comment wording, regenerated docs and types, one test row, and the PR description. Fix round, not restructure: no files added or moved, no signature changes, goal unchanged, effective additions +312 vs +311. Netero (new agent): no findings; verified CRF-34 by mutation; Note on line 55 wording. Panel (5): gon (CRF-32), leorio (CRF-33), bisky (CRF-34), ging-go (.go changed). Wildcard: mafuuu (random from Mafuuu, Meruem, Kite, Zoro, Knov, Razor). New agents for all; no contested findings. Cross-check:
Result: 0 new findings; CRF-32 re-raised at P4. Reviewed against 9554451..6f65124. Round 5Churn guard PROCEED: 1 addressed (CRF-32), 0 contested, 0 silent. Head 4e3a177 on base b6ea144 (rebased). Only change to the PR's own code since round 4: Fix round, not restructure. Netero (new agent): no findings. Panel (5): gon, leorio, mafuuu (CRF-32 raisers), ging-go (.go changed), wildcard meruem (random from Meruem, Chopper, Kite, Zoro, Knov, Razor). All: no findings; CRF-32 verified by grep over added lines. Gon did not re-raise the duplicated-key comment (round 4 kept it at P4 with removal optional; the two copies now agree). Result: no new findings; no open findings. APPROVE event. Reviewed against b6ea144..4e3a177. About deep-reviewCRF = Coder Review Finding (P0-P4, Nit, Note)
|
There was a problem hiding this comment.
This PR adds GET /api/experimental/chats/{chat}/previews, which rebuilds the chat's preview list from its stored open_preview tool results. Findings: 2 P2, 4 P3, 7 P4, 2 Nit.
Out of scope (needs a ticket or explicit acceptance by a human):
agent/agentpreview/preview.go:110: preview IDs come from a per-process counter, so after an agent restart a stored ID can serve a different path than the one it was opened for. A listed file preview shows that other path or returns 404 until the model republishes it, even after this endpoint keys file previews by path.coderd/x/chatd/turn_environment.go:2669: on a deployment without an app hostname,open_previewis not registered, so a chat creator can register a dynamic tool with that name. Its results pass this endpoint's SQL filters if the deployment later sets an app hostname; the revalidation limits them to the chat's own workspace.coderd/database/queries/chats.sql:519:GetChatUserPromptsByChatIDreturns prompt text without NUL decoding, so prompt history shows U+E000 sentinel pairs for prompts that contain NUL or U+E000.coderd/database/queries/chats.sql:539:GetChatMessagesWithFilePartsByChatIDruns the same unindexedcontent @>scan over every user and assistant message, so the attachments endpoint's cost grows with chat length.Makefile:1500:coderd/apidoc/.genlistssite/node_modules/.installedtwice; the second entry has no effect.
Notes:
coderd/database/queries/chats.sql:563: no index serves thecontent @>filter, so each request reads every tool message in the chat. A local measurement with 2000 tool messages of about 8 KB took 100 ms. This cost is acceptable for a fetch on page load and becomes a database load problem if the UI refetches on every message event.- CI: Flake Check and Storybook failed on this head. The job logs were not readable from the review environment.
TestChatPreviewspassed 35 times locally with-shuffle=on, and the onlysite/change is the generatedChatPreviewtype.
🤖 This review was automatically generated with Coder Agents.
|
/coder-agents-review |
There was a problem hiding this comment.
Round 2 of the review of the chat previews endpoint. New findings: 3 P3, 3 P4. One round 1 finding, about the endpoint description and the ChatPreview contract, is re-raised at P3.
Verified fixed: file previews keyed by path, the presence check, NUL decoding, the tool_name filter test, the oldest-first doc comment, the PR description, and maps.Copy. The reviewers who raised the contested findings accepted the defenses for the shared validator, the error-result test row, cross-kind url_path and path, the app hostname check, the unbound-chat test, the REST field descriptions, the Path name, and the endpoint type name.
Out of scope (needs a ticket or explicit acceptance by a human):
coderd/x/chatd/ARCHITECTURE.md:1179: aTODO(DanielleMaywood)HTML comment from #31284 stands in for documentingopen_previewin the tool list, so the rendered tool list leaves it out.
Notes:
coderd/exp_chats_previews.go:96: after an agent restart, a file preview entry keeps its pre-restart preview ID, which returns 404, until the model reopens that path. Port entries behave the same way while their server is down.- CI: 18 jobs fail on this head, including
fmt,lint, andtest-js, which build nothing this diff changes. The job logs were not readable from the review environment. Locally,go build,TestChatPreviewswith-race -count=10, and the chatprompt and dbauthz tests pass.
🤖 This review was automatically generated with Coder Agents.
|
Replies to the review body. Out of scope: @DanielleMaywood has accepted the
|
|
/coder-agents-review |
There was a problem hiding this comment.
Round 3 of the review of the chat previews endpoint. New findings: 3 P4.
Verified fixed: the endpoint description and ChatPreview contract, the DecodeNulInJSON doc comment, the part ? 'result' filter, and the role-filter and 65536-port test rows. Reviewers checked the query and test fixes by removing each guard and watching TestChatPreviews fail. The reviewers who raised the architecture document TODO and the missing log line for dropped rows accepted the defenses; AGENTS.md:75-76 assigns the architecture document update to the human author.
Notes:
- CI was still pending when this review ran (19 passed, 6 pending).
🤖 This review was automatically generated with Coder Agents.
|
/coder-agents-review |
There was a problem hiding this comment.
Round 4 of the review of the chat previews endpoint. No new findings. One round 3 finding, about naming the file preview kind, is re-raised at P4 because one instance was missed.
Verified fixed: the PR description now says the workspace check skips error results, and the "Neither" row fails the test when the exactly-one check is changed to hasPort && hasPreviewID.
Notes:
- CI was still pending when this review ran (12 passed, 13 pending).
🤖 This review was automatically generated with Coder Agents.
|
/coder-agents-review |
There was a problem hiding this comment.
Round 5 of the review of the chat previews endpoint. No new findings, and no findings remain open.
The doc comment at coderd/exp_chats_previews.go:55 now says "per agent and path", so every added line names the kind "file and directory previews" consistently. The only change since round 4 is that line; TestChatPreviews passes at this head.
Notes:
- CI was still pending when this review ran (2 passed, 20 pending).
🤖 This review was automatically generated with Coder Agents.
GET /api/experimental/chats/{chat}/previews lists the previews that
open_preview opened in the chat's current workspace, one per agent, port,
and URL path, or per agent and path, ordered by first open. The newest
result supplies the title and preview ID. Results come from the chat's own
user-visible tool messages, so access follows chat read permission.
|
Superseded by the strict-mode preview stack starting at #32438. |
Adds
GET /api/experimental/chats/{chat}/previews, which lists the previewsopen_previewopened in the chat's current workspace: one entry per agent, port, and URL path, or per agent and path, ordered by first open, with the newest result supplying the title and preview ID. Results come from the chat's own user-visible tool messages, so access follows chat read permission.Decision log
workspace_id, so the workspace check skips them.url_pathis part of the entry key, so the same port at another path is a separate entry. Aurl_paththatIsPreviewURLPathrejects skips the row.Stack (proof of concept for the embedded previews RFC, review bottom-up):
coder_apptabs