55 lines
3.5 KiB
TypeScript
55 lines
3.5 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { randomUUID } from "node:crypto"
|
|
import Fastify from "fastify"
|
|
import { mcpProtectedObjectSchema } from "@mmapp/contracts/mcp"
|
|
import { pool } from "../../db/index.js"
|
|
import type { AuthUser } from "../../lib/permissions.js"
|
|
import authPlugin from "../../plugins/auth.js"
|
|
import { createMcpRoutes } from "../../routes/mcp.js"
|
|
|
|
/** Owned disposable SQL only. Exercises the actual JWT administration route;
|
|
* no RouterOS requests, downloads, alert runners or secret payloads. */
|
|
export async function acceptProtectionRegistrationScope(actor: AuthUser) {
|
|
const app = Fastify(), ids: string[] = [], marker = randomUUID()
|
|
await app.register(authPlugin)
|
|
await app.register(createMcpRoutes(), { prefix: "/api" })
|
|
const token = app.jwt.sign({ sub: actor.id, apps: actor.apps, permissions: actor.permissions }, { iss: (await import("../../config.js")).env.authIssuer, expiresIn: "1h" })
|
|
const headers = { authorization: "Bearer " + token }
|
|
const counts = async () => ({
|
|
protections: (await pool.query("SELECT count(*)::int AS n FROM mcp_protections WHERE created_by=$1", [actor.id])).rows[0].n,
|
|
audit: (await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE actor_id=$1 AND action='protection.create'", [actor.id])).rows[0].n,
|
|
})
|
|
try {
|
|
const before = await counts()
|
|
for (const kind of ["wireguard_peer", "ethernet", "future-native-kind"]) {
|
|
const denied = await app.inject({ method: "POST", url: "/api/mcp/protections", headers, payload: { object: { kind, id: "*3", protection: "deny" }, reason: marker } })
|
|
assert.equal(denied.statusCode, 400, denied.body)
|
|
assert.equal(denied.json().code, "INVALID_INPUT")
|
|
}
|
|
assert.deepEqual(await counts(), before, "Invalid native scope creates neither a protection nor a creation audit")
|
|
for (const object of [
|
|
{ kind: "wireguard_peer", id: "*3", serverId: 141, protection: "deny" },
|
|
{ kind: "wireguard_peer", id: "*3", serverId: 142, protection: "deny" },
|
|
{ kind: "server", id: "141", protection: "deny" },
|
|
{ kind: "ping-probe", id: marker, protection: "human-approval-required" },
|
|
{ kind: "user", id: "scope-test-" + marker, protection: "deny" },
|
|
]) {
|
|
const response = await app.inject({ method: "POST", url: "/api/mcp/protections", headers, payload: { object, reason: marker } })
|
|
assert.equal(response.statusCode, 200, response.body)
|
|
const id = response.json().id as string; ids.push(id)
|
|
assert.deepEqual((await pool.query("SELECT object_ref FROM mcp_protections WHERE id=$1", [id])).rows[0].object_ref, object)
|
|
}
|
|
const after = await counts()
|
|
assert.equal(after.protections, before.protections + 5); assert.equal(after.audit, before.audit + 5)
|
|
const scoped = (await pool.query("SELECT object_ref FROM mcp_protections WHERE id=ANY($1::uuid[])", [ids.slice(0, 2)])).rows.map(row => row.object_ref.serverId).sort()
|
|
assert.deepEqual(scoped, [141, 142], "Equal native IDs retain distinct device scopes")
|
|
// Do not change the shared persisted-row schema: old ambiguous records
|
|
// should not prevent unrelated plans from reading their protections.
|
|
assert.equal(mcpProtectedObjectSchema.safeParse({ kind: "wireguard_peer", id: "*3", protection: "deny" }).success, true)
|
|
console.log("MCP protection registration HTTP: unscoped native IDs rejected without INSERT/audit; scoped duplicate IDs and global app/probe/server references preserved; legacy read schema unchanged")
|
|
} finally {
|
|
await app.close()
|
|
await pool.query("DELETE FROM mcp_protections WHERE id=ANY($1::uuid[])", [ids])
|
|
}
|
|
}
|