agent-orchestrator icon indicating copy to clipboard operation
agent-orchestrator copied to clipboard

security(preview): markdown preview served same-origin with no CSP enables RCE via postCreate config write

Open OrcInc opened this issue 4 months ago • 7 comments

Summary

The browser-panel preview renders workspace Markdown/HTML with raw HTML passthrough and no sanitizer and no Content-Security-Policy, served as text/html at the daemon's own loopback origin (http://127.0.0.1:<port>). That origin hosts the control API, including write endpoints such as PUT /api/v1/projects/{id}/config. A <script> in a previewed file therefore runs same-origin with the API and can set postCreate, which the session manager later runs via sh -c — turning "open the Browser tab on a repo" into arbitrary command execution.

The existing CSRF/origin defense (localControlRequest, backend/internal/httpd/router.go) does not stop this: it rejects Origin-bearing non-loopback requests on state-changing routes, but admits every loopback origin, so a script running inside a same-origin preview at http://127.0.0.1:<port> passes the check cleanly. The defense exists but is structurally bypassable by this vector.

Severity

Medium-to-high — not critical. It is not a privilege escalation for agent-authored content in a normal session (the agent already holds a shell in that worktree). It becomes a real finding where workspace content is not already shell-equivalent:

  1. Unvetted repositories. Cloning/registering a repo to inspect it is not consent to execute it. Opening the Browser tab on a freshly cloned untrusted repo runs its README.md before any agent acts — same class as the VS Code markdown-preview CVEs.
  2. Approval-gate bypass. Harnesses that gate shell commands behind user approval but write files freely: a prompt-injected agent that writes a README.md converts "write a file" into "run a command", bypassing the human-in-the-loop prompt.

Exploitation requires the user to open the Browser tab on a repository they have not vetted — not merely to run the product as designed.

Affected

  • Component: backend/internal/preview + backend/internal/httpd (loopback preview path), sink in backend/internal/session_manager.
  • Version: present at commit a9fd0e0d and on current main.

The chain (each link verified against source)

# Link Location
1 Markdown renderer enables raw HTML passthrough, no sanitizer (ghtml.WithUnsafe()) backend/internal/preview/markdown.go:18
2 <script> survives rendering verbatim verified by executing RenderMarkdown
3 Rendered output served as text/html at daemon origin, no CSP (gosec G705 suppressed on a trust assumption) backend/internal/httpd/controllers/sessions.go (servePreviewMarkdown)
4 Newest .md in the workspace is auto-selected; no user action beyond opening preview backend/internal/preview/entry.go:43
5 Loopback listener has no authentication (authMiddleware runs only on the LAN listener) backend/internal/httpd/auth.go:139, lan_listener.go
6 The localControlRequest origin guard exists but admits every loopback origin — a same-origin preview script (http://127.0.0.1:<port>) passes the check, so write endpoints are reachable backend/internal/httpd/router.go:80-108, :231, controllers/projects.go:31
7 ProjectConfig.Validate never inspects postCreate backend/internal/domain/projectconfig.go:122
8 postCreate reaches sh -c (cmd /c on Windows) with cmd.Dir = workspace backend/internal/session_manager/manager.go:2191
9 Electron hardening (contextIsolation/sandbox) does not contain in-page fetch frontend/src/main/browser-view-host.ts:223

Reproduce (verification-level, no weaponized payload)

  1. Create a repo whose only markdown file is README.md containing a script that fetch()es PUT /api/v1/projects/{id}/config with a body setting postCreate to an innocuous marker command (e.g. writing a sentinel file).
  2. Register it (ao project add) and open the Browser tab on it.
  3. The preview auto-selects README.md, serves it same-origin, and the script executes and writes the project config.
  4. Trigger the workspace-create path (spawn a session); observe the marker command run via sh -c.

The chain is established by code review plus one executed proof (raw-HTML passthrough); no end-to-end exploit was run against a live daemon.

Remediation (in order of value)

  1. Send a restrictive CSP on all preview responses, e.g. default-src 'none'; style-src 'unsafe-inline'; img-src 'self' data:; connect-src 'none'. This breaks the chain even for .html files (which cannot be sanitized). Serving preview from a different origin does not fix it: the daemon needs no cookies and isLoopbackOrigin admits every loopback origin — exactly the gap that lets the existing localControlRequest defense be bypassed same-origin.
  2. Validate postCreate in ProjectConfig.Validate, or require an explicit local-control path for config writes that set it — closes the sink independent of the renderer.
  3. Sanitize Markdown (drop WithUnsafe() or add a sanitizer pass). Lower value than (1); does nothing for .html previews.

Secondary finding — symlink escape in ConfinedPath (arbitrary file read)

ConfinedPath (backend/internal/preview/entry.go:137) confines lexically (filepath.Abs + filepath.Rel + .. check) and never calls filepath.EvalSymlinks, so an in-workspace symlink resolving outside the workspace is served by http.ServeFile. Git can carry symlinks, so an untrusted repo can ship one. Low impact standalone: password-gated over LAN, and over loopback the primary finding already yields execution.

Fix: EvalSymlinks the resolved path and re-check containment against the evaluated root.

Root cause

The comment at markdown.go:14 — "preview content is workspace-local and agent-trusted" — conflates being in the workspace with being authorized to execute. The gosec suppression at sessions.go inherits the same assumption.

Related work

  • #2581 (fix(preview): reject unsafe local file URLs) — confines preview file/absolute-path targets to the session workspace. Partially addresses path traversal but does not add CSP, does not change WithUnsafe(), and leaves ConfinedPath lexical (no EvalSymlinks). Reinforces the same "agent-trusted" assumption at the path level only.
  • #307 (merged) / #1263 (merged) — original TS-era CSRF/origin hardening + loopback default. The Go rewrite ports these as localControlRequest. This issue shows the ported defense is bypassable same-origin.
  • #2449 — renderer-side preview uses marked + DOMPurify, but the daemon-side goldmark renderer (backend/internal/preview/markdown.go, WithUnsafe()) is a separate unsanitized path — the one this issue targets.

OrcInc avatar Jul 17 '26 15:07 OrcInc

Assigning to @Kaushik2003 — the bot account lacks assign permissions on this repo. Could a maintainer (@Priyanchew or @yyovil) assign this when you get a chance?

OrcInc avatar Jul 17 '26 15:07 OrcInc

I'm working on this issue.

Kaushik2003 avatar Jul 17 '26 15:07 Kaushik2003

@Kaushik2003 Valid issue, but a few corrections are needed:

  • localControlRequest does not allow loopback origins, it rejects any request with an Origin header. The config endpoint simply isn’t protected by it; it uses the global CORS middleware, which allows loopback origins.
  • Electron’s sandbox and disabled Node integration do not disable ordinary browser JavaScript. Raw <script> content in the preview still executes and can call the same origin daemon API.
  • I’d rate this medium severity, not high: it requires attacker controlled workspace content to be loaded in the desktop preview and postCreate executes during a subsequent spawn. Impact is lower when the agent already has unrestricted shell access, but it can bypass approval-gated workflows.
  • Validating postCreate alone is not sufficient because arbitrary shell commands are its intended behavior. Preview isolation plus stricter API origin controls/CSP is the stronger fix.
  • The symlink escape appears valid but should be tracked separately as an arbitrary file read issue.

aprv10 avatar Jul 17 '26 18:07 aprv10

Thanks for contributing to Agent Orchestrator.

This issue is being picked up by the current external contributor on-call pair:

  • @neversettle17-101
  • @somewherelostt

If someone is already working on this, please continue as usual. The on-call pair is added for visibility, tracking, and support, not to take over the work. If you need help with review, direction, reproduction, or next steps, please tag @neversettle17-101 and @somewherelostt here.

For faster context or live questions, you can also join the AO Discord.

Join the session here: https://discord.gg/H6ZDcUXmq

Come by if you want to see what is being built, ask questions, or just hang around with the community.

somewherelostt avatar Jul 18 '26 08:07 somewherelostt

Assigned to @Vaibhaav-Tiwari

Rishet11 avatar Sep 14 '26 21:09 Rishet11

Assigned to @Vaibhaav-Tiwari

Rishet11 avatar Sep 27 '26 17:09 Rishet11

Fixed by #7077 (sandboxed previews, exact renderer-origin checks); the symlink escape was handled earlier by #2818. Both on main.

Rishet11 avatar Oct 25 '26 16:10 Rishet11