prodlint 0.5.0 → 0.7.0
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/README.md +31 -11
- package/action.yml +2 -2
- package/dist/cli.js +1650 -3
- package/dist/index.js +1649 -2
- package/dist/mcp.js +1651 -4
- package/package.json +2 -2
package/dist/cli.js
CHANGED
|
@@ -245,6 +245,81 @@ function findLoopsAST(ast) {
|
|
|
245
245
|
});
|
|
246
246
|
return loops;
|
|
247
247
|
}
|
|
248
|
+
function isUserInputNode(node) {
|
|
249
|
+
if (node.type === "MemberExpression") {
|
|
250
|
+
const mem = node;
|
|
251
|
+
if (mem.object.type === "MemberExpression") {
|
|
252
|
+
const inner = mem.object;
|
|
253
|
+
if (inner.object.type === "Identifier") {
|
|
254
|
+
const objName = inner.object.name;
|
|
255
|
+
if (objName === "req" || objName === "request") {
|
|
256
|
+
if (inner.property.type === "Identifier") {
|
|
257
|
+
const prop = inner.property.name;
|
|
258
|
+
if (prop === "query" || prop === "body" || prop === "params") return true;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (node.type === "CallExpression") {
|
|
265
|
+
const call = node;
|
|
266
|
+
if (call.callee.type === "MemberExpression") {
|
|
267
|
+
const callee = call.callee;
|
|
268
|
+
if (callee.property.type === "Identifier" && callee.property.name === "get") {
|
|
269
|
+
if (callee.object.type === "Identifier") {
|
|
270
|
+
const name = callee.object.name;
|
|
271
|
+
if (name === "searchParams" || name === "formData") return true;
|
|
272
|
+
}
|
|
273
|
+
if (callee.object.type === "MemberExpression") {
|
|
274
|
+
const inner = callee.object;
|
|
275
|
+
if (inner.property.type === "Identifier" && inner.property.name === "searchParams") {
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
function isStaticString(node) {
|
|
285
|
+
if (node.type === "StringLiteral") return true;
|
|
286
|
+
if (node.type === "TemplateLiteral") {
|
|
287
|
+
return node.expressions.length === 0;
|
|
288
|
+
}
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
function findUseEffectRanges(ast) {
|
|
292
|
+
const ranges = [];
|
|
293
|
+
walkAST(ast.program, (node) => {
|
|
294
|
+
if (node.type !== "CallExpression") return;
|
|
295
|
+
const call = node;
|
|
296
|
+
if (call.callee.type !== "Identifier" || call.callee.name !== "useEffect") return;
|
|
297
|
+
const callback = call.arguments[0];
|
|
298
|
+
if (!callback || !callback.loc) return;
|
|
299
|
+
ranges.push({
|
|
300
|
+
start: callback.loc.start.line - 1,
|
|
301
|
+
end: callback.loc.end.line - 1
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
return ranges;
|
|
305
|
+
}
|
|
306
|
+
function subtreeContains(node, predicate) {
|
|
307
|
+
if (predicate(node)) return true;
|
|
308
|
+
for (const key of Object.keys(node)) {
|
|
309
|
+
if (key === "start" || key === "end" || key === "loc" || key === "type") continue;
|
|
310
|
+
const val = node[key];
|
|
311
|
+
if (Array.isArray(val)) {
|
|
312
|
+
for (const item of val) {
|
|
313
|
+
if (item && typeof item === "object" && item.type) {
|
|
314
|
+
if (subtreeContains(item, predicate)) return true;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
} else if (val && typeof val === "object" && val.type) {
|
|
318
|
+
if (subtreeContains(val, predicate)) return true;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
248
323
|
|
|
249
324
|
// src/utils/frameworks.ts
|
|
250
325
|
var FRAMEWORK_SAFE_METHODS = {
|
|
@@ -314,7 +389,8 @@ var SCAN_EXTENSIONS = [
|
|
|
314
389
|
"jsx",
|
|
315
390
|
"mjs",
|
|
316
391
|
"cjs",
|
|
317
|
-
"json"
|
|
392
|
+
"json",
|
|
393
|
+
"sql"
|
|
318
394
|
];
|
|
319
395
|
var AST_EXTENSIONS = /* @__PURE__ */ new Set(["ts", "tsx", "js", "jsx", "mjs", "cjs"]);
|
|
320
396
|
var MAX_FILE_SIZE = 1024 * 1024;
|
|
@@ -1146,6 +1222,81 @@ var unsafeHtmlRule = {
|
|
|
1146
1222
|
fileExtensions: ["ts", "tsx", "js", "jsx"],
|
|
1147
1223
|
check(file, _project) {
|
|
1148
1224
|
const findings = [];
|
|
1225
|
+
if (file.ast) {
|
|
1226
|
+
try {
|
|
1227
|
+
walkAST(file.ast.program, (node) => {
|
|
1228
|
+
if (node.type === "JSXAttribute") {
|
|
1229
|
+
const attr = node;
|
|
1230
|
+
if (attr.name?.type === "JSXIdentifier" && attr.name.name === "dangerouslySetInnerHTML" && attr.loc) {
|
|
1231
|
+
if (attr.value && subtreeContains(attr.value, (n) => {
|
|
1232
|
+
if (n.type !== "CallExpression") return false;
|
|
1233
|
+
const call = n;
|
|
1234
|
+
if (call.callee.type === "MemberExpression") {
|
|
1235
|
+
const mem = call.callee;
|
|
1236
|
+
return mem.object.type === "Identifier" && mem.object.name === "JSON" && mem.property.type === "Identifier" && mem.property.name === "stringify";
|
|
1237
|
+
}
|
|
1238
|
+
return false;
|
|
1239
|
+
})) {
|
|
1240
|
+
return;
|
|
1241
|
+
}
|
|
1242
|
+
const line = attr.loc.start.line;
|
|
1243
|
+
findings.push({
|
|
1244
|
+
ruleId: "unsafe-html",
|
|
1245
|
+
file: file.relativePath,
|
|
1246
|
+
line,
|
|
1247
|
+
column: file.lines[line - 1].indexOf("dangerouslySetInnerHTML") + 1,
|
|
1248
|
+
message: "dangerouslySetInnerHTML is an XSS risk \u2014 sanitize with DOMPurify or similar",
|
|
1249
|
+
severity: "critical",
|
|
1250
|
+
category: "security"
|
|
1251
|
+
});
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
if (node.type === "ObjectProperty") {
|
|
1255
|
+
const prop = node;
|
|
1256
|
+
if (prop.key?.type === "Identifier" && prop.key.name === "dangerouslySetInnerHTML" && prop.loc) {
|
|
1257
|
+
if (prop.value && subtreeContains(prop.value, (n) => {
|
|
1258
|
+
if (n.type !== "CallExpression") return false;
|
|
1259
|
+
const call = n;
|
|
1260
|
+
if (call.callee.type === "MemberExpression") {
|
|
1261
|
+
const mem = call.callee;
|
|
1262
|
+
return mem.object.type === "Identifier" && mem.object.name === "JSON" && mem.property.type === "Identifier" && mem.property.name === "stringify";
|
|
1263
|
+
}
|
|
1264
|
+
return false;
|
|
1265
|
+
})) {
|
|
1266
|
+
return;
|
|
1267
|
+
}
|
|
1268
|
+
const line = prop.loc.start.line;
|
|
1269
|
+
findings.push({
|
|
1270
|
+
ruleId: "unsafe-html",
|
|
1271
|
+
file: file.relativePath,
|
|
1272
|
+
line,
|
|
1273
|
+
column: file.lines[line - 1].indexOf("dangerouslySetInnerHTML") + 1,
|
|
1274
|
+
message: "dangerouslySetInnerHTML is an XSS risk \u2014 sanitize with DOMPurify or similar",
|
|
1275
|
+
severity: "critical",
|
|
1276
|
+
category: "security"
|
|
1277
|
+
});
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
if (node.type === "AssignmentExpression") {
|
|
1281
|
+
const assign = node;
|
|
1282
|
+
if (assign.left?.type === "MemberExpression" && assign.left.property?.type === "Identifier" && assign.left.property.name === "innerHTML" && assign.loc) {
|
|
1283
|
+
const line = assign.loc.start.line;
|
|
1284
|
+
findings.push({
|
|
1285
|
+
ruleId: "unsafe-html",
|
|
1286
|
+
file: file.relativePath,
|
|
1287
|
+
line,
|
|
1288
|
+
column: file.lines[line - 1].indexOf(".innerHTML") + 1,
|
|
1289
|
+
message: "Direct innerHTML assignment is an XSS risk",
|
|
1290
|
+
severity: "critical",
|
|
1291
|
+
category: "security"
|
|
1292
|
+
});
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
});
|
|
1296
|
+
return findings;
|
|
1297
|
+
} catch {
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1149
1300
|
for (let i = 0; i < file.lines.length; i++) {
|
|
1150
1301
|
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
1151
1302
|
const line = file.lines[i];
|
|
@@ -1399,6 +1550,22 @@ var DIRECT_INPUT_PATTERNS = [
|
|
|
1399
1550
|
var WARNING_PATTERNS = [
|
|
1400
1551
|
/redirect\s*\(\s*(?:url|returnUrl|returnTo|redirectUrl|redirectTo|next|callbackUrl|destination|redirect|goto|to|target|uri|href)\s*[,)]/
|
|
1401
1552
|
];
|
|
1553
|
+
var SUSPICIOUS_VAR_NAMES = /* @__PURE__ */ new Set([
|
|
1554
|
+
"url",
|
|
1555
|
+
"returnUrl",
|
|
1556
|
+
"returnTo",
|
|
1557
|
+
"redirectUrl",
|
|
1558
|
+
"redirectTo",
|
|
1559
|
+
"next",
|
|
1560
|
+
"callbackUrl",
|
|
1561
|
+
"destination",
|
|
1562
|
+
"redirect",
|
|
1563
|
+
"goto",
|
|
1564
|
+
"to",
|
|
1565
|
+
"target",
|
|
1566
|
+
"uri",
|
|
1567
|
+
"href"
|
|
1568
|
+
]);
|
|
1402
1569
|
var openRedirectRule = {
|
|
1403
1570
|
id: "open-redirect",
|
|
1404
1571
|
name: "Open Redirect",
|
|
@@ -1408,6 +1575,62 @@ var openRedirectRule = {
|
|
|
1408
1575
|
fileExtensions: ["ts", "tsx", "js", "jsx"],
|
|
1409
1576
|
check(file, _project) {
|
|
1410
1577
|
const findings = [];
|
|
1578
|
+
if (file.ast) {
|
|
1579
|
+
try {
|
|
1580
|
+
walkAST(file.ast.program, (node) => {
|
|
1581
|
+
if (node.type !== "CallExpression") return;
|
|
1582
|
+
const call = node;
|
|
1583
|
+
if (!call.loc) return;
|
|
1584
|
+
let isRedirect = false;
|
|
1585
|
+
if (call.callee.type === "Identifier" && call.callee.name === "redirect") {
|
|
1586
|
+
isRedirect = true;
|
|
1587
|
+
}
|
|
1588
|
+
if (call.callee.type === "MemberExpression") {
|
|
1589
|
+
const mem = call.callee;
|
|
1590
|
+
if (mem.object.type === "Identifier" && mem.object.name === "NextResponse" && mem.property.type === "Identifier" && mem.property.name === "redirect") {
|
|
1591
|
+
isRedirect = true;
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
if (!isRedirect) return;
|
|
1595
|
+
const arg = call.arguments[0];
|
|
1596
|
+
if (!arg) return;
|
|
1597
|
+
if (isStaticString(arg)) return;
|
|
1598
|
+
let targetArg = arg;
|
|
1599
|
+
if (arg.type === "NewExpression" && arg.callee.type === "Identifier" && arg.callee.name === "URL") {
|
|
1600
|
+
targetArg = arg.arguments[0];
|
|
1601
|
+
if (!targetArg) return;
|
|
1602
|
+
if (isStaticString(targetArg)) return;
|
|
1603
|
+
}
|
|
1604
|
+
const lineNum = call.loc.start.line;
|
|
1605
|
+
const col = call.loc.start.column + 1;
|
|
1606
|
+
if (isUserInputNode(targetArg)) {
|
|
1607
|
+
findings.push({
|
|
1608
|
+
ruleId: "open-redirect",
|
|
1609
|
+
file: file.relativePath,
|
|
1610
|
+
line: lineNum,
|
|
1611
|
+
column: col,
|
|
1612
|
+
message: "User input in redirect \u2014 validate against an allowlist to prevent open redirect",
|
|
1613
|
+
severity: "warning",
|
|
1614
|
+
category: "security"
|
|
1615
|
+
});
|
|
1616
|
+
return;
|
|
1617
|
+
}
|
|
1618
|
+
if (targetArg.type === "Identifier" && SUSPICIOUS_VAR_NAMES.has(targetArg.name)) {
|
|
1619
|
+
findings.push({
|
|
1620
|
+
ruleId: "open-redirect",
|
|
1621
|
+
file: file.relativePath,
|
|
1622
|
+
line: lineNum,
|
|
1623
|
+
column: col,
|
|
1624
|
+
message: "Possible user input in redirect \u2014 verify the URL is validated before use",
|
|
1625
|
+
severity: "warning",
|
|
1626
|
+
category: "security"
|
|
1627
|
+
});
|
|
1628
|
+
}
|
|
1629
|
+
});
|
|
1630
|
+
return findings;
|
|
1631
|
+
} catch {
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1411
1634
|
for (let i = 0; i < file.lines.length; i++) {
|
|
1412
1635
|
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
1413
1636
|
const line = file.lines[i];
|
|
@@ -2055,6 +2278,34 @@ var shallowCatchRule = {
|
|
|
2055
2278
|
if (isTestFile(file.relativePath)) return [];
|
|
2056
2279
|
if (isScriptFile(file.relativePath)) return [];
|
|
2057
2280
|
const findings = [];
|
|
2281
|
+
if (file.ast) {
|
|
2282
|
+
try {
|
|
2283
|
+
walkAST(file.ast.program, (node) => {
|
|
2284
|
+
if (node.type !== "CatchClause") return;
|
|
2285
|
+
if (!node.loc) return;
|
|
2286
|
+
const catchLine = node.loc.start.line - 1;
|
|
2287
|
+
const body = node.body;
|
|
2288
|
+
if (!body || !body.loc) return;
|
|
2289
|
+
const bodyStart = body.loc.start.line - 1;
|
|
2290
|
+
const bodyEnd = body.loc.end.line - 1;
|
|
2291
|
+
const bodyLines = file.lines.slice(bodyStart + 1, bodyEnd);
|
|
2292
|
+
const { score, label } = scoreCatchBody(bodyLines);
|
|
2293
|
+
if (score <= 1) {
|
|
2294
|
+
findings.push({
|
|
2295
|
+
ruleId: "shallow-catch",
|
|
2296
|
+
file: file.relativePath,
|
|
2297
|
+
line: catchLine + 1,
|
|
2298
|
+
column: file.lines[catchLine].indexOf("catch") + 1,
|
|
2299
|
+
message: score === 0 ? "Empty catch block \u2014 errors are silently swallowed" : `Decorative error handler: ${label}`,
|
|
2300
|
+
severity: score === 0 ? "warning" : "info",
|
|
2301
|
+
category: "reliability"
|
|
2302
|
+
});
|
|
2303
|
+
}
|
|
2304
|
+
});
|
|
2305
|
+
return findings;
|
|
2306
|
+
} catch {
|
|
2307
|
+
}
|
|
2308
|
+
}
|
|
2058
2309
|
for (let i = 0; i < file.lines.length; i++) {
|
|
2059
2310
|
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
2060
2311
|
const trimmed = file.lines[i].trim();
|
|
@@ -2388,6 +2639,7 @@ var insecureCookieRule = {
|
|
|
2388
2639
|
|
|
2389
2640
|
// src/rules/leaked-env-in-logs.ts
|
|
2390
2641
|
var CONSOLE_WITH_ENV = /console\.(log|warn|error|info|debug)\s*\([^)]*process\.env\./;
|
|
2642
|
+
var CONSOLE_METHODS = /* @__PURE__ */ new Set(["log", "warn", "error", "info", "debug"]);
|
|
2391
2643
|
var leakedEnvInLogsRule = {
|
|
2392
2644
|
id: "leaked-env-in-logs",
|
|
2393
2645
|
name: "Leaked Env in Logs",
|
|
@@ -2399,6 +2651,48 @@ var leakedEnvInLogsRule = {
|
|
|
2399
2651
|
if (isTestFile(file.relativePath)) return [];
|
|
2400
2652
|
if (isScriptFile(file.relativePath)) return [];
|
|
2401
2653
|
const findings = [];
|
|
2654
|
+
if (file.ast) {
|
|
2655
|
+
try {
|
|
2656
|
+
walkAST(file.ast.program, (node) => {
|
|
2657
|
+
if (node.type !== "CallExpression") return;
|
|
2658
|
+
const call = node;
|
|
2659
|
+
if (!call.loc) return;
|
|
2660
|
+
if (call.callee.type !== "MemberExpression") return;
|
|
2661
|
+
const mem = call.callee;
|
|
2662
|
+
if (mem.object.type !== "Identifier" || mem.object.name !== "console") return;
|
|
2663
|
+
if (mem.property.type !== "Identifier" || !CONSOLE_METHODS.has(mem.property.name)) return;
|
|
2664
|
+
let hasEnvAccess = false;
|
|
2665
|
+
for (const arg of call.arguments) {
|
|
2666
|
+
if (subtreeContains(arg, (n) => {
|
|
2667
|
+
if (n.type !== "MemberExpression") return false;
|
|
2668
|
+
const m = n;
|
|
2669
|
+
if (m.object.type === "MemberExpression") {
|
|
2670
|
+
const inner = m.object;
|
|
2671
|
+
return inner.object.type === "Identifier" && inner.object.name === "process" && inner.property.type === "Identifier" && inner.property.name === "env";
|
|
2672
|
+
}
|
|
2673
|
+
return false;
|
|
2674
|
+
})) {
|
|
2675
|
+
hasEnvAccess = true;
|
|
2676
|
+
break;
|
|
2677
|
+
}
|
|
2678
|
+
}
|
|
2679
|
+
if (hasEnvAccess) {
|
|
2680
|
+
findings.push({
|
|
2681
|
+
ruleId: "leaked-env-in-logs",
|
|
2682
|
+
file: file.relativePath,
|
|
2683
|
+
line: call.loc.start.line,
|
|
2684
|
+
column: call.loc.start.column + 1,
|
|
2685
|
+
message: "process.env value in console output \u2014 may leak secrets in production logs",
|
|
2686
|
+
severity: "warning",
|
|
2687
|
+
category: "security",
|
|
2688
|
+
fix: "Remove process.env.* from console output \u2014 log a redacted summary instead"
|
|
2689
|
+
});
|
|
2690
|
+
}
|
|
2691
|
+
});
|
|
2692
|
+
return findings;
|
|
2693
|
+
} catch {
|
|
2694
|
+
}
|
|
2695
|
+
}
|
|
2402
2696
|
for (let i = 0; i < file.lines.length; i++) {
|
|
2403
2697
|
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
2404
2698
|
const line = file.lines[i];
|
|
@@ -2503,6 +2797,15 @@ var nextServerActionValidationRule = {
|
|
|
2503
2797
|
// src/rules/missing-transaction.ts
|
|
2504
2798
|
var PRISMA_WRITE_OPS = /prisma\.\w+\.(?:create|update|delete|upsert|createMany|updateMany|deleteMany)\s*\(/;
|
|
2505
2799
|
var PRISMA_TRANSACTION = /\$transaction\s*\(/;
|
|
2800
|
+
var PRISMA_WRITE_METHODS = /* @__PURE__ */ new Set([
|
|
2801
|
+
"create",
|
|
2802
|
+
"update",
|
|
2803
|
+
"delete",
|
|
2804
|
+
"upsert",
|
|
2805
|
+
"createMany",
|
|
2806
|
+
"updateMany",
|
|
2807
|
+
"deleteMany"
|
|
2808
|
+
]);
|
|
2506
2809
|
var missingTransactionRule = {
|
|
2507
2810
|
id: "missing-transaction",
|
|
2508
2811
|
name: "Missing Transaction",
|
|
@@ -2514,6 +2817,60 @@ var missingTransactionRule = {
|
|
|
2514
2817
|
if (isTestFile(file.relativePath)) return [];
|
|
2515
2818
|
if (isScriptFile(file.relativePath)) return [];
|
|
2516
2819
|
if (!project.declaredDependencies.has("@prisma/client") && !project.detectedFrameworks.has("prisma")) return [];
|
|
2820
|
+
if (file.ast) {
|
|
2821
|
+
try {
|
|
2822
|
+
const parentMap = /* @__PURE__ */ new Map();
|
|
2823
|
+
walkAST(file.ast.program, (node, parent) => {
|
|
2824
|
+
if (parent) parentMap.set(node, parent);
|
|
2825
|
+
});
|
|
2826
|
+
const writesByScope = /* @__PURE__ */ new Map();
|
|
2827
|
+
const transactionScopes = /* @__PURE__ */ new Set();
|
|
2828
|
+
walkAST(file.ast.program, (node) => {
|
|
2829
|
+
if (node.type !== "CallExpression") return;
|
|
2830
|
+
const call = node;
|
|
2831
|
+
if (!call.loc) return;
|
|
2832
|
+
if (call.callee.type === "MemberExpression") {
|
|
2833
|
+
const mem = call.callee;
|
|
2834
|
+
if (mem.property.type === "Identifier" && mem.property.name === "$transaction") {
|
|
2835
|
+
const scope2 = findEnclosingFunction(node, parentMap);
|
|
2836
|
+
transactionScopes.add(scope2);
|
|
2837
|
+
return;
|
|
2838
|
+
}
|
|
2839
|
+
}
|
|
2840
|
+
if (call.callee.type !== "MemberExpression") return;
|
|
2841
|
+
const outerMem = call.callee;
|
|
2842
|
+
if (outerMem.property.type !== "Identifier") return;
|
|
2843
|
+
if (!PRISMA_WRITE_METHODS.has(outerMem.property.name)) return;
|
|
2844
|
+
if (outerMem.object.type !== "MemberExpression") return;
|
|
2845
|
+
const innerMem = outerMem.object;
|
|
2846
|
+
if (innerMem.object.type !== "Identifier" || innerMem.object.name !== "prisma") return;
|
|
2847
|
+
const scope = findEnclosingFunction(node, parentMap);
|
|
2848
|
+
const existing = writesByScope.get(scope);
|
|
2849
|
+
if (existing) {
|
|
2850
|
+
existing.count++;
|
|
2851
|
+
} else {
|
|
2852
|
+
writesByScope.set(scope, { count: 1, firstLine: call.loc.start.line });
|
|
2853
|
+
}
|
|
2854
|
+
});
|
|
2855
|
+
const findings = [];
|
|
2856
|
+
for (const [scope, { count, firstLine }] of writesByScope) {
|
|
2857
|
+
if (count < 2) continue;
|
|
2858
|
+
if (transactionScopes.has(scope)) continue;
|
|
2859
|
+
findings.push({
|
|
2860
|
+
ruleId: "missing-transaction",
|
|
2861
|
+
file: file.relativePath,
|
|
2862
|
+
line: firstLine,
|
|
2863
|
+
column: 1,
|
|
2864
|
+
message: `${count} Prisma write operations without $transaction \u2014 partial writes may leave inconsistent state`,
|
|
2865
|
+
severity: "warning",
|
|
2866
|
+
category: "reliability",
|
|
2867
|
+
fix: "Wrap sequential writes in prisma.$transaction([...]) for atomicity"
|
|
2868
|
+
});
|
|
2869
|
+
}
|
|
2870
|
+
return findings;
|
|
2871
|
+
} catch {
|
|
2872
|
+
}
|
|
2873
|
+
}
|
|
2517
2874
|
let writeCount = 0;
|
|
2518
2875
|
let firstWriteLine = -1;
|
|
2519
2876
|
const hasTransaction = PRISMA_TRANSACTION.test(file.content);
|
|
@@ -2537,6 +2894,1276 @@ var missingTransactionRule = {
|
|
|
2537
2894
|
}];
|
|
2538
2895
|
}
|
|
2539
2896
|
};
|
|
2897
|
+
function findEnclosingFunction(node, parentMap) {
|
|
2898
|
+
let current = parentMap.get(node);
|
|
2899
|
+
while (current) {
|
|
2900
|
+
if (current.type === "FunctionDeclaration" || current.type === "FunctionExpression" || current.type === "ArrowFunctionExpression") {
|
|
2901
|
+
return current;
|
|
2902
|
+
}
|
|
2903
|
+
current = parentMap.get(current);
|
|
2904
|
+
}
|
|
2905
|
+
return null;
|
|
2906
|
+
}
|
|
2907
|
+
|
|
2908
|
+
// src/rules/redirect-in-try-catch.ts
|
|
2909
|
+
var redirectInTryCatchRule = {
|
|
2910
|
+
id: "redirect-in-try-catch",
|
|
2911
|
+
name: "Redirect Inside Try/Catch",
|
|
2912
|
+
description: "Detects Next.js redirect() inside try/catch blocks \u2014 redirect throws internally and the catch swallows it",
|
|
2913
|
+
category: "reliability",
|
|
2914
|
+
severity: "critical",
|
|
2915
|
+
fileExtensions: ["ts", "tsx", "js", "jsx"],
|
|
2916
|
+
check(file, _project) {
|
|
2917
|
+
if (isTestFile(file.relativePath)) return [];
|
|
2918
|
+
if (!/redirect\s*\(/.test(file.content)) return [];
|
|
2919
|
+
const findings = [];
|
|
2920
|
+
let tryDepth = 0;
|
|
2921
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
2922
|
+
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
2923
|
+
const line = file.lines[i];
|
|
2924
|
+
const trimmed = line.trim();
|
|
2925
|
+
if (/\btry\s*\{/.test(trimmed) || trimmed === "try {") {
|
|
2926
|
+
tryDepth++;
|
|
2927
|
+
}
|
|
2928
|
+
if (/\}\s*catch\s*[\s(]/.test(trimmed)) {
|
|
2929
|
+
}
|
|
2930
|
+
if (tryDepth > 0) {
|
|
2931
|
+
const match = /\bredirect\s*\(/.exec(line);
|
|
2932
|
+
if (match && !/\/\//.test(line.slice(0, match.index))) {
|
|
2933
|
+
findings.push({
|
|
2934
|
+
ruleId: "redirect-in-try-catch",
|
|
2935
|
+
file: file.relativePath,
|
|
2936
|
+
line: i + 1,
|
|
2937
|
+
column: match.index + 1,
|
|
2938
|
+
message: "redirect() inside try/catch \u2014 Next.js redirect throws internally, the catch block will intercept it",
|
|
2939
|
+
severity: "critical",
|
|
2940
|
+
category: "reliability",
|
|
2941
|
+
fix: 'Move redirect() outside the try/catch block, or re-throw redirect errors in the catch: if (e instanceof Error && e.message === "NEXT_REDIRECT") throw e'
|
|
2942
|
+
});
|
|
2943
|
+
}
|
|
2944
|
+
}
|
|
2945
|
+
for (const ch of trimmed) {
|
|
2946
|
+
if (ch === "{" && tryDepth > 0) {
|
|
2947
|
+
}
|
|
2948
|
+
}
|
|
2949
|
+
if (tryDepth > 0 && /^\}\s*$/.test(trimmed)) {
|
|
2950
|
+
let nextLine = "";
|
|
2951
|
+
for (let j = i + 1; j < file.lines.length; j++) {
|
|
2952
|
+
nextLine = file.lines[j].trim();
|
|
2953
|
+
if (nextLine) break;
|
|
2954
|
+
}
|
|
2955
|
+
if (!/^catch\b/.test(nextLine) && !/^finally\b/.test(nextLine)) {
|
|
2956
|
+
tryDepth--;
|
|
2957
|
+
}
|
|
2958
|
+
}
|
|
2959
|
+
}
|
|
2960
|
+
return findings;
|
|
2961
|
+
}
|
|
2962
|
+
};
|
|
2963
|
+
|
|
2964
|
+
// src/rules/missing-revalidation.ts
|
|
2965
|
+
var USE_SERVER2 = /['"]use server['"]/;
|
|
2966
|
+
var DB_MUTATIONS = [
|
|
2967
|
+
/\.insert\s*\(/,
|
|
2968
|
+
/\.update\s*\(/,
|
|
2969
|
+
/\.delete\s*\(/,
|
|
2970
|
+
/\.upsert\s*\(/,
|
|
2971
|
+
/\.create\s*\(/,
|
|
2972
|
+
/\.createMany\s*\(/,
|
|
2973
|
+
/\.updateMany\s*\(/,
|
|
2974
|
+
/\.deleteMany\s*\(/,
|
|
2975
|
+
/\.remove\s*\(/,
|
|
2976
|
+
/\.save\s*\(/,
|
|
2977
|
+
/\.destroy\s*\(/
|
|
2978
|
+
];
|
|
2979
|
+
var REVALIDATION = [
|
|
2980
|
+
/revalidatePath\s*\(/,
|
|
2981
|
+
/revalidateTag\s*\(/,
|
|
2982
|
+
/redirect\s*\(/
|
|
2983
|
+
];
|
|
2984
|
+
var missingRevalidationRule = {
|
|
2985
|
+
id: "missing-revalidation",
|
|
2986
|
+
name: "Missing Revalidation After Mutation",
|
|
2987
|
+
description: "Detects server actions that mutate data without calling revalidatePath or revalidateTag \u2014 UI shows stale data",
|
|
2988
|
+
category: "reliability",
|
|
2989
|
+
severity: "warning",
|
|
2990
|
+
fileExtensions: ["ts", "tsx", "js", "jsx"],
|
|
2991
|
+
check(file, _project) {
|
|
2992
|
+
if (isTestFile(file.relativePath)) return [];
|
|
2993
|
+
if (!USE_SERVER2.test(file.content)) return [];
|
|
2994
|
+
const hasMutation = DB_MUTATIONS.some((p) => p.test(file.content));
|
|
2995
|
+
if (!hasMutation) return [];
|
|
2996
|
+
const hasRevalidation = REVALIDATION.some((p) => p.test(file.content));
|
|
2997
|
+
if (hasRevalidation) return [];
|
|
2998
|
+
let reportLine = 1;
|
|
2999
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
3000
|
+
if (DB_MUTATIONS.some((p) => p.test(file.lines[i]))) {
|
|
3001
|
+
reportLine = i + 1;
|
|
3002
|
+
break;
|
|
3003
|
+
}
|
|
3004
|
+
}
|
|
3005
|
+
return [{
|
|
3006
|
+
ruleId: "missing-revalidation",
|
|
3007
|
+
file: file.relativePath,
|
|
3008
|
+
line: reportLine,
|
|
3009
|
+
column: 1,
|
|
3010
|
+
message: "Server action mutates data without revalidatePath() or revalidateTag() \u2014 UI will show stale data",
|
|
3011
|
+
severity: "warning",
|
|
3012
|
+
category: "reliability",
|
|
3013
|
+
fix: 'Add revalidatePath("/affected-route") after the mutation'
|
|
3014
|
+
}];
|
|
3015
|
+
}
|
|
3016
|
+
};
|
|
3017
|
+
|
|
3018
|
+
// src/rules/use-client-overuse.ts
|
|
3019
|
+
var CLIENT_APIS = [
|
|
3020
|
+
/\buseState\b/,
|
|
3021
|
+
/\buseEffect\b/,
|
|
3022
|
+
/\buseRef\b/,
|
|
3023
|
+
/\buseReducer\b/,
|
|
3024
|
+
/\buseCallback\b/,
|
|
3025
|
+
/\buseMemo\b/,
|
|
3026
|
+
/\buseContext\b/,
|
|
3027
|
+
/\buseLayoutEffect\b/,
|
|
3028
|
+
/\buseInsertionEffect\b/,
|
|
3029
|
+
/\buseTransition\b/,
|
|
3030
|
+
/\buseDeferredValue\b/,
|
|
3031
|
+
/\buseSyncExternalStore\b/,
|
|
3032
|
+
/\buseFormStatus\b/,
|
|
3033
|
+
/\buseFormState\b/,
|
|
3034
|
+
/\buseOptimistic\b/,
|
|
3035
|
+
/\bonClick\b\s*[=:]/,
|
|
3036
|
+
/\bonChange\b\s*[=:]/,
|
|
3037
|
+
/\bonSubmit\b\s*[=:]/,
|
|
3038
|
+
/\bonBlur\b\s*[=:]/,
|
|
3039
|
+
/\bonFocus\b\s*[=:]/,
|
|
3040
|
+
/\bonKeyDown\b\s*[=:]/,
|
|
3041
|
+
/\bonKeyUp\b\s*[=:]/,
|
|
3042
|
+
/\bonMouseDown\b\s*[=:]/,
|
|
3043
|
+
/\bonMouseUp\b\s*[=:]/,
|
|
3044
|
+
/\bonScroll\b\s*[=:]/,
|
|
3045
|
+
/\bonInput\b\s*[=:]/,
|
|
3046
|
+
/\bonDrag\b/,
|
|
3047
|
+
/\bonDrop\b/,
|
|
3048
|
+
/\bonTouchStart\b/,
|
|
3049
|
+
/\bcreateContext\b/,
|
|
3050
|
+
/\bwindow\./,
|
|
3051
|
+
/\bdocument\./,
|
|
3052
|
+
/\blocalStorage\b/,
|
|
3053
|
+
/\bsessionStorage\b/,
|
|
3054
|
+
/\bnavigator\b/,
|
|
3055
|
+
/\bIntersectionObserver\b/,
|
|
3056
|
+
/\bResizeObserver\b/,
|
|
3057
|
+
/\bMutationObserver\b/
|
|
3058
|
+
];
|
|
3059
|
+
var useClientOveruseRule = {
|
|
3060
|
+
id: "use-client-overuse",
|
|
3061
|
+
name: '"use client" Overuse',
|
|
3062
|
+
description: `Detects files with "use client" that don't use any client-side APIs \u2014 unnecessary client rendering`,
|
|
3063
|
+
category: "ai-quality",
|
|
3064
|
+
severity: "info",
|
|
3065
|
+
fileExtensions: ["tsx", "jsx"],
|
|
3066
|
+
check(file, _project) {
|
|
3067
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3068
|
+
if (isConfigFile(file.relativePath)) return [];
|
|
3069
|
+
if (!isClientComponent(file.content)) return [];
|
|
3070
|
+
const usesClientApi = CLIENT_APIS.some((p) => p.test(file.content));
|
|
3071
|
+
if (usesClientApi) return [];
|
|
3072
|
+
return [{
|
|
3073
|
+
ruleId: "use-client-overuse",
|
|
3074
|
+
file: file.relativePath,
|
|
3075
|
+
line: 1,
|
|
3076
|
+
column: 1,
|
|
3077
|
+
message: '"use client" directive but no client-side APIs (hooks, event handlers, browser APIs) \u2014 this component could be a server component',
|
|
3078
|
+
severity: "info",
|
|
3079
|
+
category: "ai-quality",
|
|
3080
|
+
fix: 'Remove "use client" to let Next.js render this as a server component for better performance'
|
|
3081
|
+
}];
|
|
3082
|
+
}
|
|
3083
|
+
};
|
|
3084
|
+
|
|
3085
|
+
// src/rules/env-fallback-secret.ts
|
|
3086
|
+
var SENSITIVE_ENV = /process\.env\.(JWT_SECRET|SECRET_KEY|AUTH_SECRET|SESSION_SECRET|ENCRYPTION_KEY|API_SECRET|PRIVATE_KEY|SIGNING_KEY|NEXTAUTH_SECRET|TOKEN_SECRET|APP_SECRET|COOKIE_SECRET|HASH_SECRET)\s*(?:\|\||&&|\?\?)\s*['"`]/i;
|
|
3087
|
+
var ENV_FALLBACK = /process\.env\.\w*(SECRET|KEY|PASSWORD|TOKEN)\w*\s*(?:\|\||\?\?)\s*['"`]/i;
|
|
3088
|
+
var envFallbackSecretRule = {
|
|
3089
|
+
id: "env-fallback-secret",
|
|
3090
|
+
name: "Secret with Fallback Value",
|
|
3091
|
+
description: "Detects security-sensitive env vars with hardcoded fallback values \u2014 if the env var is missing, the fallback becomes the production secret",
|
|
3092
|
+
category: "security",
|
|
3093
|
+
severity: "critical",
|
|
3094
|
+
fileExtensions: ["ts", "tsx", "js", "jsx", "mjs", "cjs"],
|
|
3095
|
+
check(file, _project) {
|
|
3096
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3097
|
+
const findings = [];
|
|
3098
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
3099
|
+
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
3100
|
+
const line = file.lines[i];
|
|
3101
|
+
const directMatch = SENSITIVE_ENV.exec(line);
|
|
3102
|
+
if (directMatch) {
|
|
3103
|
+
findings.push({
|
|
3104
|
+
ruleId: "env-fallback-secret",
|
|
3105
|
+
file: file.relativePath,
|
|
3106
|
+
line: i + 1,
|
|
3107
|
+
column: directMatch.index + 1,
|
|
3108
|
+
message: `Secret env var has a hardcoded fallback \u2014 if ${directMatch[1] || "the var"} is unset, this literal becomes the production secret`,
|
|
3109
|
+
severity: "critical",
|
|
3110
|
+
category: "security",
|
|
3111
|
+
fix: 'Throw an error if the env var is missing: const secret = process.env.SECRET ?? (() => { throw new Error("SECRET is required") })()'
|
|
3112
|
+
});
|
|
3113
|
+
continue;
|
|
3114
|
+
}
|
|
3115
|
+
const genericMatch = ENV_FALLBACK.exec(line);
|
|
3116
|
+
if (genericMatch && !isConfigFile(file.relativePath)) {
|
|
3117
|
+
findings.push({
|
|
3118
|
+
ruleId: "env-fallback-secret",
|
|
3119
|
+
file: file.relativePath,
|
|
3120
|
+
line: i + 1,
|
|
3121
|
+
column: genericMatch.index + 1,
|
|
3122
|
+
message: "Security-sensitive env var has a hardcoded fallback \u2014 defaults to a literal string when missing",
|
|
3123
|
+
severity: "warning",
|
|
3124
|
+
category: "security",
|
|
3125
|
+
fix: "Fail fast when required env vars are missing instead of falling back to a default value"
|
|
3126
|
+
});
|
|
3127
|
+
}
|
|
3128
|
+
}
|
|
3129
|
+
return findings;
|
|
3130
|
+
}
|
|
3131
|
+
};
|
|
3132
|
+
|
|
3133
|
+
// src/rules/verbose-error-response.ts
|
|
3134
|
+
var ERROR_LEAK_PATTERNS = [
|
|
3135
|
+
{ pattern: /error\.stack/, msg: "error.stack exposed \u2014 leaks internal file paths and code structure" },
|
|
3136
|
+
{ pattern: /error\.message/, msg: "error.message may leak internal details to clients" }
|
|
3137
|
+
];
|
|
3138
|
+
var verboseErrorResponseRule = {
|
|
3139
|
+
id: "verbose-error-response",
|
|
3140
|
+
name: "Verbose Error Response",
|
|
3141
|
+
description: "Detects error details (stack traces, error messages) sent directly in API responses",
|
|
3142
|
+
category: "security",
|
|
3143
|
+
severity: "warning",
|
|
3144
|
+
fileExtensions: ["ts", "tsx", "js", "jsx"],
|
|
3145
|
+
check(file, _project) {
|
|
3146
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3147
|
+
if (!isApiRoute(file.relativePath) && !/['"]use server['"]/.test(file.content)) return [];
|
|
3148
|
+
const findings = [];
|
|
3149
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
3150
|
+
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
3151
|
+
const line = file.lines[i];
|
|
3152
|
+
for (const { pattern, msg } of ERROR_LEAK_PATTERNS) {
|
|
3153
|
+
const match = pattern.exec(line);
|
|
3154
|
+
if (match) {
|
|
3155
|
+
const severity = pattern.source.includes("stack") ? "warning" : "info";
|
|
3156
|
+
findings.push({
|
|
3157
|
+
ruleId: "verbose-error-response",
|
|
3158
|
+
file: file.relativePath,
|
|
3159
|
+
line: i + 1,
|
|
3160
|
+
column: match.index + 1,
|
|
3161
|
+
message: msg,
|
|
3162
|
+
severity,
|
|
3163
|
+
category: "security",
|
|
3164
|
+
fix: 'Return a generic error message: { error: "Internal server error" }. Log the real error server-side.'
|
|
3165
|
+
});
|
|
3166
|
+
break;
|
|
3167
|
+
}
|
|
3168
|
+
}
|
|
3169
|
+
}
|
|
3170
|
+
return findings;
|
|
3171
|
+
}
|
|
3172
|
+
};
|
|
3173
|
+
|
|
3174
|
+
// src/rules/missing-webhook-verification.ts
|
|
3175
|
+
var WEBHOOK_PATH = /webhook/i;
|
|
3176
|
+
var VERIFICATION_PATTERNS = [
|
|
3177
|
+
/constructEvent\s*\(/,
|
|
3178
|
+
// Stripe
|
|
3179
|
+
/webhooks\.verify\s*\(/,
|
|
3180
|
+
// Clerk, GitHub
|
|
3181
|
+
/verify\s*\(/,
|
|
3182
|
+
// Generic
|
|
3183
|
+
/verifySignature\s*\(/,
|
|
3184
|
+
// Generic
|
|
3185
|
+
/validateWebhook\s*\(/,
|
|
3186
|
+
// Generic
|
|
3187
|
+
/svix.*verify/i,
|
|
3188
|
+
// Svix (used by Clerk, Resend)
|
|
3189
|
+
/crypto\.timingSafeEqual\s*\(/,
|
|
3190
|
+
// Manual HMAC comparison
|
|
3191
|
+
/hmac/i,
|
|
3192
|
+
// HMAC verification
|
|
3193
|
+
/x-hub-signature/i,
|
|
3194
|
+
// GitHub webhooks
|
|
3195
|
+
/stripe-signature/i,
|
|
3196
|
+
// Stripe signature header
|
|
3197
|
+
/svix-signature/i,
|
|
3198
|
+
// Svix signature header
|
|
3199
|
+
/webhook-secret/i
|
|
3200
|
+
];
|
|
3201
|
+
var missingWebhookVerificationRule = {
|
|
3202
|
+
id: "missing-webhook-verification",
|
|
3203
|
+
name: "Missing Webhook Verification",
|
|
3204
|
+
description: "Detects webhook endpoints without signature verification \u2014 anyone can send fake events",
|
|
3205
|
+
category: "security",
|
|
3206
|
+
severity: "critical",
|
|
3207
|
+
fileExtensions: ["ts", "tsx", "js", "jsx"],
|
|
3208
|
+
check(file, _project) {
|
|
3209
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3210
|
+
if (!isApiRoute(file.relativePath)) return [];
|
|
3211
|
+
if (!WEBHOOK_PATH.test(file.relativePath)) return [];
|
|
3212
|
+
const hasVerification = VERIFICATION_PATTERNS.some((p) => p.test(file.content));
|
|
3213
|
+
if (hasVerification) return [];
|
|
3214
|
+
let handlerLine = 1;
|
|
3215
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
3216
|
+
if (/export\s+(async\s+)?function\s+POST\b/.test(file.lines[i])) {
|
|
3217
|
+
handlerLine = i + 1;
|
|
3218
|
+
break;
|
|
3219
|
+
}
|
|
3220
|
+
}
|
|
3221
|
+
return [{
|
|
3222
|
+
ruleId: "missing-webhook-verification",
|
|
3223
|
+
file: file.relativePath,
|
|
3224
|
+
line: handlerLine,
|
|
3225
|
+
column: 1,
|
|
3226
|
+
message: "Webhook endpoint has no signature verification \u2014 anyone can forge events to this route",
|
|
3227
|
+
severity: "critical",
|
|
3228
|
+
category: "security",
|
|
3229
|
+
fix: "Verify the webhook signature before processing. For Stripe: stripe.webhooks.constructEvent(body, sig, secret)"
|
|
3230
|
+
}];
|
|
3231
|
+
}
|
|
3232
|
+
};
|
|
3233
|
+
|
|
3234
|
+
// src/rules/server-action-auth.ts
|
|
3235
|
+
var USE_SERVER3 = /['"]use server['"]/;
|
|
3236
|
+
var AUTH_PATTERNS2 = [
|
|
3237
|
+
/getServerSession\s*\(/,
|
|
3238
|
+
/getSession\s*\(/,
|
|
3239
|
+
/\.auth\.getUser\s*\(/,
|
|
3240
|
+
/auth\(\)/,
|
|
3241
|
+
/authenticate\s*\(/,
|
|
3242
|
+
/isAuthenticated/,
|
|
3243
|
+
/requireAuth/,
|
|
3244
|
+
/withAuth/,
|
|
3245
|
+
/getToken\s*\(/,
|
|
3246
|
+
/verifyToken\s*\(/,
|
|
3247
|
+
/jwt\.verify\s*\(/,
|
|
3248
|
+
/createServerComponentClient/,
|
|
3249
|
+
/currentUser\s*\(/,
|
|
3250
|
+
/getAuth\s*\(/,
|
|
3251
|
+
/cookies\(\).*auth/s,
|
|
3252
|
+
/session/i
|
|
3253
|
+
];
|
|
3254
|
+
var PUBLIC_ACTION_NAMES = [
|
|
3255
|
+
/contact/i,
|
|
3256
|
+
/subscribe/i,
|
|
3257
|
+
/newsletter/i,
|
|
3258
|
+
/feedback/i,
|
|
3259
|
+
/signup/i,
|
|
3260
|
+
/login/i,
|
|
3261
|
+
/register/i
|
|
3262
|
+
];
|
|
3263
|
+
var serverActionAuthRule = {
|
|
3264
|
+
id: "server-action-auth",
|
|
3265
|
+
name: "Server Action Without Auth",
|
|
3266
|
+
description: "Detects server actions that perform mutations without any authentication check",
|
|
3267
|
+
category: "security",
|
|
3268
|
+
severity: "warning",
|
|
3269
|
+
fileExtensions: ["ts", "tsx", "js", "jsx"],
|
|
3270
|
+
check(file, project) {
|
|
3271
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3272
|
+
if (!USE_SERVER3.test(file.content)) return [];
|
|
3273
|
+
if (project.hasAuthMiddleware) return [];
|
|
3274
|
+
for (const p of PUBLIC_ACTION_NAMES) {
|
|
3275
|
+
if (p.test(file.relativePath)) return [];
|
|
3276
|
+
}
|
|
3277
|
+
const hasAuth = AUTH_PATTERNS2.some((p) => p.test(file.content));
|
|
3278
|
+
if (hasAuth) return [];
|
|
3279
|
+
const hasMutation = /\.(insert|update|delete|create|upsert|remove|destroy|save|push|set)\s*\(/i.test(file.content) || /\b(INSERT|UPDATE|DELETE)\b/.test(file.content);
|
|
3280
|
+
if (!hasMutation) return [];
|
|
3281
|
+
let reportLine = 1;
|
|
3282
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
3283
|
+
if (USE_SERVER3.test(file.lines[i])) {
|
|
3284
|
+
reportLine = i + 1;
|
|
3285
|
+
break;
|
|
3286
|
+
}
|
|
3287
|
+
}
|
|
3288
|
+
return [{
|
|
3289
|
+
ruleId: "server-action-auth",
|
|
3290
|
+
file: file.relativePath,
|
|
3291
|
+
line: reportLine,
|
|
3292
|
+
column: 1,
|
|
3293
|
+
message: "Server action performs mutations without any authentication check \u2014 anyone can call this action",
|
|
3294
|
+
severity: "warning",
|
|
3295
|
+
category: "security",
|
|
3296
|
+
fix: 'Add auth check: const session = await auth(); if (!session) throw new Error("Unauthorized")'
|
|
3297
|
+
}];
|
|
3298
|
+
}
|
|
3299
|
+
};
|
|
3300
|
+
|
|
3301
|
+
// src/rules/eval-injection.ts
|
|
3302
|
+
var EVAL_PATTERNS = [
|
|
3303
|
+
{ pattern: /\beval\s*\(/, msg: "eval() executes arbitrary code \u2014 never use with dynamic input" },
|
|
3304
|
+
{ pattern: /\bnew\s+Function\s*\(/, msg: "new Function() is equivalent to eval \u2014 avoid dynamic code execution" },
|
|
3305
|
+
{ pattern: /\bsetTimeout\s*\(\s*['"`]/, msg: "setTimeout with a string argument is eval \u2014 pass a function instead" },
|
|
3306
|
+
{ pattern: /\bsetInterval\s*\(\s*['"`]/, msg: "setInterval with a string argument is eval \u2014 pass a function instead" }
|
|
3307
|
+
];
|
|
3308
|
+
var evalInjectionRule = {
|
|
3309
|
+
id: "eval-injection",
|
|
3310
|
+
name: "Eval / Code Injection",
|
|
3311
|
+
description: "Detects eval(), new Function(), and string arguments to setTimeout/setInterval",
|
|
3312
|
+
category: "security",
|
|
3313
|
+
severity: "critical",
|
|
3314
|
+
fileExtensions: ["ts", "tsx", "js", "jsx", "mjs", "cjs"],
|
|
3315
|
+
check(file, _project) {
|
|
3316
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3317
|
+
const findings = [];
|
|
3318
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
3319
|
+
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
3320
|
+
const line = file.lines[i];
|
|
3321
|
+
for (const { pattern, msg } of EVAL_PATTERNS) {
|
|
3322
|
+
const match = pattern.exec(line);
|
|
3323
|
+
if (match) {
|
|
3324
|
+
findings.push({
|
|
3325
|
+
ruleId: "eval-injection",
|
|
3326
|
+
file: file.relativePath,
|
|
3327
|
+
line: i + 1,
|
|
3328
|
+
column: match.index + 1,
|
|
3329
|
+
message: msg,
|
|
3330
|
+
severity: "critical",
|
|
3331
|
+
category: "security"
|
|
3332
|
+
});
|
|
3333
|
+
break;
|
|
3334
|
+
}
|
|
3335
|
+
}
|
|
3336
|
+
}
|
|
3337
|
+
return findings;
|
|
3338
|
+
}
|
|
3339
|
+
};
|
|
3340
|
+
|
|
3341
|
+
// src/rules/missing-useeffect-cleanup.ts
|
|
3342
|
+
var NEEDS_CLEANUP = [
|
|
3343
|
+
/\bsetInterval\s*\(/,
|
|
3344
|
+
/\baddEventListener\s*\(/,
|
|
3345
|
+
/\.subscribe\s*\(/,
|
|
3346
|
+
/\.on\s*\(\s*['"`]/,
|
|
3347
|
+
/new\s+WebSocket\s*\(/,
|
|
3348
|
+
/new\s+EventSource\s*\(/,
|
|
3349
|
+
/new\s+IntersectionObserver\s*\(/,
|
|
3350
|
+
/new\s+ResizeObserver\s*\(/,
|
|
3351
|
+
/new\s+MutationObserver\s*\(/
|
|
3352
|
+
];
|
|
3353
|
+
var missingUseEffectCleanupRule = {
|
|
3354
|
+
id: "missing-useeffect-cleanup",
|
|
3355
|
+
name: "Missing useEffect Cleanup",
|
|
3356
|
+
description: "Detects useEffect hooks with subscriptions or timers but no cleanup return function \u2014 causes memory leaks",
|
|
3357
|
+
category: "reliability",
|
|
3358
|
+
severity: "warning",
|
|
3359
|
+
fileExtensions: ["tsx", "jsx", "ts", "js"],
|
|
3360
|
+
check(file, _project) {
|
|
3361
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3362
|
+
if (!isClientComponent(file.content)) return [];
|
|
3363
|
+
if (!/useEffect/.test(file.content)) return [];
|
|
3364
|
+
const findings = [];
|
|
3365
|
+
const lines = file.lines;
|
|
3366
|
+
for (let i = 0; i < lines.length; i++) {
|
|
3367
|
+
const line = lines[i];
|
|
3368
|
+
if (!/\buseEffect\s*\(/.test(line)) continue;
|
|
3369
|
+
let braceDepth = 0;
|
|
3370
|
+
let effectStart = -1;
|
|
3371
|
+
let effectEnd = -1;
|
|
3372
|
+
let started = false;
|
|
3373
|
+
for (let j = i; j < lines.length && j < i + 100; j++) {
|
|
3374
|
+
for (const ch of lines[j]) {
|
|
3375
|
+
if (ch === "(") {
|
|
3376
|
+
if (!started) {
|
|
3377
|
+
started = true;
|
|
3378
|
+
}
|
|
3379
|
+
braceDepth++;
|
|
3380
|
+
} else if (ch === ")") {
|
|
3381
|
+
braceDepth--;
|
|
3382
|
+
if (started && braceDepth === 0) {
|
|
3383
|
+
effectEnd = j;
|
|
3384
|
+
break;
|
|
3385
|
+
}
|
|
3386
|
+
} else if (ch === "{" && effectStart === -1 && started) {
|
|
3387
|
+
effectStart = j;
|
|
3388
|
+
}
|
|
3389
|
+
}
|
|
3390
|
+
if (effectEnd !== -1) break;
|
|
3391
|
+
}
|
|
3392
|
+
if (effectStart === -1 || effectEnd === -1) continue;
|
|
3393
|
+
const effectBody = lines.slice(effectStart, effectEnd + 1).join("\n");
|
|
3394
|
+
const needsCleanup = NEEDS_CLEANUP.some((p) => p.test(effectBody));
|
|
3395
|
+
if (!needsCleanup) continue;
|
|
3396
|
+
const hasReturn = /return\s*(?:\(\s*\)\s*=>|function|\(\))/.test(effectBody) || /return\s*\(\s*\)\s*\{/.test(effectBody) || /return\s+\w+\s*;?\s*$/.test(effectBody);
|
|
3397
|
+
if (!hasReturn) {
|
|
3398
|
+
const hasCleanupReturn = /return\s+(?:\(\)|(?:\(\s*\)\s*=>)|(?:function))/.test(effectBody);
|
|
3399
|
+
if (!hasCleanupReturn) {
|
|
3400
|
+
findings.push({
|
|
3401
|
+
ruleId: "missing-useeffect-cleanup",
|
|
3402
|
+
file: file.relativePath,
|
|
3403
|
+
line: i + 1,
|
|
3404
|
+
column: 1,
|
|
3405
|
+
message: "useEffect with subscription/timer but no cleanup return \u2014 will leak memory on unmount",
|
|
3406
|
+
severity: "warning",
|
|
3407
|
+
category: "reliability",
|
|
3408
|
+
fix: "Return a cleanup function: useEffect(() => { const id = setInterval(...); return () => clearInterval(id); }, [])"
|
|
3409
|
+
});
|
|
3410
|
+
}
|
|
3411
|
+
}
|
|
3412
|
+
}
|
|
3413
|
+
return findings;
|
|
3414
|
+
}
|
|
3415
|
+
};
|
|
3416
|
+
|
|
3417
|
+
// src/rules/next-public-sensitive.ts
|
|
3418
|
+
var SENSITIVE_PATTERN = /NEXT_PUBLIC_\w*(SECRET|PRIVATE|PASSWORD|DATABASE_URL|SERVICE_ROLE|SERVICE_KEY|ADMIN_KEY|sk_live|sk_test|SIGNING|ENCRYPTION)/i;
|
|
3419
|
+
var nextPublicSensitiveRule = {
|
|
3420
|
+
id: "next-public-sensitive",
|
|
3421
|
+
name: "Sensitive Env Var with NEXT_PUBLIC_ Prefix",
|
|
3422
|
+
description: "Detects NEXT_PUBLIC_ prefix on environment variables that should be server-only \u2014 exposes secrets to the browser",
|
|
3423
|
+
category: "security",
|
|
3424
|
+
severity: "critical",
|
|
3425
|
+
fileExtensions: ["ts", "tsx", "js", "jsx", "mjs", "env", "env.local", "env.production"],
|
|
3426
|
+
check(file, _project) {
|
|
3427
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3428
|
+
const findings = [];
|
|
3429
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
3430
|
+
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
3431
|
+
const line = file.lines[i];
|
|
3432
|
+
const match = SENSITIVE_PATTERN.exec(line);
|
|
3433
|
+
if (match) {
|
|
3434
|
+
findings.push({
|
|
3435
|
+
ruleId: "next-public-sensitive",
|
|
3436
|
+
file: file.relativePath,
|
|
3437
|
+
line: i + 1,
|
|
3438
|
+
column: match.index + 1,
|
|
3439
|
+
message: `NEXT_PUBLIC_ prefix on a sensitive env var \u2014 this value will be embedded in the client-side JavaScript bundle`,
|
|
3440
|
+
severity: "critical",
|
|
3441
|
+
category: "security",
|
|
3442
|
+
fix: "Remove the NEXT_PUBLIC_ prefix. Access this value only in server components, API routes, or server actions."
|
|
3443
|
+
});
|
|
3444
|
+
}
|
|
3445
|
+
}
|
|
3446
|
+
return findings;
|
|
3447
|
+
}
|
|
3448
|
+
};
|
|
3449
|
+
|
|
3450
|
+
// src/rules/ssrf-risk.ts
|
|
3451
|
+
var USER_INPUT_IN_FETCH = [
|
|
3452
|
+
/fetch\s*\(\s*(?:req|request)\.(?:body|query|params|nextUrl)/,
|
|
3453
|
+
/fetch\s*\(\s*(?:url|href|endpoint|target|link|src)\s*[,)]/,
|
|
3454
|
+
/fetch\s*\(\s*searchParams\.get\s*\(/,
|
|
3455
|
+
/fetch\s*\(\s*formData\.get\s*\(/,
|
|
3456
|
+
/new\s+URL\s*\(\s*(?:req|request)\.(?:body|query)/,
|
|
3457
|
+
/axios\s*[.(]\s*(?:req|request)\.(?:body|query)/,
|
|
3458
|
+
/axios\.get\s*\(\s*(?:url|href|endpoint|target|link)\s*[,)]/
|
|
3459
|
+
];
|
|
3460
|
+
var VALIDATION_PATTERNS3 = [
|
|
3461
|
+
/allowlist/i,
|
|
3462
|
+
/allowedUrls/i,
|
|
3463
|
+
/allowedHosts/i,
|
|
3464
|
+
/allowedDomains/i,
|
|
3465
|
+
/whitelist/i,
|
|
3466
|
+
/validUrl/i,
|
|
3467
|
+
/validateUrl/i,
|
|
3468
|
+
/URL\.canParse/,
|
|
3469
|
+
/new\s+URL\s*\(.*\)\.host/,
|
|
3470
|
+
/\.startsWith\s*\(\s*['"]https?:\/\//,
|
|
3471
|
+
/\.hostname\s*[!=]==?\s*['"`]/
|
|
3472
|
+
];
|
|
3473
|
+
var SUSPICIOUS_URL_VARS = /* @__PURE__ */ new Set([
|
|
3474
|
+
"url",
|
|
3475
|
+
"href",
|
|
3476
|
+
"endpoint",
|
|
3477
|
+
"target",
|
|
3478
|
+
"link",
|
|
3479
|
+
"src"
|
|
3480
|
+
]);
|
|
3481
|
+
var ssrfRiskRule = {
|
|
3482
|
+
id: "ssrf-risk",
|
|
3483
|
+
name: "SSRF Risk",
|
|
3484
|
+
description: "Detects fetch/HTTP calls with user-controlled URLs without validation \u2014 allows attackers to probe internal services",
|
|
3485
|
+
category: "security",
|
|
3486
|
+
severity: "warning",
|
|
3487
|
+
fileExtensions: ["ts", "tsx", "js", "jsx"],
|
|
3488
|
+
check(file, _project) {
|
|
3489
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3490
|
+
if (!isApiRoute(file.relativePath) && !/['"]use server['"]/.test(file.content)) return [];
|
|
3491
|
+
const findings = [];
|
|
3492
|
+
if (file.ast) {
|
|
3493
|
+
try {
|
|
3494
|
+
const hasValidationInCode = VALIDATION_PATTERNS3.some((p) => p.test(file.content));
|
|
3495
|
+
walkAST(file.ast.program, (node) => {
|
|
3496
|
+
if (node.type !== "CallExpression") return;
|
|
3497
|
+
const call = node;
|
|
3498
|
+
if (!call.loc) return;
|
|
3499
|
+
let isFetchLike = false;
|
|
3500
|
+
if (call.callee.type === "Identifier" && call.callee.name === "fetch") {
|
|
3501
|
+
isFetchLike = true;
|
|
3502
|
+
}
|
|
3503
|
+
if (call.callee.type === "MemberExpression") {
|
|
3504
|
+
const mem = call.callee;
|
|
3505
|
+
if (mem.object.type === "Identifier" && mem.object.name === "axios") {
|
|
3506
|
+
isFetchLike = true;
|
|
3507
|
+
}
|
|
3508
|
+
}
|
|
3509
|
+
if (!isFetchLike) return;
|
|
3510
|
+
const arg = call.arguments[0];
|
|
3511
|
+
if (!arg) return;
|
|
3512
|
+
if (isStaticString(arg)) return;
|
|
3513
|
+
const lineNum = call.loc.start.line;
|
|
3514
|
+
const col = call.loc.start.column + 1;
|
|
3515
|
+
if (isUserInputNode(arg)) {
|
|
3516
|
+
findings.push({
|
|
3517
|
+
ruleId: "ssrf-risk",
|
|
3518
|
+
file: file.relativePath,
|
|
3519
|
+
line: lineNum,
|
|
3520
|
+
column: col,
|
|
3521
|
+
message: "User-controlled URL passed to fetch \u2014 validate against an allowlist to prevent SSRF",
|
|
3522
|
+
severity: "warning",
|
|
3523
|
+
category: "security",
|
|
3524
|
+
fix: "Validate the URL against an allowlist of permitted domains before making the request"
|
|
3525
|
+
});
|
|
3526
|
+
return;
|
|
3527
|
+
}
|
|
3528
|
+
if (arg.type === "Identifier" && SUSPICIOUS_URL_VARS.has(arg.name)) {
|
|
3529
|
+
if (hasValidationInCode) return;
|
|
3530
|
+
findings.push({
|
|
3531
|
+
ruleId: "ssrf-risk",
|
|
3532
|
+
file: file.relativePath,
|
|
3533
|
+
line: lineNum,
|
|
3534
|
+
column: col,
|
|
3535
|
+
message: "User-controlled URL passed to fetch \u2014 validate against an allowlist to prevent SSRF",
|
|
3536
|
+
severity: "warning",
|
|
3537
|
+
category: "security",
|
|
3538
|
+
fix: "Validate the URL against an allowlist of permitted domains before making the request"
|
|
3539
|
+
});
|
|
3540
|
+
}
|
|
3541
|
+
});
|
|
3542
|
+
return findings;
|
|
3543
|
+
} catch {
|
|
3544
|
+
}
|
|
3545
|
+
}
|
|
3546
|
+
const hasValidation = VALIDATION_PATTERNS3.some((p) => p.test(file.content));
|
|
3547
|
+
if (hasValidation) return [];
|
|
3548
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
3549
|
+
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
3550
|
+
const line = file.lines[i];
|
|
3551
|
+
for (const pattern of USER_INPUT_IN_FETCH) {
|
|
3552
|
+
const match = pattern.exec(line);
|
|
3553
|
+
if (match) {
|
|
3554
|
+
findings.push({
|
|
3555
|
+
ruleId: "ssrf-risk",
|
|
3556
|
+
file: file.relativePath,
|
|
3557
|
+
line: i + 1,
|
|
3558
|
+
column: match.index + 1,
|
|
3559
|
+
message: "User-controlled URL passed to fetch \u2014 validate against an allowlist to prevent SSRF",
|
|
3560
|
+
severity: "warning",
|
|
3561
|
+
category: "security",
|
|
3562
|
+
fix: "Validate the URL against an allowlist of permitted domains before making the request"
|
|
3563
|
+
});
|
|
3564
|
+
break;
|
|
3565
|
+
}
|
|
3566
|
+
}
|
|
3567
|
+
}
|
|
3568
|
+
return findings;
|
|
3569
|
+
}
|
|
3570
|
+
};
|
|
3571
|
+
|
|
3572
|
+
// src/rules/path-traversal.ts
|
|
3573
|
+
var FS_WITH_USER_INPUT = [
|
|
3574
|
+
{ pattern: /(?:readFile|readFileSync|createReadStream)\s*\(\s*(?:req|request)\.(?:query|body|params)/, msg: "File read with user-controlled path \u2014 allows reading arbitrary files" },
|
|
3575
|
+
{ pattern: /(?:readFile|readFileSync|createReadStream)\s*\(\s*(?:filePath|fileName|path|file|name)\s*[,)]/, msg: "File read with potentially user-controlled path \u2014 validate before use" },
|
|
3576
|
+
{ pattern: /(?:writeFile|writeFileSync|createWriteStream)\s*\(\s*(?:req|request)\.(?:query|body|params)/, msg: "File write with user-controlled path \u2014 allows writing arbitrary files" },
|
|
3577
|
+
{ pattern: /(?:unlink|unlinkSync|rm|rmSync)\s*\(\s*(?:req|request)\.(?:query|body|params)/, msg: "File delete with user-controlled path \u2014 allows deleting arbitrary files" },
|
|
3578
|
+
{ pattern: /path\.join\s*\([^)]*(?:req|request)\.(?:query|body|params)/, msg: "path.join with user input \u2014 still vulnerable to traversal with ../" },
|
|
3579
|
+
{ pattern: /\.sendFile\s*\(\s*(?:req|request)\.(?:query|body|params)/, msg: "express.sendFile with user-controlled path \u2014 validate against a base directory" }
|
|
3580
|
+
];
|
|
3581
|
+
var SANITIZATION_PATTERNS = [
|
|
3582
|
+
/path\.resolve\s*\(.*\)\.startsWith/,
|
|
3583
|
+
/\.replace\s*\(\s*['"]\.\.['"],?\s*['"].*['"]\s*\)/,
|
|
3584
|
+
/\.includes\s*\(\s*['"]\.\.['"].*\)/,
|
|
3585
|
+
/normalize/,
|
|
3586
|
+
/sanitize/i,
|
|
3587
|
+
/realpath/
|
|
3588
|
+
];
|
|
3589
|
+
var FS_FUNCTION_NAMES = /* @__PURE__ */ new Set([
|
|
3590
|
+
"readFile",
|
|
3591
|
+
"readFileSync",
|
|
3592
|
+
"createReadStream",
|
|
3593
|
+
"writeFile",
|
|
3594
|
+
"writeFileSync",
|
|
3595
|
+
"createWriteStream",
|
|
3596
|
+
"unlink",
|
|
3597
|
+
"unlinkSync",
|
|
3598
|
+
"rm",
|
|
3599
|
+
"rmSync"
|
|
3600
|
+
]);
|
|
3601
|
+
var SUSPICIOUS_PATH_VARS = /* @__PURE__ */ new Set([
|
|
3602
|
+
"filePath",
|
|
3603
|
+
"fileName",
|
|
3604
|
+
"path",
|
|
3605
|
+
"file",
|
|
3606
|
+
"name"
|
|
3607
|
+
]);
|
|
3608
|
+
var pathTraversalRule = {
|
|
3609
|
+
id: "path-traversal",
|
|
3610
|
+
name: "Path Traversal",
|
|
3611
|
+
description: "Detects filesystem operations with user-controlled paths \u2014 allows reading/writing arbitrary files via ../ sequences",
|
|
3612
|
+
category: "security",
|
|
3613
|
+
severity: "critical",
|
|
3614
|
+
fileExtensions: ["ts", "tsx", "js", "jsx"],
|
|
3615
|
+
check(file, _project) {
|
|
3616
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3617
|
+
const findings = [];
|
|
3618
|
+
if (file.ast) {
|
|
3619
|
+
try {
|
|
3620
|
+
walkAST(file.ast.program, (node) => {
|
|
3621
|
+
if (node.type !== "CallExpression") return;
|
|
3622
|
+
const call = node;
|
|
3623
|
+
if (!call.loc) return;
|
|
3624
|
+
let fnName = null;
|
|
3625
|
+
if (call.callee.type === "Identifier" && FS_FUNCTION_NAMES.has(call.callee.name)) {
|
|
3626
|
+
fnName = call.callee.name;
|
|
3627
|
+
}
|
|
3628
|
+
if (call.callee.type === "MemberExpression") {
|
|
3629
|
+
const mem = call.callee;
|
|
3630
|
+
if (mem.property.type === "Identifier") {
|
|
3631
|
+
if (FS_FUNCTION_NAMES.has(mem.property.name)) {
|
|
3632
|
+
fnName = mem.property.name;
|
|
3633
|
+
}
|
|
3634
|
+
if (mem.property.name === "join" && mem.object.type === "Identifier" && mem.object.name === "path") {
|
|
3635
|
+
fnName = "path.join";
|
|
3636
|
+
}
|
|
3637
|
+
if (mem.property.name === "sendFile") {
|
|
3638
|
+
fnName = "sendFile";
|
|
3639
|
+
}
|
|
3640
|
+
}
|
|
3641
|
+
}
|
|
3642
|
+
if (!fnName) return;
|
|
3643
|
+
const argsToCheck = fnName === "path.join" ? call.arguments : [call.arguments[0]];
|
|
3644
|
+
for (const arg of argsToCheck) {
|
|
3645
|
+
if (!arg) continue;
|
|
3646
|
+
if (isStaticString(arg)) continue;
|
|
3647
|
+
const lineNum = call.loc.start.line;
|
|
3648
|
+
const col = call.loc.start.column + 1;
|
|
3649
|
+
if (isUserInputNode(arg)) {
|
|
3650
|
+
const action = fnName.includes("write") || fnName.includes("Write") ? "write" : fnName.includes("unlink") || fnName === "rm" || fnName === "rmSync" ? "delete" : fnName === "sendFile" ? "send" : "read";
|
|
3651
|
+
findings.push({
|
|
3652
|
+
ruleId: "path-traversal",
|
|
3653
|
+
file: file.relativePath,
|
|
3654
|
+
line: lineNum,
|
|
3655
|
+
column: col,
|
|
3656
|
+
message: fnName === "path.join" ? "path.join with user input \u2014 still vulnerable to traversal with ../" : `File ${action} with user-controlled path \u2014 allows ${action === "send" ? "sending" : action + "ing"} arbitrary files`,
|
|
3657
|
+
severity: "critical",
|
|
3658
|
+
category: "security",
|
|
3659
|
+
fix: "Validate the resolved path starts with an expected base directory: path.resolve(base, input).startsWith(path.resolve(base))"
|
|
3660
|
+
});
|
|
3661
|
+
return;
|
|
3662
|
+
}
|
|
3663
|
+
if (arg.type === "Identifier" && SUSPICIOUS_PATH_VARS.has(arg.name)) {
|
|
3664
|
+
findings.push({
|
|
3665
|
+
ruleId: "path-traversal",
|
|
3666
|
+
file: file.relativePath,
|
|
3667
|
+
line: lineNum,
|
|
3668
|
+
column: col,
|
|
3669
|
+
message: `File operation with potentially user-controlled path \u2014 validate before use`,
|
|
3670
|
+
severity: "warning",
|
|
3671
|
+
category: "security",
|
|
3672
|
+
fix: "Validate the resolved path starts with an expected base directory: path.resolve(base, input).startsWith(path.resolve(base))"
|
|
3673
|
+
});
|
|
3674
|
+
return;
|
|
3675
|
+
}
|
|
3676
|
+
}
|
|
3677
|
+
});
|
|
3678
|
+
return findings;
|
|
3679
|
+
} catch {
|
|
3680
|
+
}
|
|
3681
|
+
}
|
|
3682
|
+
const hasSanitization = SANITIZATION_PATTERNS.some((p) => p.test(file.content));
|
|
3683
|
+
if (hasSanitization) return [];
|
|
3684
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
3685
|
+
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
3686
|
+
const line = file.lines[i];
|
|
3687
|
+
for (const { pattern, msg } of FS_WITH_USER_INPUT) {
|
|
3688
|
+
const match = pattern.exec(line);
|
|
3689
|
+
if (match) {
|
|
3690
|
+
const severity = /req|request/.test(match[0]) ? "critical" : "warning";
|
|
3691
|
+
findings.push({
|
|
3692
|
+
ruleId: "path-traversal",
|
|
3693
|
+
file: file.relativePath,
|
|
3694
|
+
line: i + 1,
|
|
3695
|
+
column: match.index + 1,
|
|
3696
|
+
message: msg,
|
|
3697
|
+
severity,
|
|
3698
|
+
category: "security",
|
|
3699
|
+
fix: "Validate the resolved path starts with an expected base directory: path.resolve(base, input).startsWith(path.resolve(base))"
|
|
3700
|
+
});
|
|
3701
|
+
break;
|
|
3702
|
+
}
|
|
3703
|
+
}
|
|
3704
|
+
}
|
|
3705
|
+
return findings;
|
|
3706
|
+
}
|
|
3707
|
+
};
|
|
3708
|
+
|
|
3709
|
+
// src/rules/hydration-mismatch.ts
|
|
3710
|
+
var BROWSER_ONLY_PATTERNS = [
|
|
3711
|
+
{ pattern: /\bwindow\./, msg: "window access in server-rendered code \u2014 will differ between server and client" },
|
|
3712
|
+
{ pattern: /\bdocument\./, msg: "document access in server-rendered code \u2014 undefined on the server" },
|
|
3713
|
+
{ pattern: /\blocalStorage\b/, msg: "localStorage in render path \u2014 undefined on server, causes hydration mismatch" },
|
|
3714
|
+
{ pattern: /\bsessionStorage\b/, msg: "sessionStorage in render path \u2014 undefined on server, causes hydration mismatch" },
|
|
3715
|
+
{ pattern: /\bnavigator\./, msg: "navigator access in render path \u2014 undefined on server" }
|
|
3716
|
+
];
|
|
3717
|
+
var NONDETERMINISTIC_PATTERNS = [
|
|
3718
|
+
{ pattern: /\bnew\s+Date\s*\(\s*\)/, msg: "new Date() in render path \u2014 server and client will have different timestamps, causing hydration mismatch" },
|
|
3719
|
+
{ pattern: /\bDate\.now\s*\(\s*\)/, msg: "Date.now() in render path \u2014 different on server vs client" },
|
|
3720
|
+
{ pattern: /\bMath\.random\s*\(\s*\)/, msg: "Math.random() in render path \u2014 produces different values on server vs client" }
|
|
3721
|
+
];
|
|
3722
|
+
var hydrationMismatchRule = {
|
|
3723
|
+
id: "hydration-mismatch",
|
|
3724
|
+
name: "Hydration Mismatch Risk",
|
|
3725
|
+
description: "Detects browser-only APIs and non-deterministic calls in server component render paths",
|
|
3726
|
+
category: "reliability",
|
|
3727
|
+
severity: "warning",
|
|
3728
|
+
fileExtensions: ["tsx", "jsx", "ts", "js"],
|
|
3729
|
+
check(file, _project) {
|
|
3730
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3731
|
+
if (isClientComponent(file.content)) return [];
|
|
3732
|
+
if (!/(?:^|\/)(?:src\/)?app\//.test(file.relativePath)) return [];
|
|
3733
|
+
if (/route\.[jt]sx?$/.test(file.relativePath)) return [];
|
|
3734
|
+
const findings = [];
|
|
3735
|
+
let useEffectRanges = [];
|
|
3736
|
+
if (file.ast) {
|
|
3737
|
+
try {
|
|
3738
|
+
useEffectRanges = findUseEffectRanges(file.ast);
|
|
3739
|
+
} catch {
|
|
3740
|
+
}
|
|
3741
|
+
}
|
|
3742
|
+
const hasAstRanges = useEffectRanges.length > 0 || file.ast != null;
|
|
3743
|
+
let insideUseEffect = false;
|
|
3744
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
3745
|
+
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
3746
|
+
const line = file.lines[i];
|
|
3747
|
+
if (hasAstRanges) {
|
|
3748
|
+
const inEffect = useEffectRanges.some((r) => i >= r.start && i <= r.end);
|
|
3749
|
+
if (inEffect) continue;
|
|
3750
|
+
} else {
|
|
3751
|
+
if (/\buseEffect\s*\(/.test(line)) {
|
|
3752
|
+
insideUseEffect = true;
|
|
3753
|
+
}
|
|
3754
|
+
if (insideUseEffect) {
|
|
3755
|
+
if (/^\s*\}\s*,\s*\[/.test(line) || /^\s*\}\s*\)\s*;?\s*$/.test(line)) {
|
|
3756
|
+
insideUseEffect = false;
|
|
3757
|
+
}
|
|
3758
|
+
continue;
|
|
3759
|
+
}
|
|
3760
|
+
}
|
|
3761
|
+
for (const { pattern, msg } of [...BROWSER_ONLY_PATTERNS, ...NONDETERMINISTIC_PATTERNS]) {
|
|
3762
|
+
const match = pattern.exec(line);
|
|
3763
|
+
if (match) {
|
|
3764
|
+
findings.push({
|
|
3765
|
+
ruleId: "hydration-mismatch",
|
|
3766
|
+
file: file.relativePath,
|
|
3767
|
+
line: i + 1,
|
|
3768
|
+
column: match.index + 1,
|
|
3769
|
+
message: msg,
|
|
3770
|
+
severity: "warning",
|
|
3771
|
+
category: "reliability",
|
|
3772
|
+
fix: 'Move this to a useEffect hook, or add "use client" if this component needs browser APIs'
|
|
3773
|
+
});
|
|
3774
|
+
break;
|
|
3775
|
+
}
|
|
3776
|
+
}
|
|
3777
|
+
}
|
|
3778
|
+
return findings;
|
|
3779
|
+
}
|
|
3780
|
+
};
|
|
3781
|
+
|
|
3782
|
+
// src/rules/server-component-fetch-self.ts
|
|
3783
|
+
var SELF_FETCH_PATTERNS = [
|
|
3784
|
+
/fetch\s*\(\s*['"`]\/api\//,
|
|
3785
|
+
/fetch\s*\(\s*['"`]http:\/\/localhost/,
|
|
3786
|
+
/fetch\s*\(\s*['"`]https?:\/\/localhost/,
|
|
3787
|
+
/fetch\s*\(\s*`\$\{.*\}\/api\//,
|
|
3788
|
+
/fetch\s*\(\s*(?:process\.env\.\w+\s*\+\s*)?['"`]\/api\//
|
|
3789
|
+
];
|
|
3790
|
+
var serverComponentFetchSelfRule = {
|
|
3791
|
+
id: "server-component-fetch-self",
|
|
3792
|
+
name: "Server Component Fetching Own API",
|
|
3793
|
+
description: "Detects server components that fetch their own API routes instead of calling data logic directly \u2014 unnecessary network roundtrip",
|
|
3794
|
+
category: "performance",
|
|
3795
|
+
severity: "info",
|
|
3796
|
+
fileExtensions: ["tsx", "jsx", "ts", "js"],
|
|
3797
|
+
check(file, _project) {
|
|
3798
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3799
|
+
if (isClientComponent(file.content)) return [];
|
|
3800
|
+
if (!/(?:^|\/)(?:src\/)?app\//.test(file.relativePath)) return [];
|
|
3801
|
+
if (/route\.[jt]sx?$/.test(file.relativePath)) return [];
|
|
3802
|
+
const findings = [];
|
|
3803
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
3804
|
+
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
3805
|
+
const line = file.lines[i];
|
|
3806
|
+
for (const pattern of SELF_FETCH_PATTERNS) {
|
|
3807
|
+
const match = pattern.exec(line);
|
|
3808
|
+
if (match) {
|
|
3809
|
+
findings.push({
|
|
3810
|
+
ruleId: "server-component-fetch-self",
|
|
3811
|
+
file: file.relativePath,
|
|
3812
|
+
line: i + 1,
|
|
3813
|
+
column: match.index + 1,
|
|
3814
|
+
message: "Server component fetches its own API route \u2014 call the data logic directly instead of making a network request to yourself",
|
|
3815
|
+
severity: "info",
|
|
3816
|
+
category: "performance",
|
|
3817
|
+
fix: 'Import and call the data function directly instead of fetch("/api/...")'
|
|
3818
|
+
});
|
|
3819
|
+
break;
|
|
3820
|
+
}
|
|
3821
|
+
}
|
|
3822
|
+
}
|
|
3823
|
+
return findings;
|
|
3824
|
+
}
|
|
3825
|
+
};
|
|
3826
|
+
|
|
3827
|
+
// src/rules/unsafe-file-upload.ts
|
|
3828
|
+
var UPLOAD_PATTERNS = [
|
|
3829
|
+
/\.get\s*\(\s*['"`]file['"`]\s*\)/,
|
|
3830
|
+
/\.get\s*\(\s*['"`]image['"`]\s*\)/,
|
|
3831
|
+
/\.get\s*\(\s*['"`]upload['"`]\s*\)/,
|
|
3832
|
+
/\.get\s*\(\s*['"`]attachment['"`]\s*\)/,
|
|
3833
|
+
/\.get\s*\(\s*['"`]document['"`]\s*\)/,
|
|
3834
|
+
/\.get\s*\(\s*['"`]avatar['"`]\s*\)/,
|
|
3835
|
+
/\.get\s*\(\s*['"`]photo['"`]\s*\)/,
|
|
3836
|
+
/\.type\s*===?\s*['"`]file['"`]/,
|
|
3837
|
+
/req\.file\b/,
|
|
3838
|
+
/multer/i,
|
|
3839
|
+
/busboy/i,
|
|
3840
|
+
/formidable/i
|
|
3841
|
+
];
|
|
3842
|
+
var VALIDATION_PATTERNS4 = [
|
|
3843
|
+
/\.type\b.*(?:image|video|audio|pdf|text)\//,
|
|
3844
|
+
/content-type/i,
|
|
3845
|
+
/mime/i,
|
|
3846
|
+
/\.size\s*[><!]/,
|
|
3847
|
+
/maxFileSize/i,
|
|
3848
|
+
/maxSize/i,
|
|
3849
|
+
/fileSizeLimit/i,
|
|
3850
|
+
/allowedTypes/i,
|
|
3851
|
+
/acceptedTypes/i,
|
|
3852
|
+
/fileFilter/i,
|
|
3853
|
+
/\.endsWith\s*\(\s*['"`]\./,
|
|
3854
|
+
/\.extension/i
|
|
3855
|
+
];
|
|
3856
|
+
var unsafeFileUploadRule = {
|
|
3857
|
+
id: "unsafe-file-upload",
|
|
3858
|
+
name: "Unsafe File Upload",
|
|
3859
|
+
description: "Detects file upload handlers without type or size validation",
|
|
3860
|
+
category: "security",
|
|
3861
|
+
severity: "warning",
|
|
3862
|
+
fileExtensions: ["ts", "tsx", "js", "jsx"],
|
|
3863
|
+
check(file, _project) {
|
|
3864
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3865
|
+
if (!isApiRoute(file.relativePath) && !/['"]use server['"]/.test(file.content)) return [];
|
|
3866
|
+
const hasUpload = UPLOAD_PATTERNS.some((p) => p.test(file.content));
|
|
3867
|
+
if (!hasUpload) return [];
|
|
3868
|
+
const hasValidation = VALIDATION_PATTERNS4.some((p) => p.test(file.content));
|
|
3869
|
+
if (hasValidation) return [];
|
|
3870
|
+
let reportLine = 1;
|
|
3871
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
3872
|
+
if (UPLOAD_PATTERNS.some((p) => p.test(file.lines[i]))) {
|
|
3873
|
+
reportLine = i + 1;
|
|
3874
|
+
break;
|
|
3875
|
+
}
|
|
3876
|
+
}
|
|
3877
|
+
return [{
|
|
3878
|
+
ruleId: "unsafe-file-upload",
|
|
3879
|
+
file: file.relativePath,
|
|
3880
|
+
line: reportLine,
|
|
3881
|
+
column: 1,
|
|
3882
|
+
message: "File upload without type or size validation \u2014 accepts any file type and size",
|
|
3883
|
+
severity: "warning",
|
|
3884
|
+
category: "security",
|
|
3885
|
+
fix: "Validate file type (check MIME type, not just extension) and enforce a size limit before processing"
|
|
3886
|
+
}];
|
|
3887
|
+
}
|
|
3888
|
+
};
|
|
3889
|
+
|
|
3890
|
+
// src/rules/supabase-missing-rls.ts
|
|
3891
|
+
var CREATE_TABLE = /CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?(?:public\.)?(\w+)/gi;
|
|
3892
|
+
var ENABLE_RLS = /ALTER\s+TABLE\s+(?:(?:public|"public")\.)?(\w+)\s+ENABLE\s+ROW\s+LEVEL\s+SECURITY/gi;
|
|
3893
|
+
var supabaseMissingRlsRule = {
|
|
3894
|
+
id: "supabase-missing-rls",
|
|
3895
|
+
name: "Missing Row-Level Security",
|
|
3896
|
+
description: "Detects SQL migrations that create tables without enabling Row-Level Security \u2014 all data is publicly accessible",
|
|
3897
|
+
category: "security",
|
|
3898
|
+
severity: "critical",
|
|
3899
|
+
fileExtensions: ["sql"],
|
|
3900
|
+
check(file, project) {
|
|
3901
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3902
|
+
if (!/migration|supabase|schema/i.test(file.relativePath)) return [];
|
|
3903
|
+
const content = file.content;
|
|
3904
|
+
const findings = [];
|
|
3905
|
+
const tables = [];
|
|
3906
|
+
CREATE_TABLE.lastIndex = 0;
|
|
3907
|
+
let match;
|
|
3908
|
+
while ((match = CREATE_TABLE.exec(content)) !== null) {
|
|
3909
|
+
const name = match[1];
|
|
3910
|
+
if (name.startsWith("_") || name === "schema_migrations") continue;
|
|
3911
|
+
const beforeMatch = content.slice(0, match.index);
|
|
3912
|
+
const line = beforeMatch.split("\n").length;
|
|
3913
|
+
tables.push({ name, line });
|
|
3914
|
+
}
|
|
3915
|
+
if (tables.length === 0) return [];
|
|
3916
|
+
const rlsTables = /* @__PURE__ */ new Set();
|
|
3917
|
+
ENABLE_RLS.lastIndex = 0;
|
|
3918
|
+
while ((match = ENABLE_RLS.exec(content)) !== null) {
|
|
3919
|
+
rlsTables.add(match[1].toLowerCase());
|
|
3920
|
+
}
|
|
3921
|
+
for (const filePath of project.allFiles) {
|
|
3922
|
+
if (!filePath.endsWith(".sql")) continue;
|
|
3923
|
+
if (filePath === file.relativePath) continue;
|
|
3924
|
+
}
|
|
3925
|
+
for (const table of tables) {
|
|
3926
|
+
if (!rlsTables.has(table.name.toLowerCase())) {
|
|
3927
|
+
findings.push({
|
|
3928
|
+
ruleId: "supabase-missing-rls",
|
|
3929
|
+
file: file.relativePath,
|
|
3930
|
+
line: table.line,
|
|
3931
|
+
column: 1,
|
|
3932
|
+
message: `Table "${table.name}" created without ENABLE ROW LEVEL SECURITY \u2014 all rows are publicly accessible via the Supabase API`,
|
|
3933
|
+
severity: "critical",
|
|
3934
|
+
category: "security",
|
|
3935
|
+
fix: `Add: ALTER TABLE ${table.name} ENABLE ROW LEVEL SECURITY; and create appropriate policies`
|
|
3936
|
+
});
|
|
3937
|
+
}
|
|
3938
|
+
}
|
|
3939
|
+
return findings;
|
|
3940
|
+
}
|
|
3941
|
+
};
|
|
3942
|
+
|
|
3943
|
+
// src/rules/deprecated-oauth-flow.ts
|
|
3944
|
+
var IMPLICIT_GRANT = /response_type\s*[=:]\s*['"`]?token['"`]?/;
|
|
3945
|
+
var deprecatedOauthFlowRule = {
|
|
3946
|
+
id: "deprecated-oauth-flow",
|
|
3947
|
+
name: "Deprecated OAuth Flow",
|
|
3948
|
+
description: "Detects OAuth Implicit Grant flow (response_type=token) \u2014 deprecated in OAuth 2.1, vulnerable to token interception",
|
|
3949
|
+
category: "security",
|
|
3950
|
+
severity: "warning",
|
|
3951
|
+
fileExtensions: ["ts", "tsx", "js", "jsx", "mjs", "cjs"],
|
|
3952
|
+
check(file, _project) {
|
|
3953
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3954
|
+
const findings = [];
|
|
3955
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
3956
|
+
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
3957
|
+
const line = file.lines[i];
|
|
3958
|
+
const match = IMPLICIT_GRANT.exec(line);
|
|
3959
|
+
if (match) {
|
|
3960
|
+
findings.push({
|
|
3961
|
+
ruleId: "deprecated-oauth-flow",
|
|
3962
|
+
file: file.relativePath,
|
|
3963
|
+
line: i + 1,
|
|
3964
|
+
column: match.index + 1,
|
|
3965
|
+
message: "OAuth Implicit Grant flow (response_type=token) is deprecated \u2014 tokens are exposed in the URL fragment",
|
|
3966
|
+
severity: "warning",
|
|
3967
|
+
category: "security",
|
|
3968
|
+
fix: "Use Authorization Code flow with PKCE: response_type=code with code_challenge and code_verifier"
|
|
3969
|
+
});
|
|
3970
|
+
}
|
|
3971
|
+
}
|
|
3972
|
+
return findings;
|
|
3973
|
+
}
|
|
3974
|
+
};
|
|
3975
|
+
|
|
3976
|
+
// src/rules/jwt-no-expiry.ts
|
|
3977
|
+
var JWT_SIGN = /jwt\.sign\s*\(/;
|
|
3978
|
+
var HAS_EXPIRY = [
|
|
3979
|
+
/expiresIn/,
|
|
3980
|
+
/exp\s*:/,
|
|
3981
|
+
/expirationTime/,
|
|
3982
|
+
/maxAge/
|
|
3983
|
+
];
|
|
3984
|
+
var EXPIRY_KEYS = /* @__PURE__ */ new Set(["expiresIn", "exp", "expirationTime", "maxAge"]);
|
|
3985
|
+
var jwtNoExpiryRule = {
|
|
3986
|
+
id: "jwt-no-expiry",
|
|
3987
|
+
name: "JWT Without Expiration",
|
|
3988
|
+
description: "Detects jwt.sign() calls without an expiresIn option \u2014 tokens never expire, compromised tokens are valid forever",
|
|
3989
|
+
category: "security",
|
|
3990
|
+
severity: "warning",
|
|
3991
|
+
fileExtensions: ["ts", "tsx", "js", "jsx", "mjs", "cjs"],
|
|
3992
|
+
check(file, _project) {
|
|
3993
|
+
if (isTestFile(file.relativePath)) return [];
|
|
3994
|
+
if (!JWT_SIGN.test(file.content)) return [];
|
|
3995
|
+
const findings = [];
|
|
3996
|
+
if (file.ast) {
|
|
3997
|
+
try {
|
|
3998
|
+
walkAST(file.ast.program, (node) => {
|
|
3999
|
+
if (node.type !== "CallExpression") return;
|
|
4000
|
+
const call = node;
|
|
4001
|
+
if (!call.loc) return;
|
|
4002
|
+
if (call.callee.type !== "MemberExpression") return;
|
|
4003
|
+
const mem = call.callee;
|
|
4004
|
+
if (mem.object.type !== "Identifier" || mem.object.name !== "jwt") return;
|
|
4005
|
+
if (mem.property.type !== "Identifier" || mem.property.name !== "sign") return;
|
|
4006
|
+
let hasExpiry = false;
|
|
4007
|
+
for (const arg of call.arguments) {
|
|
4008
|
+
if (arg.type === "ObjectExpression") {
|
|
4009
|
+
const obj = arg;
|
|
4010
|
+
for (const prop of obj.properties) {
|
|
4011
|
+
if (prop.type === "ObjectProperty" && prop.key.type === "Identifier" && EXPIRY_KEYS.has(prop.key.name)) {
|
|
4012
|
+
hasExpiry = true;
|
|
4013
|
+
break;
|
|
4014
|
+
}
|
|
4015
|
+
}
|
|
4016
|
+
}
|
|
4017
|
+
if (hasExpiry) break;
|
|
4018
|
+
}
|
|
4019
|
+
if (!hasExpiry && call.arguments[0]?.type === "ObjectExpression") {
|
|
4020
|
+
const payload = call.arguments[0];
|
|
4021
|
+
for (const prop of payload.properties) {
|
|
4022
|
+
if (prop.type === "ObjectProperty" && prop.key.type === "Identifier" && prop.key.name === "exp") {
|
|
4023
|
+
hasExpiry = true;
|
|
4024
|
+
break;
|
|
4025
|
+
}
|
|
4026
|
+
}
|
|
4027
|
+
}
|
|
4028
|
+
if (!hasExpiry) {
|
|
4029
|
+
findings.push({
|
|
4030
|
+
ruleId: "jwt-no-expiry",
|
|
4031
|
+
file: file.relativePath,
|
|
4032
|
+
line: call.loc.start.line,
|
|
4033
|
+
column: call.loc.start.column + 1,
|
|
4034
|
+
message: "jwt.sign() without expiresIn \u2014 tokens never expire, a compromised token is valid forever",
|
|
4035
|
+
severity: "warning",
|
|
4036
|
+
category: "security",
|
|
4037
|
+
fix: 'Add expiration: jwt.sign(payload, secret, { expiresIn: "1h" })'
|
|
4038
|
+
});
|
|
4039
|
+
}
|
|
4040
|
+
});
|
|
4041
|
+
return findings;
|
|
4042
|
+
} catch {
|
|
4043
|
+
}
|
|
4044
|
+
}
|
|
4045
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
4046
|
+
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
4047
|
+
const line = file.lines[i];
|
|
4048
|
+
const match = JWT_SIGN.exec(line);
|
|
4049
|
+
if (!match) continue;
|
|
4050
|
+
const context = file.lines.slice(i, i + 6).join("\n");
|
|
4051
|
+
const hasExpiry = HAS_EXPIRY.some((p) => p.test(context));
|
|
4052
|
+
if (!hasExpiry) {
|
|
4053
|
+
findings.push({
|
|
4054
|
+
ruleId: "jwt-no-expiry",
|
|
4055
|
+
file: file.relativePath,
|
|
4056
|
+
line: i + 1,
|
|
4057
|
+
column: match.index + 1,
|
|
4058
|
+
message: "jwt.sign() without expiresIn \u2014 tokens never expire, a compromised token is valid forever",
|
|
4059
|
+
severity: "warning",
|
|
4060
|
+
category: "security",
|
|
4061
|
+
fix: 'Add expiration: jwt.sign(payload, secret, { expiresIn: "1h" })'
|
|
4062
|
+
});
|
|
4063
|
+
}
|
|
4064
|
+
}
|
|
4065
|
+
return findings;
|
|
4066
|
+
}
|
|
4067
|
+
};
|
|
4068
|
+
|
|
4069
|
+
// src/rules/client-side-auth-only.ts
|
|
4070
|
+
var CLIENT_AUTH_PATTERNS = [
|
|
4071
|
+
/localStorage\.(?:get|set)Item\s*\(\s*['"`](?:token|auth|session|jwt|access_token|user)['"`]/,
|
|
4072
|
+
/sessionStorage\.(?:get|set)Item\s*\(\s*['"`](?:token|auth|session|jwt|access_token|user)['"`]/
|
|
4073
|
+
];
|
|
4074
|
+
var PASSWORD_CHECK = /(?:password|passwd)\s*[!=]==?\s*['"`]/;
|
|
4075
|
+
var clientSideAuthOnlyRule = {
|
|
4076
|
+
id: "client-side-auth-only",
|
|
4077
|
+
name: "Client-Side Auth Only",
|
|
4078
|
+
description: "Detects authentication logic implemented only in client-side code \u2014 easily bypassed via browser DevTools",
|
|
4079
|
+
category: "security",
|
|
4080
|
+
severity: "critical",
|
|
4081
|
+
fileExtensions: ["tsx", "jsx", "ts", "js"],
|
|
4082
|
+
check(file, _project) {
|
|
4083
|
+
if (isTestFile(file.relativePath)) return [];
|
|
4084
|
+
if (!isClientComponent(file.content)) return [];
|
|
4085
|
+
const findings = [];
|
|
4086
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
4087
|
+
const line = file.lines[i];
|
|
4088
|
+
const match = PASSWORD_CHECK.exec(line);
|
|
4089
|
+
if (match) {
|
|
4090
|
+
findings.push({
|
|
4091
|
+
ruleId: "client-side-auth-only",
|
|
4092
|
+
file: file.relativePath,
|
|
4093
|
+
line: i + 1,
|
|
4094
|
+
column: match.index + 1,
|
|
4095
|
+
message: "Password comparison in client-side code \u2014 the password is visible in the JavaScript bundle",
|
|
4096
|
+
severity: "critical",
|
|
4097
|
+
category: "security",
|
|
4098
|
+
fix: "Move authentication logic to a server action or API route"
|
|
4099
|
+
});
|
|
4100
|
+
}
|
|
4101
|
+
}
|
|
4102
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
4103
|
+
const line = file.lines[i];
|
|
4104
|
+
for (const pattern of CLIENT_AUTH_PATTERNS) {
|
|
4105
|
+
const match = pattern.exec(line);
|
|
4106
|
+
if (match) {
|
|
4107
|
+
findings.push({
|
|
4108
|
+
ruleId: "client-side-auth-only",
|
|
4109
|
+
file: file.relativePath,
|
|
4110
|
+
line: i + 1,
|
|
4111
|
+
column: match.index + 1,
|
|
4112
|
+
message: "Auth token in localStorage \u2014 accessible to any script on the page (XSS risk). Use httpOnly cookies instead.",
|
|
4113
|
+
severity: "warning",
|
|
4114
|
+
category: "security",
|
|
4115
|
+
fix: "Store auth tokens in httpOnly cookies set by the server, not in localStorage"
|
|
4116
|
+
});
|
|
4117
|
+
break;
|
|
4118
|
+
}
|
|
4119
|
+
}
|
|
4120
|
+
}
|
|
4121
|
+
return findings;
|
|
4122
|
+
}
|
|
4123
|
+
};
|
|
4124
|
+
|
|
4125
|
+
// src/rules/missing-abort-controller.ts
|
|
4126
|
+
var FETCH_CALL = /\bfetch\s*\(/;
|
|
4127
|
+
var HAS_TIMEOUT = [
|
|
4128
|
+
/AbortController/,
|
|
4129
|
+
/abort/i,
|
|
4130
|
+
/signal\s*:/,
|
|
4131
|
+
/timeout/i,
|
|
4132
|
+
/setTimeout.*abort/s
|
|
4133
|
+
];
|
|
4134
|
+
var missingAbortControllerRule = {
|
|
4135
|
+
id: "missing-abort-controller",
|
|
4136
|
+
name: "Missing Abort Controller",
|
|
4137
|
+
description: "Detects fetch calls in API routes without timeout or AbortController \u2014 requests can hang indefinitely",
|
|
4138
|
+
category: "performance",
|
|
4139
|
+
severity: "info",
|
|
4140
|
+
fileExtensions: ["ts", "tsx", "js", "jsx"],
|
|
4141
|
+
check(file, _project) {
|
|
4142
|
+
if (isTestFile(file.relativePath)) return [];
|
|
4143
|
+
if (!isApiRoute(file.relativePath) && !/['"]use server['"]/.test(file.content)) return [];
|
|
4144
|
+
if (!FETCH_CALL.test(file.content)) return [];
|
|
4145
|
+
const hasTimeout = HAS_TIMEOUT.some((p) => p.test(file.content));
|
|
4146
|
+
if (hasTimeout) return [];
|
|
4147
|
+
let reportLine = 1;
|
|
4148
|
+
for (let i = 0; i < file.lines.length; i++) {
|
|
4149
|
+
if (isCommentLine(file.lines, i, file.commentMap)) continue;
|
|
4150
|
+
if (FETCH_CALL.test(file.lines[i])) {
|
|
4151
|
+
reportLine = i + 1;
|
|
4152
|
+
break;
|
|
4153
|
+
}
|
|
4154
|
+
}
|
|
4155
|
+
return [{
|
|
4156
|
+
ruleId: "missing-abort-controller",
|
|
4157
|
+
file: file.relativePath,
|
|
4158
|
+
line: reportLine,
|
|
4159
|
+
column: 1,
|
|
4160
|
+
message: "fetch() without timeout or AbortController \u2014 request will hang indefinitely if the upstream server doesn't respond",
|
|
4161
|
+
severity: "info",
|
|
4162
|
+
category: "performance",
|
|
4163
|
+
fix: "Add a timeout: const controller = new AbortController(); setTimeout(() => controller.abort(), 10000); fetch(url, { signal: controller.signal })"
|
|
4164
|
+
}];
|
|
4165
|
+
}
|
|
4166
|
+
};
|
|
2540
4167
|
|
|
2541
4168
|
// src/rules/index.ts
|
|
2542
4169
|
var rules = [
|
|
@@ -2555,6 +4182,19 @@ var rules = [
|
|
|
2555
4182
|
leakedEnvInLogsRule,
|
|
2556
4183
|
insecureRandomRule,
|
|
2557
4184
|
nextServerActionValidationRule,
|
|
4185
|
+
envFallbackSecretRule,
|
|
4186
|
+
verboseErrorResponseRule,
|
|
4187
|
+
missingWebhookVerificationRule,
|
|
4188
|
+
serverActionAuthRule,
|
|
4189
|
+
evalInjectionRule,
|
|
4190
|
+
nextPublicSensitiveRule,
|
|
4191
|
+
ssrfRiskRule,
|
|
4192
|
+
pathTraversalRule,
|
|
4193
|
+
unsafeFileUploadRule,
|
|
4194
|
+
supabaseMissingRlsRule,
|
|
4195
|
+
deprecatedOauthFlowRule,
|
|
4196
|
+
jwtNoExpiryRule,
|
|
4197
|
+
clientSideAuthOnlyRule,
|
|
2558
4198
|
// Reliability
|
|
2559
4199
|
hallucinatedImportsRule,
|
|
2560
4200
|
errorHandlingRule,
|
|
@@ -2563,11 +4203,17 @@ var rules = [
|
|
|
2563
4203
|
missingLoadingStateRule,
|
|
2564
4204
|
missingErrorBoundaryRule,
|
|
2565
4205
|
missingTransactionRule,
|
|
4206
|
+
redirectInTryCatchRule,
|
|
4207
|
+
missingRevalidationRule,
|
|
4208
|
+
missingUseEffectCleanupRule,
|
|
4209
|
+
hydrationMismatchRule,
|
|
2566
4210
|
// Performance
|
|
2567
4211
|
noSyncFsRule,
|
|
2568
4212
|
noNPlusOneRule,
|
|
2569
4213
|
noUnboundedQueryRule,
|
|
2570
4214
|
noDynamicImportLoopRule,
|
|
4215
|
+
serverComponentFetchSelfRule,
|
|
4216
|
+
missingAbortControllerRule,
|
|
2571
4217
|
// AI Quality
|
|
2572
4218
|
aiSmellsRule,
|
|
2573
4219
|
placeholderContentRule,
|
|
@@ -2575,7 +4221,8 @@ var rules = [
|
|
|
2575
4221
|
staleFallbackRule,
|
|
2576
4222
|
comprehensionDebtRule,
|
|
2577
4223
|
codebaseConsistencyRule,
|
|
2578
|
-
deadExportsRule
|
|
4224
|
+
deadExportsRule,
|
|
4225
|
+
useClientOveruseRule
|
|
2579
4226
|
];
|
|
2580
4227
|
|
|
2581
4228
|
// src/scanner.ts
|
|
@@ -2758,7 +4405,7 @@ async function main() {
|
|
|
2758
4405
|
}
|
|
2759
4406
|
function printHelp() {
|
|
2760
4407
|
console.log(`
|
|
2761
|
-
prodlint -
|
|
4408
|
+
prodlint - The linter for vibe-coded apps
|
|
2762
4409
|
|
|
2763
4410
|
Usage:
|
|
2764
4411
|
npx prodlint [path] [options]
|