Files
MikrotikManager/backend/src/services/bandwidth-rest-speed-original.sql-helper.ts
T

216 lines
18 KiB
TypeScript

/** Owned application SQL + genuine Fastify/JWT + actual localhost TCP only.
* This does not dispatch bandwidth, publish history, activate a caller or use
* signed operator/node control state. No fake SQL rows or native methods. */
import assert from "node:assert/strict"
import { createHash, randomBytes, randomUUID } from "node:crypto"
import { createServer, request as httpRequest, type ClientRequest } from "node:http"
import { writeFile, readFile, mkdir, unlink } from "node:fs/promises"
import { join } from "node:path"
import type { FastifyInstance } from "fastify"
import { assertOwnedBandwidthSql } from "./bandwidth-admission-process.sql-helper.js"
export async function acceptRestSpeedBandwidthOriginalSql() {
assertOwnedBandwidthSql(); assert.equal(process.env.MMAPP_OWN_REST_SPEED_ORIGINAL, "true")
const { pool } = await import("../db/index.js")
const { default: Fastify } = await import("fastify")
const { SignJWT } = await import("jose")
const { default: authPlugin, requireAuth } = await import("../plugins/auth.js")
const { env } = await import("../config.js")
const { createRestSpeedBandwidthOriginalProvider } = await import("./bandwidth-rest-speed-original.js")
assert.equal(env.authRequired, true)
const directory = process.env.MCP_STATE_DIR!, keyPath = join(directory, "encryption-key"), generationPath = join(directory, "access-generation")
const key = randomBytes(32), generation = randomBytes(32)
await writeFile(keyPath, key, { flag: "wx", mode: 0o600 }); await writeFile(generationPath, generation, { flag: "wx", mode: 0o600 })
const actor = "owned-rest-speed-" + randomUUID(), probeId = "owned-rest-speed-probe-" + randomUUID()
const users = ["OWNED_REST_SPEED_SOURCE_USER", "OWNED_REST_SPEED_DESTINATION_USER"], passwords = ["OWNED_REST_SPEED_SOURCE_PASSWORD", "OWNED_REST_SPEED_DESTINATION_PASSWORD"]
const privateValues = [...users, ...passwords], checks: string[] = []
const hidden = (value: string) => assert(!privateValues.some(secret => value.includes(secret)))
const barrier = async <T>(pending: Promise<T>, message: string): Promise<T> => {
let timer: ReturnType<typeof setTimeout> | undefined
try { return await Promise.race([pending, new Promise<never>((_yes, no) => { timer = setTimeout(() => no(Error(message)), 10000) })]) }
finally { clearTimeout(timer) }
}
let getCalls = 0, postCalls = 0, protocolErrors = 0, holdNative = false
let heldStarted!: () => void, heldClosed!: () => void, releaseHeld!: () => void
let started = Promise.resolve(), closed = Promise.resolve(), hold = Promise.resolve()
const resetHeld = () => {
started = new Promise<void>(yes => { heldStarted = yes }); closed = new Promise<void>(yes => { heldClosed = yes }); hold = new Promise<void>(yes => { releaseHeld = yes })
}
resetHeld()
const getOrder: number[] = []
const routers = [0, 1].map(index => createServer(async (request, response) => {
try {
if (request.method !== "GET") { postCalls++; throw Error("Only read-only GET is allowed") }
assert.equal(request.url, "/rest/ip/address"); assert.equal(request.socket.remoteAddress, "127.0.0.1")
assert.equal(request.headers.authorization === "Basic " + Buffer.from(users[index] + ":" + passwords[index]).toString("base64"), true)
getCalls++; getOrder.push(index)
response.setHeader("content-type", "application/json")
if (holdNative && index === 1) { response.once("close", heldClosed); heldStarted(); await hold; if (response.destroyed) return }
const rows = index === 0 ? [{ interface: "gre-source", address: "198.51.100.1/30" }] : [
{ interface: "gre-destination", address: "198.51.100.99/30", dynamic: "true" },
{ interface: "gre-destination", address: "198.51.100.77/30", disabled: "true" },
{ interface: "gre-destination", address: "198.51.100.66/30", invalid: "true" },
{ interface: "other", address: "198.51.100.55/30" },
{ interface: "gre-destination", address: "198.51.100.2/30", dynamic: "false" },
]
response.end(JSON.stringify(rows))
} catch { protocolErrors++; response.statusCode = 502; response.end('{"error":"Owned read-only fixture protocol mismatch"}') }
}))
const owner = await pool.connect()
let caller: ClientRequest | undefined
let app: FastifyInstance | undefined, sourceId = 0, destinationId = 0, restoredQuery = false
const actualQuery = pool.query
let credentialQueries = 0, credentialPayloadRows = 0, metadataQueries = 0
let afterMetadata: (() => Promise<void>) | undefined, beforeCredentials: (() => Promise<void>) | undefined
let lastCredentialRows = -1, routeMode: "positive" | "capture" | "pin-drift" | "hold" = "positive"
let drift: (() => Promise<void>) | undefined, handlerFinished!: () => void
let handlerDone = Promise.resolve()
const command = (interfaces = true) => ({ runId: "owned-private-run", probeId, srcServerId: sourceId, dstServerId: destinationId,
srcInterface: interfaces ? "gre-source" : "", dstInterface: interfaces ? "gre-destination" : "", protocol: "udp", direction: "receive", durationSec: 3 })
const restoreProbe = async (interfaces = true) => owner.query("UPDATE public.uptime_speed_probes SET src_interface=$2,dst_interface=$3,protocol='udp',direction='receive',duration_sec=3,enabled=true WHERE id=$1", [probeId, interfaces ? "gre-source" : "", interfaces ? "gre-destination" : ""])
const cutOff = async () => owner.query("INSERT INTO public.mcp_owner_revocations(owner_id,invalid_before) VALUES($1,now()) ON CONFLICT(owner_id) DO UPDATE SET invalid_before=EXCLUDED.invalid_before", [actor])
const clearCutoff = async () => owner.query("DELETE FROM public.mcp_owner_revocations WHERE owner_id=$1", [actor])
const rotateEndpoint = async (id: number) => {
const before = (await owner.query("SELECT verify_ssl,routing_endpoint_epoch::text AS epoch FROM public.servers WHERE id=$1", [id])).rows[0]
assert(before); assert.equal(typeof before.verify_ssl, "boolean")
// Migration 0029 deliberately discards an epoch-only UPDATE. Change an
// actual endpoint field and let the unchanged trigger mint its own epoch.
const after = (await owner.query("UPDATE public.servers SET verify_ssl=NOT verify_ssl WHERE id=$1 RETURNING verify_ssl,routing_endpoint_epoch::text AS epoch", [id])).rows[0]
assert(after); assert.equal(after.verify_ssl, !before.verify_ssl); assert.notEqual(after.epoch, before.epoch)
}
const history = async () => (await owner.query("SELECT (SELECT count(*)::int FROM public.uptime_speed_test_runs) AS speed,(SELECT count(*)::int FROM public.scheduler_runs) AS scheduler,(SELECT count(*)::int FROM public.server_probe_executions) AS diagnostic")).rows[0]
try {
for (const router of routers) await new Promise<void>((yes, no) => { router.once("error", no); router.listen(0, "127.0.0.1", yes) })
const ids = []
for (let index = 0; index < routers.length; index++) ids.push(Number((await owner.query("INSERT INTO public.servers(name,host,port,use_ssl,verify_ssl,enabled,username,password) VALUES($1,'127.0.0.1',$2,false,false,true,$3,$4) RETURNING id", ["owned-rest-speed-" + index, (routers[index].address() as { port: number }).port, users[index], passwords[index]])).rows[0].id))
;[sourceId, destinationId] = ids
assert.notEqual(sourceId, destinationId)
await owner.query("INSERT INTO public.uptime_speed_probes(id,src_server_id,dst_server_id,src_interface,dst_interface,protocol,direction,duration_sec,enabled) VALUES($1,$2,$3,'gre-source','gre-destination','udp','receive',3,true)", [probeId, sourceId, destinationId])
const baselineHistory = await history()
// Observation only: original query/config/parameters and actual PG rows are
// forwarded unchanged. Fixture mutations use a different explicit client.
pool.query = (function(this: typeof pool, ...args: unknown[]) {
const config = args[0] as string | { text: string; query_timeout?: number }, text = typeof config === "string" ? config : config.text
return (async () => {
const credentials = text.startsWith("/* rest-speed-original:credentials */"), metadata = text.startsWith("/* rest-speed-original:metadata */")
if (credentials) { credentialQueries++; assert(typeof config !== "string" && config.query_timeout! > 0); const hook = beforeCredentials; beforeCredentials = undefined; await hook?.() }
const result = await Reflect.apply(actualQuery, this, args)
if (credentials) { lastCredentialRows = result.rows.length; credentialPayloadRows += result.rows.length }
if (metadata) { metadataQueries++; const hook = afterMetadata; afterMetadata = undefined; await hook?.() }
return result
})()
}) as typeof pool.query
const provider = createRestSpeedBandwidthOriginalProvider()
app = Fastify(); await app.register(authPlugin); app.addHook("preHandler", requireAuth)
app.setErrorHandler((_error, _request, reply) => { reply.code(403).send({ refused: true }) })
app.post("/api/uptime/speed-test", async request => {
try {
const scope = { deadlineAt: Date.now() + 30000 }, token = await provider.capture(request, request.body, scope)
if (routeMode === "hold") throw Error("Held native request unexpectedly completed")
if (routeMode === "pin-drift") { await drift?.(); await assert.rejects(provider.readOriginal(token, scope)); return { ok: true, driftRefused: true } }
if (routeMode === "positive") {
const intent = await provider.readOriginal(token, scope), context = await provider.readSpeedContext(token, scope)
assert.deepEqual(intent.nativeBody, { address: "198.51.100.2", user: users[1], password: passwords[1], protocol: "udp", direction: "receive", duration: "3s" })
assert.equal(intent.socketTimeoutMs, 30000); assert.equal(intent.callerKind, "rest-speed"); assert.equal(intent.credentialServerId, destinationId)
assert.deepEqual(intent.actualRemotePeer, { kind: "server", serverId: destinationId }); assert(intent.expiresAt <= scope.deadlineAt)
assert.equal(context.srcAddress, "198.51.100.1"); assert.equal(context.dstAddress, "198.51.100.2")
assert.equal((await provider.readPeer(token, { kind: "server", serverId: sourceId }, scope)).endpoint.password, passwords[0])
await assert.rejects(provider.capture(request, request.body, scope))
}
return { ok: true }
} finally { handlerFinished?.() }
})
const jwt = await new SignJWT({ sub: actor, apps: ["mm"], permissions: ["mm:uptime:write"], iat: Math.floor(Date.now() / 1000) - 60, exp: Math.floor(Date.now() / 1000) + 3600 })
.setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).sign(new TextEncoder().encode(env.jwtSecret))
const send = async (interfaces = true, withProbe = true) => {
const payload: Record<string, unknown> = command(interfaces); if (!withProbe) delete payload.probeId
const result = await app!.inject({ method: "POST", url: "/api/uptime/speed-test", headers: { authorization: "Bearer " + jwt }, payload }); hidden(result.body); return result
}
assert.equal((await send()).statusCode, 200); assert.deepEqual(getOrder, [1, 0]); assert.equal(postCalls, 0)
checks.push("actual signed JWT/Fastify request -> original provider -> real bounded metadata/credentials/probe SQL -> two actual localhost Basic-auth GETs; exact static-first body/address/interfaces/30s baseline; private cap cannot recapture")
await owner.query("UPDATE public.uptime_speed_probes SET enabled=false WHERE id=$1", [probeId])
try { assert.equal((await send()).statusCode, 200, "unchanged disabled manual probe remains eligible") } finally { await restoreProbe() }
assert.equal((await send(true, false)).statusCode, 200, "ad-hoc NULL probe predicate preserves baseline")
checks.push("actual credential SELECT preserves exact disabled manual-probe snapshot and NULL ad-hoc probe; no enabled=true restriction added")
routeMode = "capture"
for (const fault of ["key", "generation", "revoke", "probe", "disabled"] as const) {
const beforeGets = getCalls, beforeSecrets = credentialPayloadRows
afterMetadata = async () => {
if (fault === "key") await writeFile(keyPath, randomBytes(32))
if (fault === "generation") await writeFile(generationPath, randomBytes(32))
if (fault === "revoke") await cutOff()
if (fault === "probe") await owner.query("UPDATE public.uptime_speed_probes SET duration_sec=4 WHERE id=$1", [probeId])
if (fault === "disabled") await owner.query("UPDATE public.servers SET enabled=false WHERE id=$1", [sourceId])
}
try { assert.equal((await send()).statusCode, 403, fault); assert.equal(getCalls, beforeGets, fault); assert.equal(credentialPayloadRows, beforeSecrets, fault) }
finally { afterMetadata = undefined; await writeFile(keyPath, key); await writeFile(generationPath, generation); await clearCutoff(); await restoreProbe(); await owner.query("UPDATE public.servers SET enabled=true WHERE id=$1", [sourceId]) }
}
checks.push("actual SQL/file changes after metadata await: key/generation/owner revocation/probe projection/disabled participant refuse before any native GET or credential payload transfer")
{
const beforeGets = getCalls, beforeSecrets = credentialPayloadRows
beforeCredentials = () => rotateEndpoint(destinationId)
try { assert.equal((await send()).statusCode, 403); assert.equal(lastCredentialRows, 0); assert.equal(getCalls, beforeGets); assert.equal(credentialPayloadRows, beforeSecrets) }
finally { beforeCredentials = undefined; await owner.query("UPDATE public.servers SET verify_ssl=false WHERE id=$1", [destinationId]) }
checks.push("real separate PG session rotates endpoint epoch at actual credential SELECT boundary; MATERIALIZED complete-pair gate returns zero credential rows and zero native GET")
}
for (const change of ["duration", "enabled"] as const) {
const beforeGets = getCalls, beforeSecrets = credentialPayloadRows, beforeQueries = credentialQueries
beforeCredentials = async () => {
await owner.query(change === "duration" ? "UPDATE public.uptime_speed_probes SET duration_sec=4 WHERE id=$1"
: "UPDATE public.uptime_speed_probes SET enabled=false WHERE id=$1", [probeId])
}
try {
assert.equal((await send()).statusCode, 403, change)
assert.equal(credentialQueries, beforeQueries + 1); assert.equal(lastCredentialRows, 0)
assert.equal(credentialPayloadRows, beforeSecrets); assert.equal(getCalls, beforeGets)
} finally { beforeCredentials = undefined; await restoreProbe() }
}
checks.push("actual separate-session probe duration/enabled UPDATE immediately before unchanged credential SQL dispatch returns zero credential payload rows and zero native GET; complete-pair and exact captured probe share one MATERIALIZED SELECT")
await restoreProbe(false); routeMode = "pin-drift"
for (const fault of ["key", "generation", "epoch", "probe", "revoke"] as const) {
const beforeGets = getCalls
drift = async () => {
if (fault === "key") await writeFile(keyPath, randomBytes(32))
if (fault === "generation") await writeFile(generationPath, randomBytes(32))
if (fault === "epoch") await rotateEndpoint(sourceId)
if (fault === "probe") await owner.query("UPDATE public.uptime_speed_probes SET direction='both' WHERE id=$1", [probeId])
if (fault === "revoke") await cutOff()
}
try { assert.equal((await send(false)).statusCode, 200, fault); assert.equal(getCalls, beforeGets, fault) }
finally { drift = undefined; await writeFile(keyPath, key); await writeFile(generationPath, generation); await clearCutoff(); await restoreProbe(false); await owner.query("UPDATE public.servers SET verify_ssl=false WHERE id=$1", [sourceId]) }
}
checks.push("private capability retains original key/generation/epoch/probe/owner after capture; real subsequent changes refuse readOriginal without renewing intent or performing a native call")
routeMode = "capture"
const external = join(directory, "owner-revocations", createHash("sha256").update(actor).digest("hex") + ".json")
await mkdir(join(directory, "owner-revocations"), { mode: 0o700 })
const beforeExternal = getCalls
await writeFile(external, JSON.stringify({ invalidBefore: Date.now() }), { flag: "wx", mode: 0o600 })
try { assert.equal((await send(false)).statusCode, 403); assert.equal(getCalls, beforeExternal) } finally { await unlink(external) }
await restoreProbe(); routeMode = "hold"; holdNative = true; resetHeld()
handlerDone = new Promise<void>(yes => { handlerFinished = yes })
const base = await app.listen({ host: "127.0.0.1", port: 0 }), beforePendingGets = getCalls
caller = httpRequest(base + "/api/uptime/speed-test", { method: "POST", headers: { authorization: "Bearer " + jwt, "content-type": "application/json" } })
caller.on("error", () => {})
caller.end(JSON.stringify(command()))
await barrier(started, "Actual native GET did not enter held responder")
caller.destroy(Error("Owned original REST client disconnected"))
await barrier(closed, "Actual original REST disconnect did not close pending native TCP response")
await barrier(handlerDone, "Original provider handler remained pending after actual TCP disconnect")
assert.equal(getCalls, beforePendingGets + 1); assert.equal(postCalls, 0)
holdNative = false; releaseHeld()
checks.push("real Fastify TCP caller disconnect aborts original private request signal and closes a genuinely pending native HTTP GET socket; no second GET, POST, history or automatic replay")
assert.deepEqual(await history(), baselineHistory); assert.equal(postCalls, 0); assert.equal(protocolErrors, 0)
assert.equal((await readFile(keyPath)).length, 32); assert.equal((await readFile(generationPath)).length, 32)
pool.query = actualQuery; restoredQuery = true
return { checks, actualReadOnlyNativeGets: getCalls, actualNativePosts: postCalls, metadataQueries, credentialQueries, credentialPayloadRows,
actualSql: true, actualFastifyJwt: true, actualPendingTcpClosed: true, historyUnchanged: true, automaticReplay: false, realChrCalls: 0, runtimeCallerActivation: false }
} finally {
caller?.destroy()
holdNative = false; releaseHeld?.()
if (!restoredQuery) pool.query = actualQuery
await app?.close()
for (const router of routers) { router.closeAllConnections(); if (router.listening) await new Promise<void>((yes, no) => router.close(error => error ? no(error) : yes())) }
owner.release(true)
}
}