Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Fix conversation file links (#642)

Joshua Francebbe249ec7742
diff --git a/plugins/web-ui/server/index.ts b/plugins/web-ui/server/index.ts
index 42cabfcb2..4c15b1c6f 100644
--- a/plugins/web-ui/server/index.ts
+++ b/plugins/web-ui/server/index.ts
@@ -1272,6 +1272,43 @@ const apiRoutes: readonly WebRoute[] = [
return relayCore(res, "GET", `/v1/sessions/${encodeURIComponent(id)}?${qs.toString()}`);
},
},
+ {
+ method: "GET",
+ path: "/api/files/by-name/content",
+ handle: async (c) => {
+ const { res, url, user } = c;
+ const name = url.searchParams.get("name")?.trim();
+ if (!name) return json(res, 400, { error: "bad_request", message: "name required" });
+ let cursor: string | undefined;
+ let match: { id: string; createdAt: number } | undefined;
+ for (let page = 0; page < 50; page++) {
+ const qs = new URLSearchParams({ viewer: user, limit: "200" });
+ if (cursor) qs.set("cursor", cursor);
+ const listed = await coreFetch("GET", `/v1/files?${qs.toString()}`);
+ if (listed.status !== 200) return relay(res, listed);
+ let body: {
+ owned?: Array<{ id?: string; name?: string; createdAt?: number; openable?: boolean }>;
+ shared?: Array<{ id?: string; name?: string; createdAt?: number; openable?: boolean }>;
+ nextCursor?: string;
+ };
+ try {
+ body = JSON.parse(listed.text) as typeof body;
+ } catch {
+ return json(res, 502, { error: "upstream_error" });
+ }
+ for (const file of [...(body.owned ?? []), ...(body.shared ?? [])]) {
+ if (file.name !== name || file.openable === false || typeof file.id !== "string") continue;
+ const createdAt = typeof file.createdAt === "number" ? file.createdAt : 0;
+ if (!match || createdAt > match.createdAt) match = { id: file.id, createdAt };
+ }
+ cursor = body.nextCursor;
+ if (!cursor) break;
+ }
+ if (!match) return json(res, 404, { error: "not_found" });
+ res.writeHead(302, { location: `/api/files/${encodeURIComponent(match.id)}/content` });
+ return res.end();
+ },
+ },
{
method: "GET",
path: "/api/files/:id/content",
diff --git a/plugins/web-ui/src/markdown-sanitize.ts b/plugins/web-ui/src/markdown-sanitize.ts
index 81dadd7d4..85d80dce2 100644
--- a/plugins/web-ui/src/markdown-sanitize.ts
+++ b/plugins/web-ui/src/markdown-sanitize.ts
@@ -7,10 +7,36 @@ export const MARKDOWN_SANITIZE_CONFIG: Config = {
ADD_ATTR: ["target", "encoding"],
};
+const SANDBOX_WORKSPACE_LINK = /\shref=(["'])sandbox:\/home\/sprite\/workspace\/([^"']+)\1/gi;
+
+function decodedBasename(path: string): string | null {
+ const encoded = path.split("/").at(-1);
+ if (!encoded) return null;
+ try {
+ return decodeURIComponent(encoded);
+ } catch {
+ return null;
+ }
+}
+
+export function rewriteSandboxFileLinks(html: string): string {
+ return html.replace(SANDBOX_WORKSPACE_LINK, (match, quote: string, path: string) => {
+ const name = decodedBasename(path);
+ if (!name) return match;
+ const href = `/api/files/by-name/content?name=${encodeURIComponent(name)}`;
+ return ` href=${quote}${href}${quote}`;
+ });
+}
+
let installed = false;
export function installMarkdownSanitizer(): void {
if (installed) return;
installed = true;
- marked.use({ hooks: { postprocess: (html: string) => String(DOMPurify.sanitize(html, MARKDOWN_SANITIZE_CONFIG)) } });
+ marked.use({
+ hooks: {
+ postprocess: (html: string) =>
+ String(DOMPurify.sanitize(rewriteSandboxFileLinks(html), MARKDOWN_SANITIZE_CONFIG)),
+ },
+ });
}
diff --git a/plugins/web-ui/test/markdown-sanitize.test.ts b/plugins/web-ui/test/markdown-sanitize.test.ts
index 6da17ade3..ba7b3deef 100644
--- a/plugins/web-ui/test/markdown-sanitize.test.ts
+++ b/plugins/web-ui/test/markdown-sanitize.test.ts
@@ -4,7 +4,7 @@ import { marked } from "marked";
import katex from "katex";
import { JSDOM } from "jsdom";
import createDOMPurify from "dompurify";
-import { MARKDOWN_SANITIZE_CONFIG } from "../src/markdown-sanitize.ts";
+import { MARKDOWN_SANITIZE_CONFIG, rewriteSandboxFileLinks } from "../src/markdown-sanitize.ts";
const DOMPurify = createDOMPurify(new JSDOM("").window as unknown as Window & typeof globalThis);
const sanitize = (html: string): string => DOMPurify.sanitize(html, MARKDOWN_SANITIZE_CONFIG) as string;
@@ -12,7 +12,7 @@ const sanitize = (html: string): string => DOMPurify.sanitize(html, MARKDOWN_SAN
const renderer = new marked.Renderer();
const originalLink = renderer.link.bind(renderer);
renderer.link = (token) => originalLink(token).replace("<a ", '<a target="_blank" rel="noopener noreferrer" ');
-const render = (md: string): string => sanitize(marked.parse(md, { async: false, renderer }));
+const render = (md: string): string => sanitize(rewriteSandboxFileLinks(marked.parse(md, { async: false, renderer })));
test("strips every script-bearing vector marked would otherwise pass through", () => {
const vectors: Array<[string, string]> = [
@@ -34,6 +34,11 @@ test("strips every script-bearing vector marked would otherwise pass through", (
}
});
+test("turns sandbox workspace links into real file-library downloads", () => {
+ const out = render("[download](sandbox:/home/sprite/workspace/reports/interview-list.csv)");
+ assert.match(out, /href="\/api\/files\/by-name\/content\?name=interview-list\.csv"/);
+});
+
test("preserves ordinary links (target=_blank), code fences, and inline PNG images", () => {
const link = render("[docs](https://example.com/a)");
assert.match(link, /href="https:\/\/example\.com\/a"/);
diff --git a/plugins/web-ui/test/sandbox-link-download.test.ts b/plugins/web-ui/test/sandbox-link-download.test.ts
new file mode 100644
index 000000000..0ee1159e3
--- /dev/null
+++ b/plugins/web-ui/test/sandbox-link-download.test.ts
@@ -0,0 +1,63 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { createServer, type IncomingMessage } from "node:http";
+import type { AddressInfo } from "node:net";
+
+const calls: string[] = [];
+const core = createServer((req: IncomingMessage, res) => {
+ calls.push(req.url ?? "");
+ if ((req.url ?? "").startsWith("/v1/files?")) {
+ res.writeHead(200, { "content-type": "application/json" });
+ return void res.end(
+ JSON.stringify({
+ owned: [
+ {
+ id: "artifact-1",
+ name: "interview-list.csv",
+ createdAt: 10,
+ openable: true,
+ },
+ ],
+ shared: [],
+ }),
+ );
+ }
+ res.writeHead(404, { "content-type": "application/json" });
+ res.end(JSON.stringify({ error: "not_found" }));
+});
+await new Promise<void>((resolve) => core.listen(0, resolve));
+const coreUrl = `http://localhost:${(core.address() as AddressInfo).port}`;
+
+process.env.NODE_ENV = "test";
+process.env.ALLOW_UNSIGNED_TEST_IDENTITY = "1";
+process.env.CORE_API_URL = coreUrl;
+process.env.CORE_SIGNING_SECRET = "sandbox-link-download-test";
+process.env.WEB_UI_PRINCIPALS = "alice";
+
+const { handler } = await import("../server/index.ts");
+const surface = createServer((req, res) => void handler(req, res));
+await new Promise<void>((resolve) => surface.listen(0, resolve));
+const base = `http://localhost:${(surface.address() as AddressInfo).port}`;
+
+test.after(() => {
+ surface.close();
+ core.close();
+});
+
+test("sandbox markdown download resolves an authorized file-library item", async () => {
+ const response = await fetch(`${base}/api/files/by-name/content?name=interview-list.csv`, {
+ headers: { cookie: "webuiuser=alice" },
+ redirect: "manual",
+ });
+ assert.equal(response.status, 302);
+ assert.equal(response.headers.get("location"), "/api/files/artifact-1/content");
+ assert.ok(calls.some((url) => url.startsWith("/v1/files?") && url.includes("viewer=alice")));
+});
+
+test("sandbox markdown download stays unavailable when no visible file matches", async () => {
+ const response = await fetch(`${base}/api/files/by-name/content?name=missing.csv`, {
+ headers: { cookie: "webuiuser=alice" },
+ redirect: "manual",
+ });
+ assert.equal(response.status, 404);
+});