From f0e08f9731dc0cc014e0ca6e27f7330f09538b2b Mon Sep 17 00:00:00 2001 From: Denozordec Date: Sun, 22 Nov 2026 11:31:14 +0700 Subject: [PATCH] =?UTF-8?q?fix(mcp):=20=D1=83=D1=87=D0=B8=D1=82=D1=8B?= =?UTF-8?q?=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BF=D1=80=D0=B5=D1=84=D0=B8=D0=BA?= =?UTF-8?q?=D1=81=D1=8B=20=D0=BF=D1=80=D0=B8=20=D0=BF=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D0=BA=D0=B5=20=D0=B0=D0=B4=D1=80=D0=B5=D1=81=D0=BE?= =?UTF-8?q?=D0=B2=20=D1=81=D0=BE=D1=81=D0=B5=D0=B4=D0=B5=D0=B9=20BGP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/modules/mcp/integration.test.ts | 22 ++++++++++++++++++- .../src/modules/mcp/routing-dependencies.ts | 15 ++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/backend/src/modules/mcp/integration.test.ts b/backend/src/modules/mcp/integration.test.ts index ba18cf24..a52977b9 100644 --- a/backend/src/modules/mcp/integration.test.ts +++ b/backend/src/modules/mcp/integration.test.ts @@ -1629,13 +1629,33 @@ try { await assert.rejects(invokeMcpTool(ospfPrincipal, "mm_ospf_instance_create_plan", { serverId: 1, fields: { name: "blocked-ospf", "router-id": "192.0.2.1", version: "3", vrf: "main" } })) await pool.query("DELETE FROM mcp_protections WHERE id=$1", [vrfProtection]) bgpFixtures = true - const bgpAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "BGP dependency test", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:bgp:read", "mm:bgp:write", "mm:network:read"], scope: { serverIds: [1], operationIds: ["ros.bgp_instance.delete", "ros.bgp_instance.update", "ros.bgp_connection.create", "ros.vrf.delete"], allowGlobal: false } }), actor) + const bgpAccess = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "BGP dependency test", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:bgp:read", "mm:bgp:write", "mm:network:read"], scope: { serverIds: [1], operationIds: ["ros.bgp_instance.delete", "ros.bgp_instance.update", "ros.bgp_connection.create", "ros.bgp_connection.update", "ros.vrf.delete"], allowGlobal: false } }), actor) const bgpPrincipal = await authenticateMcpToken(bgpAccess.token) await assert.rejects(invokeMcpTool(bgpPrincipal, "mm_bgp_instance_delete_plan", { serverId: 1, id: "*bgp" }), /используется подключением/) const peerFields = { name: "new-peer", instance: "missing", "remote.address": "192.0.2.2", "local.address": "192.0.2.1", "remote.as": 64513, "local.role": "ebgp" } await assert.rejects(invokeMcpTool(bgpPrincipal, "mm_bgp_connection_create_plan", { serverId: 1, fields: peerFields }), /Не подтверждена зависимость/) await assert.rejects(invokeMcpTool(bgpPrincipal, "mm_bgp_connection_create_plan", { serverId: 1, fields: { ...peerFields, instance: "fixture-bgp", "local.address": "fd00::1" } }), /одного семейства/) await assert.rejects(invokeMcpTool(bgpPrincipal, "mm_bgp_connection_create_plan", { serverId: 1, fields: { ...peerFields, instance: "fixture-bgp", "remote.address": "0.0.0.0/0" } })) + const savedPeer = fixtureBgp.connection[0] + const preparePassivePeer = () => invokeMcpTool(bgpPrincipal, "mm_bgp_connection_update_plan", { + serverId: 1, id: savedPeer[".id"], fields: { connect: false, listen: true }, + }) as Promise<{ state: string; changes: { steps: { step: { fields: unknown } }[] } }> + try { + for (const [local, remote] of [["10.255.255.100", "10.255.255.11/32"], ["2001:db8::1", "2001:db8::2/128"]]) { + fixtureBgp.connection[0] = { ...savedPeer, "local.address": local, "remote.address": remote } + const plan = await preparePassivePeer() + assert.equal(plan.state, "awaiting-approval") + assert.equal(plan.changes.steps.length, 1) + assert.deepEqual(plan.changes.steps[0].step.fields, { connect: "false", listen: "true" }) + } + fixtureBgp.connection[0] = { ...savedPeer, "remote.address": "2001:db8::2/128" } + await assert.rejects(preparePassivePeer(), /одного семейства/) + fixtureBgp.connection[0] = { ...savedPeer, "remote.address": "192.0.2.2/33" } + await assert.rejects(preparePassivePeer(), /Некорректный адрес или префикс/) + } finally { + fixtureBgp.connection[0] = savedPeer + } + console.log("✓ MCP BGP dependency validation accepts stored IPv4/IPv6 CIDR for a passive-mode update and rejects mixed families and invalid prefix lengths") const peerProtection = randomUUID() await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,'integration')", [peerProtection, JSON.stringify({ kind: "bgp_connection", id: "*peer", serverId: 1, protection: "deny" })]) await assert.rejects(invokeMcpTool(bgpPrincipal, "mm_bgp_instance_update_plan", { serverId: 1, id: "*bgp", fields: { as: 64514 } })) diff --git a/backend/src/modules/mcp/routing-dependencies.ts b/backend/src/modules/mcp/routing-dependencies.ts index 1cb4a00b..07348d2e 100644 --- a/backend/src/modules/mcp/routing-dependencies.ts +++ b/backend/src/modules/mcp/routing-dependencies.ts @@ -134,6 +134,15 @@ export async function resolveOspfInterfaceSelectors(serverId: number, selectors: return names } +function bgpAddressFamily(value: string): number { + if (!value.includes("/")) return isIP(value) + try { + return isIP(canonicalBgpPolicyPrefix(value).split("/")[0]) + } catch { + return 0 + } +} + export async function addRoutingDependencies(serverId: number, resource: string, action: RosStep["action"], current: ConfigurationRow | undefined, fields: Record, snapshots: RosSnapshot[], objects: ChangeEvidence["objects"]) { const read = async (id: string) => { const existing = snapshots.find(snapshot => snapshot.resource === id) @@ -227,7 +236,11 @@ export async function addRoutingDependencies(serverId: number, resource: string, } if (resource === "bgp_connection") { const local = fields["local.address"] ?? current?.["local.address"], remote = fields["remote.address"] ?? current?.["remote.address"] - if (typeof local === "string" && typeof remote === "string" && isIP(local) !== isIP(remote)) throw new McpError("INVALID_INPUT", "Адреса BGP-соединения должны быть одного семейства", 400) + if (typeof local === "string" && typeof remote === "string") { + const localFamily = bgpAddressFamily(local), remoteFamily = bgpAddressFamily(remote) + if (!localFamily || !remoteFamily) throw new McpError("INVALID_INPUT", "Некорректный адрес или префикс BGP-соединения", 400) + if (localFamily !== remoteFamily) throw new McpError("INVALID_INPUT", "Адреса BGP-соединения должны быть одного семейства", 400) + } } if (resource === "bgp_instance" && current && ["as", "router-id", "disabled"].some(key => fields[key] !== undefined && fields[key] !== current[key])) { for (const row of (await read("bgp_connection")).rows.filter(row => [current.name, current[".id"]].includes(row.instance))) objects.push({ kind: "bgp_connection", id: String(row[".id"]), serverId })