// The chat page's local service holds the user's magpie gateway key, their // conversations and their MCP tools, so it answers only the chat page it // served: a request needs the per-process token from that page's HTML and // this service's own Host. Everything here runs against a fake gateway on a // free port, with a temporary config folder; nothing reaches the network. import { afterAll, beforeAll, expect, test } from "bun:test" import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs" import { request } from "node:http" import { runInNewContext } from "node:vm" import { tmpdir } from "node:os" import { join } from "node:path" const dir = import.meta.dir const tmp = mkdtempSync(join(tmpdir(), "page-chat-")) const cfg = join(tmp, "magpie") mkdirSync(cfg, { recursive: true }) writeFileSync(join(cfg, "caller-keys.json"), JSON.stringify([{ name: "chat", secret: "mk-test-key" }])) process.env.XDG_CONFIG_HOME = tmp delete process.env.MAGPIE_API_KEY delete process.env.MAGPIE_GATEWAY_KEY delete process.env.MAGPIE_ADDR const seen = [] // magpie's own MCP servers the fake gateway lists on /v1/magpie/mcp; null // answers 404, as a magpie from before the list does let ownList = null const ownAuth = [] const gateway = Bun.serve({ port: 0, hostname: "127.0.0.1", async fetch(req) { const url = new URL(req.url) if (url.pathname === "/v1/magpie/mcp") { return ownList ? Response.json({ servers: ownList }) : new Response("no", { status: 404 }) } if (url.pathname.startsWith("/mcp/magpie/")) { // one of magpie's own servers: its tool is named for its path ownAuth.push(req.headers.get("authorization")) const msg = await req.json() if (msg.id === undefined) return new Response(null, { status: 202 }) const tool = "own_" + url.pathname.slice("/mcp/magpie/".length).replace(/\W/g, "_") const result = msg.method === "initialize" ? { protocolVersion: "2025-03-26", capabilities: { tools: {} }, serverInfo: { name: tool, version: "1" } } : msg.method === "tools/list" ? { tools: [{ name: tool, description: "search " + tool, inputSchema: { type: "object", properties: {} } }] } : {} return Response.json({ jsonrpc: "2.0", id: msg.id, result }) } if (url.pathname === "/v1/models") return Response.json({ data: [{ id: "fake/model", owned_by: "fake" }] }) if (url.pathname === "/v1/chat/completions") { seen.push({ auth: req.headers.get("authorization"), body: await req.json() }) const chunk = (o) => `data: ${JSON.stringify(o)}\n\n` const body = chunk({ choices: [{ delta: { content: "hel" } }] }) + chunk({ choices: [{ delta: { content: "lo" }, finish_reason: "stop" }] }) + "data: [DONE]\n\n" return new Response(body, { headers: { "content-type": "text/event-stream" } }) } return new Response("no", { status: 404 }) }, }) process.env.MAGPIE_CHAT_GATEWAY = `http://127.0.0.1:${gateway.port}` let svc, port, token beforeAll(async () => { const { startServer } = await import("./lib/server.mjs") svc = await startServer({ configDir: cfg, options: { port: 0 } }) port = svc.port const page = await raw("GET", "/") token = /name="magpie-chat-token" content="([0-9a-f]+)"/.exec(page.body)?.[1] }) afterAll(async () => { await svc?.close() gateway.stop(true) rmSync(tmp, { recursive: true, force: true }) }) // node:http, so the test sets Host and Origin exactly as a hostile page would function raw(method, path, headers = {}, body) { return rawOn(port, method, path, headers, body) } function rawOn(port, method, path, headers = {}, body) { return new Promise((resolve, reject) => { const req = request({ host: "127.0.0.1", port, method, path, headers: { host: `127.0.0.1:${port}`, ...headers } }, (res) => { let data = "" res.setEncoding("utf8") res.on("data", (c) => (data += c)) res.on("end", () => resolve({ status: res.statusCode, headers: res.headers, body: data })) }) req.on("error", reject) if (body !== undefined) req.write(typeof body === "string" ? body : JSON.stringify(body)) req.end() }) } const chatBody = { model: "fake/model", messages: [{ role: "user", content: "hi" }] } const json = { "content-type": "application/json" } test("the page's HTML carries the token and no CORS header", async () => { const res = await raw("GET", "/", { origin: "null" }) expect(res.status).toBe(200) expect(token).toMatch(/^[0-9a-f]{64}$/) expect(res.headers["access-control-allow-origin"]).toBeUndefined() }) test("a page without the token can't chat or read conversations (Origin null, as a sandboxed frame sends)", async () => { const before = seen.length const chat = await raw("POST", "/api/chat", { ...json, origin: "null" }, chatBody) expect(chat.status).toBe(403) expect(seen.length).toBe(before) // nothing went to the gateway for (const path of ["/api/conversations", "/api/settings", "/api/bootstrap", "/api/library", "/api/mcp/tools"]) { expect((await raw("GET", path, { origin: "null" })).status).toBe(403) expect((await raw("GET", path)).status).toBe(403) // no Origin at all } const wrong = await raw("GET", "/api/conversations", { origin: "null", "x-magpie-chat-token": "0".repeat(64) }) expect(wrong.status).toBe(403) expect(JSON.parse(wrong.body).reload).toBe(true) }) test("another site's Origin is refused even with the token", async () => { const res = await raw("GET", "/api/conversations", { origin: "https://evil.example", "x-magpie-chat-token": token }) expect(res.status).toBe(403) }) test("a DNS-rebound Host is refused, the page and the API both", async () => { for (const path of ["/", "/api/conversations", "/api/ping"]) { const res = await raw("GET", path, { host: `evil.example:${port}`, "x-magpie-chat-token": token }) expect(res.status).toBe(403) expect(res.body).not.toContain(token) } expect((await raw("GET", "/api/ping", { host: `localhost:${port}` })).status).toBe(200) }) test("a preflight lets the token header through", async () => { const res = await raw("OPTIONS", "/api/chat", { origin: "null", "access-control-request-headers": "content-type, x-magpie-chat-token" }) expect(res.status).toBe(204) expect(res.headers["access-control-allow-headers"]).toContain("x-magpie-chat-token") }) test("with the token, a chat streams from the gateway with the caller key", async () => { const before = seen.length const res = await raw("POST", "/api/chat", { ...json, origin: "null", "x-magpie-chat-token": token }, chatBody) expect(res.status).toBe(200) const events = res.body.split("\n\n").filter(Boolean).map((b) => JSON.parse(b.split("\n").find((l) => l.startsWith("data: ")).slice(6))) expect(events.filter((e) => e.type === "delta").map((e) => e.text).join("")).toBe("hello") expect(events.at(-1).type).toBe("done") expect(seen.length).toBe(before + 1) const got = seen.at(-1) expect(got.auth).toBe("Bearer mk-test-key") expect(got.body.model).toBe("fake/model") expect(got.body.stream).toBe(true) expect(got.body.messages.at(-1)).toEqual({ role: "user", content: "hi" }) }) function chatPage(setup = "") { const page = { document: { readyState: "loading", querySelector: () => ({ getAttribute: () => token }), addEventListener() {} }, TextDecoder, fetch: (path, init) => { if (path !== "api/chat") throw new Error("unexpected request: " + path) return fetch(`http://127.0.0.1:${port}/api/chat`, init) }, window: { addEventListener() {} }, location: { search: "" }, MagpieEpub: { isEpub: f => f.name?.endsWith(".epub") }, } runInNewContext(readFileSync(join(dir, "chat/app.js"), "utf8") + "\n" + setup, page) return page } test("a saved EPUB reaches the gateway once, including on a follow-up", async () => { const page = chatPage("toast = message => globalThis.warnings.push(message);") page.warnings = [] const chapters = "The lamp is blue.\n\nThe lamp goes out." const file = { name: "lamp.epub", kind: "file", data: "data:;base64,Ym9vaw==" } const image = { name: "lamp.png", kind: "image", data: "data:image/png;base64,cG5n" } const broken = { ...file, name: "broken.epub", data: "data:;base64,YmFk" } const h = { ...json, "x-magpie-chat-token": token } page.MagpieEpub.read = async data => { if (data === broken.data) throw new Error("damaged") expect(data).toBe(file.data) return { text: chapters, imagePages: 1 } } const past = [ { role: "user", content: "", files: [file, image] }, { role: "assistant", content: "A blue lamp." }, { role: "user", content: "What changed?", files: [{ ...file, name: "copy.epub" }, broken] }, ] const saved = JSON.parse((await raw("POST", "/api/conversations", h, { messages: past })).body) const history = JSON.parse((await raw("GET", "/api/conversations/" + saved.id, h)).body).messages const events = [] await page.streamChat({ ...chatBody, messages: history }, event => events.push(event)) expect(events.at(-1).type).toBe("done") const sent = seen.at(-1).body.messages expect(sent.at(-3).content[0].text).toContain(chapters) expect(sent.at(-3).content[0].text).toContain("1 个章节仅含图片") expect(sent.at(-3).content[1]).toEqual({ type: "image_url", image_url: { url: image.data } }) expect(sent.at(-1).content).toContain("What changed?") expect(sent.at(-1).content).toContain("broken.epub:damaged") expect(page.warnings).toEqual(["旧附件未发送。broken.epub:damaged"]) expect(JSON.stringify(sent).match(/The lamp goes out/g)).toHaveLength(1) expect(JSON.stringify(sent)).not.toContain("Ym9vaw==") expect(history[0].content).toBe("") expect(history[0].files[0].data).toBe(file.data) }) test("switching chats discards a book that is still being read", async () => { const page = chatPage(` updateSend = renderAttachments = renderMessages = renderSessions = scrollBottom = () => {}; api = async () => ({ id: "other", messages: [] }); readAsDataURL = () => globalThis.reading; globalThis.pending = () => pendingBooks; `) const reading = Promise.withResolvers() page.reading = reading.promise page.MagpieEpub.read = async () => ({ text: "A page", characters: 5 }) page.state.attachments.push({ name: "ready.epub" }) const job = page.addFiles([{ name: "first.epub", type: "application/epub+zip", size: 12 }]) expect(page.pending()).toBe(1) page.openConversation("other") await Promise.resolve() reading.resolve("data:;base64,Ym9vaw==") await job expect(page.state.convId).toBe("other") expect(page.state.attachments).toHaveLength(0) expect(page.pending()).toBe(0) }) test("conversations save and list with the token", async () => { const h = { ...json, origin: "null", "x-magpie-chat-token": token } const saved = JSON.parse((await raw("POST", "/api/conversations", h, { title: "t", messages: [{ role: "user", content: "x" }] })).body) expect(saved.id).toBeTruthy() const list = JSON.parse((await raw("GET", "/api/conversations", h)).body) expect(list.items.some((c) => c.id === saved.id)).toBe(true) }) test("a remote provider icon redirects with a Location", async () => { writeFileSync(join(cfg, "providers.json"), JSON.stringify([{ id: "remote", icon: "https://example.com/i.png" }])) const res = await raw("GET", "/api/icon/remote") expect(res.status).toBe(302) expect(res.headers.location).toBe("https://example.com/i.png") const badge = await raw("GET", "/api/icon/nothing-here") expect(badge.status).toBe(200) expect(badge.headers["content-type"]).toContain("image/svg+xml") }) test("the launcher page only navigates: no fetch, XHR or socket, scripts from files", () => { const boot = readFileSync(join(dir, "page", "boot.js"), "utf8").replace(/^\s*\/\/.*$/gm, "") expect(boot).not.toMatch(/\bfetch\s*\(|XMLHttpRequest|WebSocket|EventSource|sendBeacon/) expect(boot).toContain("location.replace") const html = readFileSync(join(dir, "page", "index.html"), "utf8") expect(html).toContain('src="_magpie/bridge.js"') expect(html).toContain('src="boot.js"') expect(html).not.toMatch(/