From 2503a38f505831d23011485343d5d41a1472d267 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Mon, 19 Oct 2026 17:54:19 +0100 Subject: [PATCH] web: proxy /artifacts/ through serve.mjs so images actually load The page resolves artifact URLs against the configured server. In a proxied deployment that is the page's own origin, and serve.mjs only proxied /connect, so every artifact request 404'd: broken gallery thumbnails, a blank viewer and no session.html. Forward GET/HEAD /artifacts/ to the same upstream and stream the reply (status, headers, body). Verified: 10/10 thumbnails load on run 430 and the viewer's image has naturalWidth > 0. --- web/AGENTS.md | 4 +++- web/README.md | 11 +++++++---- web/serve.mjs | 38 ++++++++++++++++++++++++++++++++++---- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/web/AGENTS.md b/web/AGENTS.md index 3027b45a..4d1468f1 100644 --- a/web/AGENTS.md +++ b/web/AGENTS.md @@ -80,7 +80,9 @@ default macOS location. an upgrade whose `Origin` host does not match the request `Host`. A page served from port 8090 opening `ws://HOST:8080/connect` is refused with **403**. The static server therefore also proxies `/connect` (dropping `Origin`), so the page -and the socket are one origin. **Do not "fix" this by relaxing the server's Origin +and the socket are one origin. It proxies `/artifacts/` too: artifact URLs resolve +against the configured server (the page origin when proxied), so without it images, +the viewer and `session.html` 404 even though the socket works. **Do not "fix" this by relaxing the server's Origin check** — that check is the only thing standing between a random web page and an unauthenticated control API. Keep the proxy. diff --git a/web/README.md b/web/README.md index 1b507ee1..7e2893ce 100644 --- a/web/README.md +++ b/web/README.md @@ -29,7 +29,7 @@ what is done; this file covers how to run, test and verify it. ```sh cd web -node serve.mjs # http://127.0.0.1:8090/ (proxies /connect -> 127.0.0.1:8080) +node serve.mjs # http://127.0.0.1:8090/ (proxies /connect and /artifacts/ -> 127.0.0.1:8080) node serve.mjs --host 0.0.0.0 # reachable from other devices (trusted LAN/VPN) node serve.mjs --port 8095 --pipelines 127.0.0.1:8080 ``` @@ -76,7 +76,7 @@ keeping them out of the tree keeps a delivery checkout clean. | Feature logic and models | `src/runs.js`, `src/runDetail.js`, `src/changes.js`, `src/artifacts.js`, `src/pipelines.js`, `src/templates.js`, `src/graph.js`, `src/chat.js`, `src/markdown.js`, `src/search.js`, `src/attention.js`, `src/actions.js`, `src/connection.js`, `src/settings.js`, `src/notify.js`, `src/runEvents.js`, `src/routes.js` | | Shell and views | `src/app.js` (boot, state, navigation, render dispatch), view modules under `src/views/` | | Styling | per-area stylesheets linked from `index.html` | -| Server + `/connect` proxy | `serve.mjs` | +| Server + `/connect` and `/artifacts/` proxy | `serve.mjs` | | Tests | `test/*.test.mjs`, `test/smoke.mjs`, `test/browser.mjs`, `test/ui-smoke.mjs` | | Acceptance gate | `PARITY.md` | | Working map | `AGENTS.md` | @@ -97,8 +97,11 @@ request Origin "HOST:8090" is not authorized for Host "HOST:8080" ``` A Node or `curl` client sends no `Origin` and is accepted — which is why the -protocol smoke can pass while a browser cannot. `serve.mjs` therefore also proxies -`/connect`, dropping `Origin`, so the page and the socket are one origin. +protocol smoke can pass while a browser cannot. `serve.mjs` therefore proxies `/connect` (dropping `Origin`) so the page and +the socket are one origin. It proxies `/artifacts/` for the same reason: artifact +URLs resolve against the configured server, which in a proxied deployment is the +page's own origin, so the gallery's images, the viewer, `session.html` and +downloads would otherwise 404 while the WebSocket worked. **Do not "fix" this by relaxing the server's Origin check** — that check is the only thing between a random web page and an unauthenticated control API. Keep the diff --git a/web/serve.mjs b/web/serve.mjs index 23c55e8d..0b12d85a 100644 --- a/web/serve.mjs +++ b/web/serve.mjs @@ -1,8 +1,10 @@ // Zero-dependency static file server + WebSocket proxy for the spike. // node serve.mjs [--port 8090] [--host 0.0.0.0] [--pipelines 127.0.0.1:8080] // -// Serves this directory and proxies GET/Upgrade /connect to the Pipelines -// server, so the page and the WebSocket share one origin. +// Serves this directory, proxies GET/Upgrade /connect and GET/HEAD /artifacts/ to +// the Pipelines server, so the page, the WebSocket and artifact files share one +// origin. The API and its artifacts live on the server's port; the page is served +// from another, so artifact URLs resolved against the page would 404 without this. // // Why the proxy exists: internal/server/websocket.go calls // `websocket.Accept(w, r, nil)` (github.com/coder/websocket), which rejects an @@ -13,7 +15,7 @@ // server has no authentication and needs none. // // The default host is 127.0.0.1. Use --host 0.0.0.0 only on a trusted LAN/VPN. -import { createServer } from "node:http"; +import { createServer, request } from "node:http"; import { readFile, stat } from "node:fs/promises"; import { extname, join, normalize, resolve, sep } from "node:path"; import { fileURLToPath } from "node:url"; @@ -94,8 +96,36 @@ async function serveIndex(res) { res.end(body); } +/** + * Proxy one artifact request to the Pipelines server. Artifact URLs are resolved + * against the configured server, which in a proxied deployment is the page's own + * origin, so without this the gallery's thumbnails, the viewer and the session + * export would all 404 while the WebSocket worked. + */ +function proxyArtifact(req, res) { + const headers = { ...req.headers }; + delete headers.origin; + headers.host = `${targetHost}:${targetPort}`; + const upstream = request( + { host: targetHost, port: targetPort, method: req.method, path: req.url, headers }, + (reply) => { + res.writeHead(reply.statusCode ?? 502, reply.headers); + reply.pipe(res); + }, + ); + upstream.on("error", () => { + if (!res.headersSent) res.writeHead(502, { "Content-Type": "text/plain; charset=utf-8" }); + res.end("Artifact unavailable"); + }); + req.pipe(upstream); +} + const server = createServer(async (req, res) => { const pathname = (req.url ?? "/").split("?")[0]; + if (pathname.startsWith("/artifacts/")) { + proxyArtifact(req, res); + return; + } if (pathname === "/favicon.ico") { res.writeHead(204).end(); // no icon; keeps the console free of a 404 return; @@ -189,5 +219,5 @@ const host = flag(process.argv, "--host", "127.0.0.1"); server.listen(port, host, () => { const shown = host === "0.0.0.0" ? "127.0.0.1" : host; console.log(`web spike: http://${shown}:${port}/ (bound ${host}:${port})`); - console.log(`proxying /connect -> ws://${targetHost}:${targetPort}/connect`); + console.log(`proxying /connect (ws) and /artifacts/ (http) -> ${targetHost}:${targetPort}`); });