periderm-cli 0.1.76 → 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.
@@ -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
- export async function predictDefect(metrics) {
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 false;
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
- const meanP = totalP / forest.length;
69
- // Threshold: >0.5 = predicted defective (matches sklearn's default)
70
- return meanP > 0.5;
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 { findLegalPages } from "./legal.js";
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,10 @@ 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|G-[A-Z0-9]{6,}/i,
20
- "Google Analytics / gtag",
21
- ],
18
+ [/gtag\s*\(|googletagmanager|google-analytics|GA_MEASUREMENT_ID/i, "Google Analytics / gtag"],
19
+ // GA4 measurement IDs are always uppercase and quoted, e.g. "G-AB12CD34EF".
20
+ // Must stay case-sensitive: with /i this matched Tailwind classes like `bg-background`.
21
+ [/['"`]G-[A-Z0-9]{8,12}['"`]/, "Google Analytics / gtag"],
22
22
  [/fbq\s*\(|facebook\.net\/en_US\/fbevents|connect\.facebook\.net/i, "Meta / Facebook Pixel"],
23
23
  [/posthog\.(?:capture|init)|import\s+posthog/i, "PostHog analytics"],
24
24
  [/mixpanel\.(?:track|init)|from\s+['"]mixpanel/i, "Mixpanel"],
@@ -88,18 +88,15 @@ export const POLICY_SIGNAL_COUNT = TRACKING_IN_CODE.length + COOKIE_TRACKING_IN_
88
88
  export async function runPolicyChecks(root, files) {
89
89
  const out = [];
90
90
  const relFiles = files.map((f) => path.relative(root, f));
91
- const legalPaths = findLegalPages(relFiles);
91
+ const legal = await collectLegalText(root);
92
+ const legalPaths = legal.files;
92
93
  if (legalPaths.length === 0)
93
94
  return { findings: out, legalPagesFound: 0, signalsChecked: 0 };
94
- let legalText = "";
95
- for (const rel of legalPaths) {
96
- const content = await readMaybe(path.join(root, rel));
97
- if (content)
98
- legalText += `\n${content}`;
99
- }
100
- 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()) {
101
97
  return { findings: out, legalPagesFound: legalPaths.length, signalsChecked: 0 };
102
98
  }
99
+ const legalText = legal.text;
103
100
  const legalLower = legalText.toLowerCase();
104
101
  // Collect what the codebase actually does
105
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 { isLegalPage } from "./legal.js";
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 legalFilePaths = _files.filter((f) => isLegalPage(path.relative(root, f)));
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
- let combinedLegalContent = "";
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 fg([
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
- const sqlFiles = await fg(["**/*.sql", "!**/node_modules/**", "!**/dist/**", "!**/build/**", "!**/.next/**"], { cwd: root, absolute: true, dot: false });
519
- for (const sqlAbs of sqlFiles) {
520
- const content = await readMaybe(sqlAbs);
521
- if (!content)
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
- if (/security\s+definer/i.test(content) && /grant\s+execute\s+on\s+function/i.test(content)) {
524
- if (/(anon|public|authenticated)/i.test(content)) {
525
- const rel = path.relative(root, sqlAbs);
526
- out.push({
527
- id: "definer-rpc-execute",
528
- category: "Security & Permissions",
529
- severity: "critical",
530
- file: rel,
531
- line: 1,
532
- message: "Signed-in users may have EXECUTE on a definer-scoped RPC.",
533
- why: "SECURITY DEFINER functions run with the privileges of the creator (usually the postgres superuser). Granting EXECUTE to anon, public, or authenticated users can allow them to bypass Row Level Security (RLS) and escalate privileges.",
534
- fix: "Ensure you restrict EXECUTE to a specific service role, or rewrite the function to be SECURITY INVOKER (the default) so it respects the caller's RLS policies.",
535
- aiPrompt: `In ${rel}, a SECURITY DEFINER function is defined and seems to be granted to an untrusted role (anon, public, or authenticated). Review the function and change it to SECURITY INVOKER or revoke EXECUTE from public roles.`,
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
  }
@@ -1,28 +1,75 @@
1
1
  import fg from "fast-glob";
2
- export async function walk(root, extraIgnores = []) {
3
- const baseIgnores = [
4
- "!**/node_modules/**",
5
- "!**/dist/**",
6
- "!**/build/**",
7
- "!**/.next/**",
8
- "!**/.output/**",
9
- "!**/.git/**",
10
- "!**/coverage/**",
11
- "!**/*.d.ts",
12
- "!**/routeTree.gen.*",
13
- "!**/packages/cli/**", // never scan the scanner itself
14
- "!**/scripts/**",
15
- "!**/test.js",
16
- "!**/test2.js",
17
- "!**/test-server.js",
18
- "!**/fix-*.mjs",
19
- "!**/*.{test,spec}.{ts,tsx,js,jsx,mjs,cjs}",
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
- const cloudIgnoreGlobs = extraIgnores.map((ig) => (ig.startsWith("!") ? ig : `!${ig}`));
23
- return fg(["**/*.{ts,tsx,js,jsx,mjs,cjs,dart,swift}", ...baseIgnores, ...cloudIgnoreGlobs], {
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.76",
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,