xploitscan-shared-rules 1.29.0 → 1.31.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 +325 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +325 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -12296,6 +12296,226 @@ 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
|
+
|
|
12406
|
+
// src/quality/rules/autosave-armed-by-failed-load.ts
|
|
12407
|
+
import _traverse5 from "@babel/traverse";
|
|
12408
|
+
var traverse5 = typeof _traverse5 === "function" ? _traverse5 : _traverse5.default;
|
|
12409
|
+
var PERSISTENT_WRITE = /\bfetch\s*\([^)]{0,400}?method\s*:\s*["'`](?:POST|PUT|PATCH)["'`]|\blocalStorage\s*\.\s*setItem\s*\(|\bsessionStorage\s*\.\s*setItem\s*\(/is;
|
|
12410
|
+
var EMPTY_INITIAL = /\buseState\s*(?:<[^>]*>)?\s*\(\s*(?:\[\s*\]|\{\s*\}|""|''|``)\s*\)/;
|
|
12411
|
+
function calleeIdentifier(node) {
|
|
12412
|
+
if (node.type !== "CallExpression") return null;
|
|
12413
|
+
const c = node.callee;
|
|
12414
|
+
if (c.type === "Identifier") return c.name;
|
|
12415
|
+
if (c.type === "MemberExpression" && c.property.type === "Identifier") return c.property.name;
|
|
12416
|
+
return null;
|
|
12417
|
+
}
|
|
12418
|
+
function isEffectCall2(node) {
|
|
12419
|
+
const name = calleeIdentifier(node);
|
|
12420
|
+
return name === "useEffect" || name === "useLayoutEffect";
|
|
12421
|
+
}
|
|
12422
|
+
function stateFromSetter(setter) {
|
|
12423
|
+
const m = /^set([A-Z][A-Za-z0-9_]*)$/.exec(setter);
|
|
12424
|
+
if (!m) return null;
|
|
12425
|
+
return m[1].charAt(0).toLowerCase() + m[1].slice(1);
|
|
12426
|
+
}
|
|
12427
|
+
function identifiersIn(path) {
|
|
12428
|
+
const names = /* @__PURE__ */ new Set();
|
|
12429
|
+
path.traverse({
|
|
12430
|
+
Identifier(id) {
|
|
12431
|
+
names.add(id.node.name);
|
|
12432
|
+
}
|
|
12433
|
+
});
|
|
12434
|
+
const n = path.node;
|
|
12435
|
+
if (n.type === "Identifier" && n.name) names.add(n.name);
|
|
12436
|
+
return names;
|
|
12437
|
+
}
|
|
12438
|
+
var autosaveArmedByFailedLoad = {
|
|
12439
|
+
id: "QA010",
|
|
12440
|
+
title: "Autosave is armed by a load that failed",
|
|
12441
|
+
severity: "high",
|
|
12442
|
+
category: "Correctness",
|
|
12443
|
+
description: "The flag that permits this component to write the user's state back is set in a `finally`, so a load that failed arms it exactly like one that succeeded \u2014 while the component is still holding the empty values it initialised with. Opening the page during a blip is enough to write those empties over whatever the user had saved, and the write looks like any other.",
|
|
12444
|
+
fix: "Set the arming flag on the success path only, and record the failure in its own state. The component can stay readable and interactive; it just must not write anything back until a load actually succeeds.",
|
|
12445
|
+
precisionNotes: 'Fires on the ARMING, not on a missing guard: the shipped defect had a guard (`if (stateLoaded && !stateLoadFailed) saveState()`), and a rule keyed on unguarded writes would have missed it while reporting every correct autosave. Requires all of: a client file; a `useEffect` whose body reaches a persistent write (fetch with POST/PUT/PATCH, or localStorage/sessionStorage.setItem) either directly or through a same-file callback it names; that write gated on an identifier; a `set<Identifier>` for that same identifier called inside a `finally` block; and at least one `useState` in the file initialised to an empty literal \u2014 `[]`, `{}` or `""`, never `0`, because the empty literal is what turns "wrote too early" into "erased what was there". The join between the finally-set flag and the write\'s guard is the whole narrowing: `finally { setLoading(false) }` is everywhere and correct, and is only reportable when that same flag is what permits the write.',
|
|
12446
|
+
check(ctx) {
|
|
12447
|
+
const hits = [];
|
|
12448
|
+
for (const file of ctx.files) {
|
|
12449
|
+
if (file.excluded) continue;
|
|
12450
|
+
if (!file.isClient) continue;
|
|
12451
|
+
if (file.isServer) continue;
|
|
12452
|
+
const parsed = file.parsed;
|
|
12453
|
+
if (!parsed) continue;
|
|
12454
|
+
if (!EMPTY_INITIAL.test(file.code)) continue;
|
|
12455
|
+
const armedInFinally = /* @__PURE__ */ new Map();
|
|
12456
|
+
traverse5(parsed.ast, {
|
|
12457
|
+
TryStatement(path) {
|
|
12458
|
+
const finalizer = path.get("finalizer");
|
|
12459
|
+
if (!finalizer || Array.isArray(finalizer) || !finalizer.node) return;
|
|
12460
|
+
finalizer.traverse({
|
|
12461
|
+
CallExpression(call) {
|
|
12462
|
+
const name = calleeIdentifier(call.node);
|
|
12463
|
+
if (!name) return;
|
|
12464
|
+
const state = stateFromSetter(name);
|
|
12465
|
+
if (!state) return;
|
|
12466
|
+
armedInFinally.set(state, nodeStart(call.node));
|
|
12467
|
+
}
|
|
12468
|
+
});
|
|
12469
|
+
}
|
|
12470
|
+
});
|
|
12471
|
+
if (armedInFinally.size === 0) continue;
|
|
12472
|
+
const writingFunctions = /* @__PURE__ */ new Set();
|
|
12473
|
+
traverse5(parsed.ast, {
|
|
12474
|
+
Function(path) {
|
|
12475
|
+
const src = file.code.slice(nodeStart(path.node), nodeEnd(path.node));
|
|
12476
|
+
if (!PERSISTENT_WRITE.test(src)) return;
|
|
12477
|
+
let owner = path.parentPath;
|
|
12478
|
+
for (let hops = 0; owner && hops < 3; hops++, owner = owner.parentPath) {
|
|
12479
|
+
const node = owner.node;
|
|
12480
|
+
if (node.type === "VariableDeclarator" && node.id.type === "Identifier") {
|
|
12481
|
+
writingFunctions.add(node.id.name);
|
|
12482
|
+
break;
|
|
12483
|
+
}
|
|
12484
|
+
if (node.type !== "CallExpression") break;
|
|
12485
|
+
}
|
|
12486
|
+
const n = path.node;
|
|
12487
|
+
if (n.id && n.id.name) writingFunctions.add(n.id.name);
|
|
12488
|
+
}
|
|
12489
|
+
});
|
|
12490
|
+
const reported = /* @__PURE__ */ new Set();
|
|
12491
|
+
traverse5(parsed.ast, {
|
|
12492
|
+
CallExpression(path) {
|
|
12493
|
+
if (!isEffectCall2(path.node)) return;
|
|
12494
|
+
const cb = path.get("arguments.0");
|
|
12495
|
+
if (!cb || !cb.node) return;
|
|
12496
|
+
const body = file.code.slice(nodeStart(cb.node), nodeEnd(cb.node));
|
|
12497
|
+
const names = identifiersIn(cb);
|
|
12498
|
+
const writesDirectly = PERSISTENT_WRITE.test(body);
|
|
12499
|
+
const writesViaCallback = [...writingFunctions].some((fn) => names.has(fn));
|
|
12500
|
+
if (!writesDirectly && !writesViaCallback) return;
|
|
12501
|
+
for (const [state, armLine] of armedInFinally) {
|
|
12502
|
+
if (!names.has(state)) continue;
|
|
12503
|
+
if (reported.has(state)) continue;
|
|
12504
|
+
if (!new RegExp(`\\bif\\s*\\([^)]*\\b${state}\\b`).test(body)) continue;
|
|
12505
|
+
reported.add(state);
|
|
12506
|
+
hits.push({
|
|
12507
|
+
file: file.path,
|
|
12508
|
+
line: lineAt(file.content, armLine),
|
|
12509
|
+
detail: `\`${state}\` is what permits this component to write the user's state back, and it is set in a \`finally\` \u2014 so a load that threw arms the write exactly like one that succeeded, while the component still holds its initial empty values.`
|
|
12510
|
+
});
|
|
12511
|
+
}
|
|
12512
|
+
}
|
|
12513
|
+
});
|
|
12514
|
+
}
|
|
12515
|
+
return hits;
|
|
12516
|
+
}
|
|
12517
|
+
};
|
|
12518
|
+
|
|
12299
12519
|
// src/quality/examples.ts
|
|
12300
12520
|
var QUALITY_EXAMPLES = {
|
|
12301
12521
|
QA001: {
|
|
@@ -12320,6 +12540,108 @@ var QUALITY_EXAMPLES = {
|
|
|
12320
12540
|
return new Response(JSON.stringify({ error: "Could not load items" }), { status: 500 });
|
|
12321
12541
|
}
|
|
12322
12542
|
});`
|
|
12543
|
+
},
|
|
12544
|
+
QA009: {
|
|
12545
|
+
path: "app/settings/ApiKeysSection.tsx",
|
|
12546
|
+
language: "tsx",
|
|
12547
|
+
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.",
|
|
12548
|
+
vulnerable: `"use client";
|
|
12549
|
+
|
|
12550
|
+
export function ApiKeysSection() {
|
|
12551
|
+
const [keys, setKeys] = useState([]);
|
|
12552
|
+
|
|
12553
|
+
const load = async () => {
|
|
12554
|
+
try {
|
|
12555
|
+
const res = await fetch("/api/api-keys");
|
|
12556
|
+
const data = await res.json();
|
|
12557
|
+
setKeys(data.keys || []);
|
|
12558
|
+
} catch {
|
|
12559
|
+
/* ignore */
|
|
12560
|
+
}
|
|
12561
|
+
};
|
|
12562
|
+
|
|
12563
|
+
useEffect(() => { load(); }, []);
|
|
12564
|
+
}`,
|
|
12565
|
+
safe: `"use client";
|
|
12566
|
+
|
|
12567
|
+
export function ApiKeysSection() {
|
|
12568
|
+
const [keys, setKeys] = useState([]);
|
|
12569
|
+
const [loadError, setLoadError] = useState(null);
|
|
12570
|
+
|
|
12571
|
+
const load = async () => {
|
|
12572
|
+
const res = await fetch("/api/api-keys");
|
|
12573
|
+
if (!res.ok) {
|
|
12574
|
+
setLoadError("Could not load your API keys. Try again.");
|
|
12575
|
+
return;
|
|
12576
|
+
}
|
|
12577
|
+
const data = await res.json();
|
|
12578
|
+
setKeys(data.keys ?? []);
|
|
12579
|
+
};
|
|
12580
|
+
|
|
12581
|
+
useEffect(() => { load(); }, []);
|
|
12582
|
+
}`
|
|
12583
|
+
},
|
|
12584
|
+
QA010: {
|
|
12585
|
+
path: "app/checklist/page.tsx",
|
|
12586
|
+
language: "tsx",
|
|
12587
|
+
difference: "The arming flag is set on the success path only and the failure is recorded separately, so a load that threw leaves autosave disarmed and the saved state untouched.",
|
|
12588
|
+
vulnerable: `"use client";
|
|
12589
|
+
|
|
12590
|
+
export function Checklist() {
|
|
12591
|
+
const [overrides, setOverrides] = useState({});
|
|
12592
|
+
const [stateLoaded, setStateLoaded] = useState(false);
|
|
12593
|
+
|
|
12594
|
+
useEffect(() => {
|
|
12595
|
+
(async () => {
|
|
12596
|
+
try {
|
|
12597
|
+
const res = await fetch("/api/checklist");
|
|
12598
|
+
const data = await res.json();
|
|
12599
|
+
if (data.state) setOverrides(data.state.overrides);
|
|
12600
|
+
} catch {
|
|
12601
|
+
// Non-critical
|
|
12602
|
+
} finally {
|
|
12603
|
+
setStateLoaded(true);
|
|
12604
|
+
}
|
|
12605
|
+
})();
|
|
12606
|
+
}, []);
|
|
12607
|
+
|
|
12608
|
+
const saveState = useCallback(() => {
|
|
12609
|
+
fetch("/api/checklist", { method: "POST", body: JSON.stringify({ overrides }) });
|
|
12610
|
+
}, [overrides]);
|
|
12611
|
+
|
|
12612
|
+
useEffect(() => {
|
|
12613
|
+
if (stateLoaded) saveState();
|
|
12614
|
+
}, [overrides, stateLoaded, saveState]);
|
|
12615
|
+
}`,
|
|
12616
|
+
safe: `"use client";
|
|
12617
|
+
|
|
12618
|
+
export function Checklist() {
|
|
12619
|
+
const [overrides, setOverrides] = useState({});
|
|
12620
|
+
const [stateLoaded, setStateLoaded] = useState(false);
|
|
12621
|
+
const [stateLoadFailed, setStateLoadFailed] = useState(false);
|
|
12622
|
+
|
|
12623
|
+
useEffect(() => {
|
|
12624
|
+
(async () => {
|
|
12625
|
+
try {
|
|
12626
|
+
const res = await fetch("/api/checklist");
|
|
12627
|
+
if (!res.ok) throw new Error(\`checklist load failed: HTTP \${res.status}\`);
|
|
12628
|
+
const data = await res.json();
|
|
12629
|
+
if (data.state) setOverrides(data.state.overrides);
|
|
12630
|
+
setStateLoaded(true);
|
|
12631
|
+
} catch {
|
|
12632
|
+
setStateLoadFailed(true);
|
|
12633
|
+
}
|
|
12634
|
+
})();
|
|
12635
|
+
}, []);
|
|
12636
|
+
|
|
12637
|
+
const saveState = useCallback(() => {
|
|
12638
|
+
fetch("/api/checklist", { method: "POST", body: JSON.stringify({ overrides }) });
|
|
12639
|
+
}, [overrides]);
|
|
12640
|
+
|
|
12641
|
+
useEffect(() => {
|
|
12642
|
+
if (stateLoaded && !stateLoadFailed) saveState();
|
|
12643
|
+
}, [overrides, stateLoaded, stateLoadFailed, saveState]);
|
|
12644
|
+
}`
|
|
12323
12645
|
},
|
|
12324
12646
|
QA002: {
|
|
12325
12647
|
path: "app/settings/ApiKeyRow.tsx",
|
|
@@ -12527,7 +12849,9 @@ var qualityRules = [
|
|
|
12527
12849
|
mouseOnlyControl,
|
|
12528
12850
|
undeclaredModal,
|
|
12529
12851
|
unboundedList,
|
|
12530
|
-
unassociatedLabel
|
|
12852
|
+
unassociatedLabel,
|
|
12853
|
+
failedReadEmptyState,
|
|
12854
|
+
autosaveArmedByFailedLoad
|
|
12531
12855
|
];
|
|
12532
12856
|
function scanQuality(files, rules = qualityRules) {
|
|
12533
12857
|
const ctx = buildQualityContext(files);
|