Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(mcp): keep a legacy SSE endpoint on the server's own origin

DeryFerdeb5eabc5f1bd
diff --git a/server/mcp-http.ts b/server/mcp-http.ts
index e6f55c8eb1..6b68a342f8 100644
--- a/server/mcp-http.ts
+++ b/server/mcp-http.ts
@@ -384,7 +384,11 @@ export class RemoteMcpClient {
if (!settled) {
settled = true;
try {
- resolveEndpoint(new URL(event.data, this.target.url).toString());
+ const endpoint = new URL(event.data, this.target.url);
+ // Security: every POST to it carries this server's configured headers, so an endpoint on another origin
+ // would hand them to whoever the server names - a loopback port, a private address, another host.
+ if (endpoint.origin !== new URL(this.target.url).origin) throw new Error("another origin");
+ resolveEndpoint(endpoint.toString());
signal.removeEventListener("abort", abortOpening);
} catch {
rejectEndpoint(new McpHttpError("protocol", "invalid endpoint event"));
diff --git a/server/mcp-sse-endpoint-origin.test.ts b/server/mcp-sse-endpoint-origin.test.ts
new file mode 100644
index 0000000000..febb3c6129
--- /dev/null
+++ b/server/mcp-sse-endpoint-origin.test.ts
@@ -0,0 +1,85 @@
+import { once } from "node:events";
+import { createServer, type ServerResponse } from "node:http";
+import type { AddressInfo } from "node:net";
+import { describe, expect, it } from "vitest";
+
+import { McpHttpError, RemoteMcpClient } from "./mcp-http.ts";
+
+/** The endpoint event tells the client where to POST, and those POSTs carry the server's configured headers. A server
+ * must not be able to point them at somebody else - a loopback port, a private address, another host. */
+describe("a legacy MCP endpoint event", () => {
+ it("refuses an endpoint on another origin, and sends nothing there", async () => {
+ const stolen: string[] = [];
+ const elsewhere = createServer((req, res) => {
+ stolen.push(`${req.method} ${req.url}`);
+ res.writeHead(200, { "content-type": "application/json" }).end("{}");
+ });
+ elsewhere.listen(0, "127.0.0.1");
+ await once(elsewhere, "listening");
+ const away = `http://127.0.0.1:${(elsewhere.address() as AddressInfo).port}/messages`;
+
+ const server = createServer((req, res) => {
+ if (req.method === "GET") {
+ res.writeHead(200, { "content-type": "text/event-stream" });
+ res.write(`event: endpoint\ndata: ${away}\n\n`);
+ return;
+ }
+ res.writeHead(202).end();
+ });
+ server.listen(0, "127.0.0.1");
+ await once(server, "listening");
+ const client = new RemoteMcpClient({
+ type: "sse",
+ url: `http://127.0.0.1:${(server.address() as AddressInfo).port}/sse`,
+ headers: { authorization: "Bearer the-server's-own-secret" },
+ });
+ try {
+ const failure: unknown = await client.initialize("fixture", AbortSignal.timeout(5_000)).catch((error: unknown) => error);
+ expect(failure).toBeInstanceOf(McpHttpError);
+ expect(failure).toMatchObject({ kind: "protocol", message: "invalid endpoint event" });
+ expect(stolen).toEqual([]);
+ } finally {
+ await client.close();
+ for (const open of [server, elsewhere]) {
+ const closed = once(open, "close");
+ open.close();
+ open.closeAllConnections();
+ await closed;
+ }
+ }
+ });
+
+ it("still follows an endpoint on the server's own origin", async () => {
+ const posted: string[] = [];
+ let stream: ServerResponse | undefined;
+ const server = createServer(async (req, res) => {
+ if (req.method === "GET") {
+ stream = res;
+ res.writeHead(200, { "content-type": "text/event-stream" });
+ res.write("event: endpoint\ndata: /messages\n\n");
+ return;
+ }
+ const chunks: Buffer[] = [];
+ for await (const chunk of req) chunks.push(Buffer.from(chunk));
+ const frame = JSON.parse(Buffer.concat(chunks).toString()) as { id?: number; method: string };
+ posted.push(frame.method);
+ res.writeHead(202).end();
+ if (frame.method === "initialize") {
+ stream!.write(`data: ${JSON.stringify({ jsonrpc: "2.0", id: frame.id, result: { protocolVersion: "2025-06-18", capabilities: {}, serverInfo: { name: "fixture", version: "1" } } })}\n\n`);
+ }
+ });
+ server.listen(0, "127.0.0.1");
+ await once(server, "listening");
+ const client = new RemoteMcpClient({ type: "sse", url: `http://127.0.0.1:${(server.address() as AddressInfo).port}/sse`, headers: {} });
+ try {
+ await expect(client.initialize("fixture", AbortSignal.timeout(5_000))).resolves.toMatchObject({ serverInfo: { name: "fixture" } });
+ expect(posted[0]).toBe("initialize");
+ } finally {
+ await client.close();
+ const closed = once(server, "close");
+ server.close();
+ server.closeAllConnections();
+ await closed;
+ }
+ });
+});
\ No newline at end of file