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.
- package/dist/index.js +38 -5
- package/dist/report/agent-rules.js +73 -0
- package/dist/report/ai-instructions.js +102 -83
- package/dist/report/markdown.js +5 -4
- package/dist/review/deep.js +18 -1
- package/dist/scanner/checks.js +62 -19
- package/dist/scanner/compliance-checks.js +361 -0
- package/dist/scanner/context-agent.js +1 -1
- 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 +10 -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
|
@@ -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
|
+
}
|
|
@@ -52,7 +52,7 @@ async function groqJson(apiKey, system, user) {
|
|
|
52
52
|
"Content-Type": "application/json",
|
|
53
53
|
},
|
|
54
54
|
body: JSON.stringify({
|
|
55
|
-
model: "
|
|
55
|
+
model: "qwen/qwen3.8-27b",
|
|
56
56
|
temperature: 0.1,
|
|
57
57
|
response_format: { type: "json_object" },
|
|
58
58
|
messages: [
|
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
|
}
|