102 lines
5.1 KiB
TypeScript
102 lines
5.1 KiB
TypeScript
import { readFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { describe, expect, test } from "vitest";
|
|
import { routeAccessInventory, routeAccessKey, type RouteAccessKey, type RouteMethod } from "./route-access-inventory.js";
|
|
|
|
const routeAccessInventoryDoc = readFileSync(join(process.cwd(), "docs/security/route-access-inventory.md"), "utf8");
|
|
|
|
const requiredSectionHeadings = [
|
|
"# Route Access Inventory",
|
|
"## Scope and Source of Truth",
|
|
"## Redaction Rules",
|
|
"## Access Class Legend",
|
|
"## Full Route Access Table",
|
|
"## Verification Commands",
|
|
];
|
|
|
|
const forbiddenSecretMarkers = [
|
|
/\bBearer\s+[-._~+/A-Za-z0-9]{20,}=*/,
|
|
/\b(?:sk|pk|rk)_(?:live|test)_[A-Za-z0-9_]+/,
|
|
/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/,
|
|
/\b(?:password|secret|token|api[-_ ]?key|jwt|credential)\s*[:=]\s*[^\s,;`|]+/i,
|
|
/\b(?:TENANT|CUSTOMER|FIXTURE)[-_][A-Za-z0-9]+\b/,
|
|
/\b(?:DATABASE_URL|SUPABASE_SERVICE_ROLE_KEY|JWT_SECRET|API_KEY|AUTH_TOKEN)\b/,
|
|
];
|
|
|
|
function missingItems(items: readonly string[], text: string) {
|
|
return items.filter((item) => !text.includes(item)).sort();
|
|
}
|
|
|
|
function escapeRegExp(text: string) {
|
|
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
}
|
|
|
|
const routeMethods: readonly RouteMethod[] = ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "ALL"];
|
|
|
|
function isRouteMethod(value: string): value is RouteMethod {
|
|
return routeMethods.includes(value as RouteMethod);
|
|
}
|
|
|
|
function documentedRouteKeys(text: string) {
|
|
return Array.from(text.matchAll(/^\|\s+`([A-Z]+) (\/[^`]+)`\s+\|/gm), (match) => {
|
|
const method = match[1] ?? "";
|
|
const path = match[2] ?? "";
|
|
if (!isRouteMethod(method)) {
|
|
throw new Error(`Unsupported route method in route access inventory documentation: ${method}`);
|
|
}
|
|
return `${method} ${path}` satisfies RouteAccessKey;
|
|
}).sort();
|
|
}
|
|
|
|
describe("route access inventory documentation", () => {
|
|
test("contains the required scope, redaction, legend, table, and verification sections", () => {
|
|
const missingHeadings = missingItems(requiredSectionHeadings, routeAccessInventoryDoc);
|
|
|
|
expect(missingHeadings, `Missing route access inventory document sections:\n${missingHeadings.join("\n")}`).toEqual([]);
|
|
expect(routeAccessInventoryDoc).toContain("docs/security/csrf-inventory.md");
|
|
expect(routeAccessInventoryDoc).toContain("apps/api/src/route-access-inventory.ts");
|
|
expect(routeAccessInventoryDoc).toContain("apps/api/src/route-access-inventory.test.ts");
|
|
expect(routeAccessInventoryDoc).toContain("apps/api/src/route-access-inventory-doc.test.ts");
|
|
});
|
|
|
|
test("documents every classified route key from the typed inventory exactly once", () => {
|
|
const routeKeys = routeAccessInventory.map(routeAccessKey);
|
|
const missingRouteKeys = missingItems(routeKeys, routeAccessInventoryDoc);
|
|
const extraRouteKeys = documentedRouteKeys(routeAccessInventoryDoc)
|
|
.filter((key) => !routeKeys.includes(key))
|
|
.sort();
|
|
const duplicateRouteKeys = routeKeys
|
|
.filter((key) => routeAccessInventoryDoc.match(new RegExp(String.raw`\|\s+\`${escapeRegExp(key)}\`\s+\|`, "g"))?.length !== 1)
|
|
.sort();
|
|
|
|
expect(missingRouteKeys, `Missing route access inventory document route keys:\n${missingRouteKeys.join("\n")}`).toEqual([]);
|
|
expect(extraRouteKeys, `Stale route access inventory document route keys:\n${extraRouteKeys.join("\n")}`).toEqual([]);
|
|
expect(duplicateRouteKeys, `Route access inventory document keys not present exactly once in the table:\n${duplicateRouteKeys.join("\n")}`).toEqual([]);
|
|
});
|
|
|
|
test("keeps access classes and verification commands visible for future route changes", () => {
|
|
const missingAccessClasses = missingItems(Array.from(new Set(routeAccessInventory.map((entry) => `\`${entry.accessClass}\``))), routeAccessInventoryDoc);
|
|
const missingVerificationCommands = missingItems(
|
|
[
|
|
"pnpm exec vitest run apps/api/src/route-access-inventory.test.ts apps/api/src/route-access-inventory-doc.test.ts",
|
|
"pnpm exec eslint apps/api/src/route-access-inventory.ts apps/api/src/route-access-inventory.test.ts apps/api/src/route-access-inventory-doc.test.ts --max-warnings=0",
|
|
],
|
|
routeAccessInventoryDoc
|
|
);
|
|
|
|
expect(missingAccessClasses, `Missing route access class legend entries:\n${missingAccessClasses.join("\n")}`).toEqual([]);
|
|
expect(missingVerificationCommands, `Missing route access inventory verification commands:\n${missingVerificationCommands.join("\n")}`).toEqual([]);
|
|
});
|
|
|
|
test("does not expose secret-like markers, fixture identifiers, customer data, or environment variable names", () => {
|
|
const leakedMarkers = forbiddenSecretMarkers
|
|
.map((pattern) => ({ pattern: pattern.toString(), match: routeAccessInventoryDoc.match(pattern)?.[0] }))
|
|
.filter((result): result is { pattern: string; match: string } => result.match !== undefined);
|
|
|
|
expect(
|
|
leakedMarkers.map((result) => `${result.pattern} matched ${JSON.stringify(result.match)}`),
|
|
`Forbidden secret-like markers in route access inventory document:\n${leakedMarkers.map((result) => `${result.pattern} matched ${JSON.stringify(result.match)}`).join("\n")}`
|
|
).toEqual([]);
|
|
});
|
|
});
|