xploitscan-shared-rules 1.29.0 → 1.30.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/dist/index.cjs +149 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +149 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -12296,6 +12296,113 @@ var unassociatedLabel = {
|
|
|
12296
12296
|
}
|
|
12297
12297
|
};
|
|
12298
12298
|
|
|
12299
|
+
// src/quality/rules/failed-read-empty-state.ts
|
|
12300
|
+
import _traverse4 from "@babel/traverse";
|
|
12301
|
+
var traverse4 = typeof _traverse4 === "function" ? _traverse4 : _traverse4.default;
|
|
12302
|
+
var READ_ERROR_PATH_SIGNALS = ERROR_PATH_SIGNALS.filter(
|
|
12303
|
+
(re) => re.source !== /\.catch\s*\(/.source
|
|
12304
|
+
);
|
|
12305
|
+
function isEmptyCollectionLiteral(node) {
|
|
12306
|
+
if (node.type === "ArrayExpression") return node.elements.length === 0;
|
|
12307
|
+
if (node.type === "ObjectExpression") return node.properties.length === 0;
|
|
12308
|
+
return false;
|
|
12309
|
+
}
|
|
12310
|
+
var SETTER = /^set[A-Z][A-Za-z0-9_]*$/;
|
|
12311
|
+
function setterName(callee) {
|
|
12312
|
+
if (callee.type === "Identifier" && SETTER.test(callee.name)) return callee.name;
|
|
12313
|
+
return null;
|
|
12314
|
+
}
|
|
12315
|
+
function isPropertyRead(node) {
|
|
12316
|
+
return node.type === "MemberExpression" || node.type === "OptionalMemberExpression";
|
|
12317
|
+
}
|
|
12318
|
+
function emptyFallbackArgument(node) {
|
|
12319
|
+
if (node.type !== "LogicalExpression") return null;
|
|
12320
|
+
if (node.operator !== "||" && node.operator !== "??") return null;
|
|
12321
|
+
if (!isEmptyCollectionLiteral(node.right)) return null;
|
|
12322
|
+
if (!isPropertyRead(node.left)) return null;
|
|
12323
|
+
return node;
|
|
12324
|
+
}
|
|
12325
|
+
function isEffectCall(node) {
|
|
12326
|
+
if (node.type !== "CallExpression") return false;
|
|
12327
|
+
const callee = node.callee;
|
|
12328
|
+
const name = callee.type === "Identifier" ? callee.name : callee.type === "MemberExpression" && callee.property.type === "Identifier" ? callee.property.name : null;
|
|
12329
|
+
return name === "useEffect" || name === "useLayoutEffect";
|
|
12330
|
+
}
|
|
12331
|
+
function looksLikeComponent(path) {
|
|
12332
|
+
const parent = path.parent;
|
|
12333
|
+
if (parent.type === "VariableDeclarator" && parent.id.type === "Identifier") {
|
|
12334
|
+
return /^[A-Z]/.test(parent.id.name);
|
|
12335
|
+
}
|
|
12336
|
+
const node = path.node;
|
|
12337
|
+
if ((node.type === "FunctionDeclaration" || node.type === "FunctionExpression") && node.id && /^[A-Z]/.test(node.id.name)) {
|
|
12338
|
+
return true;
|
|
12339
|
+
}
|
|
12340
|
+
return false;
|
|
12341
|
+
}
|
|
12342
|
+
function unitFor(fetchPath) {
|
|
12343
|
+
let candidate = null;
|
|
12344
|
+
for (let fn = fetchPath.getFunctionParent(); fn !== null; fn = fn.getFunctionParent()) {
|
|
12345
|
+
if (looksLikeComponent(fn)) break;
|
|
12346
|
+
candidate = fn;
|
|
12347
|
+
if (fn.parent && isEffectCall(fn.parent)) return fn;
|
|
12348
|
+
}
|
|
12349
|
+
return candidate;
|
|
12350
|
+
}
|
|
12351
|
+
var failedReadEmptyState = {
|
|
12352
|
+
id: "QA009",
|
|
12353
|
+
title: "A failed read is rendered as an empty state",
|
|
12354
|
+
severity: "high",
|
|
12355
|
+
category: "Correctness",
|
|
12356
|
+
description: "This loader turns a failed or unexpected response into an empty list and renders it, so the page shows the same thing it would show a brand-new account: no sessions, no scans, no members. Nothing tells the user the request failed, and anything they do next \u2014 including saving \u2014 acts on data the app never actually received.",
|
|
12357
|
+
fix: "Check the response before using it: on a non-2xx, or a body without the field you expected, set an error state and render that instead of the empty state. Keep the `|| []` only for a response you have already confirmed arrived.",
|
|
12358
|
+
precisionNotes: "Client files only, and only a unit containing the literal global `fetch(` \u2014 throwing clients (ky, wretch, ofetch) reject on non-2xx, so `|| []` after them is correct, and in-file wrappers that check `.ok` and rethrow leave their callers containing no `fetch(` at all. The empty fallback must be `|| []`/`?? []`/`{}` over a PROPERTY READ passed straight to a `setX` state setter: `setRows(rows || [])` defaults a value the component already holds, and a pure helper's argument is never rendered. The unit is the effect callback, or the outermost non-component function \u2014 the nearest function cannot see the `.catch` below it, and the whole component would let an unrelated toast.error exonerate a broken loader. Exoneration is QA002's ERROR_PATH_SIGNALS with `.catch(` REMOVED: for a write a `.catch` means the author handled it, for a read `.catch(() => setLoaded(true))` is the defect itself. Measured at 6 fires over 5 sites in 437 real .tsx files with zero false positives, and silent on all three sites after they were fixed by hand.",
|
|
12359
|
+
check(ctx) {
|
|
12360
|
+
const hits = [];
|
|
12361
|
+
for (const file of ctx.files) {
|
|
12362
|
+
if (file.excluded) continue;
|
|
12363
|
+
if (!file.isClient) continue;
|
|
12364
|
+
if (file.isServer) continue;
|
|
12365
|
+
if (/^\s*["']use server["']/m.test(file.code)) continue;
|
|
12366
|
+
const parsed = file.parsed;
|
|
12367
|
+
if (!parsed) continue;
|
|
12368
|
+
const reported = /* @__PURE__ */ new Set();
|
|
12369
|
+
traverse4(parsed.ast, {
|
|
12370
|
+
CallExpression(path) {
|
|
12371
|
+
if (!isFetchCallee(path.node.callee)) return;
|
|
12372
|
+
const unit = unitFor(path);
|
|
12373
|
+
if (!unit) return;
|
|
12374
|
+
const start = nodeStart(unit.node);
|
|
12375
|
+
if (reported.has(start)) return;
|
|
12376
|
+
const unitSource = file.code.slice(start, nodeEnd(unit.node));
|
|
12377
|
+
if (READ_ERROR_PATH_SIGNALS.some((re) => re.test(unitSource))) return;
|
|
12378
|
+
let hit = null;
|
|
12379
|
+
unit.traverse({
|
|
12380
|
+
CallExpression(inner) {
|
|
12381
|
+
if (hit) return;
|
|
12382
|
+
const name = setterName(inner.node.callee);
|
|
12383
|
+
if (!name) return;
|
|
12384
|
+
const arg = inner.node.arguments[0];
|
|
12385
|
+
if (!arg) return;
|
|
12386
|
+
const fallback = emptyFallbackArgument(arg);
|
|
12387
|
+
if (!fallback) return;
|
|
12388
|
+
hit = { line: lineAt(file.content, nodeStart(fallback)), setter: name };
|
|
12389
|
+
}
|
|
12390
|
+
});
|
|
12391
|
+
if (!hit) return;
|
|
12392
|
+
reported.add(start);
|
|
12393
|
+
const { line, setter } = hit;
|
|
12394
|
+
hits.push({
|
|
12395
|
+
file: file.path,
|
|
12396
|
+
line,
|
|
12397
|
+
detail: `\`${setter}\` is handed an empty collection whenever this read fails or the body is missing that field, and nothing in this loader tells the user the request did not succeed. The page renders its empty state \u2014 the same screen a brand-new account sees.`
|
|
12398
|
+
});
|
|
12399
|
+
}
|
|
12400
|
+
});
|
|
12401
|
+
}
|
|
12402
|
+
return hits;
|
|
12403
|
+
}
|
|
12404
|
+
};
|
|
12405
|
+
|
|
12299
12406
|
// src/quality/examples.ts
|
|
12300
12407
|
var QUALITY_EXAMPLES = {
|
|
12301
12408
|
QA001: {
|
|
@@ -12320,6 +12427,46 @@ var QUALITY_EXAMPLES = {
|
|
|
12320
12427
|
return new Response(JSON.stringify({ error: "Could not load items" }), { status: 500 });
|
|
12321
12428
|
}
|
|
12322
12429
|
});`
|
|
12430
|
+
},
|
|
12431
|
+
QA009: {
|
|
12432
|
+
path: "app/settings/ApiKeysSection.tsx",
|
|
12433
|
+
language: "tsx",
|
|
12434
|
+
difference: "The loader checks the response and sets an error the page can render, so a failed read no longer looks like an account with no keys.",
|
|
12435
|
+
vulnerable: `"use client";
|
|
12436
|
+
|
|
12437
|
+
export function ApiKeysSection() {
|
|
12438
|
+
const [keys, setKeys] = useState([]);
|
|
12439
|
+
|
|
12440
|
+
const load = async () => {
|
|
12441
|
+
try {
|
|
12442
|
+
const res = await fetch("/api/api-keys");
|
|
12443
|
+
const data = await res.json();
|
|
12444
|
+
setKeys(data.keys || []);
|
|
12445
|
+
} catch {
|
|
12446
|
+
/* ignore */
|
|
12447
|
+
}
|
|
12448
|
+
};
|
|
12449
|
+
|
|
12450
|
+
useEffect(() => { load(); }, []);
|
|
12451
|
+
}`,
|
|
12452
|
+
safe: `"use client";
|
|
12453
|
+
|
|
12454
|
+
export function ApiKeysSection() {
|
|
12455
|
+
const [keys, setKeys] = useState([]);
|
|
12456
|
+
const [loadError, setLoadError] = useState(null);
|
|
12457
|
+
|
|
12458
|
+
const load = async () => {
|
|
12459
|
+
const res = await fetch("/api/api-keys");
|
|
12460
|
+
if (!res.ok) {
|
|
12461
|
+
setLoadError("Could not load your API keys. Try again.");
|
|
12462
|
+
return;
|
|
12463
|
+
}
|
|
12464
|
+
const data = await res.json();
|
|
12465
|
+
setKeys(data.keys ?? []);
|
|
12466
|
+
};
|
|
12467
|
+
|
|
12468
|
+
useEffect(() => { load(); }, []);
|
|
12469
|
+
}`
|
|
12323
12470
|
},
|
|
12324
12471
|
QA002: {
|
|
12325
12472
|
path: "app/settings/ApiKeyRow.tsx",
|
|
@@ -12527,7 +12674,8 @@ var qualityRules = [
|
|
|
12527
12674
|
mouseOnlyControl,
|
|
12528
12675
|
undeclaredModal,
|
|
12529
12676
|
unboundedList,
|
|
12530
|
-
unassociatedLabel
|
|
12677
|
+
unassociatedLabel,
|
|
12678
|
+
failedReadEmptyState
|
|
12531
12679
|
];
|
|
12532
12680
|
function scanQuality(files, rules = qualityRules) {
|
|
12533
12681
|
const ctx = buildQualityContext(files);
|