64 lines
2.8 KiB
JavaScript
64 lines
2.8 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { spawn } from "node:child_process";
|
|
import { mkdtemp, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import net from "node:net";
|
|
import { createServer } from "node:http";
|
|
import { once } from "node:events";
|
|
|
|
// The Pipelines chat tools' MCP endpoint (/mcp/chat/<token>) is for harness
|
|
// processes on the server's own machine. The authenticated proxy must never
|
|
// forward it, even for a signed-in browser.
|
|
const here = fileURLToPath(new URL("..", import.meta.url));
|
|
|
|
async function freePort() {
|
|
const server = net.createServer().listen(0, "127.0.0.1");
|
|
await once(server, "listening");
|
|
const { port } = server.address();
|
|
await new Promise((done) => server.close(done));
|
|
return port;
|
|
}
|
|
|
|
test("the proxy does not expose the loopback MCP chat-tools endpoint", async (t) => {
|
|
const requests = [];
|
|
const upstream = createServer((req, res) => {
|
|
requests.push(`${req.method} ${req.url}`);
|
|
res.writeHead(200, { "Content-Type": "application/json" }).end('{"jsonrpc":"2.0","id":1,"result":{}}');
|
|
}).listen(0, "127.0.0.1");
|
|
await once(upstream, "listening");
|
|
t.after(() => new Promise((done) => upstream.close(done)));
|
|
const dir = await mkdtemp(join(tmpdir(), "serve-mcp-"));
|
|
const auth = join(dir, "auth.json");
|
|
await writeFile(auth, JSON.stringify({ password: "password-1", secret: "s".repeat(40) }));
|
|
const port = await freePort();
|
|
const child = spawn(process.execPath, ["serve.mjs", "--port", String(port),
|
|
"--pipelines", `127.0.0.1:${upstream.address().port}`], {
|
|
cwd: here,
|
|
env: { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth },
|
|
stdio: ["ignore", "pipe", "inherit"],
|
|
});
|
|
t.after(async () => { const exited = once(child, "exit"); child.kill(); await exited; });
|
|
await once(child.stdout, "data");
|
|
const base = `http://127.0.0.1:${port}`;
|
|
const login = await fetch(base + "/login", {
|
|
method: "POST", body: new URLSearchParams({ password: "password-1" }), redirect: "manual",
|
|
});
|
|
const cookie = login.headers.get("set-cookie").split(";")[0];
|
|
const token = "a".repeat(64);
|
|
for (const path of [`/mcp/chat/${token}`, "/mcp", "/mcp/", `/%6dcp/chat/${token}`]) {
|
|
for (const method of ["GET", "POST"]) {
|
|
const reply = await fetch(base + path, {
|
|
method,
|
|
headers: { Cookie: cookie, "Content-Type": "application/json", Accept: "application/json, text/event-stream" },
|
|
body: method === "POST" ? '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' : undefined,
|
|
});
|
|
const text = await reply.text();
|
|
assert.ok(!text.includes('"jsonrpc"'), `${method} ${path} reached the server: ${reply.status} ${text}`);
|
|
}
|
|
}
|
|
assert.deepEqual(requests, []);
|
|
});
|