Repository navigation
feat(coderd): add open_preview chat tool - #31284
DanielleMaywood wants to merge 1 commit into
Conversation
ibetitsmike
left a comment
There was a problem hiding this comment.
Adversarial security review — #31284 (open_preview chat tool)
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 S1 (P1, contested; validated) — port mode is the escape hatch, and there is no gesture/approval gate
OpenPreviewArgs.Port(coderd/x/chatd/chattool/openpreview.go:52) produces a result the UI turns into a plain<port>--agent--ws--userorigin (PreviewPanel.tsx:67). That origin is not covered by #31283'spreview--CORS denial and does inherit port shares (validated through the real proxy:ACAO+ACAC: trueagainst the owner's other workspace; public share → unauthenticated 200). The tool description recommends port mode first ("Use port for an existing HTTP web app"), so the mitigated mode is the one the model is steered away from.- The tool has no confirmation step; it is appended unconditionally for root chats (
turn_environment.go:2427) and the UI auto-renders its result (#31286). A prompt-injected model withexecutecanpython3 -m http.server 8080serving its own HTML and callopen_preview(port: 8080)in the same turn.
Suggested fix: either drop port mode, or give port-mode previews a preview---class slug (e.g. result carries preview_slug: "preview--p8080" and routing in #31283 maps it to the port with AppSharingLevelOwner forced), so appurl.PreviewID covers both modes. Consider gating open_preview behind the same approval mechanism as other side-effecting tools, or at least making the result not auto-render (see #31286).
Unverified (single reviewer) — P3
- Tool is always registered, even without a wildcard access URL. Publication on the agent (daemon-lifetime, 16-slot cap) happens regardless; the UI merely hides the pane. There is also no admin switch to disable the built-in (MCP allow/deny lists don't cover built-ins; no experiment flag), which contradicts the docs' "tool configuration from the control plane" promise.
getPreviewWorkspacecan persistWorkspaceID=Bwith A'sAgentName/Conn(turn_environment.go:1938) when the owner PATCHes the chat workspace betweengetWorkspaceConnand the snapshot read (reviewer demonstrated with a gomock test; root cause isUpdateChatBuildAgentBindinghaving noworkspace_idguard — pre-existing). Owner-only impact: the catalog attributes an agent/preview to the wrong workspace.- Every
getWorkspaceConnerror is wrapped byworkspaceLoadErrorResponse(openpreview.go:95) → "workspace is probably gone; use create_workspace", including a 30 s dial timeout. Other tools returnerr.Error(). This steers the model into a side-effecting rebind on transient errors. - Title filter is
unicode.IsControlonly (openpreview.go:75) — RLO/ZWSP/BOM/U+2028 are accepted → label spoofing in the preview selector. No HTML injection (React escapes; verified). - Raw model
pathis persisted and displayed as "the published path" (openpreview.go:118) instead of the agent's canonical path; no abs/NUL/length check in coderd (agent bounds the body at 16 KiB). A model can make the catalog show a misleading path while the pinned tree is elsewhere after symlink resolution. Suggest returning the canonical path from the agent and storing that.
nit
coderd/x/chatd/ARCHITECTURE.md:1014still carries a<!-- TODO: document … -->and repeats "owner-only"; #31287 doesn't resolve it.TestTurnWorkspaceContextGetWorkspaceConnLazyValidationSwitchesWorkspaceAgentwas replaced rather than kept alongside the newgetPreviewWorkspacetest.
Verified negatives (no action)
- Tool policy: root-only; excluded in child/plan/explore (existing
TestBuildTurnEnvironmentToolPolicypasses). - No MCP/inline/workspace/dynamic tool can be named literally
open_previewwith NULLmcp_server_config_id(all contain__; built-in wins over dynamic; child chats have no dynamic tools;SubmitToolResultskeyed by pending call IDs). appurl.IsPreviewIDregex is anchored (^…$, tested), sopreview_idis safe to splice into a hostname; ports 1–999 are excluded consistently withPortRegex.- Lock ordering in
getPreviewWorkspacehas no inversion; dial identity (AsChatd→ServerTailnet.AgentConn) is pre-existing for all workspace tools.
ff3deae to
885deea
Compare
d929b3d to
85e2217
Compare
85e2217 to
4def999
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: 7 of 8 files reviewed, 1 unsupported; 22 rules.
Candidates: 384 found, 379 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; 5 questions, 5 answered, 0 abstained, 0 cached; 0 uncalibrated findings held back; 5262 input and 1400 output tokens.
4def999 to
b21d1ad
Compare
d766e67 to
f5c9b86
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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f5c9b86478
ℹ️ 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".
f5c9b86 to
95cad1c
Compare
5daa3e1 to
4774502
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 9 files reviewed, 1 unsupported; 53 rules.
Candidates: 1110 found, 1077 outside changed lines, 1 already discussed, 32 evaluated.
Findings: 1 (1 sites, 0 pre-existing sites), 0 over budget, 0 failed.
Model: deepseek/deepseek-v4.1-flash; 32 questions, 30 answered, 2 abstained (unsure 1, truncated 1), 8 cached; 0 uncalibrated findings held back; 32724 input and 39077 output tokens.
4774502 to
970ff4f
Compare
970ff4f to
45a8a8f
Compare
|
/coder-agents-review |
|
Chat: Review posted | View chat Review history
deep-review v0.13.0 | Round 8 | Last posted: Round 8, 35 findings (5 P2, 7 P3, 15 P4, 8 Nit), APPROVE. Review Finding inventoryFinding inventory: PR #31284Findings
Contested and acknowledgedCRF-3 (P4, openpreview.go:82) - Tool description hardcodes agent's 64-path limit
CRF-9 (P2, coderd.go:948) - open_preview has no experiment or operator switch
CRF-14 (P3, turn_environment.go:2581) - enablePreviews gate untested
CRF-15 (P4, openpreview.go:108) - Port upper bound untested
CRF-16 (P4, openpreview.go:94) - Neither-port-nor-path case untested
CRF-18 (P4, openpreview.go:109) - Port error does not explain the range
CRF-20 (P4, turn_environment.go:2581) - open_preview name not reserved from dynamic tools when previews are disabled
CRF-21 (P4, openpreview.go:62) - IsPreviewURLPath is a URL rule living in chattool
CRF-22 (P4, chatd_internal_test.go:1340) - getWorkspaceConn agent-switch test replaced by a getPreviewWorkspace test
CRF-4 (P4, turn_environment.go:1982) - getPreviewWorkspace re-check branch untested
CRF-40 (P4, turn_environment.go:1928) - conn/workspace pairing still breakable in the selectWorkspace window
CRF-43 (P3, turn_environment.go:1920) - lazy-validation switch caches the conn under the returned row's workspace ID
Round logRound 1Full panel (round 1). Netero first pass: 1 P2, 1 P3, 1 P4, 2 Notes, no P0, panel proceeded. Law not run (effective additions 358). Panel: ging-go, kurapika, melody, ryosuke, takumi, luffy (specialists); pariston, mafuuu (core); bisky, gon, leorio, hisoka (pool); zoro (wildcard). Multi-domain target 9-12; 13 including wildcard. Result: 5 P2, 6 P3, 8 P4, 3 Nit posted; 8 dropped; 5 out of scope. CRF-13 severity settled at P3: Gon's P2 asked the agent to write prose, which repo AGENTS.md:75 forbids, but AGENTS.md:76 requires reviewers to flag added TODOs. Reviewed against 2209295..2ce038a. Round 2Fix round (no files added or moved;
Round 3Fix round (no files added or moved; changes fix CRF-11, CRF-14, CRF-31, CRF-40). Churn guard PROCEED: 4 addressed, 1 contested (CRF-4). Netero (reused) no new findings; accepted CRF-4. Panel: bisky (reused for CRF-4 dispute); razor (CRF-40 raiser); gon, leorio (comment changes, CRF-11 and CRF-31); takumi (locking change); ging-go, ryosuke (Go specialists); chopper (wildcard). CRF-4 closed 2/2. CRF-11, CRF-14, CRF-31 verified fixed. CRF-40 re-raised at P4 (Razor, Takumi converge on the selectWorkspace window). New: CRF-43 P3, CRF-44 P3, CRF-45 Nit. Gon's doc-comment bloat dropped. Reviewed against 96a0777..31acf45. Round 4Fix round (changes fix CRF-44, CRF-45; turn_environment.go unchanged). Churn guard PROCEED: 2 addressed, 2 contested. Netero (reused) no new findings, verified CRF-44 by mutation. Panel: razor, takumi (CRF-40 raisers, reused), ryosuke (CRF-43 raiser, reused), chopper (CRF-44 raiser, reused), gon (CRF-45 raiser, reused), zoro (wildcard, new). CRF-43 closed 1/1. CRF-40 re-raised by Razor with a real-entry-point repro; Takumi accepted. New: CRF-50 P4. Reviewed against be60b96..9554451. Round 5Fix round (changes fix CRF-40, CRF-50). Churn guard PROCEED: 2 addressed, 0 contested. Netero (reused) raised CRF-51 P4 (CRF-40 fix untested, mutation-verified). Panel: razor, takumi (CRF-40 raisers, new agents, no dispute), gon (CRF-50 comment), zoro (wildcard). CRF-40 and CRF-50 verified fixed; Takumi confirmed by scratch test that the CRF-51 test is writable. New: CRF-52 P4, CRF-53 Nit (Gon P2 posted as Nit, consistent with prior comment-wording findings: the comment is accurate and the code below it shows the drop). Reviewed against caf373e..b6ea144. Round 6Fix round (changes fix CRF-51, CRF-52, CRF-53). Churn guard PROCEED: 3 addressed. Netero (reused) verified CRF-51 by mutation and raised CRF-54 Nit. Panel: zoro (CRF-52 raiser), gon (CRF-53 raiser, comments), takumi (locking refactor), knov (wildcard); all new agents. All three fixes verified; Takumi and Knov confirmed the conn/agent/workspace invariant holds at every writer. New: CRF-54 Nit, CRF-55 Nit. Reviewed against caf373e..79a4c67. Round 7Fix round (rename and comment rewording only; fixes CRF-54, CRF-55). Churn guard PROCEED: 2 addressed. Netero (reused) no findings. Panel: gon (CRF-55 raiser), leorio (comments), takumi (locking code), chopper (wildcard); all new agents, all no findings. All in-scope findings resolved. Reviewed against caf373e..baa3150. Round 8Fix round (two-line wording change in openpreview.go; no prior finding targets it). Churn guard PROCEED: 0 open findings. Netero (reused) no findings. Panel: leorio, gon (model-facing text), luffy (product behavior wording), kite (wildcard); all new, all no findings. Kite verified the wording against #31286's toast flow. No open findings. Reviewed against caf373e..1a15f58. About deep-reviewCRF = Coder Review Finding (P0-P4, Nit, Note)
|
There was a problem hiding this comment.
This PR adds the open_preview root-chat tool, registered when a wildcard app hostname is set, and records port or agent-published file previews as tool results. Findings: 5 P2, 6 P3, 8 P4, 3 Nit.
The failing Flake Check run was cancelled by a newer run; it did not fail a test.
Out of scope (needs a ticket or explicit acceptance by a human):
agent/agentpreview/preview.go:110: preview IDs come from a counter that restarts at00000001in each agent process, so an ID issued before a restart names a different path afterward and stored results open the wrong content.agent/agentpreview/api.go:70:handleOpenpassesreq.Pathtoopenwithoutfilepath.IsAbs, so a relative path resolves against the agent's working directory; the coderd check in this PR is the only absolute-path guard.coderd/x/chatd/turn_environment.go:1933:getWorkspaceConncan cache a conn whilec.agentis zeroed by a concurrentclearCachedWorkspaceState, so the cache-hit disconnect check at:1801is skipped for the rest of the turn.coderd/exp_chats_previews.go(#31285): the 1000-65535 port range is written a third time, so a routing change leaves the tool and the endpoint on the old range with no compile error.agent/agentpreview/preview.go:31andagent/agentpreview/api.go:73: the agent errors "previews are closed" and "Could not open preview." do not tell the model that the agent is stopping or what to change.
🤖 This review was automatically generated with Coder Agents.
|
/coder-agents-review |
There was a problem hiding this comment.
Round 2 on c2b194d. Open findings: 2 P3, 2 P4, 1 Nit. One P3 and one Nit are new. One P3 and one P4 were raised again because they were not fully fixed. The P4 re-check gap was dropped in round 1 and is posted now.
The fixes were checked against the code, several by mutation tests: Windows paths reach the agent, preview IDs are random, getWorkspaceConn caches the agent with its conn, agent errors have no transport details, the url_path rules are tested, and the doc and naming changes are in. That fix also resolves the round 1 out-of-scope item about the cache-hit disconnect check. The reviewers who raised them accepted your replies on the 64-path limit, the experiment gate, the port bound and empty-target tests, the port error text, the dynamic tool name, where IsPreviewURLPath lives, and the renamed test. The enablePreviews gate test is still requested (reply on that thread).
Out of scope (needs a ticket or explicit acceptance by a human):
coderd/x/chatd/turn_environment.go:1464: in the state described onturn_environment.go:1928,getWorkspaceConnLockedreturns the previous workspace's conn to every tool, soexecute,read_fileandwrite_filerun on the previous workspace for the rest of the turn while the chat is bound to the new one.agent/agentpreview/preview.go:46: the agent uses "file previews" for single-file previews only, whileappurl.go:151andworkspaceapps/proxy.go:773use it for every preview opened withpath.
Notes:
- CI failures on this head are cancellations by a newer run (
changes,lint,Flake Check), not test failures. - After a lazy-validation switch loses the race to cache its conn,
c.agentis the winning conn's agent whilecurrentChat.AgentIDis the switched agent (turn_environment.go:1960). The PR description states this, and the next cache hit re-checks the winning agent.
🤖 This review was automatically generated with Coder Agents.
|
Out-of-scope items from the round 2 body:
|
|
/coder-agents-review |
There was a problem hiding this comment.
Round 3 on 31acf45. Open findings: 2 P3 (new), 1 P4 (the conn/workspace pairing fix is partial), 1 Nit.
Fixes checked against the code: the RootWithoutPreviews row fails when the gate is forced on, the file preview naming is consistent, and getPreviewWorkspace has its doc comment. The reviewers who raised the untested re-check guard in getPreviewWorkspace accepted your reply. The lock move in getWorkspaceConn closes the dial race from round 2, but two other writers can still put a wrong workspace ID next to the cached conn (reply on the conn/workspace thread, and the inline comment at turn_environment.go:1920). The pairing fix is the same for both: record the workspace ID the conn was dialed for and return it from getWorkspaceConn.
Out of scope (needs a ticket or explicit acceptance by a human):
coderd/database/queries/chats.sql:1488:UpdateChatBuildAgentBindingupdates by chat ID with noworkspace_idguard, so a lazy-validation switch that racescreate_workspacewrites workspace A's build and agent IDs onto a chat already bound to workspace B, and later turns run every workspace tool on A.coderd/x/chatd/turn_environment.go:1572:loadWorkspaceAgentLockedchecks the workspace against thepersistBuildAgentBindingresult in the same way, so the same race caches A's agent under workspace B.coderd/x/chatd/turn_environment.go:1464: the round 2 item about other tools using the previous workspace's conn is only partly fixed. In theselectWorkspacewindow described on the conn/workspace thread,executestill gets A's conn.
Notes:
- Reverting the lock move in
getWorkspaceConn(turn_environment.go:1919) leaves the chatd tests passing; a deterministic test would need a hook between two adjacent statements.
🤖 This review was automatically generated with Coder Agents.
|
On the out-of-scope items in the review body:
|
|
/coder-agents-review |
There was a problem hiding this comment.
Round 4 on 9554451. Open findings: 2 P4 (one new, one contested and raised again).
The outdated-agent 404 message and the noPreviews comment are fixed; a mutation test confirmed the OutdatedAgent row catches the 404 branch. The reviewer who raised the UpdateChatBuildAgentBinding caching issue accepted your reply: the mislabel predates this PR and needs the guarded query, which stays listed as out of scope. The conn/workspace pairing in the selectWorkspace window is still open. The reply on that thread shows it through getPreviewWorkspace, without calling ensureWorkspaceAgent directly. It needs either the small fix there or a human decision to accept the gap.
🤖 This review was automatically generated with Coder Agents.
|
/coder-agents-review |
There was a problem hiding this comment.
Round 5 on b6ea144. Open findings: 2 P4, 1 Nit, all on the conn/workspace pairing fix.
The pairing fix in the ensureWorkspaceAgent mismatch branch is verified: every write that sets c.conn also sets its agent and workspace ID under c.mu, and every write that clears agentLoaded now clears c.conn. The old conn is released once, after unlocking. The comment on the 404 mapping is accurate; the publish handler writes only 400 or 200. The remaining items are a test for the fix, one shared reset helper, and a comment wording change.
🤖 This review was automatically generated with Coder Agents.
|
/coder-agents-review |
There was a problem hiding this comment.
Round 6 on 79a4c67. Open findings: 2 Nits.
The round 5 findings are fixed and verified. A mutation of the ensureWorkspaceAgent mismatch branch now fails TestTurnWorkspaceContext_EnsureWorkspaceAgentIgnoresCachedAgentForDifferentWorkspace. All three full cache clears go through resetCacheLocked, and every write to c.conn keeps its agent and workspace ID with it. The mismatch-branch comment names cachedWorkspaceID. Clearing cachedWorkspaceID in the mismatch branch is safe: if loadWorkspaceAgentLocked fails, the cache is fully empty and the next call reloads from the chat row.
🤖 This review was automatically generated with Coder Agents.
|
/coder-agents-review |
There was a problem hiding this comment.
Round 7 on baa3150. No open findings.
The last two Nits are fixed: the getPreviewWorkspace doc comment lists the three fields it returns, and the cache reset helper is now clearCachedWorkspaceStateLocked at its definition and all three callers. The change since round 6 is the rename and comment wording only; lock order, field writes and release order are unchanged, and the chatd and chattool tests pass with -race.
The out-of-scope items from earlier rounds are unchanged and still need a ticket or explicit acceptance by a human, chiefly the missing workspace_id guard in UpdateChatBuildAgentBinding (coderd/database/queries/chats.sql:1488).
🤖 This review was automatically generated with Coder Agents.
|
/coder-agents-review |
There was a problem hiding this comment.
Round 8 on 1a15f58. No open findings.
The change since round 7 rewords the OpenPreview doc comment and the first sentence of the tool description to say the tool offers a preview that the user opens from the chat. That matches the handler, which only validates, publishes and returns a result, and the #31286 UI, which shows a toast with an "Open preview" action. No other text in the stack still says the tool opens or selects the preview.
The out-of-scope items from earlier rounds still need a ticket or explicit acceptance by a human, chiefly the missing workspace_id guard in UpdateChatBuildAgentBinding (coderd/database/queries/chats.sql:1488).
Out of scope (needs a ticket or explicit acceptance by a human):
- #31286 commit
6c17e4e6b3: the commit message says a freshopen_previewresult selects its preview and shows the tab, but the code now shows a toast and opens the preview only on click, so the squash commit would describe behavior the code no longer has.
Notes:
- The tool is still named
open_previewwhile the description says it offers a preview. The description's first sentence states the behavior, and renaming would change the tool name #31285 selects rows by.
🤖 This review was automatically generated with Coder Agents.
Root chats outside plan mode get open_preview. It takes a short title and exactly one of port (an existing HTTP server) or path (an HTML file or a directory with index.html, published through the workspace agent). The result records the workspace, agent name and either the port or the preview ID the agent returned, which the UI uses to build the preview app URL.
Adds the
open_previewtool for root chats outside plan mode, registered only when the deployment has a wildcard app hostname. It takes a short title and exactly one ofport(an existing HTTP server, optionally at aurl_path) orpath(an.htmlfile, or a directory containingindex.html, published through the workspace agent). The result records the workspace, agent name and either the port or the preview ID that the workspace agent returned, which the UI uses to build the preview app URL. File previews last until the agent restarts (see the agent PR).Decision log
url_pathopens a port preview at a path and query, such as/?folder=/home/coder/project.IsPreviewURLPathaccepts only a path that starts with a single/, with no host, backslash, fragment or control characters, at most 2048 characters, so a client can append it to the preview origin without changing the origin./is stored as empty so the root lists as one preview.preview--<port>hostnames, so they get the same isolation as file previews (see the routing PR).chattool.OpenPreviewResultis the shared typed shape.code-serverapp./out/,/outand symlinks to one path share its ID.workspacetools.AgentAPIErrorMessage, as inedit_files.getWorkspaceConnnow caches the agent with its conn. Before, a parallel call that cleared the cache during a dial (or a lazy-validation agent switch that lost the race) left the cached conn paired with an empty or different agent, sogetPreviewWorkspacecould return an empty agent name. This changes shared chatd code: after such a race, the cached agent follows the conn that was kept.coderd/x/chatd/ARCHITECTURE.mdonly gets a TODO; the prose is left for the human author.Stack (proof of concept for the embedded previews RFC, review bottom-up):
coder_apptabs