Repository navigation
Expand file tree
/
Copy pathsecuritySettingsWriteGuard.test.ts
More file actions
56 lines (51 loc) · 2.29 KB
/
Copy pathsecuritySettingsWriteGuard.test.ts
File metadata and controls
56 lines (51 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// @vitest-environment node
//
// Issue #856: the security switches are written by backend commands only. No
// non-test frontend file may pass one of these keys to a store `set`/`delete`.
import fs from "node:fs";
import path from "node:path";
import { describe, expect, it } from "vitest";
const PROTECTED_KEYS = ["redRiskAlwaysConfirm", "riskClassifierEnabled", "riskClassifierProviderId"];
const SRC = __dirname;
/** Returns the offending store calls (1-based line + text) in `text`. */
export function findProtectedStoreWrites(text: string): string[] {
// Constants that hold a protected key, e.g. `const ENABLED_KEY = "riskClassifierEnabled"`.
const names = [...text.matchAll(/\b(\w+)\s*(?::\s*[\w<>]+\s*)?=\s*["'`](\w+)["'`]/g)]
.filter((m) => PROTECTED_KEYS.includes(m[2]))
.map((m) => m[1]);
const needles = [...PROTECTED_KEYS, ...names];
const bad: string[] = [];
for (const m of text.matchAll(/\w*[sS]tore\w*\s*\.\s*(set|delete)\s*\(([^)]*)\)/gi)) {
const args = m[2];
if (needles.some((n) => new RegExp(`\\b${n}\\b`).test(args))) {
const line = text.slice(0, m.index).split("\n").length;
bad.push(`${line}: ${m[0]}`);
}
}
return bad;
}
function sourceFiles(dir: string): string[] {
return fs.readdirSync(dir, { withFileTypes: true }).flatMap((e) => {
const p = path.join(dir, e.name);
if (e.isDirectory()) return sourceFiles(p);
return /\.tsx?$/.test(e.name) && !/\.test\.tsx?$/.test(e.name) ? [p] : [];
});
}
describe("security settings are never written through the store (issue #856)", () => {
it("no non-test source passes a protected key to store.set/delete", () => {
const offenders = sourceFiles(SRC).flatMap((f) =>
findProtectedStoreWrites(fs.readFileSync(f, "utf8")).map((b) => `${path.relative(SRC, f)}:${b}`),
);
expect(offenders).toEqual([]);
});
it("counter-check: detects direct and constant-based writes in a sample", () => {
const sample = [
'const KEY = "redRiskAlwaysConfirm";',
"await store.set(KEY, false);",
'await store.delete("riskClassifierEnabled");',
'await settingsStore.set("riskClassifierProviderId", null);',
].join("\n");
expect(findProtectedStoreWrites(sample)).toHaveLength(3);
expect(findProtectedStoreWrites('await store.set("uiLanguage", "de");')).toEqual([]);
});
});