159 lines
5.4 KiB
TypeScript
159 lines
5.4 KiB
TypeScript
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
import { join, resolve } from "node:path";
|
|
import { describe, expect, test } from "vitest";
|
|
import { getEnvVariableDefinition } from "./index.js";
|
|
|
|
const rootPath = (...segments: string[]) => resolve(process.cwd(), ...segments);
|
|
const readProjectFile = (path: string) => readFileSync(rootPath(path), "utf8");
|
|
|
|
const scannedProductionFiles = [
|
|
"apps/api/src/config.ts",
|
|
"apps/api/src/app.ts",
|
|
"apps/worker/src/runtime.ts",
|
|
"apps/worker/src/health.ts",
|
|
"packages/queue/src/connection.ts",
|
|
"packages/mail/src/mailjet.ts",
|
|
"packages/config/src/api.ts",
|
|
"packages/config/src/web.ts",
|
|
"packages/config/src/worker.ts",
|
|
"packages/config/src/routing.ts",
|
|
] as const;
|
|
|
|
const providerBoundaryFiles = new Set<string>([
|
|
"apps/api/src/app.ts",
|
|
"apps/worker/src/runtime.ts",
|
|
"apps/worker/src/health.ts",
|
|
"packages/queue/src/connection.ts",
|
|
"packages/config/src/api.ts",
|
|
"packages/config/src/web.ts",
|
|
"packages/config/src/worker.ts",
|
|
"packages/config/src/routing.ts",
|
|
]);
|
|
|
|
const packageEnvReadBaseline = new Set<string>([
|
|
"packages/cli/src/commands/dsgvo/delete.ts",
|
|
"packages/cli/src/commands/feature-flags/invalidate.ts",
|
|
"packages/cli/src/commands/tenant/list-snapshots.ts",
|
|
"packages/cli/src/commands/tenant/restore.ts",
|
|
"packages/config/src/api.ts",
|
|
"packages/config/src/routing.ts",
|
|
"packages/config/src/web.ts",
|
|
"packages/config/src/worker.ts",
|
|
"packages/db/src/client.server.ts",
|
|
"packages/db/src/seed.server.ts",
|
|
"packages/logger/src/index.ts",
|
|
"packages/mail/scripts/preview-server.ts",
|
|
"packages/feature-flags/src/publisher.ts",
|
|
"packages/queue/src/connection.ts",
|
|
"packages/queue/src/priority.ts",
|
|
"packages/shared-store/src/factory.ts",
|
|
]);
|
|
|
|
const catalogGuardKeys = ["API_PORT", "WEB_ORIGIN", "ADMIN_ORIGIN", "PORT", "HOST", "SITE_KEY_EXAMPLE_SITE"] as const;
|
|
|
|
type DirectEnvRead = {
|
|
file: string;
|
|
key: string;
|
|
};
|
|
|
|
function stripComments(source: string) {
|
|
return source
|
|
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
.split(/\r?\n/)
|
|
.map((line) => line.replace(/\/\/.*$/, ""))
|
|
.join("\n");
|
|
}
|
|
|
|
function findDirectEnvReads(file: string, source: string): DirectEnvRead[] {
|
|
const executableSource = stripComments(source);
|
|
const directReads = [
|
|
...executableSource.matchAll(/\bprocess\.env\.([A-Z][A-Z0-9_]*)\b/g),
|
|
...executableSource.matchAll(/\bprocess\.env\[(["'])([A-Z][A-Z0-9_]*)\1\]/g),
|
|
];
|
|
|
|
return directReads.map((match) => ({ file, key: match[2] ?? match[1] }));
|
|
}
|
|
|
|
function formatDirectEnvReads(directReads: readonly DirectEnvRead[]) {
|
|
return directReads.map(({ file, key }) => `${file}:${key}`).join(", ");
|
|
}
|
|
|
|
function listSourceFiles(root: string): string[] {
|
|
const absoluteRoot = rootPath(root);
|
|
const ignoredSegments = new Set(["generated", "dist", "build", "node_modules"]);
|
|
const files: string[] = [];
|
|
|
|
function walk(directory: string) {
|
|
for (const entry of readdirSync(directory)) {
|
|
if (ignoredSegments.has(entry)) {
|
|
continue;
|
|
}
|
|
|
|
const absolutePath = join(directory, entry);
|
|
const stats = statSync(absolutePath);
|
|
if (stats.isDirectory()) {
|
|
walk(absolutePath);
|
|
continue;
|
|
}
|
|
|
|
if (!/\.(ts|tsx|mts|cts)$/.test(entry) || /\.test\./.test(entry) || /\.spec\./.test(entry)) {
|
|
continue;
|
|
}
|
|
|
|
files.push(absolutePath.replace(`${process.cwd()}/`, ""));
|
|
}
|
|
}
|
|
|
|
walk(absoluteRoot);
|
|
return files.sort();
|
|
}
|
|
|
|
describe("approved env boundary enforcement", () => {
|
|
test("detects dot and bracket process.env reads after stripping comments", () => {
|
|
const directReads = findDirectEnvReads(
|
|
"inline-sample.ts",
|
|
`
|
|
// process.env.JWT_SECRET is documentation only.
|
|
const token = process.env.API_ACCESS_TOKEN;
|
|
const port = process.env["API_PORT"];
|
|
`
|
|
);
|
|
|
|
expect(directReads).toEqual([
|
|
{ file: "inline-sample.ts", key: "API_ACCESS_TOKEN" },
|
|
{ file: "inline-sample.ts", key: "API_PORT" },
|
|
]);
|
|
});
|
|
|
|
test("keeps direct production env reads inside approved provider boundaries", () => {
|
|
const directReads = scannedProductionFiles.flatMap((file) => findDirectEnvReads(file, readProjectFile(file)));
|
|
const forbiddenReads = directReads.filter(({ file }) => !providerBoundaryFiles.has(file));
|
|
|
|
expect(forbiddenReads, `Forbidden direct env reads: ${formatDirectEnvReads(forbiddenReads)}`).toEqual([]);
|
|
});
|
|
|
|
test("blocks new package-level direct env reads outside the reviewed baseline", () => {
|
|
const filesWithDirectReads = listSourceFiles("packages").filter((file) => findDirectEnvReads(file, readProjectFile(file)).length > 0);
|
|
const unexpectedFiles = filesWithDirectReads.filter((file) => !packageEnvReadBaseline.has(file));
|
|
|
|
expect(unexpectedFiles, `Unexpected package env reads: ${unexpectedFiles.join(", ")}`).toEqual([]);
|
|
});
|
|
|
|
test("catalogs protected API, platform and dynamic env keys", () => {
|
|
for (const key of catalogGuardKeys) {
|
|
expect(getEnvVariableDefinition(key), `${key} must resolve to an env catalog entry or dynamic prefix entry`).toBeDefined();
|
|
}
|
|
|
|
expect(getEnvVariableDefinition("SITE_KEY_EXAMPLE_SITE")).toMatchObject({
|
|
name: "SITE_KEY_*",
|
|
dynamicPrefix: "SITE_KEY_",
|
|
});
|
|
});
|
|
|
|
test("keeps S06 enforcement ownership visible in the inventory without reading env value files", () => {
|
|
const inventory = readProjectFile("docs/env-access-inventory.md");
|
|
|
|
expect(inventory).toContain("S06 enforcement");
|
|
});
|
|
});
|