Files
MikrotikManager/backend/scripts/acceptance/chr-recursive-direct-links.mjs
T

87 lines
4.7 KiB
JavaScript

import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { lstat, readFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { decodeBody, SOURCES } from './chr-recursive-observation.mjs';
const reviewedReportSha256 = 'fccf5171c8e50730a3e7c1fcceeeb77f1e946589ac4a56c6e04668e0ff4663f9';
const sha256 = bytes => createHash('sha256').update(bytes).digest('hex');
const opaqueId = value => typeof value === 'string' && /^\*[a-f0-9]+$/i.test(value);
const decimalIndex = value => typeof value === 'string' && /^[1-9][0-9]{0,15}$/.test(value);
export function inspectDirectLinks(routes, nexthops, interfaces, tables) {
function keyed(rows, field, valid) {
const index = new Map();
for (const row of rows) {
const key = row[field];
assert(valid(key) && !index.has(key), 'Missing or ambiguous ' + field);
index.set(key, row);
}
return index;
}
const byNexthopId = keyed(nexthops, '.id', opaqueId);
const byInterfaceIndex = keyed(interfaces, 'ifindex', decimalIndex);
const byTableName = keyed(tables, 'name', value => typeof value === 'string' && value !== '' && !/^[0-9]+$/.test(value));
let opaqueReferences = 0, namedInterfaceCorroborations = 0, interfaceIdSubstitutionRefusals = 0;
for (const route of routes) {
assert(byTableName.has(route['routing-table']), 'Missing named routing table');
if (!Object.hasOwn(route, 'nexthop-id')) continue;
const nexthop = byNexthopId.get(route['nexthop-id']);
assert(nexthop, 'No exact opaque nexthop match');
opaqueReferences++;
const gateway = route['immediate-gw'];
const name = typeof gateway === 'string' ? /%([^%@]+)$/.exec(gateway)?.[1] : undefined;
if (!name) continue;
const index = nexthop['immediate-gw.interface-idx'];
const observed = byInterfaceIndex.get(index);
assert(observed?.name === name && interfaces.filter(row => row.name === name).length === 1,
'No exact named interface corroboration for ifindex');
namedInterfaceCorroborations++;
if (!interfaces.some(row => row.id === index && row.name === name)) interfaceIdSubstitutionRefusals++;
}
assert(opaqueReferences && namedInterfaceCorroborations && interfaceIdSubstitutionRefusals, 'Capture lacks discriminating direct links');
return { opaqueReferences, namedInterfaceCorroborations, interfaceIdSubstitutionRefusals,
tableNames: byTableName.size, recursiveLinkRows: nexthops.filter(row => Object.keys(row).some(key => key === 'links' || key.startsWith('links.'))).length };
}
async function readPinned(directory, filename, digest) {
const file = path.join(directory, filename), stat = await lstat(file);
assert(stat.isFile() && !stat.isSymbolicLink() && stat.size <= 1048576, 'Unavailable bounded raw file');
const bytes = await readFile(file);
assert(bytes.length === stat.size && sha256(bytes) === digest, 'Raw capture hash changed');
return bytes;
}
export async function checkReviewedDirectLinks(directory) {
const report = JSON.parse(await readPinned(directory, 'report.json', reviewedReportSha256));
const sources = [];
for (const source of report.sources) {
assert(source.complete && source.versionBracketEqual && SOURCES[source.source]?.serverId === source.serverId);
const read = async (index, resource) => {
const record = source.records[index];
assert(record.resource === resource && record.status === 200 && record.version === '7.24.5 (stable)');
assert(record.file === `server${source.serverId}-${String(index + 1).padStart(2, '0')}-${resource.slice(1).replaceAll('/', '-')}.json`);
return decodeBody(await readPinned(directory, record.file, record.rawSha256)).rows;
};
const routes = await read(1, '/routing/route'), nexthops = await read(2, '/routing/nexthop');
assert.deepEqual(await read(7, '/routing/route'), routes);
assert.deepEqual(await read(6, '/routing/nexthop'), nexthops);
assert.equal(source.records[1].rawSha256, source.records[7].rawSha256);
assert.equal(source.records[2].rawSha256, source.records[6].rawSha256);
const interfaces = await read(3, '/interface'), tables = await read(4, '/routing/table');
sources.push({ serverId: source.serverId, ...inspectDirectLinks(routes, nexthops, interfaces, tables) });
}
return { reviewedReportSha256, sources, recursiveIdentity: 'unknown', semanticProof: false };
}
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
try {
assert(process.argv.length === 3, 'Supply the reviewed offline capture directory');
process.stdout.write(JSON.stringify(await checkReviewedDirectLinks(process.argv[2])) + '\n');
} catch {
process.stderr.write('Reviewed direct-link corroboration refused.\n');
process.exitCode = 1;
}
}