Files
2026-07-08 16:15:37 +02:00

46 lines
2.2 KiB
TypeScript

import fs from "node:fs";
import path from "node:path";
import { describe, expect, test } from "vitest";
import { resolveRepoPath } from "~/utils/testing/repoPaths";
// AK-P16a · static authorization-boundary guard for the calendar mutation routes
// (DoD A.5 / DoD 3). Asserts every state-changing action runs the module/permission
// guard, the owner-check (where applicable) and CSRF validation BEFORE it reads the
// form body — so a missing module/permission/owner/CSRF can never reach a mutation.
const appRoot = resolveRepoPath("apps/web/app");
const mutationRoutes = [
"routes/app.$tenant/settings.integrations.calendar._index.tsx",
"routes/app.$tenant/settings.integrations.calendar.$id.tsx",
];
describe("calendar route authorization boundary", () => {
test("guards permission + CSRF before parsing the form body", () => {
for (const relativePath of mutationRoutes) {
const source = fs.readFileSync(path.join(appRoot, relativePath), "utf8");
const formDataAt = source.indexOf("await request.formData()");
expect(formDataAt, relativePath).toBeGreaterThanOrEqual(0);
const permissionAt = source.indexOf("requireCalendarPermission(ctx, CALENDAR_PERMISSIONS.connectOwn)");
expect(permissionAt, relativePath).toBeGreaterThanOrEqual(0);
expect(permissionAt, relativePath).toBeLessThan(formDataAt);
const csrfAt = source.indexOf("await validateCSRFToken(request)");
expect(csrfAt, relativePath).toBeGreaterThanOrEqual(0);
expect(csrfAt, relativePath).toBeLessThan(formDataAt);
}
});
test("detail route owner-checks the connection before mutating", () => {
const source = fs.readFileSync(path.join(appRoot, "routes/app.$tenant/settings.integrations.calendar.$id.tsx"), "utf8");
const ownerAt = source.indexOf("requireConnectionOwner(ctx, connection.userId)");
const formDataAt = source.indexOf("await request.formData()");
expect(ownerAt).toBeGreaterThanOrEqual(0);
// The action's owner-check (the second occurrence) precedes the form read.
const actionOwnerAt = source.indexOf("requireConnectionOwner(ctx, connection.userId)", ownerAt + 1);
expect(actionOwnerAt).toBeGreaterThanOrEqual(0);
expect(actionOwnerAt).toBeLessThan(formDataAt);
});
});