Files
Netcatty/electron/bridges/externalMcp/httpSecurity.cjs
T
0ccafbabf7 feat: optional Streamable HTTP for External MCP (#3790)
* feat: add optional Streamable HTTP for External MCP

Serve the existing catalog tools and approval path on a loopback HTTP
endpoint so clients that cannot spawn the stdio launcher, such as Codex
in WSL, can connect with a bearer token. The listener stays off until
the user enables it.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: close HTTP sockets before stop and export the Codex bearer

Destroy tracked connections before awaiting server.close so disable does not hang on a half-open request. Prefix the Codex snippet with export so the child process receives the token.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: advertise a real client host and return 413 intact

Wildcard binds no longer copy 127.0.0.1 into client configs; the URL stays empty until a reachable client address is set. Oversized POSTs receive a JSON-RPC 413 before the socket is closed.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: recheck the bearer token after the HTTP body arrives

A half-open POST already passed auth before readBody. Recheck the live token before dispatch so a rotation during that wait cannot still run a tool.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: expire idle Streamable HTTP sessions

JSON responses do not keep a socket open, so a client that never sends DELETE used to pin an MCP session until the listener stopped. Sweep sessions that have been idle for 15 minutes, and skip any session with a request still in flight.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: return 404 when an HTTP session id is unknown

GET and DELETE with a nonempty session id that is no longer active now return 404, matching POST and the Streamable HTTP session contract. A missing session id stays 400.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: accept bracketed IPv6 origins

Node reports URL.hostname as [::1], so an otherwise valid loopback or wildcard Origin was rejected. Compare the address with the brackets removed.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: release rejected HTTP handshakes that never open a session

A 406 or 415 from the SDK returns normally before onsessioninitialized. Detach the approval client and close the MCP server when that happens so rejected initializes do not accumulate.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-20 16:39:56 +08:00

202 lines
6.6 KiB
JavaScript

"use strict";
const crypto = require("node:crypto");
const net = require("node:net");
const DEFAULT_EXTERNAL_MCP_HTTP_PORT = 47391;
const DEFAULT_EXTERNAL_MCP_HTTP_HOST = "127.0.0.1";
const EXTERNAL_MCP_HTTP_PATH = "/mcp";
const MIN_EXTERNAL_MCP_HTTP_PORT = 1024;
const MAX_EXTERNAL_MCP_HTTP_PORT = 65535;
const EXTERNAL_MCP_HTTP_BEARER_ENV = "NETCATTY_MCP_BEARER_TOKEN";
function isWildcardBind(address) {
return address === "0.0.0.0" || address === "::";
}
function unbracketHostname(hostname) {
const host = String(hostname || "");
if (host.startsWith("[") && host.endsWith("]") && host.length > 2) return host.slice(1, -1);
return host;
}
function isLoopbackHost(hostname) {
const host = unbracketHostname(hostname).toLowerCase();
return host === "localhost" || host === "127.0.0.1" || host === "::1";
}
function isIpLiteral(hostname) {
return net.isIP(unbracketHostname(hostname)) !== 0;
}
function normalizeHttpPort(value) {
const parsed = typeof value === "number" ? value : Number(String(value ?? "").trim());
if (!Number.isInteger(parsed) || parsed < MIN_EXTERNAL_MCP_HTTP_PORT || parsed > MAX_EXTERNAL_MCP_HTTP_PORT) {
return {
ok: false,
errorCode: "invalid_port",
error: `Enter a port from ${MIN_EXTERNAL_MCP_HTTP_PORT} to ${MAX_EXTERNAL_MCP_HTTP_PORT}.`,
};
}
return { ok: true, port: parsed };
}
function normalizeListenAddress(value) {
const address = String(value ?? "").trim();
if (!address || net.isIP(address) === 0) {
return {
ok: false,
errorCode: "invalid_address",
error: "Listen address must be an IP address such as 0.0.0.0 or a vEthernet address.",
};
}
return { ok: true, address };
}
function resolveEffectiveListenAddress({ allowCustomBind, listenAddress }) {
if (!allowCustomBind) return DEFAULT_EXTERNAL_MCP_HTTP_HOST;
const normalized = normalizeListenAddress(listenAddress);
if (!normalized.ok) {
const error = new Error(normalized.error);
error.code = "INVALID_ADDRESS";
error.errorCode = normalized.errorCode;
throw error;
}
return normalized.address;
}
function formatHostForUrl(host) {
if (host.includes(":") && !host.startsWith("[")) return `[${host}]`;
return host;
}
function normalizeClientHost(value) {
const address = String(value ?? "").trim();
if (!address) return { ok: true, address: "" };
if (net.isIP(address) === 0 || isWildcardBind(address)) {
return {
ok: false,
errorCode: "invalid_client_host",
error: "Client address must be a reachable IP, not 0.0.0.0 or ::.",
};
}
return { ok: true, address };
}
function buildClientUrl(listenAddress, port, clientHost) {
const host = isWildcardBind(listenAddress) ? String(clientHost || "").trim() : listenAddress;
if (!host || isWildcardBind(host) || net.isIP(host) === 0) return null;
return `http://${formatHostForUrl(host)}:${port}${EXTERNAL_MCP_HTTP_PATH}`;
}
function parseHostHeader(hostHeader) {
const raw = String(hostHeader || "").trim();
if (!raw) return null;
if (raw.startsWith("[")) {
const end = raw.indexOf("]");
if (end <= 1) return null;
const hostname = raw.slice(1, end);
const rest = raw.slice(end + 1);
if (rest === "") return { hostname, port: null };
if (!rest.startsWith(":")) return null;
const port = rest.slice(1);
if (!/^\d+$/.test(port)) return null;
return { hostname, port };
}
const colon = raw.lastIndexOf(":");
if (colon === -1) return { hostname: raw, port: null };
const hostname = raw.slice(0, colon);
const port = raw.slice(colon + 1);
if (!hostname || !/^\d+$/.test(port)) return null;
return { hostname, port };
}
function isAllowedHostname(hostname, listenAddress) {
const host = unbracketHostname(hostname);
if (isLoopbackHost(host)) return true;
if (isWildcardBind(listenAddress)) return isIpLiteral(host);
return host.toLowerCase() === String(listenAddress || "").toLowerCase();
}
function invalidHost() {
return { ok: false, status: 421, errorCode: "host", message: "Invalid Host header" };
}
function invalidOrigin() {
return { ok: false, status: 403, errorCode: "origin", message: "Invalid Origin header" };
}
function validateHostHeader(hostHeader, listenAddress, port) {
const parsed = parseHostHeader(hostHeader);
if (!parsed || parsed.port == null) return invalidHost();
if (parsed.port !== String(port)) return invalidHost();
if (!isAllowedHostname(parsed.hostname, listenAddress)) return invalidHost();
return { ok: true };
}
function validateOriginHeader(originHeader, listenAddress) {
if (originHeader == null || originHeader === "") return { ok: true };
let url;
try {
url = new URL(originHeader);
} catch {
return invalidOrigin();
}
if (url.protocol !== "http:" && url.protocol !== "https:") return invalidOrigin();
if (url.username || url.password) return invalidOrigin();
if (!isAllowedHostname(url.hostname, listenAddress)) return invalidOrigin();
return { ok: true };
}
function validateRequestHeaders({ hostHeader, originHeader, listenAddress, port }) {
const host = validateHostHeader(hostHeader, listenAddress, port);
if (!host.ok) return host;
return validateOriginHeader(originHeader, listenAddress);
}
function readBearerToken(authorizationHeader) {
if (typeof authorizationHeader !== "string") return "";
const match = /^Bearer\s+(\S+)\s*$/i.exec(authorizationHeader.trim());
return match ? match[1] : "";
}
function tokensEqual(presented, expected) {
if (typeof expected !== "string" || expected.length === 0) return false;
const presentedText = typeof presented === "string" ? presented : "";
const presentedHash = crypto.createHash("sha256").update(presentedText).digest();
const expectedHash = crypto.createHash("sha256").update(expected).digest();
return crypto.timingSafeEqual(presentedHash, expectedHash);
}
function describeListenError(error, port) {
if (error?.code === "EADDRINUSE") {
return { errorCode: "port_in_use", error: `Port ${port} is already in use.` };
}
if (error?.code === "INVALID_ADDRESS" || error?.errorCode === "invalid_address") {
return { errorCode: "invalid_address", error: error.message };
}
return { errorCode: "listen_failed", error: error?.message || String(error) };
}
module.exports = {
DEFAULT_EXTERNAL_MCP_HTTP_HOST,
DEFAULT_EXTERNAL_MCP_HTTP_PORT,
EXTERNAL_MCP_HTTP_BEARER_ENV,
EXTERNAL_MCP_HTTP_PATH,
MAX_EXTERNAL_MCP_HTTP_PORT,
MIN_EXTERNAL_MCP_HTTP_PORT,
buildClientUrl,
describeListenError,
normalizeClientHost,
isAllowedHostname,
isLoopbackHost,
isWildcardBind,
normalizeHttpPort,
normalizeListenAddress,
parseHostHeader,
readBearerToken,
resolveEffectiveListenAddress,
tokensEqual,
validateRequestHeaders,
};