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.cjs
CHANGED
|
@@ -12602,6 +12602,113 @@ var unassociatedLabel = {
|
|
|
12602
12602
|
}
|
|
12603
12603
|
};
|
|
12604
12604
|
|
|
12605
|
+
// src/quality/rules/failed-read-empty-state.ts
|
|
12606
|
+
var import_traverse5 = __toESM(require("@babel/traverse"), 1);
|
|
12607
|
+
var traverse4 = typeof import_traverse5.default === "function" ? import_traverse5.default : import_traverse5.default.default;
|
|
12608
|
+
var READ_ERROR_PATH_SIGNALS = ERROR_PATH_SIGNALS.filter(
|
|
12609
|
+
(re) => re.source !== /\.catch\s*\(/.source
|
|
12610
|
+
);
|
|
12611
|
+
function isEmptyCollectionLiteral(node) {
|
|
12612
|
+
if (node.type === "ArrayExpression") return node.elements.length === 0;
|
|
12613
|
+
if (node.type === "ObjectExpression") return node.properties.length === 0;
|
|
12614
|
+
return false;
|
|
12615
|
+
}
|
|
12616
|
+
var SETTER = /^set[A-Z][A-Za-z0-9_]*$/;
|
|
12617
|
+
function setterName(callee) {
|
|
12618
|
+
if (callee.type === "Identifier" && SETTER.test(callee.name)) return callee.name;
|
|
12619
|
+
return null;
|
|
12620
|
+
}
|
|
12621
|
+
function isPropertyRead(node) {
|
|
12622
|
+
return node.type === "MemberExpression" || node.type === "OptionalMemberExpression";
|
|
12623
|
+
}
|
|
12624
|
+
function emptyFallbackArgument(node) {
|
|
12625
|
+
if (node.type !== "LogicalExpression") return null;
|
|
12626
|
+
if (node.operator !== "||" && node.operator !== "??") return null;
|
|
12627
|
+
if (!isEmptyCollectionLiteral(node.right)) return null;
|
|
12628
|
+
if (!isPropertyRead(node.left)) return null;
|
|
12629
|
+
return node;
|
|
12630
|
+
}
|
|
12631
|
+
function isEffectCall(node) {
|
|
12632
|
+
if (node.type !== "CallExpression") return false;
|
|
12633
|
+
const callee = node.callee;
|
|
12634
|
+
const name = callee.type === "Identifier" ? callee.name : callee.type === "MemberExpression" && callee.property.type === "Identifier" ? callee.property.name : null;
|
|
12635
|
+
return name === "useEffect" || name === "useLayoutEffect";
|
|
12636
|
+
}
|
|
12637
|
+
function looksLikeComponent(path) {
|
|
12638
|
+
const parent = path.parent;
|
|
12639
|
+
if (parent.type === "VariableDeclarator" && parent.id.type === "Identifier") {
|
|
12640
|
+
return /^[A-Z]/.test(parent.id.name);
|
|
12641
|
+
}
|
|
12642
|
+
const node = path.node;
|
|
12643
|
+
if ((node.type === "FunctionDeclaration" || node.type === "FunctionExpression") && node.id && /^[A-Z]/.test(node.id.name)) {
|
|
12644
|
+
return true;
|
|
12645
|
+
}
|
|
12646
|
+
return false;
|
|
12647
|
+
}
|
|
12648
|
+
function unitFor(fetchPath) {
|
|
12649
|
+
let candidate = null;
|
|
12650
|
+
for (let fn = fetchPath.getFunctionParent(); fn !== null; fn = fn.getFunctionParent()) {
|
|
12651
|
+
if (looksLikeComponent(fn)) break;
|
|
12652
|
+
candidate = fn;
|
|
12653
|
+
if (fn.parent && isEffectCall(fn.parent)) return fn;
|
|
12654
|
+
}
|
|
12655
|
+
return candidate;
|
|
12656
|
+
}
|
|
12657
|
+
var failedReadEmptyState = {
|
|
12658
|
+
id: "QA009",
|
|
12659
|
+
title: "A failed read is rendered as an empty state",
|
|
12660
|
+
severity: "high",
|
|
12661
|
+
category: "Correctness",
|
|
12662
|
+
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.",
|
|
12663
|
+
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.",
|
|
12664
|
+
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.",
|
|
12665
|
+
check(ctx) {
|
|
12666
|
+
const hits = [];
|
|
12667
|
+
for (const file of ctx.files) {
|
|
12668
|
+
if (file.excluded) continue;
|
|
12669
|
+
if (!file.isClient) continue;
|
|
12670
|
+
if (file.isServer) continue;
|
|
12671
|
+
if (/^\s*["']use server["']/m.test(file.code)) continue;
|
|
12672
|
+
const parsed = file.parsed;
|
|
12673
|
+
if (!parsed) continue;
|
|
12674
|
+
const reported = /* @__PURE__ */ new Set();
|
|
12675
|
+
traverse4(parsed.ast, {
|
|
12676
|
+
CallExpression(path) {
|
|
12677
|
+
if (!isFetchCallee(path.node.callee)) return;
|
|
12678
|
+
const unit = unitFor(path);
|
|
12679
|
+
if (!unit) return;
|
|
12680
|
+
const start = nodeStart(unit.node);
|
|
12681
|
+
if (reported.has(start)) return;
|
|
12682
|
+
const unitSource = file.code.slice(start, nodeEnd(unit.node));
|
|
12683
|
+
if (READ_ERROR_PATH_SIGNALS.some((re) => re.test(unitSource))) return;
|
|
12684
|
+
let hit = null;
|
|
12685
|
+
unit.traverse({
|
|
12686
|
+
CallExpression(inner) {
|
|
12687
|
+
if (hit) return;
|
|
12688
|
+
const name = setterName(inner.node.callee);
|
|
12689
|
+
if (!name) return;
|
|
12690
|
+
const arg = inner.node.arguments[0];
|
|
12691
|
+
if (!arg) return;
|
|
12692
|
+
const fallback = emptyFallbackArgument(arg);
|
|
12693
|
+
if (!fallback) return;
|
|
12694
|
+
hit = { line: lineAt(file.content, nodeStart(fallback)), setter: name };
|
|
12695
|
+
}
|
|
12696
|
+
});
|
|
12697
|
+
if (!hit) return;
|
|
12698
|
+
reported.add(start);
|
|
12699
|
+
const { line, setter } = hit;
|
|
12700
|
+
hits.push({
|
|
12701
|
+
file: file.path,
|
|
12702
|
+
line,
|
|
12703
|
+
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.`
|
|
12704
|
+
});
|
|
12705
|
+
}
|
|
12706
|
+
});
|
|
12707
|
+
}
|
|
12708
|
+
return hits;
|
|
12709
|
+
}
|
|
12710
|
+
};
|
|
12711
|
+
|
|
12605
12712
|
// src/quality/examples.ts
|
|
12606
12713
|
var QUALITY_EXAMPLES = {
|
|
12607
12714
|
QA001: {
|
|
@@ -12626,6 +12733,46 @@ var QUALITY_EXAMPLES = {
|
|
|
12626
12733
|
return new Response(JSON.stringify({ error: "Could not load items" }), { status: 500 });
|
|
12627
12734
|
}
|
|
12628
12735
|
});`
|
|
12736
|
+
},
|
|
12737
|
+
QA009: {
|
|
12738
|
+
path: "app/settings/ApiKeysSection.tsx",
|
|
12739
|
+
language: "tsx",
|
|
12740
|
+
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.",
|
|
12741
|
+
vulnerable: `"use client";
|
|
12742
|
+
|
|
12743
|
+
export function ApiKeysSection() {
|
|
12744
|
+
const [keys, setKeys] = useState([]);
|
|
12745
|
+
|
|
12746
|
+
const load = async () => {
|
|
12747
|
+
try {
|
|
12748
|
+
const res = await fetch("/api/api-keys");
|
|
12749
|
+
const data = await res.json();
|
|
12750
|
+
setKeys(data.keys || []);
|
|
12751
|
+
} catch {
|
|
12752
|
+
/* ignore */
|
|
12753
|
+
}
|
|
12754
|
+
};
|
|
12755
|
+
|
|
12756
|
+
useEffect(() => { load(); }, []);
|
|
12757
|
+
}`,
|
|
12758
|
+
safe: `"use client";
|
|
12759
|
+
|
|
12760
|
+
export function ApiKeysSection() {
|
|
12761
|
+
const [keys, setKeys] = useState([]);
|
|
12762
|
+
const [loadError, setLoadError] = useState(null);
|
|
12763
|
+
|
|
12764
|
+
const load = async () => {
|
|
12765
|
+
const res = await fetch("/api/api-keys");
|
|
12766
|
+
if (!res.ok) {
|
|
12767
|
+
setLoadError("Could not load your API keys. Try again.");
|
|
12768
|
+
return;
|
|
12769
|
+
}
|
|
12770
|
+
const data = await res.json();
|
|
12771
|
+
setKeys(data.keys ?? []);
|
|
12772
|
+
};
|
|
12773
|
+
|
|
12774
|
+
useEffect(() => { load(); }, []);
|
|
12775
|
+
}`
|
|
12629
12776
|
},
|
|
12630
12777
|
QA002: {
|
|
12631
12778
|
path: "app/settings/ApiKeyRow.tsx",
|
|
@@ -12833,7 +12980,8 @@ var qualityRules = [
|
|
|
12833
12980
|
mouseOnlyControl,
|
|
12834
12981
|
undeclaredModal,
|
|
12835
12982
|
unboundedList,
|
|
12836
|
-
unassociatedLabel
|
|
12983
|
+
unassociatedLabel,
|
|
12984
|
+
failedReadEmptyState
|
|
12837
12985
|
];
|
|
12838
12986
|
function scanQuality(files, rules = qualityRules) {
|
|
12839
12987
|
const ctx = buildQualityContext(files);
|