Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(mcp): accept bare loopback Host so standard ports (and tests) work
Elian Doran2dbfb9cfaf3e
diff --git a/apps/server/spec/etapi/mcp.spec.ts b/apps/server/spec/etapi/mcp.spec.tsindex 71a0565292..26dc6a9d40 100644--- a/apps/server/spec/etapi/mcp.spec.ts+++ b/apps/server/spec/etapi/mcp.spec.ts@@ -29,6 +29,10 @@ function parseSseResponse(text: string) {function mcpPost(app: Application) {return supertest(app).post("/mcp")+ // supertest binds to an ephemeral port, so its default Host (127.0.0.1:<random>)+ // would be rejected by the DNS-rebinding allow-list. Present a bare loopback Host,+ // matching what a real client on a standard port sends.+ .set("Host", "localhost").set("Accept", MCP_ACCEPT).set("Content-Type", "application/json");}diff --git a/apps/server/src/routes/mcp.ts b/apps/server/src/routes/mcp.tsindex daefb8bf54..af86ff99fa 100644--- a/apps/server/src/routes/mcp.ts+++ b/apps/server/src/routes/mcp.ts@@ -20,8 +20,20 @@ import port from "../services/port.js";* 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.+ *+ * Both the port-qualified form (`localhost:8080`) and the bare form (`localhost`)+ * are listed: a client reaching the server on a standard port (80/443) omits the+ * port from the Host header. Bare loopback names still only ever resolve to+ * loopback, so allowing them does not widen the DNS-rebinding surface.*/-const MCP_ALLOWED_HOSTS = [`localhost:${port}`, `127.0.0.1:${port}`, `[::1]:${port}`];+const MCP_ALLOWED_HOSTS = [+ "localhost",+ `localhost:${port}`,+ "127.0.0.1",+ `127.0.0.1:${port}`,+ "[::1]",+ `[::1]:${port}`+];function isLoopback(addr: string | undefined): boolean {if (!addr) return false;