mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 16:42:19 +00:00
* 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>
138 lines
4.7 KiB
JavaScript
138 lines
4.7 KiB
JavaScript
"use strict";
|
|
|
|
const { describe, it } = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
|
|
const {
|
|
DEFAULT_EXTERNAL_MCP_HTTP_PORT,
|
|
buildClientUrl,
|
|
normalizeHttpPort,
|
|
normalizeListenAddress,
|
|
readBearerToken,
|
|
resolveEffectiveListenAddress,
|
|
tokensEqual,
|
|
validateRequestHeaders,
|
|
} = require("./httpSecurity.cjs");
|
|
|
|
describe("external MCP HTTP security", () => {
|
|
it("keeps an unlikely fixed default port and rejects privileged or empty ports", () => {
|
|
assert.equal(DEFAULT_EXTERNAL_MCP_HTTP_PORT, 47391);
|
|
assert.equal(normalizeHttpPort(47391).port, 47391);
|
|
assert.equal(normalizeHttpPort("8080").port, 8080);
|
|
assert.equal(normalizeHttpPort(80).ok, false);
|
|
assert.equal(normalizeHttpPort(0).ok, false);
|
|
assert.equal(normalizeHttpPort("").ok, false);
|
|
assert.equal(normalizeHttpPort(65536).errorCode, "invalid_port");
|
|
});
|
|
|
|
it("binds loopback unless the custom address option is explicit", () => {
|
|
assert.equal(resolveEffectiveListenAddress({
|
|
allowCustomBind: false,
|
|
listenAddress: "0.0.0.0",
|
|
}), "127.0.0.1");
|
|
assert.equal(resolveEffectiveListenAddress({
|
|
allowCustomBind: true,
|
|
listenAddress: "0.0.0.0",
|
|
}), "0.0.0.0");
|
|
assert.equal(resolveEffectiveListenAddress({
|
|
allowCustomBind: true,
|
|
listenAddress: "172.22.0.1",
|
|
}), "172.22.0.1");
|
|
assert.equal(normalizeListenAddress("windows-host").ok, false);
|
|
assert.throws(
|
|
() => resolveEffectiveListenAddress({ allowCustomBind: true, listenAddress: "localhost" }),
|
|
(error) => error.code === "INVALID_ADDRESS",
|
|
);
|
|
});
|
|
|
|
it("does not advertise loopback for a wildcard bind", () => {
|
|
assert.equal(buildClientUrl("0.0.0.0", 47391), null);
|
|
assert.equal(buildClientUrl("::", 47391, "127.0.0.1"), "http://127.0.0.1:47391/mcp");
|
|
assert.equal(buildClientUrl("0.0.0.0", 47391, "172.22.0.1"), "http://172.22.0.1:47391/mcp");
|
|
assert.equal(buildClientUrl("0.0.0.0", 47391, "0.0.0.0"), null);
|
|
assert.equal(buildClientUrl("172.22.0.1", 47391), "http://172.22.0.1:47391/mcp");
|
|
});
|
|
|
|
it("accepts only an exact bearer token", () => {
|
|
assert.equal(readBearerToken("Bearer secret-token"), "secret-token");
|
|
assert.equal(readBearerToken("bearer secret-token"), "secret-token");
|
|
assert.equal(readBearerToken("Basic secret-token"), "");
|
|
assert.equal(readBearerToken("Bearer"), "");
|
|
assert.equal(tokensEqual("secret-token", "secret-token"), true);
|
|
assert.equal(tokensEqual("wrong", "secret-token"), false);
|
|
assert.equal(tokensEqual("", "secret-token"), false);
|
|
assert.equal(tokensEqual("secret-token", ""), false);
|
|
});
|
|
|
|
it("rejects DNS-rebinding host and origin values", () => {
|
|
const base = { listenAddress: "127.0.0.1", port: 47391 };
|
|
assert.equal(validateRequestHeaders({
|
|
...base,
|
|
hostHeader: "127.0.0.1:47391",
|
|
originHeader: undefined,
|
|
}).ok, true);
|
|
assert.equal(validateRequestHeaders({
|
|
...base,
|
|
hostHeader: "localhost:47391",
|
|
originHeader: "http://localhost:6274",
|
|
}).ok, true);
|
|
assert.equal(validateRequestHeaders({
|
|
...base,
|
|
hostHeader: "evil.example:47391",
|
|
originHeader: undefined,
|
|
}).status, 421);
|
|
assert.equal(validateRequestHeaders({
|
|
...base,
|
|
hostHeader: "127.0.0.1:47391",
|
|
originHeader: "https://evil.example",
|
|
}).status, 403);
|
|
assert.equal(validateRequestHeaders({
|
|
...base,
|
|
hostHeader: "127.0.0.1:80",
|
|
originHeader: undefined,
|
|
}).status, 421);
|
|
assert.equal(validateRequestHeaders({
|
|
...base,
|
|
hostHeader: "",
|
|
originHeader: undefined,
|
|
}).status, 421);
|
|
|
|
assert.equal(validateRequestHeaders({
|
|
listenAddress: "0.0.0.0",
|
|
port: 47391,
|
|
hostHeader: "172.22.0.1:47391",
|
|
originHeader: undefined,
|
|
}).ok, true);
|
|
assert.equal(validateRequestHeaders({
|
|
listenAddress: "0.0.0.0",
|
|
port: 47391,
|
|
hostHeader: "evil.example:47391",
|
|
originHeader: undefined,
|
|
}).status, 421);
|
|
assert.equal(validateRequestHeaders({
|
|
listenAddress: "172.22.0.1",
|
|
port: 47391,
|
|
hostHeader: "172.22.0.1:47391",
|
|
originHeader: "http://172.22.0.1:6274",
|
|
}).ok, true);
|
|
assert.equal(validateRequestHeaders({
|
|
listenAddress: "172.22.0.1",
|
|
port: 47391,
|
|
hostHeader: "8.8.8.8:47391",
|
|
originHeader: undefined,
|
|
}).status, 421);
|
|
assert.equal(validateRequestHeaders({
|
|
listenAddress: "::1",
|
|
port: 47391,
|
|
hostHeader: "[::1]:47391",
|
|
originHeader: "http://[::1]:6274",
|
|
}).ok, true);
|
|
assert.equal(validateRequestHeaders({
|
|
listenAddress: "::",
|
|
port: 47391,
|
|
hostHeader: "[2001:db8::1]:47391",
|
|
originHeader: "http://[2001:db8::1]:6274",
|
|
}).ok, true);
|
|
});
|
|
});
|