periderm-cli 0.1.77 → 0.1.78
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +13 -5
- package/dist/report/markdown.js +5 -4
- package/dist/review/deep.js +1 -1
- package/dist/scanner/checks.js +43 -17
- package/dist/scanner/compliance-checks.js +361 -0
- package/dist/scanner/index.js +43 -20
- package/dist/scanner/metrics.js +107 -14
- package/dist/scanner/ml-predictor.js +21 -15
- package/dist/scanner/policy-checks.js +7 -13
- package/dist/scanner/project-context.js +16 -0
- package/dist/scanner/repo-checks.js +112 -37
- package/dist/scanner/walk.js +68 -21
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -60,6 +60,7 @@ program
|
|
|
60
60
|
.command("scan")
|
|
61
61
|
.description("Scan the current directory and write a markdown report.")
|
|
62
62
|
.option("--cwd <dir>", "Directory to scan", process.cwd())
|
|
63
|
+
.option("--local", "Scan without uploading results to the dashboard")
|
|
63
64
|
.action(async (opts) => {
|
|
64
65
|
const root = path.resolve(opts.cwd);
|
|
65
66
|
const cfg = readConfig();
|
|
@@ -166,7 +167,10 @@ program
|
|
|
166
167
|
if (!(await hasAgentRules(root))) {
|
|
167
168
|
console.info(chalk.dim(" tip: run `periderm agents` so AI coding agents (Cursor, Claude Code, Copilot, Codex…) always scan & fix before saying they're done."));
|
|
168
169
|
}
|
|
169
|
-
if (
|
|
170
|
+
if (opts.local) {
|
|
171
|
+
console.info(chalk.dim(" local-only scan: results were not uploaded to your dashboard."));
|
|
172
|
+
}
|
|
173
|
+
else if (!cfg.token) {
|
|
170
174
|
console.info(chalk.yellow("no token configured — run `periderm login` first to sync to dashboard."));
|
|
171
175
|
}
|
|
172
176
|
else {
|
|
@@ -210,7 +214,7 @@ program
|
|
|
210
214
|
});
|
|
211
215
|
program
|
|
212
216
|
.command("review")
|
|
213
|
-
.description("Run AI deep review on the last scan (Scale/Unlimited).
|
|
217
|
+
.description("Run the AI deep review on the last scan (Scale/Unlimited plans).")
|
|
214
218
|
.option("--deep", "Run the AI agent deep review")
|
|
215
219
|
.option("--cwd <dir>", "Project directory", process.cwd())
|
|
216
220
|
.action(async (opts) => {
|
|
@@ -298,7 +302,11 @@ program
|
|
|
298
302
|
await run();
|
|
299
303
|
try {
|
|
300
304
|
const watcher = fs.watch(root, { recursive: true });
|
|
301
|
-
for await (const
|
|
305
|
+
for await (const ev of watcher) {
|
|
306
|
+
// Dependency installs, git operations and build output would otherwise re-scan constantly.
|
|
307
|
+
if (ev.filename &&
|
|
308
|
+
/(^|[\\/])(node_modules|\.git|\.periderm|dist|build|\.next|\.output|\.vercel|coverage)([\\/]|$)/.test(ev.filename))
|
|
309
|
+
continue;
|
|
302
310
|
if (scheduled)
|
|
303
311
|
clearTimeout(scheduled);
|
|
304
312
|
scheduled = setTimeout(run, 400);
|
|
@@ -311,7 +319,7 @@ program
|
|
|
311
319
|
});
|
|
312
320
|
program
|
|
313
321
|
.command("login")
|
|
314
|
-
.description("
|
|
322
|
+
.description("Link the CLI to your Periderm account in the browser.")
|
|
315
323
|
.action(async () => {
|
|
316
324
|
const cfg = readConfig();
|
|
317
325
|
const apiUrl = cfg.apiUrl;
|
|
@@ -455,7 +463,7 @@ program
|
|
|
455
463
|
});
|
|
456
464
|
program
|
|
457
465
|
.command("whoami")
|
|
458
|
-
.description("
|
|
466
|
+
.description("Show the linked account, plan and remaining scans.")
|
|
459
467
|
.action(async () => {
|
|
460
468
|
const cfg = readConfig();
|
|
461
469
|
if (!cfg.token) {
|
package/dist/report/markdown.js
CHANGED
|
@@ -31,10 +31,10 @@ export function renderMarkdown(r) {
|
|
|
31
31
|
lines.push(`- **AI context triage:** enabled${r.meta.contextFilteredCount ? ` · ${r.meta.contextFilteredCount} finding(s) filtered as not applicable` : ""}`);
|
|
32
32
|
}
|
|
33
33
|
if (r.meta.mlPredictorLoaded) {
|
|
34
|
-
lines.push(`- **ML defect predictor:** PROMISE-trained
|
|
34
|
+
lines.push(`- **ML defect predictor:** PROMISE-trained Random Forest loaded (check id \`ai-defect-prediction\`)`);
|
|
35
35
|
}
|
|
36
36
|
else {
|
|
37
|
-
lines.push(`- **ML defect predictor:** not loaded (model file
|
|
37
|
+
lines.push(`- **ML defect predictor:** not loaded (model file missing — findings will not include \`ai-defect-prediction\`)`);
|
|
38
38
|
}
|
|
39
39
|
lines.push("");
|
|
40
40
|
}
|
|
@@ -135,8 +135,9 @@ export function renderMarkdown(r) {
|
|
|
135
135
|
lines.push(`You are reviewing a Periderm CLI scan: Launch Confidence ${r.scores.confidence}/100, Reality ${r.scores.reality}/100, Perceived ${r.scores.perceived}/100 — verdict ${verdictLabel}.`);
|
|
136
136
|
lines.push(`There are ${r.counts.critical} critical, ${r.counts.high} high, ${r.counts.medium} medium, ${r.counts.low} low findings.`);
|
|
137
137
|
lines.push(`Fix them in priority order. For each finding, open the listed file, apply the Exact fix, and keep the change minimal.`);
|
|
138
|
-
|
|
139
|
-
|
|
138
|
+
// Relative to the project root, so it is valid on the user's machine (web scans run on a server).
|
|
139
|
+
const aiInstructionsPath = ".periderm/ai-instructions.md";
|
|
140
|
+
lines.push(`IMPORTANT: Before you begin, read the instructions in \`${aiInstructionsPath}\` (in the project root; run \`periderm scan\` once if it is missing) for the full iterative process on how to run the scanner, verify fixes, and handle any authentication requirements.`);
|
|
140
141
|
lines.push("```");
|
|
141
142
|
lines.push("");
|
|
142
143
|
// Hidden watermark — only visible in raw .md source
|
package/dist/review/deep.js
CHANGED
|
@@ -113,7 +113,7 @@ Evidence rules (false positives destroy trust — a wrong finding is worse than
|
|
|
113
113
|
|
|
114
114
|
Known NON-issues — never report these:
|
|
115
115
|
- requestAnimationFrame / setTimeout loops that re-schedule themselves (e.g. Lenis, GSAP, Framer Motion "raf" loops). They are async and do not grow the stack; a cancelAnimationFrame/clearTimeout in cleanup is the complete fix.
|
|
116
|
-
- Static asset CDNs (
|
|
116
|
+
- Static asset CDNs (jsDelivr, unpkg, cdnjs, Font Awesome) are NOT analytics, tracking or "data sharing". Google Fonts' GDPR IP-transfer risk is already covered by the static check \`google-fonts-cdn\`; do not report it again or call it "tracking".
|
|
117
117
|
- Tailwind/CSS class names (bg-*, text-*, heading-*) are not tracking IDs.
|
|
118
118
|
- Self-hosted fonts via @fontsource are not third-party at all.${orgRulesSection}`;
|
|
119
119
|
const context = {
|
package/dist/scanner/checks.js
CHANGED
|
@@ -67,6 +67,9 @@ const silentCatch = (source, file, ast) => {
|
|
|
67
67
|
const catchSrc = source.slice(p.node.start ?? 0, p.node.end ?? 0);
|
|
68
68
|
if (/periderm-ignore|keep original|ignore auth|unauthenticated|fallback/i.test(catchSrc))
|
|
69
69
|
return;
|
|
70
|
+
// Like ESLint's no-empty: an empty catch with an explanatory comment is a deliberate choice.
|
|
71
|
+
if (body.length === 0 && (p.node.body.innerComments?.length ?? 0) > 0)
|
|
72
|
+
return;
|
|
70
73
|
if (body.length === 0) {
|
|
71
74
|
out.push(mkFinding({
|
|
72
75
|
id: "silent-catch",
|
|
@@ -317,7 +320,13 @@ const dangerousHtml = (source, file) => {
|
|
|
317
320
|
const expr = m[1].trim();
|
|
318
321
|
const isLiteral = /^["'`]/.test(expr);
|
|
319
322
|
const hasSanitize = /DOMPurify\.sanitize/.test(source);
|
|
320
|
-
|
|
323
|
+
// Structured data: <script type="application/ld+json"> filled with JSON.stringify(...) is the
|
|
324
|
+
// standard, safe way to embed JSON-LD (especially with "<" escaped).
|
|
325
|
+
// Only look at this element's own opening tag and this value — never neighbouring code.
|
|
326
|
+
const ownTag = source.slice(source.lastIndexOf("<", m.index), m.index);
|
|
327
|
+
const isJsonLd = /application\/ld\+json/.test(ownTag) && /JSON\.stringify\s*\(/.test(expr);
|
|
328
|
+
const escapesTags = /JSON\.stringify\s*\([^)]*\)\s*\.replace\(\s*\/<\/g/.test(expr);
|
|
329
|
+
if (!isLiteral && !hasSanitize && !isJsonLd && !escapesTags) {
|
|
321
330
|
out.push(mkFinding({
|
|
322
331
|
id: "dangerous-html",
|
|
323
332
|
category: "Security",
|
|
@@ -537,10 +546,34 @@ const unhandledFetch = (source, file, ast) => {
|
|
|
537
546
|
const callee = p.node.callee;
|
|
538
547
|
if (callee.type !== "Identifier" || callee.name !== "fetch")
|
|
539
548
|
return;
|
|
549
|
+
// A helper that just returns the fetch promise leaves handling to its (awaiting) callers.
|
|
550
|
+
let up = p.parentPath;
|
|
551
|
+
while (up && up.node.type === "AwaitExpression")
|
|
552
|
+
up = up.parentPath;
|
|
553
|
+
if (up &&
|
|
554
|
+
(up.node.type === "ReturnStatement" || up.node.type === "ArrowFunctionExpression")) {
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
540
557
|
let cur = p;
|
|
541
558
|
while (cur) {
|
|
542
559
|
if (cur.node?.type === "TryStatement")
|
|
543
560
|
return;
|
|
561
|
+
// Server handlers: a thrown network error becomes an error response, not a silent UI break.
|
|
562
|
+
if (cur.node?.type === "CallExpression") {
|
|
563
|
+
const c = cur.node.callee;
|
|
564
|
+
if (c?.type === "MemberExpression" &&
|
|
565
|
+
c.property?.type === "Identifier" &&
|
|
566
|
+
/^(handler|server|middleware|loader|action)$/.test(c.property.name))
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
if ((cur.node?.type === "ObjectProperty" || cur.node?.type === "ObjectMethod") &&
|
|
570
|
+
cur.node.key?.type === "Identifier" &&
|
|
571
|
+
/^(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS|loader|action)$/.test(cur.node.key.name))
|
|
572
|
+
return;
|
|
573
|
+
if (cur.node?.type === "FunctionDeclaration" &&
|
|
574
|
+
/^(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS|loader|action)$/.test(cur.node.id?.name ?? "") &&
|
|
575
|
+
cur.parentPath?.node?.type?.startsWith("Export"))
|
|
576
|
+
return;
|
|
544
577
|
if (cur.node?.type === "CallExpression") {
|
|
545
578
|
const c = cur.node.callee;
|
|
546
579
|
if (c?.type === "MemberExpression" &&
|
|
@@ -560,8 +593,7 @@ const unhandledFetch = (source, file, ast) => {
|
|
|
560
593
|
while ((m = re.exec(source))) {
|
|
561
594
|
const before = source.slice(Math.max(0, m.index - 2500), m.index);
|
|
562
595
|
const after = source.slice(m.index, m.index + 400);
|
|
563
|
-
const handled = /\.catch\s*\(/.test(before + after) ||
|
|
564
|
-
/try\s*\{/.test(before);
|
|
596
|
+
const handled = /\.catch\s*\(/.test(before + after) || /try\s*\{/.test(before);
|
|
565
597
|
if (!handled)
|
|
566
598
|
push(lineOf(source, m.index));
|
|
567
599
|
}
|
|
@@ -1347,32 +1379,26 @@ const CHECKS = [
|
|
|
1347
1379
|
];
|
|
1348
1380
|
export const PER_FILE_CHECK_COUNT = CHECKS.length;
|
|
1349
1381
|
export function runChecks(source, file, ast) {
|
|
1382
|
+
// Guard against callers (or fuzzers) passing something that isn't a source string / Babel File.
|
|
1383
|
+
if (typeof source !== "string" || typeof file !== "string")
|
|
1384
|
+
return [];
|
|
1385
|
+
const safeAst = ast && ast.type === "File" ? ast : null;
|
|
1350
1386
|
const out = [];
|
|
1351
1387
|
for (const c of CHECKS) {
|
|
1352
1388
|
try {
|
|
1353
|
-
out.push(...c(source, file,
|
|
1389
|
+
out.push(...c(source, file, safeAst));
|
|
1354
1390
|
}
|
|
1355
1391
|
catch (e) {
|
|
1392
|
+
// A bug in one of *our* rules must not cost the user points or show up as their finding.
|
|
1393
|
+
// Report it for us to fix and keep scanning with the other rules.
|
|
1356
1394
|
const err = e instanceof Error ? e : new Error(String(e));
|
|
1357
|
-
// Report the crash silently in the background
|
|
1358
1395
|
reportCliError({
|
|
1359
1396
|
source: "cli-scan",
|
|
1360
|
-
message: `Scanner rule
|
|
1397
|
+
message: `Scanner rule ${c.name || "anonymous"} crashed`,
|
|
1361
1398
|
stack: err.stack,
|
|
1362
1399
|
where: c.name || "anonymous check",
|
|
1363
1400
|
extra: { file, rawError: String(e) },
|
|
1364
1401
|
}).catch(() => { });
|
|
1365
|
-
out.push(mkFinding({
|
|
1366
|
-
id: "internal-error",
|
|
1367
|
-
category: "Runtime Stability",
|
|
1368
|
-
severity: "high",
|
|
1369
|
-
file,
|
|
1370
|
-
line: 1,
|
|
1371
|
-
message: `Scanner crashed: ${e}`,
|
|
1372
|
-
why: "Internal exception in scanner rule.",
|
|
1373
|
-
fix: "Report this issue.",
|
|
1374
|
-
aiPrompt: "Fix the crash in the scanner rule.",
|
|
1375
|
-
}));
|
|
1376
1402
|
}
|
|
1377
1403
|
}
|
|
1378
1404
|
return out;
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Legal traps that keep showing up in AI-built apps, detected from code + legal copy:
|
|
3
|
+
*
|
|
4
|
+
* 1. COPPA — signup with no age gate and no children's policy
|
|
5
|
+
* 2. GDPR — Google Fonts loaded from Google's CDN (leaks visitor IPs to Google)
|
|
6
|
+
* 3. CIPA — session replay without consent, or with input masking turned off
|
|
7
|
+
* 4. CAN-SPAM — marketing/broadcast email without an unsubscribe link or postal address
|
|
8
|
+
* 5. California ARL — subscription checkout with no auto-renewal terms near the button
|
|
9
|
+
* 6. DMCA — user uploads with no copyright/takedown policy or designated agent
|
|
10
|
+
*
|
|
11
|
+
* Each check only fires on a concrete code signal and is silenced by matching disclosures,
|
|
12
|
+
* so a project that does the right thing gets no noise.
|
|
13
|
+
*/
|
|
14
|
+
import fs from "node:fs/promises";
|
|
15
|
+
import path from "node:path";
|
|
16
|
+
import { isLegalPage } from "./legal.js";
|
|
17
|
+
import { listProjectFiles } from "./walk.js";
|
|
18
|
+
export const COMPLIANCE_CHECK_COUNT = 6;
|
|
19
|
+
async function readMaybe(p) {
|
|
20
|
+
try {
|
|
21
|
+
return await fs.readFile(p, "utf8");
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function lineOf(src, index) {
|
|
28
|
+
return src.slice(0, index).split("\n").length;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Legal copy from code pages *and* markdown/html documents, with comments stripped so a
|
|
32
|
+
* "note for the scanner" comment can never stand in for real policy text.
|
|
33
|
+
*
|
|
34
|
+
* `dynamic` is true when the pages load their text at runtime (DB/CMS) and contain almost no
|
|
35
|
+
* prose themselves — content checks can't verify those, so they must not guess.
|
|
36
|
+
*/
|
|
37
|
+
export async function collectLegalText(root) {
|
|
38
|
+
const candidates = await listProjectFiles(root, "tsx,jsx,md,mdx,html,htm,txt,svelte,vue,astro");
|
|
39
|
+
const files = candidates
|
|
40
|
+
.map((abs) => path.relative(root, abs).replace(/\\/g, "/"))
|
|
41
|
+
.filter(isLegalPage);
|
|
42
|
+
let text = "";
|
|
43
|
+
let loadsRemotely = false;
|
|
44
|
+
for (const rel of files) {
|
|
45
|
+
const raw = (await readMaybe(path.join(root, rel))) ?? "";
|
|
46
|
+
const stripped = raw
|
|
47
|
+
.replace(/\/\*[\s\S]*?\*\//g, " ")
|
|
48
|
+
.replace(/<!--[\s\S]*?-->/g, " ")
|
|
49
|
+
.replace(/(^|[^:"'`\\])\/\/[^\n]*/g, "$1");
|
|
50
|
+
if (/\.from\(\s*["'`][\w-]*(legal|polic|page|content|doc)|useQuery\s*\(|useServerFn\s*\(|fetch\s*\(|getStaticProps|getServerSideProps|contentful|sanity|cms/i.test(stripped)) {
|
|
51
|
+
loadsRemotely = true;
|
|
52
|
+
}
|
|
53
|
+
text += "\n" + stripped;
|
|
54
|
+
}
|
|
55
|
+
const sentences = (text.match(/[A-Za-z][a-z]+(?:[\s,]+[a-zA-Z'’-]+){7,}[.;:]/g) ?? []).length;
|
|
56
|
+
const dynamic = files.length > 0 && loadsRemotely && sentences < 10;
|
|
57
|
+
return { text: text.toLowerCase(), files, dynamic };
|
|
58
|
+
}
|
|
59
|
+
// ── 1. COPPA ────────────────────────────────────────────────────────────────
|
|
60
|
+
const SIGNUP_RE = /\.auth\.signUp\s*\(|createUserWithEmailAndPassword\s*\(|<SignUp\b|useSignUp\s*\(|\bsignUp\.create\s*\(|auth\.api\.signUpEmail|registerUser\s*\(/;
|
|
61
|
+
const AGE_GATE_RE = /\b(date[_\s-]?of[_\s-]?birth|birth[_\s-]?(date|day)|\bdob\b|age[_\s-]?(verif|confirm|gate|check)|i\s+am\s+(at\s+least|over)\s+\d{2}|(at\s+least|over)\s+(13|16|18)\s*(years|\+)|is_?adult|min(imum)?_?age)\b/i;
|
|
62
|
+
const CHILDREN_POLICY_RE = /under\s+(the\s+age\s+of\s+)?1[36]|at\s+least\s+1[368]\s+years|children'?s?\s+(privacy|data|information)|coppa|not\s+(intended|directed)\s+(for|to|at)\s+children|minimum\s+age/;
|
|
63
|
+
function coppaCheck(code, legal, legalFile, legalDynamic) {
|
|
64
|
+
const signup = code.find((f) => SIGNUP_RE.test(f.src));
|
|
65
|
+
if (!signup)
|
|
66
|
+
return [];
|
|
67
|
+
const codeHasAgeGate = code.some((f) => AGE_GATE_RE.test(f.src));
|
|
68
|
+
const policyHasAgeRule = CHILDREN_POLICY_RE.test(legal);
|
|
69
|
+
if (code.some((f) => AGE_GATE_RE.test(f.src)))
|
|
70
|
+
return [];
|
|
71
|
+
if (legalDynamic && !policyHasAgeRule) {
|
|
72
|
+
const line = lineOf(signup.src, signup.src.search(SIGNUP_RE));
|
|
73
|
+
return [
|
|
74
|
+
{
|
|
75
|
+
id: "coppa-age-not-asked",
|
|
76
|
+
category: "Legal & Compliance",
|
|
77
|
+
severity: "high",
|
|
78
|
+
file: signup.rel,
|
|
79
|
+
line,
|
|
80
|
+
message: "Signup never asks users to confirm a minimum age.",
|
|
81
|
+
why: "If a child under 13 signs up and you collect their email, COPPA penalties run up to $53,088 per child. Your legal pages load their text at runtime, so make sure they also include a children's-privacy section.",
|
|
82
|
+
fix: "Add an age confirmation to signup (e.g. 'I am at least 13 (16 in the EU)') and a 'Children's privacy' section to the privacy policy content.",
|
|
83
|
+
aiPrompt: `In ${signup.rel} near line ${line}, add an age confirmation to signup ("By creating an account you confirm you are at least 13 years old (16 in the EU/UK)", or a required checkbox), and add a "Children's privacy" section to the privacy policy content stating the service is not directed to children under 13.`,
|
|
84
|
+
},
|
|
85
|
+
];
|
|
86
|
+
}
|
|
87
|
+
if (codeHasAgeGate)
|
|
88
|
+
return [];
|
|
89
|
+
const line = lineOf(signup.src, signup.src.search(SIGNUP_RE));
|
|
90
|
+
if (!policyHasAgeRule) {
|
|
91
|
+
return [
|
|
92
|
+
{
|
|
93
|
+
id: "coppa-no-age-gate",
|
|
94
|
+
category: "Legal & Compliance",
|
|
95
|
+
severity: "high",
|
|
96
|
+
file: signup.rel,
|
|
97
|
+
line,
|
|
98
|
+
message: "Signup never asks for age and the policies say nothing about children.",
|
|
99
|
+
why: "If a child under 13 signs up and you collect their email, COPPA penalties run up to $53,088 per child. Without an age gate or a children's policy you have no defence.",
|
|
100
|
+
fix: "Add an 'I am at least 13 (16 in the EU)' confirmation or date-of-birth check to signup, and a 'Children's privacy' section to the privacy policy saying the service isn't for under-13s.",
|
|
101
|
+
aiPrompt: `In ${signup.rel} near line ${line}, signup collects personal data with no age check. Add a required checkbox "I confirm I am at least 13 years old (16 in the EU/UK)" that blocks submission when unchecked, and add a "Children's privacy" section to the privacy policy stating the service is not directed to children under 13 and how a parent can request deletion.`,
|
|
102
|
+
},
|
|
103
|
+
];
|
|
104
|
+
}
|
|
105
|
+
return [
|
|
106
|
+
{
|
|
107
|
+
id: "coppa-age-not-asked",
|
|
108
|
+
category: "Legal & Compliance",
|
|
109
|
+
severity: "medium",
|
|
110
|
+
file: signup.rel,
|
|
111
|
+
line,
|
|
112
|
+
message: "Policy sets a minimum age, but signup never asks users to confirm it.",
|
|
113
|
+
why: "A minimum age in the policy alone is weak evidence under COPPA. A recorded confirmation at signup shows you took reasonable steps.",
|
|
114
|
+
fix: `Add a required age confirmation to signup that matches the age in ${legalFile}.`,
|
|
115
|
+
aiPrompt: `In ${signup.rel} near line ${line}, add a required checkbox confirming the user meets the minimum age stated in ${legalFile}, and block signup when it is unchecked.`,
|
|
116
|
+
},
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
// ── 2. Google Fonts CDN ─────────────────────────────────────────────────────
|
|
120
|
+
const GOOGLE_FONTS_RE = /fonts\.(googleapis|gstatic)\.com/;
|
|
121
|
+
async function googleFontsCheck(root, legal) {
|
|
122
|
+
if (/google\s+fonts/.test(legal))
|
|
123
|
+
return [];
|
|
124
|
+
const files = await listProjectFiles(root, "css,scss,sass,less,html,htm,tsx,jsx,ts,js,vue,svelte,astro");
|
|
125
|
+
for (const abs of files) {
|
|
126
|
+
const rel = path.relative(root, abs).replace(/\\/g, "/");
|
|
127
|
+
const src = await readMaybe(abs);
|
|
128
|
+
if (!src)
|
|
129
|
+
continue;
|
|
130
|
+
const idx = src.search(GOOGLE_FONTS_RE);
|
|
131
|
+
if (idx === -1)
|
|
132
|
+
continue;
|
|
133
|
+
const line = lineOf(src, idx);
|
|
134
|
+
return [
|
|
135
|
+
{
|
|
136
|
+
id: "google-fonts-cdn",
|
|
137
|
+
category: "Legal & Compliance",
|
|
138
|
+
severity: "medium",
|
|
139
|
+
file: rel,
|
|
140
|
+
line,
|
|
141
|
+
message: "Google Fonts are loaded from Google's servers, sending every visitor's IP to Google.",
|
|
142
|
+
why: "A Munich court (LG München, 3 O 17493/20) ordered a site to pay damages to a single visitor for exactly this under GDPR, and copycat demand letters followed. EU visitors never consented to the transfer.",
|
|
143
|
+
fix: "Self-host the fonts (e.g. @fontsource packages or next/font, which downloads them at build time) and remove the fonts.googleapis.com / fonts.gstatic.com links. Otherwise disclose Google Fonts in the privacy policy and get consent.",
|
|
144
|
+
aiPrompt: `In ${rel} near line ${line}, fonts are loaded from fonts.googleapis.com. Replace this with self-hosted fonts: install the matching @fontsource/* packages (or use next/font/google in Next.js), import them once at the app root, delete the Google Fonts <link>/@import, and check no other file still references fonts.googleapis.com or fonts.gstatic.com.`,
|
|
145
|
+
},
|
|
146
|
+
];
|
|
147
|
+
}
|
|
148
|
+
return [];
|
|
149
|
+
}
|
|
150
|
+
// ── 3. Session replay ───────────────────────────────────────────────────────
|
|
151
|
+
const REPLAY_RE = [
|
|
152
|
+
[/LogRocket\.init\s*\(/, "LogRocket"],
|
|
153
|
+
[/@fullstory\/browser|FullStory\.init|\bFS\.init\s*\(|fullstory\.com\/s\/fs\.js/, "FullStory"],
|
|
154
|
+
[/static\.hotjar\.com|hotjar\.init|@hotjar\/browser|\bhj\s*\(\s*['"]/, "Hotjar"],
|
|
155
|
+
[/clarity\.ms\/tag|microsoft-clarity|\bclarity\s*\(\s*['"]/, "Microsoft Clarity"],
|
|
156
|
+
[/replayIntegration\s*\(|new\s+(Sentry\.)?Replay\s*\(/, "Sentry Session Replay"],
|
|
157
|
+
[
|
|
158
|
+
/session_recording\s*:\s*\{|enable_recording_console_log|startSessionRecording\s*\(/,
|
|
159
|
+
"PostHog session recording",
|
|
160
|
+
],
|
|
161
|
+
[/\brrweb\b|smartlook|mouseflow|inspectlet|logrocket/i, "session replay SDK"],
|
|
162
|
+
];
|
|
163
|
+
const UNMASKED_RE = /maskAllInputs\s*:\s*false|inputSanitizer\s*:\s*false|textSanitizer\s*:\s*false|recordCrossDomainIFrames|privacyMode\s*:\s*false|maskAllText\s*:\s*false|maskInputOptions\s*:\s*\{[^}]*password\s*:\s*false|recordInputs\s*:\s*true|mask_all_inputs\s*:\s*false/;
|
|
164
|
+
const CONSENT_RE = /cookie[-_\s]?consent|consent[-_\s]?(banner|manager|mode|given|granted)|hasConsent|getConsent|opt[-_\s]?in|CookieBanner|cookiebot|onetrust|klaro|usercentrics|iubenda/i;
|
|
165
|
+
function sessionReplayCheck(code) {
|
|
166
|
+
for (const f of code) {
|
|
167
|
+
for (const [re, tool] of REPLAY_RE) {
|
|
168
|
+
const idx = f.src.search(re);
|
|
169
|
+
if (idx === -1)
|
|
170
|
+
continue;
|
|
171
|
+
const line = lineOf(f.src, idx);
|
|
172
|
+
if (code.some((c) => UNMASKED_RE.test(c.src))) {
|
|
173
|
+
return [
|
|
174
|
+
{
|
|
175
|
+
id: "session-replay-unmasked",
|
|
176
|
+
category: "Legal & Compliance",
|
|
177
|
+
severity: "critical",
|
|
178
|
+
file: f.rel,
|
|
179
|
+
line,
|
|
180
|
+
message: `${tool} records sessions with input masking turned off, capturing what users type.`,
|
|
181
|
+
why: "Recording keystrokes and form contents can count as wiretapping under California's CIPA ($5,000 per violation, per session) and leaks passwords and card numbers into a third-party tool.",
|
|
182
|
+
fix: "Turn input and text masking back on (maskAllInputs: true), and only start recording after the user consents.",
|
|
183
|
+
aiPrompt: `Find every session-replay config in this project (${tool}, around ${f.rel}:${line}) and set input/text masking to true. Start recording only after explicit consent from the cookie/consent banner.`,
|
|
184
|
+
},
|
|
185
|
+
];
|
|
186
|
+
}
|
|
187
|
+
if (code.some((c) => CONSENT_RE.test(c.src)))
|
|
188
|
+
return [];
|
|
189
|
+
return [
|
|
190
|
+
{
|
|
191
|
+
id: "session-replay-no-consent",
|
|
192
|
+
category: "Legal & Compliance",
|
|
193
|
+
severity: "high",
|
|
194
|
+
file: f.rel,
|
|
195
|
+
line,
|
|
196
|
+
message: `${tool} session replay starts without asking for consent.`,
|
|
197
|
+
why: "Class actions under California's CIPA treat replay scripts that record clicks and typing as wiretapping ($5,000 per session). In the EU it also needs prior consent under ePrivacy/GDPR.",
|
|
198
|
+
fix: "Only initialise session replay after the user accepts a consent banner, keep input masking on, and disclose it in the privacy policy.",
|
|
199
|
+
aiPrompt: `In ${f.rel} near line ${line}, ${tool} session replay starts on page load. Gate its initialisation behind an explicit consent choice (cookie/consent banner), keep all input masking enabled, and add a privacy-policy section naming ${tool} and what it records.`,
|
|
200
|
+
},
|
|
201
|
+
];
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return [];
|
|
205
|
+
}
|
|
206
|
+
// ── 4. CAN-SPAM ─────────────────────────────────────────────────────────────
|
|
207
|
+
const EMAIL_SEND_RE = /resend\.(emails|batch|broadcasts)\.(send|create)|sgMail\.send|@sendgrid\/mail|nodemailer|createTransport\s*\(|postmark|sendEmailWithTemplate|mailgun|SendEmailCommand|mailchimp|@loops\/|loops\.sendTransactional|brevo|sendinblue/i;
|
|
208
|
+
const MARKETING_RE = /newsletter|announcement|broadcast|campaign|marketing|promo(tion|tional)?|we('| ha)?ve\s+launched|product\s+update|digest|audience|subscribers/i;
|
|
209
|
+
const UNSUBSCRIBE_RE = /unsubscribe|List-Unsubscribe|opt[-_\s]?out|manage\s+(email\s+)?preferences/i;
|
|
210
|
+
const POSTAL_RE = /\b\d{1,6}\s+[A-Za-z0-9.'\s]{2,40}\s(street|st\.?|avenue|ave\.?|road|rd\.?|boulevard|blvd\.?|lane|ln\.?|drive|dr\.?|way|suite|floor)\b|p\.?\s?o\.?\s+box|postal\s+address|mailing\s+address|company[_\s]?address/i;
|
|
211
|
+
function canSpamCheck(code) {
|
|
212
|
+
for (const f of code) {
|
|
213
|
+
if (!EMAIL_SEND_RE.test(f.src) || !MARKETING_RE.test(f.src))
|
|
214
|
+
continue;
|
|
215
|
+
const line = lineOf(f.src, f.src.search(EMAIL_SEND_RE));
|
|
216
|
+
if (!UNSUBSCRIBE_RE.test(f.src)) {
|
|
217
|
+
return [
|
|
218
|
+
{
|
|
219
|
+
id: "email-no-unsubscribe",
|
|
220
|
+
category: "Legal & Compliance",
|
|
221
|
+
severity: "high",
|
|
222
|
+
file: f.rel,
|
|
223
|
+
line,
|
|
224
|
+
message: "Marketing/broadcast email is sent with no unsubscribe link.",
|
|
225
|
+
why: "CAN-SPAM requires a working opt-out in every commercial email; each non-compliant email can cost up to $53,088. Gmail and Yahoo also require one-click unsubscribe for bulk senders.",
|
|
226
|
+
fix: "Add a visible unsubscribe link plus a List-Unsubscribe / List-Unsubscribe-Post header, honour opt-outs within 10 days, and include your postal address in the footer.",
|
|
227
|
+
aiPrompt: `In ${f.rel} near line ${line}, a marketing/broadcast email is sent without an unsubscribe option. Add an unsubscribe link to the email footer (signed per-user URL), set the List-Unsubscribe and List-Unsubscribe-Post headers, store opt-outs and skip those recipients, and add the business postal address to the footer.`,
|
|
228
|
+
},
|
|
229
|
+
];
|
|
230
|
+
}
|
|
231
|
+
if (!POSTAL_RE.test(f.src)) {
|
|
232
|
+
return [
|
|
233
|
+
{
|
|
234
|
+
id: "email-no-postal-address",
|
|
235
|
+
category: "Legal & Compliance",
|
|
236
|
+
severity: "medium",
|
|
237
|
+
file: f.rel,
|
|
238
|
+
line,
|
|
239
|
+
message: "Marketing email has an unsubscribe link but no physical postal address.",
|
|
240
|
+
why: "CAN-SPAM requires a valid physical postal address (street address, registered PO box or private mailbox) in every commercial email.",
|
|
241
|
+
fix: "Add your business postal address to the email footer.",
|
|
242
|
+
aiPrompt: `In ${f.rel}, add the business's physical postal address to the footer of every marketing/broadcast email template.`,
|
|
243
|
+
},
|
|
244
|
+
];
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return [];
|
|
248
|
+
}
|
|
249
|
+
// ── 5. Auto-renewal disclosure ──────────────────────────────────────────────
|
|
250
|
+
const RECURRING_PRICE_RE = /\/\s?mo(nth)?\b|per\s+(month|year|week)|\/\s?(yr|year)\b|billed\s+(monthly|annually|yearly)|monthly|yearly|annually/i;
|
|
251
|
+
const SUBSCRIBE_CTA_RE = /(subscribe|upgrade|start\s+(free\s+)?trial|start\s+plan|get\s+(pro|unlimited|premium)|checkout)/i;
|
|
252
|
+
const SUBSCRIPTION_SIGNAL_RE = /subscription|lemonsqueezy|lemon\s?squeezy|mode:\s*['"]subscription['"]|paddle|recurring/i;
|
|
253
|
+
/** The file must actually start a checkout — an upsell link to a pricing page isn't one. */
|
|
254
|
+
const CHECKOUT_TRIGGER_RE = /checkout\s*\(|getCheckoutUrl|createCheckout|checkout\.sessions\.create|redirectToCheckout|lemonsqueezy\.com\/(checkout|buy)|LemonSqueezy\.Url\.Open|Paddle\.Checkout|createSubscription|billing_portal|\/api\/(checkout|subscribe|billing)/i;
|
|
255
|
+
const RENEWAL_TERMS_RE = /auto-?renew|renews?\s+(automatically|each|every|monthly|annually|yearly|until)|recurring\s+(charge|billing|payment)|until\s+(you\s+)?cancel|cancel\s+(any\s?time|before)|billed\s+(monthly|annually|yearly)\s+until/i;
|
|
256
|
+
/**
|
|
257
|
+
* Text a user can actually read: JSX text and string literals — not identifiers like
|
|
258
|
+
* `channel.subscribe()` or imports like `getCheckoutUrl`.
|
|
259
|
+
*/
|
|
260
|
+
function visibleText(src) {
|
|
261
|
+
const out = [];
|
|
262
|
+
// JSX text runs: after a tag or an {expression}, up to the next tag or expression.
|
|
263
|
+
const re = /[>}]\s*([^<>{}]*[A-Za-z][^<>{}]*?)\s*(?=[<{])/g;
|
|
264
|
+
let m;
|
|
265
|
+
while ((m = re.exec(src)))
|
|
266
|
+
out.push({ text: m[1], index: m.index + 1 });
|
|
267
|
+
const strRe = /(["'`])((?:(?!\1)[^\\\n]){3,120})\1/g;
|
|
268
|
+
while ((m = strRe.exec(src))) {
|
|
269
|
+
// Skip import paths, class lists and identifiers-only strings.
|
|
270
|
+
if (/^[\w./@-]+$/.test(m[2]) ||
|
|
271
|
+
/\b(className|import|from|require)\b/.test(src.slice(Math.max(0, m.index - 20), m.index)))
|
|
272
|
+
continue;
|
|
273
|
+
if (/\s/.test(m[2]))
|
|
274
|
+
out.push({ text: m[2], index: m.index });
|
|
275
|
+
}
|
|
276
|
+
return out;
|
|
277
|
+
}
|
|
278
|
+
function renewalTermsCheck(code) {
|
|
279
|
+
const out = [];
|
|
280
|
+
for (const f of code) {
|
|
281
|
+
if (!/\.(tsx|jsx|vue|svelte|astro|html)$/.test(f.rel))
|
|
282
|
+
continue;
|
|
283
|
+
if (!/<button|<Button|<a\s|<Link\b/.test(f.src))
|
|
284
|
+
continue;
|
|
285
|
+
if (!SUBSCRIPTION_SIGNAL_RE.test(f.src) || !CHECKOUT_TRIGGER_RE.test(f.src))
|
|
286
|
+
continue;
|
|
287
|
+
const texts = visibleText(f.src);
|
|
288
|
+
const all = texts.map((t) => t.text).join(" \n ");
|
|
289
|
+
if (!RECURRING_PRICE_RE.test(all))
|
|
290
|
+
continue;
|
|
291
|
+
// Prefer real purchase buttons ("Upgrade…", "Subscribe…") over prose that mentions checkout.
|
|
292
|
+
const candidates = texts
|
|
293
|
+
.filter((t) => !/sign\s?in|log\s?in|sign\s?up/i.test(t.text) && t.text.length <= 60)
|
|
294
|
+
.sort((a, b) => a.index - b.index);
|
|
295
|
+
const cta = candidates.find((t) => /subscribe|upgrade|start\s+(free\s+)?trial|get\s+(pro|unlimited|premium)/i.test(t.text)) ?? candidates.find((t) => SUBSCRIBE_CTA_RE.test(t.text));
|
|
296
|
+
if (!cta)
|
|
297
|
+
continue;
|
|
298
|
+
if (RENEWAL_TERMS_RE.test(all))
|
|
299
|
+
continue;
|
|
300
|
+
const line = lineOf(f.src, cta.index);
|
|
301
|
+
out.push({
|
|
302
|
+
id: "subscription-no-renewal-terms",
|
|
303
|
+
category: "Legal & Compliance",
|
|
304
|
+
severity: "high",
|
|
305
|
+
file: f.rel,
|
|
306
|
+
line,
|
|
307
|
+
message: "Subscription checkout has no auto-renewal terms next to the button.",
|
|
308
|
+
why: "California's Automatic Renewal Law (and the FTC's negative-option rules) require clear renewal terms — price, frequency, that it renews until cancelled, and how to cancel — right next to the purchase button. Without them, renewals can be treated as unconditional gifts you must refund.",
|
|
309
|
+
fix: "Under the subscribe/upgrade button, state: the price and billing period, that it renews automatically until cancelled, and how to cancel (e.g. 'Cancel anytime from Billing').",
|
|
310
|
+
aiPrompt: `In ${f.rel} near line ${line}, add renewal terms directly under the subscription button, e.g. "$X/month, renews automatically until you cancel. Cancel anytime from the billing page; you keep access until the end of the paid period." Make sure it is visible before the user clicks, not hidden in a tooltip.`,
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
return out;
|
|
314
|
+
}
|
|
315
|
+
// ── 6. DMCA ─────────────────────────────────────────────────────────────────
|
|
316
|
+
const UPLOAD_RE = /\.storage\s*\.from\([^)]*\)\s*\.upload\s*\(|uploadthing|<UploadButton|<UploadDropzone|cloudinary[\w.]*\.upload|PutObjectCommand|createPresignedPost|multer\s*\(|uploadBytes(Resumable)?\s*\(|put\s*\(\s*[^,]+,\s*file|@vercel\/blob/;
|
|
317
|
+
const DMCA_POLICY_RE = /dmca|digital\s+millennium|copyright\s+(infringement|agent|policy|complaint)|designated\s+agent|notice[-\s]and[-\s]takedown|takedown\s+notice/;
|
|
318
|
+
function dmcaCheck(code, legal, legalFile, legalDynamic) {
|
|
319
|
+
// Can't read policies stored in a database, so don't claim they lack a DMCA section.
|
|
320
|
+
if (legalDynamic || DMCA_POLICY_RE.test(legal))
|
|
321
|
+
return [];
|
|
322
|
+
const f = code.find((c) => UPLOAD_RE.test(c.src));
|
|
323
|
+
if (!f)
|
|
324
|
+
return [];
|
|
325
|
+
const line = lineOf(f.src, f.src.search(UPLOAD_RE));
|
|
326
|
+
return [
|
|
327
|
+
{
|
|
328
|
+
id: "dmca-no-agent",
|
|
329
|
+
category: "Legal & Compliance",
|
|
330
|
+
severity: "medium",
|
|
331
|
+
file: f.rel,
|
|
332
|
+
line,
|
|
333
|
+
message: "Users can upload files, but there is no DMCA / copyright takedown policy.",
|
|
334
|
+
why: "Safe harbor for user uploads (17 U.S.C. §512) only applies if you register a DMCA agent with the US Copyright Office ($6) and publish how to send takedown notices. Without it you can be liable for up to $150,000 per infringed work.",
|
|
335
|
+
fix: `Register a designated agent at dmca.copyright.gov, then add a 'Copyright / DMCA' section to ${legalFile || "your terms"} with the agent's contact details and your takedown and repeat-infringer process.`,
|
|
336
|
+
aiPrompt: `This app lets users upload files (${f.rel} near line ${line}). Add a "Copyright & DMCA" section to the Terms of Service: how to send a takedown notice (required elements under 17 U.S.C. §512(c)(3)), the designated agent's name/email/address (placeholder for the owner to fill in after registering at dmca.copyright.gov), counter-notice process, and a repeat-infringer policy.`,
|
|
337
|
+
},
|
|
338
|
+
];
|
|
339
|
+
}
|
|
340
|
+
// ── entry ───────────────────────────────────────────────────────────────────
|
|
341
|
+
export async function runComplianceChecks(root, files) {
|
|
342
|
+
const code = [];
|
|
343
|
+
for (const abs of files) {
|
|
344
|
+
const rel = path.relative(root, abs).replace(/\\/g, "/");
|
|
345
|
+
if (!/\.(tsx?|jsx?|mjs|cjs|vue|svelte|astro|html)$/.test(rel))
|
|
346
|
+
continue;
|
|
347
|
+
const src = await readMaybe(abs);
|
|
348
|
+
if (src && src.length < 500_000)
|
|
349
|
+
code.push({ rel, src });
|
|
350
|
+
}
|
|
351
|
+
const { text: legal, files: legalFiles, dynamic: legalDynamic } = await collectLegalText(root);
|
|
352
|
+
const legalFile = legalFiles[0] ?? "";
|
|
353
|
+
return [
|
|
354
|
+
...coppaCheck(code, legal, legalFile, legalDynamic),
|
|
355
|
+
...(await googleFontsCheck(root, legal)),
|
|
356
|
+
...sessionReplayCheck(code),
|
|
357
|
+
...canSpamCheck(code),
|
|
358
|
+
...renewalTermsCheck(code),
|
|
359
|
+
...dmcaCheck(code, legal, legalFile, legalDynamic),
|
|
360
|
+
];
|
|
361
|
+
}
|
package/dist/scanner/index.js
CHANGED
|
@@ -7,13 +7,23 @@ import { runRepoChecks, REPO_WIDE_CHECK_COUNT } from "./repo-checks.js";
|
|
|
7
7
|
import { runPolicyChecks, POLICY_CHECK_COUNT } from "./policy-checks.js";
|
|
8
8
|
import { runSeoChecks, SEO_CHECK_COUNT } from "./seo-checks.js";
|
|
9
9
|
import { runMobileChecks, MOBILE_CHECK_COUNT } from "./mobile-checks.js";
|
|
10
|
+
import { runComplianceChecks, COMPLIANCE_CHECK_COUNT } from "./compliance-checks.js";
|
|
10
11
|
import { detectProjectContext, shouldSkipFinding, buildContextSummary } from "./project-context.js";
|
|
11
12
|
import { enhanceContextWithGroq, triageFindingsWithGroq, resolveGroqApiKey, } from "./context-agent.js";
|
|
12
13
|
import { computeScores, computeVerdict } from "../report/verdict.js";
|
|
13
14
|
import { renderMarkdown } from "../report/markdown.js";
|
|
14
15
|
import { mapWithProgress } from "./progress.js";
|
|
15
|
-
import { extractMetrics } from "./metrics.js";
|
|
16
|
-
import { initPredictor, isPredictorReady,
|
|
16
|
+
import { extractMetrics, extractMetricsFromAst } from "./metrics.js";
|
|
17
|
+
import { initPredictor, isPredictorReady, predictDefectProbability } from "./ml-predictor.js";
|
|
18
|
+
/**
|
|
19
|
+
* The defect model was trained on hand-written Java classes. Generated code, vendored UI kits,
|
|
20
|
+
* type declarations and tiny files are outside what it can judge, so we don't score them.
|
|
21
|
+
*/
|
|
22
|
+
const ML_SKIP_RE = /(\.gen\.|\.generated\.|\.d\.ts$|(^|\/)(generated|__generated__)\/|integrations\/supabase\/types\.ts$|(^|\/)components\/ui\/|\.(test|spec|stories)\.[jt]sx?$|(^|\/)(migrations|scripts)\/)/i;
|
|
23
|
+
const ML_MIN_LOC = 60;
|
|
24
|
+
const ML_MIN_FUNCTIONS = 3;
|
|
25
|
+
const ML_THRESHOLD = 0.65;
|
|
26
|
+
const ML_MAX_FLAGGED = 5;
|
|
17
27
|
export { formatScanPath } from "./progress.js";
|
|
18
28
|
function isJsLike(rel) {
|
|
19
29
|
return /\.(tsx?|jsx?|mjs|cjs)$/i.test(rel);
|
|
@@ -39,7 +49,8 @@ export async function scan(root, options = {}) {
|
|
|
39
49
|
REPO_WIDE_CHECK_COUNT +
|
|
40
50
|
POLICY_CHECK_COUNT +
|
|
41
51
|
SEO_CHECK_COUNT +
|
|
42
|
-
MOBILE_CHECK_COUNT
|
|
52
|
+
MOBILE_CHECK_COUNT +
|
|
53
|
+
COMPLIANCE_CHECK_COUNT;
|
|
43
54
|
let checksRun = 0;
|
|
44
55
|
const bumpChecks = (n) => {
|
|
45
56
|
checksRun += n;
|
|
@@ -49,6 +60,7 @@ export async function scan(root, options = {}) {
|
|
|
49
60
|
let policyPagesFound = 0;
|
|
50
61
|
onProgress?.({ type: "phase", label: "Initializing AI Defect Predictor…" });
|
|
51
62
|
await initPredictor();
|
|
63
|
+
const mlCandidates = [];
|
|
52
64
|
await mapWithProgress(files, 6, async (abs) => {
|
|
53
65
|
let src;
|
|
54
66
|
try {
|
|
@@ -63,28 +75,19 @@ export async function scan(root, options = {}) {
|
|
|
63
75
|
return;
|
|
64
76
|
}
|
|
65
77
|
const rel = path.relative(root, abs);
|
|
78
|
+
const ast = isJsLike(rel) ? parseSource(src, rel) : null;
|
|
66
79
|
if (isJsLike(rel)) {
|
|
67
|
-
const ast = parseSource(src, rel);
|
|
68
80
|
rawFindings.push(...runChecks(src, rel, ast));
|
|
69
81
|
}
|
|
70
82
|
if (isMobileSource(rel) || (isJsLike(rel) && /react-native|expo|@react-native/.test(src))) {
|
|
71
83
|
rawFindings.push(...runMobileChecks(src, rel));
|
|
72
84
|
}
|
|
73
|
-
if (isJsLike(rel) || isMobileSource(rel)) {
|
|
74
|
-
const metrics = extractMetrics(src);
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
category: "Code Quality",
|
|
80
|
-
severity: "high",
|
|
81
|
-
file: rel,
|
|
82
|
-
line: 1,
|
|
83
|
-
message: "AI Defect Prediction: High Risk",
|
|
84
|
-
why: `The ML model predicts a high probability of defects in this file based on its code metrics (LOC: ${metrics.loc}, Max CC: ${metrics.max_cc.toFixed(1)}).`,
|
|
85
|
-
fix: "Consider refactoring this file to reduce complexity, decouple logic, and add unit tests.",
|
|
86
|
-
aiPrompt: "Refactor this file to reduce cyclomatic complexity and decouple logic into smaller, testable units.",
|
|
87
|
-
});
|
|
85
|
+
if ((isJsLike(rel) || isMobileSource(rel)) && !ML_SKIP_RE.test(rel.replace(/\\/g, "/"))) {
|
|
86
|
+
const metrics = ast ? extractMetricsFromAst(ast, src) : extractMetrics(src);
|
|
87
|
+
if (metrics.loc >= ML_MIN_LOC && metrics.wmc >= ML_MIN_FUNCTIONS) {
|
|
88
|
+
const p = predictDefectProbability(metrics);
|
|
89
|
+
if (p !== null && p >= ML_THRESHOLD)
|
|
90
|
+
mlCandidates.push({ rel, p, metrics });
|
|
88
91
|
}
|
|
89
92
|
}
|
|
90
93
|
bumpChecks(PER_FILE_CHECK_COUNT);
|
|
@@ -92,6 +95,20 @@ export async function scan(root, options = {}) {
|
|
|
92
95
|
const rel = path.relative(root, _abs);
|
|
93
96
|
onProgress?.({ type: "file", file: rel, index: completed, total });
|
|
94
97
|
});
|
|
98
|
+
// Advisory only: the riskiest few files, as low severity so a statistical guess never blocks a launch.
|
|
99
|
+
for (const c of mlCandidates.sort((a, b) => b.p - a.p).slice(0, ML_MAX_FLAGGED)) {
|
|
100
|
+
rawFindings.push({
|
|
101
|
+
id: "ai-defect-prediction",
|
|
102
|
+
category: "Code Quality",
|
|
103
|
+
severity: "low",
|
|
104
|
+
file: c.rel,
|
|
105
|
+
line: 1,
|
|
106
|
+
message: "ML defect predictor: file is statistically defect-prone.",
|
|
107
|
+
why: "A Random Forest trained on the PROMISE defect dataset rates this file as likely to contain bugs, based on its size and per-function branching complexity. It is a statistical hint, not a confirmed bug.",
|
|
108
|
+
fix: `Review the most complex functions here (largest has cyclomatic complexity ${c.metrics.max_cc}; file has ${c.metrics.loc} lines, ${c.metrics.wmc} functions). Split long functions and add tests around their branches.`,
|
|
109
|
+
aiPrompt: `In ${c.rel}, find the functions with the highest cyclomatic complexity (the largest is about ${c.metrics.max_cc}). Split them into smaller, well-named functions and add unit tests that cover each branch. Do not change behaviour.`,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
95
112
|
onProgress?.({ type: "phase", label: "Running repo-wide checks…" });
|
|
96
113
|
rawFindings.push(...(await runRepoChecks(root, files, ctx)));
|
|
97
114
|
bumpChecks(REPO_WIDE_CHECK_COUNT);
|
|
@@ -101,6 +118,12 @@ export async function scan(root, options = {}) {
|
|
|
101
118
|
policySignalsChecked = policy.signalsChecked;
|
|
102
119
|
policyPagesFound = policy.legalPagesFound;
|
|
103
120
|
bumpChecks(POLICY_CHECK_COUNT);
|
|
121
|
+
onProgress?.({
|
|
122
|
+
type: "phase",
|
|
123
|
+
label: "Checking COPPA, GDPR, CAN-SPAM, auto-renewal & DMCA traps…",
|
|
124
|
+
});
|
|
125
|
+
rawFindings.push(...(await runComplianceChecks(root, files)));
|
|
126
|
+
bumpChecks(COMPLIANCE_CHECK_COUNT);
|
|
104
127
|
onProgress?.({ type: "phase", label: "Checking SEO & social meta…" });
|
|
105
128
|
rawFindings.push(...(await runSeoChecks(root, files)));
|
|
106
129
|
bumpChecks(SEO_CHECK_COUNT);
|
|
@@ -130,7 +153,7 @@ export async function scan(root, options = {}) {
|
|
|
130
153
|
counts[f.severity]++;
|
|
131
154
|
const scores = computeScores(findings);
|
|
132
155
|
const { score, verdict } = computeVerdict(counts, scores.confidence);
|
|
133
|
-
const projectName = path.basename(root);
|
|
156
|
+
const projectName = options.projectName?.trim() || path.basename(root);
|
|
134
157
|
const scannedAt = new Date().toISOString();
|
|
135
158
|
const markdown = renderMarkdown({
|
|
136
159
|
projectName,
|
package/dist/scanner/metrics.js
CHANGED
|
@@ -1,23 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code metrics for the PROMISE-trained defect predictor.
|
|
3
|
+
*
|
|
4
|
+
* The model was trained on CK metrics of Java classes (ant-1.7): loc, wmc (methods per class),
|
|
5
|
+
* npm (public methods), max_cc / avg_cc (per-method cyclomatic complexity). To feed it something
|
|
6
|
+
* comparable for JS/TS we measure real functions and real branches from the AST:
|
|
7
|
+
* - TypeScript types (`?:`, `=> T`) are not branches.
|
|
8
|
+
* - `a ?? b` in an env lookup is one decision in *that* function, not in the whole file.
|
|
9
|
+
* A regex fallback remains for Dart/Swift, which Babel can't parse.
|
|
10
|
+
*/
|
|
11
|
+
import traverseDefault from "@babel/traverse";
|
|
12
|
+
// @babel/traverse ships CJS; under ESM the callable may sit on `.default`.
|
|
13
|
+
const traverse = (traverseDefault.default ??
|
|
14
|
+
traverseDefault);
|
|
15
|
+
/** Non-blank, non-comment lines. */
|
|
16
|
+
export function countLoc(code) {
|
|
17
|
+
const withoutBlock = code.replace(/\/\*[\s\S]*?\*\//g, "");
|
|
18
|
+
let n = 0;
|
|
19
|
+
for (const line of withoutBlock.split("\n")) {
|
|
20
|
+
const t = line.trim();
|
|
21
|
+
if (t && !t.startsWith("//"))
|
|
22
|
+
n++;
|
|
23
|
+
}
|
|
24
|
+
return n;
|
|
25
|
+
}
|
|
26
|
+
function countLocInLines(code, lines) {
|
|
27
|
+
if (lines.size === 0)
|
|
28
|
+
return 0;
|
|
29
|
+
let n = 0;
|
|
30
|
+
code.split("\n").forEach((line, i) => {
|
|
31
|
+
const t = line.trim();
|
|
32
|
+
if (lines.has(i + 1) && t && !t.startsWith("//"))
|
|
33
|
+
n++;
|
|
34
|
+
});
|
|
35
|
+
return n;
|
|
36
|
+
}
|
|
37
|
+
const FUNCTION_TYPES = new Set([
|
|
38
|
+
"FunctionDeclaration",
|
|
39
|
+
"FunctionExpression",
|
|
40
|
+
"ArrowFunctionExpression",
|
|
41
|
+
"ObjectMethod",
|
|
42
|
+
"ClassMethod",
|
|
43
|
+
"ClassPrivateMethod",
|
|
44
|
+
]);
|
|
45
|
+
export function extractMetricsFromAst(ast, code) {
|
|
46
|
+
const ccByFn = new Map();
|
|
47
|
+
let npm = 0;
|
|
48
|
+
// Lines that are pure JSX markup. A 1,000-line landing page is mostly markup, not logic, and
|
|
49
|
+
// counting it as code makes the model treat marketing pages as defect-prone.
|
|
50
|
+
const markupLines = new Set();
|
|
51
|
+
const bump = (path) => {
|
|
52
|
+
const fn = path.getFunctionParent()?.node;
|
|
53
|
+
if (fn && ccByFn.has(fn))
|
|
54
|
+
ccByFn.set(fn, ccByFn.get(fn) + 1);
|
|
55
|
+
};
|
|
56
|
+
traverse(ast, {
|
|
57
|
+
enter(p) {
|
|
58
|
+
if (FUNCTION_TYPES.has(p.node.type))
|
|
59
|
+
ccByFn.set(p.node, 1);
|
|
60
|
+
if ((p.node.type === "JSXElement" || p.node.type === "JSXFragment") &&
|
|
61
|
+
!p.parentPath?.node.type.startsWith("JSX") &&
|
|
62
|
+
p.node.loc) {
|
|
63
|
+
for (let l = p.node.loc.start.line; l <= p.node.loc.end.line; l++)
|
|
64
|
+
markupLines.add(l);
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
IfStatement: bump,
|
|
68
|
+
ConditionalExpression: bump,
|
|
69
|
+
ForStatement: bump,
|
|
70
|
+
ForInStatement: bump,
|
|
71
|
+
ForOfStatement: bump,
|
|
72
|
+
WhileStatement: bump,
|
|
73
|
+
DoWhileStatement: bump,
|
|
74
|
+
CatchClause: bump,
|
|
75
|
+
SwitchCase(p) {
|
|
76
|
+
if (p.node.test)
|
|
77
|
+
bump(p);
|
|
78
|
+
},
|
|
79
|
+
LogicalExpression: bump,
|
|
80
|
+
ExportNamedDeclaration(p) {
|
|
81
|
+
const d = p.node.declaration;
|
|
82
|
+
if (!d)
|
|
83
|
+
npm += p.node.specifiers.length;
|
|
84
|
+
else if (d.type === "VariableDeclaration")
|
|
85
|
+
npm += d.declarations.length;
|
|
86
|
+
else
|
|
87
|
+
npm += 1;
|
|
88
|
+
},
|
|
89
|
+
ExportDefaultDeclaration() {
|
|
90
|
+
npm += 1;
|
|
91
|
+
},
|
|
92
|
+
ClassMethod(p) {
|
|
93
|
+
if (p.node.accessibility !== "private" && p.node.accessibility !== "protected")
|
|
94
|
+
npm += 1;
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
const loc = Math.max(0, countLoc(code) - countLocInLines(code, markupLines));
|
|
98
|
+
const ccs = [...ccByFn.values()];
|
|
99
|
+
const wmc = Math.max(1, ccs.length);
|
|
100
|
+
const max_cc = ccs.length ? Math.max(...ccs) : 1;
|
|
101
|
+
const avg_cc = ccs.length ? ccs.reduce((a, b) => a + b, 0) / ccs.length : 1;
|
|
102
|
+
return { loc, wmc, npm, max_cc, avg_cc };
|
|
103
|
+
}
|
|
104
|
+
/** Regex fallback for languages without an AST (Dart, Swift). */
|
|
1
105
|
export function extractMetrics(code) {
|
|
2
|
-
const loc = code
|
|
3
|
-
// Weighted methods per class / function count
|
|
106
|
+
const loc = countLoc(code);
|
|
4
107
|
const wmcMatches = code.match(/\b(function|func|def)\b|=>/g);
|
|
5
108
|
const wmc = wmcMatches ? wmcMatches.length : 1;
|
|
6
|
-
// Number of public methods
|
|
7
109
|
const npmMatches = code.match(/\b(export|public)\b/g);
|
|
8
110
|
const npm = npmMatches ? npmMatches.length : 0;
|
|
9
|
-
|
|
10
|
-
// Count branching operations and keywords
|
|
11
|
-
const ccMatches = code.match(/\b(if|for|while|switch|case|catch)\b|&&|\|\||\?/g);
|
|
111
|
+
const ccMatches = code.match(/\b(if|for|while|switch|case|catch)\b|&&|\|\|/g);
|
|
12
112
|
const total_cc = (ccMatches ? ccMatches.length : 0) + wmc;
|
|
13
113
|
const avg_cc = total_cc / (wmc || 1);
|
|
14
|
-
// Without AST, max_cc is a heuristic based on avg_cc + some variance
|
|
15
114
|
const max_cc = avg_cc > 1 ? avg_cc * 1.5 : 1;
|
|
16
|
-
return {
|
|
17
|
-
loc,
|
|
18
|
-
wmc,
|
|
19
|
-
npm,
|
|
20
|
-
max_cc,
|
|
21
|
-
avg_cc
|
|
22
|
-
};
|
|
115
|
+
return { loc, wmc, npm, max_cc, avg_cc };
|
|
23
116
|
}
|
|
@@ -20,6 +20,17 @@ function resolveModelPath() {
|
|
|
20
20
|
];
|
|
21
21
|
return candidates.find((p) => fs.existsSync(p)) ?? null;
|
|
22
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Load the forest from an already-parsed object. The web server uses this because bundlers
|
|
25
|
+
* (Vercel/nitro) don't ship assets/defect_model.json next to the compiled code.
|
|
26
|
+
*/
|
|
27
|
+
export function loadPredictorModel(model) {
|
|
28
|
+
if (ready || !Array.isArray(model) || model.length === 0)
|
|
29
|
+
return;
|
|
30
|
+
forest = model;
|
|
31
|
+
ready = true;
|
|
32
|
+
debugMl(`model provided in-process — ${forest.length} trees`);
|
|
33
|
+
}
|
|
23
34
|
export async function initPredictor() {
|
|
24
35
|
if (ready)
|
|
25
36
|
return;
|
|
@@ -50,22 +61,17 @@ function predictTree(tree, features) {
|
|
|
50
61
|
idx = features[feat] <= threshold ? left : right;
|
|
51
62
|
}
|
|
52
63
|
}
|
|
53
|
-
|
|
64
|
+
/** Mean defect probability across the forest (0–1), or null when the model isn't loaded. */
|
|
65
|
+
export function predictDefectProbability(metrics) {
|
|
54
66
|
if (!forest)
|
|
55
|
-
return
|
|
56
|
-
const features = [
|
|
57
|
-
metrics.loc,
|
|
58
|
-
metrics.wmc,
|
|
59
|
-
metrics.npm,
|
|
60
|
-
metrics.max_cc,
|
|
61
|
-
metrics.avg_cc,
|
|
62
|
-
];
|
|
63
|
-
// Average defect probability across all trees (majority vote)
|
|
67
|
+
return null;
|
|
68
|
+
const features = [metrics.loc, metrics.wmc, metrics.npm, metrics.max_cc, metrics.avg_cc];
|
|
64
69
|
let totalP = 0;
|
|
65
|
-
for (const tree of forest)
|
|
70
|
+
for (const tree of forest)
|
|
66
71
|
totalP += predictTree(tree, features);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
72
|
+
return totalP / forest.length;
|
|
73
|
+
}
|
|
74
|
+
/** Kept for backwards compatibility: true when probability > 0.5. */
|
|
75
|
+
export async function predictDefect(metrics) {
|
|
76
|
+
return (predictDefectProbability(metrics) ?? 0) > 0.5;
|
|
71
77
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import fs from "node:fs/promises";
|
|
6
6
|
import path from "node:path";
|
|
7
|
-
import {
|
|
7
|
+
import { collectLegalText } from "./compliance-checks.js";
|
|
8
8
|
async function readMaybe(p) {
|
|
9
9
|
try {
|
|
10
10
|
return await fs.readFile(p, "utf8");
|
|
@@ -15,10 +15,7 @@ async function readMaybe(p) {
|
|
|
15
15
|
}
|
|
16
16
|
/** Signals that user data is sent to trackers, ad networks, or data brokers. */
|
|
17
17
|
const TRACKING_IN_CODE = [
|
|
18
|
-
[
|
|
19
|
-
/gtag\s*\(|googletagmanager|google-analytics|GA_MEASUREMENT_ID/i,
|
|
20
|
-
"Google Analytics / gtag",
|
|
21
|
-
],
|
|
18
|
+
[/gtag\s*\(|googletagmanager|google-analytics|GA_MEASUREMENT_ID/i, "Google Analytics / gtag"],
|
|
22
19
|
// GA4 measurement IDs are always uppercase and quoted, e.g. "G-AB12CD34EF".
|
|
23
20
|
// Must stay case-sensitive: with /i this matched Tailwind classes like `bg-background`.
|
|
24
21
|
[/['"`]G-[A-Z0-9]{8,12}['"`]/, "Google Analytics / gtag"],
|
|
@@ -91,18 +88,15 @@ export const POLICY_SIGNAL_COUNT = TRACKING_IN_CODE.length + COOKIE_TRACKING_IN_
|
|
|
91
88
|
export async function runPolicyChecks(root, files) {
|
|
92
89
|
const out = [];
|
|
93
90
|
const relFiles = files.map((f) => path.relative(root, f));
|
|
94
|
-
const
|
|
91
|
+
const legal = await collectLegalText(root);
|
|
92
|
+
const legalPaths = legal.files;
|
|
95
93
|
if (legalPaths.length === 0)
|
|
96
94
|
return { findings: out, legalPagesFound: 0, signalsChecked: 0 };
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const content = await readMaybe(path.join(root, rel));
|
|
100
|
-
if (content)
|
|
101
|
-
legalText += `\n${content}`;
|
|
102
|
-
}
|
|
103
|
-
if (!legalText.trim()) {
|
|
95
|
+
// Policy text loaded from a DB/CMS isn't in the repo, so we can't cross-reference it.
|
|
96
|
+
if (legal.dynamic || !legal.text.trim()) {
|
|
104
97
|
return { findings: out, legalPagesFound: legalPaths.length, signalsChecked: 0 };
|
|
105
98
|
}
|
|
99
|
+
const legalText = legal.text;
|
|
106
100
|
const legalLower = legalText.toLowerCase();
|
|
107
101
|
// Collect what the codebase actually does
|
|
108
102
|
const codeSignals = new Map(); // label -> first file
|
|
@@ -80,9 +80,25 @@ export function isInternalPath(rel, ctx) {
|
|
|
80
80
|
const norm = rel.replace(/\\/g, "/");
|
|
81
81
|
return ctx.internalPathRe.some((re) => re.test(norm));
|
|
82
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Obligations that follow the user, not the page: a renewal disclosure on a dashboard billing page
|
|
85
|
+
* or an age gate on an "internal" signup still applies. Only an explicit skip_check_ids opts out.
|
|
86
|
+
*/
|
|
87
|
+
const PATH_INDEPENDENT_IDS = new Set([
|
|
88
|
+
"subscription-no-renewal-terms",
|
|
89
|
+
"coppa-no-age-gate",
|
|
90
|
+
"coppa-age-not-asked",
|
|
91
|
+
"session-replay-unmasked",
|
|
92
|
+
"session-replay-no-consent",
|
|
93
|
+
"email-no-unsubscribe",
|
|
94
|
+
"email-no-postal-address",
|
|
95
|
+
"dmca-no-agent",
|
|
96
|
+
]);
|
|
83
97
|
export function shouldSkipFinding(finding, ctx) {
|
|
84
98
|
if (ctx.skipCheckIds.has(finding.id))
|
|
85
99
|
return true;
|
|
100
|
+
if (PATH_INDEPENDENT_IDS.has(finding.id))
|
|
101
|
+
return false;
|
|
86
102
|
if (ctx.skipCategories.has(finding.category))
|
|
87
103
|
return true;
|
|
88
104
|
const rel = finding.file.replace(/\\/g, "/");
|
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import fs from "node:fs/promises";
|
|
6
6
|
import path from "node:path";
|
|
7
|
-
import fg from "fast-glob";
|
|
8
7
|
import { detectFaviconPresent } from "./project-context.js";
|
|
9
|
-
import {
|
|
8
|
+
import { collectLegalText } from "./compliance-checks.js";
|
|
9
|
+
import { listProjectFiles } from "./walk.js";
|
|
10
10
|
async function exists(p) {
|
|
11
11
|
try {
|
|
12
12
|
await fs.access(p);
|
|
@@ -133,7 +133,8 @@ export async function runRepoChecks(root, _files, ctx) {
|
|
|
133
133
|
});
|
|
134
134
|
}
|
|
135
135
|
else {
|
|
136
|
-
const
|
|
136
|
+
const legal = await collectLegalText(root);
|
|
137
|
+
const legalFilePaths = legal.files.map((rel) => path.join(root, rel));
|
|
137
138
|
if (legalFilePaths.length === 0) {
|
|
138
139
|
out.push({
|
|
139
140
|
id: "missing-legal-docs",
|
|
@@ -147,7 +148,8 @@ export async function runRepoChecks(root, _files, ctx) {
|
|
|
147
148
|
aiPrompt: "Generate a standard Privacy Policy and Terms of Service route for this application.",
|
|
148
149
|
});
|
|
149
150
|
}
|
|
150
|
-
else if (pkgRaw) {
|
|
151
|
+
else if (pkgRaw && !legal.dynamic) {
|
|
152
|
+
// (Pages that load their text from a DB/CMS can't be verified here — see collectLegalText.)
|
|
151
153
|
let combinedSource = pkgRaw;
|
|
152
154
|
for (const abs of _files) {
|
|
153
155
|
const rel = path.relative(root, abs);
|
|
@@ -160,12 +162,7 @@ export async function runRepoChecks(root, _files, ctx) {
|
|
|
160
162
|
const hasAuth = /supabase|clerk|firebase|next-auth|auth0|createClient.*auth/i.test(combinedSource);
|
|
161
163
|
const hasPayments = /stripe|braintree|paypal|lemonsqueezy/i.test(combinedSource);
|
|
162
164
|
const hasAnalytics = /posthog|mixpanel|segment|amplitude|google-analytics|gtag\s*\(|fbq\s*\(|plausible|hotjar/i.test(combinedSource);
|
|
163
|
-
|
|
164
|
-
for (const p of legalFilePaths) {
|
|
165
|
-
const content = await readMaybe(p);
|
|
166
|
-
if (content)
|
|
167
|
-
combinedLegalContent += content.toLowerCase();
|
|
168
|
-
}
|
|
165
|
+
const combinedLegalContent = legal.text;
|
|
169
166
|
const legalFile = path.relative(root, legalFilePaths[0]);
|
|
170
167
|
if (hasAuth && !/account|email|password|authentication|login/i.test(combinedLegalContent)) {
|
|
171
168
|
out.push({
|
|
@@ -449,15 +446,7 @@ export async function runRepoChecks(root, _files, ctx) {
|
|
|
449
446
|
// ─────────────────────────────────────────────────────────────────────
|
|
450
447
|
// Unused CSS classes (skip Tailwind / CSS-modules)
|
|
451
448
|
// ─────────────────────────────────────────────────────────────────────
|
|
452
|
-
const cssFiles = await
|
|
453
|
-
"**/*.css",
|
|
454
|
-
"!**/node_modules/**",
|
|
455
|
-
"!**/dist/**",
|
|
456
|
-
"!**/build/**",
|
|
457
|
-
"!**/.next/**",
|
|
458
|
-
"!**/.output/**",
|
|
459
|
-
"!**/*.module.css",
|
|
460
|
-
], { cwd: root, absolute: true, dot: false });
|
|
449
|
+
const cssFiles = (await listProjectFiles(root, "css")).filter((f) => !f.endsWith(".module.css"));
|
|
461
450
|
if (cssFiles.length) {
|
|
462
451
|
// Build a haystack of everything CSS classes might be referenced from.
|
|
463
452
|
let refHaystack = "";
|
|
@@ -515,27 +504,113 @@ export async function runRepoChecks(root, _files, ctx) {
|
|
|
515
504
|
// ─────────────────────────────────────────────────────────────────────
|
|
516
505
|
// Security Definer RPC Check (Supabase/PostgreSQL)
|
|
517
506
|
// ─────────────────────────────────────────────────────────────────────
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
507
|
+
// Migrations are timestamp-prefixed: apply them in order.
|
|
508
|
+
const sqlFiles = (await listProjectFiles(root, "sql")).sort();
|
|
509
|
+
out.push(...(await checkDefinerFunctions(root, sqlFiles)));
|
|
510
|
+
return out;
|
|
511
|
+
}
|
|
512
|
+
const UNTRUSTED_ROLE_RE = /\b(public|anon|authenticated)\b/i;
|
|
513
|
+
const CALLER_GUARD_RE = /auth\.uid\s*\(\s*\)|auth\.role\s*\(\s*\)|auth\.jwt\s*\(\s*\)|has_role\s*\(|current_setting\s*\(\s*'request\.jwt|current_user\s*(<>|!=|=)|session_user/i;
|
|
514
|
+
function normFnName(raw) {
|
|
515
|
+
return raw
|
|
516
|
+
.replace(/"/g, "")
|
|
517
|
+
.replace(/^public\./i, "")
|
|
518
|
+
.trim()
|
|
519
|
+
.toLowerCase();
|
|
520
|
+
}
|
|
521
|
+
async function checkDefinerFunctions(root, sqlFiles) {
|
|
522
|
+
const fns = new Map();
|
|
523
|
+
const createRe = /create\s+(?:or\s+replace\s+)?function\s+([\w."]+)\s*\(([\s\S]*?)\)\s*returns\s+([\w.\s]+?)\s+(?:language|as|security|set|stable|immutable|volatile)/gi;
|
|
524
|
+
for (const abs of sqlFiles) {
|
|
525
|
+
const sql = await readMaybe(abs);
|
|
526
|
+
if (!sql)
|
|
522
527
|
continue;
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
528
|
+
const rel = path.relative(root, abs);
|
|
529
|
+
// Process statements in order so a GRANT/REVOKE after a CREATE applies to it.
|
|
530
|
+
const events = [];
|
|
531
|
+
let m;
|
|
532
|
+
while ((m = createRe.exec(sql))) {
|
|
533
|
+
const name = normFnName(m[1]);
|
|
534
|
+
const startIdx = m.index;
|
|
535
|
+
// The function ends at the closing dollar-quote of its body ($$ or $tag$).
|
|
536
|
+
const after = sql.slice(startIdx);
|
|
537
|
+
const dq = after.match(/\$(\w*)\$/);
|
|
538
|
+
let endIdx = sql.length;
|
|
539
|
+
if (dq?.index !== undefined) {
|
|
540
|
+
const tag = dq[0];
|
|
541
|
+
const close = after.indexOf(tag, dq.index + tag.length);
|
|
542
|
+
const tail = close === -1 ? after.length : close + tag.length;
|
|
543
|
+
const semi = after.indexOf(";", tail);
|
|
544
|
+
endIdx = startIdx + (semi === -1 ? tail : semi + 1);
|
|
537
545
|
}
|
|
546
|
+
const stmt = sql.slice(startIdx, endIdx);
|
|
547
|
+
// Only the body counts as evidence of a caller check — not the function's own name.
|
|
548
|
+
const bodyMatch = stmt.match(/\$(\w*)\$([\s\S]*?)\$\1\$/);
|
|
549
|
+
const body = bodyMatch ? bodyMatch[2] : stmt;
|
|
550
|
+
const returns = m[3].toLowerCase();
|
|
551
|
+
const line = sql.slice(0, startIdx).split("\n").length;
|
|
552
|
+
events.push({
|
|
553
|
+
index: startIdx,
|
|
554
|
+
apply: () => {
|
|
555
|
+
const prev = fns.get(name);
|
|
556
|
+
fns.set(name, {
|
|
557
|
+
file: rel,
|
|
558
|
+
line,
|
|
559
|
+
definer: /security\s+definer/i.test(stmt),
|
|
560
|
+
guarded: CALLER_GUARD_RE.test(body),
|
|
561
|
+
writes: /\b(insert\s+into|update\s+[\w."]+\s+set|delete\s+from|truncate|drop\s|alter\s|grant\s|execute\s)/i.test(body),
|
|
562
|
+
boolHelper: /^(has|is|can)_/.test(name) && /^bool(ean)?$/.test(returns.trim()),
|
|
563
|
+
trigger: /\btrigger\b/.test(returns),
|
|
564
|
+
// CREATE OR REPLACE keeps existing privileges; a brand-new function is PUBLIC-executable.
|
|
565
|
+
exposed: prev ? prev.exposed : true,
|
|
566
|
+
});
|
|
567
|
+
},
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
const privRe = /(grant|revoke)\s+(?:all|execute)(?:\s+privileges)?\s+on\s+function\s+([\w."]+)\s*(?:\([^)]*\))?\s+(to|from)\s+([^;]+);/gi;
|
|
571
|
+
while ((m = privRe.exec(sql))) {
|
|
572
|
+
const verb = m[1].toLowerCase();
|
|
573
|
+
const name = normFnName(m[2]);
|
|
574
|
+
const roles = m[4];
|
|
575
|
+
events.push({
|
|
576
|
+
index: m.index,
|
|
577
|
+
apply: () => {
|
|
578
|
+
const fn = fns.get(name);
|
|
579
|
+
if (!fn)
|
|
580
|
+
return;
|
|
581
|
+
if (verb === "grant" && UNTRUSTED_ROLE_RE.test(roles))
|
|
582
|
+
fn.exposed = true;
|
|
583
|
+
if (verb === "revoke") {
|
|
584
|
+
const revoked = roles.toLowerCase();
|
|
585
|
+
// Fully closed only when PUBLIC is revoked (anon/authenticated inherit from it) or both are.
|
|
586
|
+
if (/\bpublic\b/.test(revoked) ||
|
|
587
|
+
(/\banon\b/.test(revoked) && /\bauthenticated\b/.test(revoked))) {
|
|
588
|
+
fn.exposed = false;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
},
|
|
592
|
+
});
|
|
538
593
|
}
|
|
594
|
+
events.sort((a, b) => a.index - b.index).forEach((e) => e.apply());
|
|
595
|
+
}
|
|
596
|
+
const out = [];
|
|
597
|
+
for (const [name, fn] of fns) {
|
|
598
|
+
if (!fn.definer || fn.trigger || !fn.exposed || fn.guarded)
|
|
599
|
+
continue;
|
|
600
|
+
// Read-only has_/is_/can_ boolean helpers are the standard pattern for RLS policies.
|
|
601
|
+
if (fn.boolHelper && !fn.writes)
|
|
602
|
+
continue;
|
|
603
|
+
out.push({
|
|
604
|
+
id: "definer-rpc-execute",
|
|
605
|
+
category: "Security & Permissions",
|
|
606
|
+
severity: fn.writes ? "critical" : "high",
|
|
607
|
+
file: fn.file,
|
|
608
|
+
line: fn.line,
|
|
609
|
+
message: `SECURITY DEFINER function \`${name}\` can be called by anon/authenticated users and never checks who the caller is.`,
|
|
610
|
+
why: "SECURITY DEFINER functions run with their owner's privileges (usually postgres), bypassing Row Level Security. Postgres lets PUBLIC execute new functions by default, and Supabase exposes them as RPC endpoints — so any visitor can call this one with any arguments.",
|
|
611
|
+
fix: `Either revoke access (REVOKE EXECUTE ON FUNCTION ${name} FROM PUBLIC, anon, authenticated;) and call it only from the service role, or add a caller check at the top (e.g. IF auth.uid() IS DISTINCT FROM _user_id THEN RAISE EXCEPTION 'forbidden'; END IF;), or make it SECURITY INVOKER.`,
|
|
612
|
+
aiPrompt: `In ${fn.file} near line ${fn.line}, the SECURITY DEFINER function ${name} is executable by untrusted roles and has no auth check. Add a new migration that either revokes EXECUTE from PUBLIC, anon and authenticated (if only the server calls it), or adds an auth.uid()/has_role() guard at the start of the function body.`,
|
|
613
|
+
});
|
|
539
614
|
}
|
|
540
615
|
return out;
|
|
541
616
|
}
|
package/dist/scanner/walk.js
CHANGED
|
@@ -1,28 +1,75 @@
|
|
|
1
1
|
import fg from "fast-glob";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { execFile } from "node:child_process";
|
|
5
|
+
const CODE_EXTS = "ts,tsx,js,jsx,mjs,cjs,dart,swift";
|
|
6
|
+
const BASE_IGNORES = [
|
|
7
|
+
"!**/node_modules/**",
|
|
8
|
+
"!**/dist/**",
|
|
9
|
+
"!**/build/**",
|
|
10
|
+
"!**/.next/**",
|
|
11
|
+
"!**/.output/**",
|
|
12
|
+
"!**/.vercel/**",
|
|
13
|
+
"!**/.git/**",
|
|
14
|
+
"!**/coverage/**",
|
|
15
|
+
"!**/*.d.ts",
|
|
16
|
+
"!**/routeTree.gen.*",
|
|
17
|
+
"!**/scripts/**",
|
|
18
|
+
"!**/test.js",
|
|
19
|
+
"!**/test2.js",
|
|
20
|
+
"!**/test-server.js",
|
|
21
|
+
"!**/fix-*.mjs",
|
|
22
|
+
"!**/*.{test,spec}.{ts,tsx,js,jsx,mjs,cjs}",
|
|
23
|
+
];
|
|
24
|
+
/** Skip Periderm's own CLI package when scanning the Periderm repo — but never a user's packages/cli. */
|
|
25
|
+
async function isPeridermCliPackage(root) {
|
|
26
|
+
try {
|
|
27
|
+
const pkg = JSON.parse(await fs.readFile(path.join(root, "packages/cli/package.json"), "utf8"));
|
|
28
|
+
return pkg?.name === "periderm-cli";
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Files git would track (tracked + untracked, minus .gitignore'd), or null outside a git repo.
|
|
36
|
+
* Without this, gitignored datasets, vendored docs and generated output get scanned and drown
|
|
37
|
+
* the report in findings nobody can act on.
|
|
38
|
+
*/
|
|
39
|
+
function gitVisibleFiles(root) {
|
|
40
|
+
return new Promise((resolve) => {
|
|
41
|
+
execFile("git", ["-C", root, "ls-files", "-z", "--cached", "--others", "--exclude-standard"], { maxBuffer: 64 * 1024 * 1024 }, (err, stdout) => {
|
|
42
|
+
if (err)
|
|
43
|
+
return resolve(null);
|
|
44
|
+
const set = new Set(stdout
|
|
45
|
+
.split("\0")
|
|
46
|
+
.filter(Boolean)
|
|
47
|
+
.map((rel) => path.resolve(root, rel)));
|
|
48
|
+
resolve(set.size ? set : null);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
const gitCache = new Map();
|
|
53
|
+
/** List project files with the given extensions, honouring Periderm's ignores and .gitignore. */
|
|
54
|
+
export async function listProjectFiles(root, exts, extraIgnores = []) {
|
|
55
|
+
const ignores = [...BASE_IGNORES];
|
|
56
|
+
if (await isPeridermCliPackage(root))
|
|
57
|
+
ignores.push("!packages/cli/**");
|
|
21
58
|
// Cloud policy ignores: normalize to negated globs
|
|
22
|
-
|
|
23
|
-
|
|
59
|
+
ignores.push(...extraIgnores.map((ig) => (ig.startsWith("!") ? ig : `!${ig}`)));
|
|
60
|
+
// A one-item brace like "*.{sql}" is matched literally by fast-glob, so only use braces for lists.
|
|
61
|
+
const pattern = exts.includes(",") ? `**/*.{${exts}}` : `**/*.${exts}`;
|
|
62
|
+
const files = await fg([pattern, ...ignores], {
|
|
24
63
|
cwd: root,
|
|
25
64
|
absolute: true,
|
|
26
65
|
dot: false,
|
|
27
66
|
});
|
|
67
|
+
const key = path.resolve(root);
|
|
68
|
+
if (!gitCache.has(key))
|
|
69
|
+
gitCache.set(key, gitVisibleFiles(root));
|
|
70
|
+
const visible = await gitCache.get(key);
|
|
71
|
+
return visible ? files.filter((f) => visible.has(path.resolve(f))) : files;
|
|
72
|
+
}
|
|
73
|
+
export async function walk(root, extraIgnores = []) {
|
|
74
|
+
return listProjectFiles(root, CODE_EXTS, extraIgnores);
|
|
28
75
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "periderm-cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.78",
|
|
4
4
|
"description": "Periderm CLI: A pre-launch checklist to catch user-losing bugs, legal liabilities, revenue leaks, runaway cloud bills and so many more before you ship.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"preferGlobal": true,
|