Skip to content

Build your personal knowledge base with Trilium Notes

fix(mcp): enable DNS rebinding protection on the MCP transport

Elian Dorance6e1373ef56
diff --git a/apps/server/src/routes/mcp.ts b/apps/server/src/routes/mcp.ts
index cd293af171..daefb8bf54 100644
--- a/apps/server/src/routes/mcp.ts
+++ b/apps/server/src/routes/mcp.ts
@@ -2,7 +2,9 @@
* MCP (Model Context Protocol) HTTP route handler.
*
* Mounts the Streamable HTTP transport at `/mcp` with a localhost-only guard.
- * No authentication is required — access is restricted to loopback addresses.
+ * No authentication is required — access is restricted to loopback addresses,
+ * with DNS-rebinding protection (Host-header allow-list) layered on top so a
+ * rebound attacker domain can't drive the transport from the victim's browser.
*/
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
@@ -11,6 +13,15 @@ import type express from "express";
import { getLog } from "@triliumnext/core";
import { createMcpServer } from "../services/mcp/mcp_server.js";
+import port from "../services/port.js";
+
+/**
+ * Host header values a legitimate (loopback) MCP client may target. The SDK's
+ * DNS-rebinding protection rejects any other Host before a tool can run, so a
+ * rebound attacker domain pointed at 127.0.0.1 — which the browser sends as its
+ * own Host — cannot reach the transport even though it originates from loopback.
+ */
+const MCP_ALLOWED_HOSTS = [`localhost:${port}`, `127.0.0.1:${port}`, `[::1]:${port}`];
function isLoopback(addr: string | undefined): boolean {
if (!addr) return false;
@@ -43,7 +54,9 @@ async function handleMcpRequest(req: express.Request, res: express.Response) {
try {
const server = createMcpServer();
const transport = new StreamableHTTPServerTransport({
- sessionIdGenerator: undefined // stateless
+ sessionIdGenerator: undefined, // stateless
+ enableDnsRebindingProtection: true,
+ allowedHosts: MCP_ALLOWED_HOSTS
});
res.on("close", () => {
diff --git a/apps/server/src/routes/transport.spec.ts b/apps/server/src/routes/transport.spec.ts
index 1eea4d3e3d..899584b3d5 100644
--- a/apps/server/src/routes/transport.spec.ts
+++ b/apps/server/src/routes/transport.spec.ts
@@ -4,6 +4,7 @@ import supertest from "supertest";
import { beforeAll, describe, expect, it } from "vitest";
import { type ApiTestContext, bootLoggedInApp, createTextNote } from "../../spec/support/internal_api.js";
+import port from "../services/port.js";
let ctx: ApiTestContext;
let app: Application;
@@ -92,17 +93,35 @@ describe("Route transport & middleware", () => {
expect(res.body.error).toContain("disabled");
});
- it("reaches the MCP transport over loopback once enabled", async () => {
+ it("reaches the MCP transport over loopback with a valid Host once enabled", async () => {
cls.init(() => optionService.setOption("mcpEnabled", "true"));
- // supertest connects over loopback, so the guard passes and the
- // request is handed to the streamable transport (any status is fine —
- // we only need the handler code to execute).
+ // supertest connects over loopback so the guard passes; a Host matching
+ // the configured port clears DNS-rebinding protection and the request
+ // is handed to the streamable transport (any status is fine — we only
+ // need the handler to execute past header validation).
const res = await supertest(app)
.post("/mcp")
+ .set("Host", `localhost:${port}`)
.set("Content-Type", "application/json")
.set("Accept", "application/json, text/event-stream")
.send({ jsonrpc: "2.0", method: "initialize", id: 1, params: { protocolVersion: "2024-11-05", capabilities: {}, clientInfo: { name: "spec", version: "1" } } });
expect(res.status).toBeGreaterThanOrEqual(200);
+ expect(JSON.stringify(res.body)).not.toContain("Invalid Host header");
+ });
+
+ it("rejects a forged Host header (DNS rebinding) with 403", async () => {
+ cls.init(() => optionService.setOption("mcpEnabled", "true"));
+ // A DNS-rebinding attacker reaches the loopback listener through the
+ // victim's browser (so the IP guard passes) but carries an attacker-
+ // controlled Host. It must be rejected before any MCP tool can run.
+ const res = await supertest(app)
+ .post("/mcp")
+ .set("Host", "attacker.example.com")
+ .set("Content-Type", "application/json")
+ .set("Accept", "application/json, text/event-stream")
+ .send({ jsonrpc: "2.0", method: "initialize", id: 1, params: { protocolVersion: "2024-11-05", capabilities: {}, clientInfo: { name: "spec", version: "1" } } })
+ .expect(403);
+ expect(res.body?.error?.message ?? "").toContain("Invalid Host header");
});
});
});