mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-25 18:16:32 +00:00
fix(ai): reject invalid IPv6 External MCP listen addresses in settings (#4295)
Fixes #4293
This commit is contained in:
3 files changed
+60
-11
No files matched your search
@@ -6,6 +6,7 @@ import {
|
||||
buildExternalMcpHttpUrl,
|
||||
buildGenericHttpMcpJson,
|
||||
DEFAULT_EXTERNAL_MCP_HTTP_PORT,
|
||||
isExternalMcpHttpIpAddress,
|
||||
} from "../../../../domain/externalMcpHttp";
|
||||
import { useI18n } from "../../../../application/i18n/I18nProvider";
|
||||
import { Button } from "../../../ui/button";
|
||||
@@ -58,15 +59,6 @@ function isWildcardAddress(value: string): boolean {
|
||||
return address === "0.0.0.0" || address === "::";
|
||||
}
|
||||
|
||||
function isIpAddress(value: string): boolean {
|
||||
const address = value.trim();
|
||||
const ipv4 = address.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
|
||||
if (ipv4) {
|
||||
return ipv4.slice(1).every((part) => Number(part) <= 255 && String(Number(part)) === part);
|
||||
}
|
||||
return address.includes(":") && /^[0-9a-fA-F:]+$/.test(address);
|
||||
}
|
||||
|
||||
function parsePort(value: string): number | null {
|
||||
if (!/^\d+$/.test(value.trim())) return null;
|
||||
const port = Number(value);
|
||||
@@ -157,7 +149,7 @@ export const ExternalMcpHttpSection: React.FC<ExternalMcpHttpSectionProps> = ({
|
||||
|
||||
const commitClientHost = () => {
|
||||
const clientHost = clientHostDraft.trim();
|
||||
if (clientHost && (!isIpAddress(clientHost) || isWildcardAddress(clientHost))) {
|
||||
if (clientHost && (!isExternalMcpHttpIpAddress(clientHost) || isWildcardAddress(clientHost))) {
|
||||
setClientHostError(true);
|
||||
return;
|
||||
}
|
||||
@@ -168,7 +160,7 @@ export const ExternalMcpHttpSection: React.FC<ExternalMcpHttpSectionProps> = ({
|
||||
|
||||
const commitAddress = () => {
|
||||
const address = addressDraft.trim();
|
||||
if (!isIpAddress(address)) {
|
||||
if (!isExternalMcpHttpIpAddress(address)) {
|
||||
setAddressError(true);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { createRequire } from "node:module";
|
||||
import { isIP } from "node:net";
|
||||
import test from "node:test";
|
||||
|
||||
import {
|
||||
@@ -9,11 +10,38 @@ import {
|
||||
buildCodexHttpToml,
|
||||
buildExternalMcpHttpUrl,
|
||||
buildGenericHttpMcpJson,
|
||||
isExternalMcpHttpIpAddress,
|
||||
} from "./externalMcpHttp.ts";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const security = require("../electron/bridges/externalMcp/httpSecurity.cjs");
|
||||
|
||||
test("HTTP IP validation accepts valid literals and agrees with the runtime", () => {
|
||||
const valid = [
|
||||
"::1", "::", "0.0.0.0", "127.0.0.1", "255.255.255.255",
|
||||
"1:2:3:4:5:6:7:8", "fe80::1", "2001:db8::1", "::ffff:127.0.0.1",
|
||||
"1:2:3:4:5:6:127.0.0.1", "FE80::ABCD", "fe80::1%eth0",
|
||||
];
|
||||
for (const address of valid) {
|
||||
assert.equal(isExternalMcpHttpIpAddress(address), true, address);
|
||||
assert.equal(isExternalMcpHttpIpAddress(address), isIP(address) !== 0, address);
|
||||
}
|
||||
});
|
||||
|
||||
test("HTTP IP validation rejects malformed literals and agrees with the runtime", () => {
|
||||
const invalid = [
|
||||
"1::2::3", ":", "1:2:3:4:5:6:7:8:9", "1:::2", ":::", "12345::", "g::1",
|
||||
"127.000.0.1", "", " ", "\t\n", "127.0.0", "256.0.0.1", "-1.0.0.1",
|
||||
"1:2:3:4:5:6:7", "1:2:3:4:5:6:7:8::", ":1:2:3:4:5:6:7", "1:2:3:4:5:6:7:",
|
||||
"::ffff:127.000.0.1", "::ffff:256.0.0.1", "1:2:3:4:5:127.0.0.1",
|
||||
"1:2:3:4:5:6::127.0.0.1", "[::1]", "localhost", "fe80::1%", "fe80::1%a%b",
|
||||
];
|
||||
for (const address of invalid) {
|
||||
assert.equal(isExternalMcpHttpIpAddress(address), false, JSON.stringify(address));
|
||||
assert.equal(isExternalMcpHttpIpAddress(address), isIP(address) !== 0, JSON.stringify(address));
|
||||
}
|
||||
});
|
||||
|
||||
test("HTTP client URL stays on loopback unless a specific listen address is set", () => {
|
||||
assert.equal(DEFAULT_EXTERNAL_MCP_HTTP_PORT, security.DEFAULT_EXTERNAL_MCP_HTTP_PORT);
|
||||
assert.equal(
|
||||
|
||||
@@ -3,6 +3,35 @@ export const DEFAULT_EXTERNAL_MCP_HTTP_PORT = 47391;
|
||||
export const DEFAULT_EXTERNAL_MCP_HTTP_HOST = "127.0.0.1";
|
||||
export const EXTERNAL_MCP_HTTP_PATH = "/mcp";
|
||||
|
||||
function isIpv4Address(value: string): boolean {
|
||||
const parts = value.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
|
||||
return parts !== null && parts.slice(1).every(
|
||||
(part) => Number(part) <= 255 && String(Number(part)) === part,
|
||||
);
|
||||
}
|
||||
|
||||
export function isExternalMcpHttpIpAddress(value: string): boolean {
|
||||
if (isIpv4Address(value)) return true;
|
||||
|
||||
const [literal, zone, ...extraZones] = value.split("%");
|
||||
if (extraZones.length || (zone !== undefined && !/^[0-9a-zA-Z.:-]+$/.test(zone))) return false;
|
||||
if (!literal.includes(":")) return false;
|
||||
|
||||
let address = literal;
|
||||
if (address.includes(".")) {
|
||||
const lastColon = address.lastIndexOf(":");
|
||||
if (!isIpv4Address(address.slice(lastColon + 1))) return false;
|
||||
// An embedded IPv4 tail occupies two IPv6 groups.
|
||||
address = `${address.slice(0, lastColon + 1)}0:0`;
|
||||
}
|
||||
|
||||
const halves = address.split("::");
|
||||
if (halves.length > 2) return false;
|
||||
const groups = halves.flatMap((half) => half ? half.split(":") : []);
|
||||
if (!groups.every((group) => /^[0-9a-fA-F]{1,4}$/.test(group))) return false;
|
||||
return halves.length === 2 ? groups.length < 8 : groups.length === 8;
|
||||
}
|
||||
|
||||
function formatHostForUrl(host: string): string {
|
||||
if (host.includes(":") && !host.startsWith("[")) return `[${host}]`;
|
||||
return host;
|
||||
|
||||
Reference in new issue
Block a user