Files
MikrotikManager/backend/src/modules/mcp/protocol-limits.owned.test.ts
T

185 lines
11 KiB
TypeScript

import assert from "node:assert/strict"
import { randomUUID } from "node:crypto"
import { basename, dirname } from "node:path"
import { setTimeout as delay } from "node:timers/promises"
import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp"
assert.equal(process.env.MMAPP_OWN_PROTOCOL_LIMITS, "true")
assert.equal(process.env.NODE_ENV, "test")
const endpoint = new URL(process.env.DATABASE_URL ?? "")
assert.equal(endpoint.protocol, "postgresql:")
assert.equal(endpoint.hostname, "127.0.0.1")
assert.equal(endpoint.username, "mcp_test")
assert.equal(endpoint.pathname, "/postgres")
assert(endpoint.port && endpoint.password && !endpoint.search && !endpoint.hash)
const state = process.env.MCP_STATE_DIR ?? ""
assert.equal(basename(state), "state")
assert.match(basename(dirname(state)), /^mm-mcp-test-/)
const { pool, closePool } = await import("../../db/index.js")
const { SYSTEM_DATABASE_OPERATION_LOCK, SYSTEM_DATABASE_REQUEST_LOCK, withOperationLock, withSystemDatabaseOperationLock } = await import("../../db/operation-lock.js")
const { applySqlMigrations, applyStandaloneSqlMigrations } = await import("../../db/migrate.js")
const { PARTITION_WRITER_ADVISORY_KEY } = await import("../../services/partition-write-transaction.js")
const { authenticateMcpToken, createMcpConnection, updateMcpSettings } = await import("./store.js")
const { recordUnauthorizedProtocol, reserveProtocolEnvelope } = await import("./protocol-limits.js")
const { canonicalMcpUrl, encryptSecret, tokenDigest } = await import("./security.js")
const { buildApp } = await import("../../index.js")
const { sharedDatabaseGate } = await import("../../services/shared-database-gate.js")
const { invokeMcpTool } = await import("./runtime.js")
const app = await buildApp({ startScheduler: false })
try {
await withOperationLock(() => pool.connect(), SYSTEM_DATABASE_OPERATION_LOCK, async () => {
await assert.rejects(applyStandaloneSqlMigrations(pool), /Операция с базой приложения уже выполняется/)
const { rows: [before] } = await pool.query("SELECT to_regclass('public.schema_migrations') AS marker")
assert.equal(before.marker, null)
await applySqlMigrations(pool)
const { rows: [after] } = await pool.query("SELECT count(*)::int AS applied FROM schema_migrations")
assert(after.applied > 0)
})
await applyStandaloneSqlMigrations(pool)
const actor = { id: "protocol-owner", apps: ["mm"], permissions: ["mm:settings:admin", "mm:servers:write"],
email: "[email protected]", name: "Owned protocol", expiresAt: Date.now() + 600_000 }
await updateMcpSettings({ enabled: true }, actor)
const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Owned protocol", mode: "approval",
permissions: ["mm:servers:write"], expiresAt: new Date(actor.expiresAt).toISOString(),
scope: { serverIds: [], operationIds: ["servers.poll", "operations.read"], allowGlobal: true }, limits: { callsPerMinute: 2 } }), actor)
const principal = await authenticateMcpToken(access.token)
const clientId = randomUUID(), family = randomUUID(), refresh = randomUUID(), resource = canonicalMcpUrl().href
await pool.query("INSERT INTO mcp_oauth_clients(id,name,redirect_uris) VALUES($1,'Owned OAuth fixture','[]')", [clientId])
await pool.query(`INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at,family_id,client_id,audience)
VALUES($1,$2,$3,'refresh',now()+interval '5 minutes',$4,$5,$6)`, [randomUUID(), principal.connectionId, tokenDigest(refresh), family, clientId, resource])
const exchange = () => app.inject({ method: "POST", url: "/oauth/token",
payload: { grant_type: "refresh_token", client_id: clientId, resource, refresh_token: refresh } })
const revoke = (token: string) => app.inject({ method: "POST", url: "/oauth/revoke", payload: { client_id: clientId, token } })
const readFamily = async () => (await pool.query("SELECT kind,revoked_at FROM mcp_credentials WHERE family_id=$1 ORDER BY id", [family])).rows
const beforeExchange = await readFamily()
await withSystemDatabaseOperationLock(() => pool.connect(), async () => {
for (const response of [await exchange(), await revoke(refresh)]) {
assert.equal(response.statusCode, 503)
assert.deepEqual(response.json(), { error: "temporarily_unavailable" })
assert.equal(response.headers["cache-control"], "no-store")
}
assert.deepEqual(await readFamily(), beforeExchange)
})
const rotated = await exchange()
assert.equal(rotated.statusCode, 200)
assert.equal((await readFamily()).length, 3)
assert.equal((await revoke(rotated.json().refresh_token)).statusCode, 200)
assert((await readFamily()).every(row => row.revoked_at !== null))
const planId = randomUUID(), operationId = randomUUID()
const evidence = { sealed: await encryptSecret(JSON.stringify({ target: { scope: "global" }, objects: [] })) }
await pool.query(`INSERT INTO mcp_plans
(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at)
VALUES($1,$2,'servers.poll',1,1,'test','{}'::jsonb,$3,'approved',now()+interval '5 minutes')`,
[planId, principal.connectionId, evidence])
await pool.query(`INSERT INTO mcp_operations (id,plan_id,connection_id,idempotency_key,state)
VALUES($1,$2,$3,$1::uuid::text,'running')`, [operationId, planId, principal.connectionId])
const address = await app.listen({ host: "127.0.0.1", port: 0 })
const stateSql = `SELECT (SELECT count(*)::int FROM mcp_budget_buckets) AS budgets,
(SELECT count(*)::int FROM mcp_unauthorized_buckets) AS unauthorized,
(SELECT count(*)::int FROM mcp_requests) AS requests`
const baseline = (await pool.query(stateSql)).rows[0]
const lock = await pool.connect()
try {
await lock.query("SELECT pg_advisory_lock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY])
const request = (token = access.token) => fetch(new URL("/mcp", address), {
method: "POST",
headers: {
authorization: `Bearer ${token}`, "content-type": "application/json",
accept: "application/json, text/event-stream", "mcp-protocol-version": "2025-06-18",
},
signal: AbortSignal.timeout(5000),
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call",
params: { name: "mm_operation_get", arguments: { operationId } } }),
})
const blocked = await sharedDatabaseGate.exclusive(() => request())
assert.equal(blocked.status, 409)
assert.deepEqual((await pool.query(stateSql)).rows[0], baseline)
await withOperationLock(() => pool.connect(), SYSTEM_DATABASE_OPERATION_LOCK, async () => {
for (const token of [access.token, "not-a-valid-token"]) {
const response = await request(token)
assert.equal(response.status, 503)
assert.equal((await response.json()).code, "PROTOCOL_STORAGE_BUSY")
}
await assert.rejects(invokeMcpTool(principal, "mm_operation_get", { operationId }), { code: "PROTOCOL_STORAGE_BUSY" })
assert.deepEqual((await pool.query(stateSql)).rows[0], baseline)
})
const blocker = await pool.connect()
let pending: ReturnType<typeof request> | undefined
let maintenance: Promise<void> | undefined
const waitForLease = async (mode: string, granted: boolean) => {
for (let attempt = 0; attempt < 100; attempt++) {
const { rowCount } = await pool.query(
"SELECT 1 FROM pg_locks WHERE locktype='advisory' AND classid=0 AND objid=$1 AND mode=$2 AND granted=$3",
[SYSTEM_DATABASE_REQUEST_LOCK, mode, granted])
if (rowCount) return
await delay(15)
}
assert.fail("Expected request lifecycle lease")
}
let response: Response
try {
await blocker.query("BEGIN")
await blocker.query("SELECT id FROM mcp_connections WHERE id=$1 FOR UPDATE", [principal.connectionId])
pending = request()
void pending.catch(() => {})
await waitForLease("ShareLock", true)
maintenance = withSystemDatabaseOperationLock(() => pool.connect(), async () => {
const { rows } = await pool.query(
"SELECT status,finished_at IS NOT NULL AS finished FROM mcp_requests WHERE connection_id=$1 AND tool IN ('mm_operation_get','protocol:tools/call')",
[principal.connectionId])
assert.deepEqual(rows, [{ status: "completed", finished: true }, { status: "completed", finished: true }])
})
void maintenance.catch(() => {})
await waitForLease("ExclusiveLock", false)
const refused = await request()
assert.equal(refused.status, 503)
assert.equal((await refused.json()).code, "PROTOCOL_STORAGE_BUSY")
await blocker.query("COMMIT")
response = await pending
await maintenance
} finally {
blocker.release(true)
await Promise.allSettled([pending, maintenance])
}
assert.equal(response.status, 200)
const body = await response.text()
const message = JSON.parse(body.split(/\r?\n/).find(line => line.startsWith("data: "))?.slice(6) ?? body)
assert.equal(message.result?.isError ?? false, false, body)
const result = message.result?.structuredContent?.result
assert.equal(result?.id, operationId, body)
assert.equal(result?.plan_id, planId)
assert.equal(result?.state, "running")
assert.deepEqual((await pool.query(
"SELECT tool FROM mcp_requests WHERE connection_id=$1 AND tool IN ('mm_operation_get','protocol:tools/call') ORDER BY tool",
[principal.connectionId],
)).rows, [{ tool: "mm_operation_get" }, { tool: "protocol:tools/call" }])
assert.equal((await pool.query("SELECT used FROM mcp_budget_buckets WHERE connection_id=$1 AND kind='protocol'", [principal.connectionId])).rows[0]?.used, 1)
await pool.query("DELETE FROM mcp_budget_buckets WHERE connection_id=$1 AND kind='protocol'", [principal.connectionId])
} finally {
await lock.query("SELECT pg_advisory_unlock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY])
lock.release()
}
// Leave room for the concurrency assertions before the next minute.
const minute = (await pool.query("SELECT date_trunc('minute',now()) = date_trunc('minute',now()+interval '5 seconds') AS safe")).rows[0].safe
if (!minute) await delay(5000)
const envelopes = await Promise.all(Array.from({ length: 16 }, () => reserveProtocolEnvelope(principal)))
assert.equal(envelopes.filter(envelope => envelope.allowed).length, 10)
const limited = await app.inject({ method: "POST", url: "/mcp",
headers: { authorization: `Bearer ${access.token}` }, payload: {} })
assert.equal(limited.statusCode, 429)
assert.equal(limited.json().code, "RATE_LIMITED")
assert.equal(limited.headers["retry-after"], String(limited.json().retryAfterSec))
for (let attempt = 0; attempt < 8; attempt++) await recordUnauthorizedProtocol("192.0.2.99", "UNAUTHORIZED")
const { rows: [counts] } = await pool.query(`SELECT
(SELECT count(*)::int FROM mcp_requests WHERE tool='protocol:rate-limit') AS denied,
(SELECT count(*)::int FROM mcp_requests WHERE tool='protocol:unauthorized') AS unauthorized,
(SELECT max(attempts)::int FROM mcp_unauthorized_buckets) AS attempts`)
assert.deepEqual(counts, { denied: 1, unauthorized: 1, attempts: 8 })
console.log("MCP request accounting respects the backup/restore lock independently of the partition lease")
} finally {
await app.close()
await closePool()
}