Files
plugins/packages/agent-console/test/v1/pr535-http.test.ts
T
qingnian108 a431798fe1 agent-console 1.0.0: a page plugin that drives the local Agent CLIs (#535)
Adds Agent Console (@magpie-community/page-agent-console 1.0.0), a community page plugin that drives the Agent CLIs on this computer (Claude Code, Codex, ACP agents) through a local daemon on 127.0.0.1, with its source in packages/agent-console and a registry entry. Claude Code is the user's own `claude` CLI driven over stream-json (daemon/src/providers/claude/cli.ts); no Anthropic SDK is installed, bundled or in the branch's history. Cold install ~45 MB (node-pty ~26 MB), stated in the README and registry summary in en and zh.

By qingnian108 (#535).

What was run at 8e8f910 (mote, macOS, throwaway HOME, no real accounts):
- packages/agent-console: `npm ci --ignore-scripts --workspaces=false`; `node scripts/verify-build.mjs`: all five bundles byte-identical (daemon/dist/main.js 08c95aa3…, app/app.js 9a6911f3…); `vitest run`: 45 files, 254 passed, 17 skipped; `npm run check` clean.
- Real Claude Code 2.1.316 (npm @anthropic-ai/claude-code, sandbox HOME/CLAUDE_CONFIG_DIR, local fake Messages API): `AC_REAL_CLAUDE=… vitest run --config vitest.real.config.ts test/real/claude-cli.real.test.ts`: 4 of 4 (initialize lists 51 slash commands, streaming turn, Bash permission approved and denied, interrupt, --resume with set_model).
- Probe: the driver's exact argv with `--permission-mode bypassPermissions --allow-dangerously-skip-permissions`: the tool runs with no can_use_tool request.
- Repo root: `HOME=$(mktemp -d) bun test`: 4006 tests, 0 fail; `bun scripts/check.mjs` exit 0.
- Install as magpie does: `npm pack --ignore-scripts` (552 KB) then `bun add --ignore-scripts`: 45 MB node_modules, no @anthropic-ai/*, index.mjs imports.
- Read every changed line since 41c7a94: argv values limited to SAFE_ARG tokens (no cmd.exe metacharacters reach the shim path), no new network hosts, no install scripts, no eval; no commit in main..8e8f910 contains claude-agent-sdk.

Not tried: magpie's desktop window loading the page; a Windows .cmd/.ps1 claude shim end to end.
2026-10-31 20:11:22 +08:00

173 lines
7.5 KiB
TypeScript

// PR #535 review reproduction: the daemon's HTTP + WS Host / Origin / token policy.
// Uses node:http and the ws client so the Host and Origin headers can be set explicitly (fetch doesn't allow it).
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import http from "node:http";
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import type { AddressInfo } from "node:net";
import WebSocket from "ws";
import { createHttpServer } from "../../daemon/src/http.js";
import { AttachmentStore } from "../../daemon/src/store/attachments.js";
import { Gateway } from "../../daemon/src/ws/gateway.js";
import type { Config } from "../../daemon/src/config.js";
import { request as rawRequest } from "./helpers-net.js";
const token = "pr535-token-0123456789abcdef";
const EVIL = "https://evil.example";
let server: http.Server;
let gateway: Gateway;
let port: number;
let dir: string;
const request = (path: string, headers: Record<string, string> = {}, method = "GET") =>
rawRequest(port, path, headers, method);
// When the WS upgrade is refused the ws client fires unexpected-response, which gives us the status code
function upgradeStatus(headers: Record<string, string>): Promise<number> {
return new Promise((resolve) => {
const ws = new WebSocket(`ws://127.0.0.1:${port}/ws`, { headers });
ws.on("unexpected-response", (_req, res) => {
resolve(res.statusCode ?? 0);
ws.terminate();
});
ws.on("open", () => {
resolve(101);
ws.terminate();
});
ws.on("error", () => resolve(-1));
});
}
// Optionally send hello after connecting; returns the first message or the close code
function wsSession(hello: string | null): Promise<{ message?: any; closeCode?: number }> {
return new Promise((resolve) => {
const ws = new WebSocket(`ws://127.0.0.1:${port}/ws`, { headers: { Origin: "null" } });
ws.on("open", () => {
if (hello !== null) ws.send(JSON.stringify({ t: "hello", token: hello, v: 1 }));
});
ws.on("message", (data) => {
const message = JSON.parse(String(data));
if (message.t === "hello.ok") {
resolve({ message });
ws.close();
}
});
ws.on("close", (code) => resolve({ closeCode: code }));
ws.on("error", () => {});
});
}
describe("PR535 HTTP and WS policy", () => {
beforeAll(async () => {
dir = mkdtempSync(join(tmpdir(), "pr535-http-"));
const app = join(dir, "app");
mkdirSync(app);
writeFileSync(join(app, "index.html"), "<!doctype html><title>safe page</title>");
writeFileSync(join(app, "app.js"), "console.log(1)");
const cfg: Config = {
port: 0,
dataDir: dir,
token,
version: "test",
idleExitMinutes: 60,
buildStamp: "test",
};
gateway = new Gateway(cfg);
server = createHttpServer(cfg, app, new AttachmentStore(join(dir, "attachments")), {
onUpgrade: (req, socket, head) => gateway.handleUpgrade(req, socket, head),
});
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
port = (server.address() as AddressInfo).port;
// Same as the daemon's main.ts: write back the actual port after listen
cfg.port = port;
});
afterAll(async () => {
gateway.closeAll();
server.closeAllConnections();
await new Promise<void>((resolve) => server.close(() => resolve()));
rmSync(dir, { recursive: true, force: true });
});
it("Origin: null GET / → 403, no token, no ACAO", async () => {
const res = await request("/", { origin: "null" });
expect(res.status).toBe(403);
expect(res.body).not.toContain(token);
expect(res.headers["access-control-allow-origin"]).toBeUndefined();
const js = await request("/app.js", { origin: "null" });
expect(js.status).toBe(403);
expect(js.headers["access-control-allow-origin"]).toBeUndefined();
});
it("GET / without Origin → 200, no token, no ACAO, hardened headers", async () => {
const res = await request("/");
expect(res.status).toBe(200);
expect(res.body).not.toContain(token);
expect(res.headers["access-control-allow-origin"]).toBeUndefined();
expect(res.headers["cache-control"]).toBe("no-store");
expect(res.headers["x-content-type-options"]).toBe("nosniff");
expect(res.headers["referrer-policy"]).toBe("no-referrer");
});
it("foreign Origin → 403 on /, on /api/ping even with the token, on OPTIONS /api/upload", async () => {
expect((await request("/", { origin: EVIL })).status).toBe(403);
const ping = await request("/api/ping", { origin: EVIL, "x-agent-console": token });
expect(ping.status).toBe(403);
expect(ping.headers["access-control-allow-origin"]).toBeUndefined();
const pre = await request("/api/upload", { origin: EVIL, "access-control-request-method": "POST" }, "OPTIONS");
expect(pre.status).toBe(403);
expect(pre.headers["access-control-allow-origin"]).toBeUndefined();
});
it("bad Host → 421 on /, /api/ping (with token) and OPTIONS /api/upload", async () => {
for (const host of [`evil.example:${port}`, `127.0.0.1:${port + 1}`]) {
expect((await request("/", { host })).status).toBe(421);
expect((await request("/api/ping", { host, "x-agent-console": token })).status).toBe(421);
expect((await request("/api/upload", { host, origin: "null" }, "OPTIONS")).status).toBe(421);
}
});
it("/api/ping: valid token + Origin null → 200 with ACAO null; no token → 403 without ACAO", async () => {
const ok = await request("/api/ping", { origin: "null", "x-agent-console": token });
expect(ok.status).toBe(200);
expect(ok.headers["access-control-allow-origin"]).toBe("null");
expect(ok.headers["access-control-allow-credentials"]).toBeUndefined();
const bad = await request("/api/ping", { origin: "null" });
expect(bad.status).toBe(403);
expect(bad.headers["access-control-allow-origin"]).toBeUndefined();
const wrong = await request("/api/ping", { origin: "null", "x-agent-console": "wrong" });
expect(wrong.status).toBe(403);
expect(wrong.headers["access-control-allow-origin"]).toBeUndefined();
});
it("OPTIONS /api/upload with Origin null → 204 ACAO null; other preflights refused", async () => {
const pre = await request("/api/upload", { origin: "null", "access-control-request-method": "POST" }, "OPTIONS");
expect(pre.status).toBe(204);
expect(pre.headers["access-control-allow-origin"]).toBe("null");
expect(pre.headers["access-control-allow-headers"]).toContain("x-agent-console");
expect(pre.headers["access-control-allow-credentials"]).toBeUndefined();
expect((await request("/api/ping", { origin: "null" }, "OPTIONS")).status).toBe(403);
expect((await request("/", { origin: "null" }, "OPTIONS")).status).toBe(403);
});
it("WS upgrade: foreign Origin → 403, bad Host → 421", async () => {
expect(await upgradeStatus({ Origin: EVIL })).toBe(403);
expect(await upgradeStatus({ Host: `evil.example:${port}` })).toBe(421);
expect(await upgradeStatus({ Host: `127.0.0.1:${port + 1}`, Origin: "null" })).toBe(421);
});
it("WS: wrong token → 4401; right token + Origin null → hello.ok", async () => {
expect((await wsSession("wrong")).closeCode).toBe(4401);
const ok = await wsSession(token);
expect(ok.message?.t).toBe("hello.ok");
});
it("WS: Origin null without hello → closed 4401 within 7 s", async () => {
const started = Date.now();
const result = await wsSession(null);
expect(result.closeCode).toBe(4401);
expect(Date.now() - started).toBeLessThan(7000);
}, 10000);
});