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.cjs
CHANGED
|
@@ -12602,6 +12602,226 @@ 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
|
+
|
|
12712
|
+
// src/quality/rules/autosave-armed-by-failed-load.ts
|
|
12713
|
+
var import_traverse6 = __toESM(require("@babel/traverse"), 1);
|
|
12714
|
+
var traverse5 = typeof import_traverse6.default === "function" ? import_traverse6.default : import_traverse6.default.default;
|
|
12715
|
+
var PERSISTENT_WRITE = /\bfetch\s*\([^)]{0,400}?method\s*:\s*["'`](?:POST|PUT|PATCH)["'`]|\blocalStorage\s*\.\s*setItem\s*\(|\bsessionStorage\s*\.\s*setItem\s*\(/is;
|
|
12716
|
+
var EMPTY_INITIAL = /\buseState\s*(?:<[^>]*>)?\s*\(\s*(?:\[\s*\]|\{\s*\}|""|''|``)\s*\)/;
|
|
12717
|
+
function calleeIdentifier(node) {
|
|
12718
|
+
if (node.type !== "CallExpression") return null;
|
|
12719
|
+
const c = node.callee;
|
|
12720
|
+
if (c.type === "Identifier") return c.name;
|
|
12721
|
+
if (c.type === "MemberExpression" && c.property.type === "Identifier") return c.property.name;
|
|
12722
|
+
return null;
|
|
12723
|
+
}
|
|
12724
|
+
function isEffectCall2(node) {
|
|
12725
|
+
const name = calleeIdentifier(node);
|
|
12726
|
+
return name === "useEffect" || name === "useLayoutEffect";
|
|
12727
|
+
}
|
|
12728
|
+
function stateFromSetter(setter) {
|
|
12729
|
+
const m = /^set([A-Z][A-Za-z0-9_]*)$/.exec(setter);
|
|
12730
|
+
if (!m) return null;
|
|
12731
|
+
return m[1].charAt(0).toLowerCase() + m[1].slice(1);
|
|
12732
|
+
}
|
|
12733
|
+
function identifiersIn(path) {
|
|
12734
|
+
const names = /* @__PURE__ */ new Set();
|
|
12735
|
+
path.traverse({
|
|
12736
|
+
Identifier(id) {
|
|
12737
|
+
names.add(id.node.name);
|
|
12738
|
+
}
|
|
12739
|
+
});
|
|
12740
|
+
const n = path.node;
|
|
12741
|
+
if (n.type === "Identifier" && n.name) names.add(n.name);
|
|
12742
|
+
return names;
|
|
12743
|
+
}
|
|
12744
|
+
var autosaveArmedByFailedLoad = {
|
|
12745
|
+
id: "QA010",
|
|
12746
|
+
title: "Autosave is armed by a load that failed",
|
|
12747
|
+
severity: "high",
|
|
12748
|
+
category: "Correctness",
|
|
12749
|
+
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.",
|
|
12750
|
+
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.",
|
|
12751
|
+
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.',
|
|
12752
|
+
check(ctx) {
|
|
12753
|
+
const hits = [];
|
|
12754
|
+
for (const file of ctx.files) {
|
|
12755
|
+
if (file.excluded) continue;
|
|
12756
|
+
if (!file.isClient) continue;
|
|
12757
|
+
if (file.isServer) continue;
|
|
12758
|
+
const parsed = file.parsed;
|
|
12759
|
+
if (!parsed) continue;
|
|
12760
|
+
if (!EMPTY_INITIAL.test(file.code)) continue;
|
|
12761
|
+
const armedInFinally = /* @__PURE__ */ new Map();
|
|
12762
|
+
traverse5(parsed.ast, {
|
|
12763
|
+
TryStatement(path) {
|
|
12764
|
+
const finalizer = path.get("finalizer");
|
|
12765
|
+
if (!finalizer || Array.isArray(finalizer) || !finalizer.node) return;
|
|
12766
|
+
finalizer.traverse({
|
|
12767
|
+
CallExpression(call) {
|
|
12768
|
+
const name = calleeIdentifier(call.node);
|
|
12769
|
+
if (!name) return;
|
|
12770
|
+
const state = stateFromSetter(name);
|
|
12771
|
+
if (!state) return;
|
|
12772
|
+
armedInFinally.set(state, nodeStart(call.node));
|
|
12773
|
+
}
|
|
12774
|
+
});
|
|
12775
|
+
}
|
|
12776
|
+
});
|
|
12777
|
+
if (armedInFinally.size === 0) continue;
|
|
12778
|
+
const writingFunctions = /* @__PURE__ */ new Set();
|
|
12779
|
+
traverse5(parsed.ast, {
|
|
12780
|
+
Function(path) {
|
|
12781
|
+
const src = file.code.slice(nodeStart(path.node), nodeEnd(path.node));
|
|
12782
|
+
if (!PERSISTENT_WRITE.test(src)) return;
|
|
12783
|
+
let owner = path.parentPath;
|
|
12784
|
+
for (let hops = 0; owner && hops < 3; hops++, owner = owner.parentPath) {
|
|
12785
|
+
const node = owner.node;
|
|
12786
|
+
if (node.type === "VariableDeclarator" && node.id.type === "Identifier") {
|
|
12787
|
+
writingFunctions.add(node.id.name);
|
|
12788
|
+
break;
|
|
12789
|
+
}
|
|
12790
|
+
if (node.type !== "CallExpression") break;
|
|
12791
|
+
}
|
|
12792
|
+
const n = path.node;
|
|
12793
|
+
if (n.id && n.id.name) writingFunctions.add(n.id.name);
|
|
12794
|
+
}
|
|
12795
|
+
});
|
|
12796
|
+
const reported = /* @__PURE__ */ new Set();
|
|
12797
|
+
traverse5(parsed.ast, {
|
|
12798
|
+
CallExpression(path) {
|
|
12799
|
+
if (!isEffectCall2(path.node)) return;
|
|
12800
|
+
const cb = path.get("arguments.0");
|
|
12801
|
+
if (!cb || !cb.node) return;
|
|
12802
|
+
const body = file.code.slice(nodeStart(cb.node), nodeEnd(cb.node));
|
|
12803
|
+
const names = identifiersIn(cb);
|
|
12804
|
+
const writesDirectly = PERSISTENT_WRITE.test(body);
|
|
12805
|
+
const writesViaCallback = [...writingFunctions].some((fn) => names.has(fn));
|
|
12806
|
+
if (!writesDirectly && !writesViaCallback) return;
|
|
12807
|
+
for (const [state, armLine] of armedInFinally) {
|
|
12808
|
+
if (!names.has(state)) continue;
|
|
12809
|
+
if (reported.has(state)) continue;
|
|
12810
|
+
if (!new RegExp(`\\bif\\s*\\([^)]*\\b${state}\\b`).test(body)) continue;
|
|
12811
|
+
reported.add(state);
|
|
12812
|
+
hits.push({
|
|
12813
|
+
file: file.path,
|
|
12814
|
+
line: lineAt(file.content, armLine),
|
|
12815
|
+
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.`
|
|
12816
|
+
});
|
|
12817
|
+
}
|
|
12818
|
+
}
|
|
12819
|
+
});
|
|
12820
|
+
}
|
|
12821
|
+
return hits;
|
|
12822
|
+
}
|
|
12823
|
+
};
|
|
12824
|
+
|
|
12605
12825
|
// src/quality/examples.ts
|
|
12606
12826
|
var QUALITY_EXAMPLES = {
|
|
12607
12827
|
QA001: {
|
|
@@ -12626,6 +12846,108 @@ var QUALITY_EXAMPLES = {
|
|
|
12626
12846
|
return new Response(JSON.stringify({ error: "Could not load items" }), { status: 500 });
|
|
12627
12847
|
}
|
|
12628
12848
|
});`
|
|
12849
|
+
},
|
|
12850
|
+
QA009: {
|
|
12851
|
+
path: "app/settings/ApiKeysSection.tsx",
|
|
12852
|
+
language: "tsx",
|
|
12853
|
+
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.",
|
|
12854
|
+
vulnerable: `"use client";
|
|
12855
|
+
|
|
12856
|
+
export function ApiKeysSection() {
|
|
12857
|
+
const [keys, setKeys] = useState([]);
|
|
12858
|
+
|
|
12859
|
+
const load = async () => {
|
|
12860
|
+
try {
|
|
12861
|
+
const res = await fetch("/api/api-keys");
|
|
12862
|
+
const data = await res.json();
|
|
12863
|
+
setKeys(data.keys || []);
|
|
12864
|
+
} catch {
|
|
12865
|
+
/* ignore */
|
|
12866
|
+
}
|
|
12867
|
+
};
|
|
12868
|
+
|
|
12869
|
+
useEffect(() => { load(); }, []);
|
|
12870
|
+
}`,
|
|
12871
|
+
safe: `"use client";
|
|
12872
|
+
|
|
12873
|
+
export function ApiKeysSection() {
|
|
12874
|
+
const [keys, setKeys] = useState([]);
|
|
12875
|
+
const [loadError, setLoadError] = useState(null);
|
|
12876
|
+
|
|
12877
|
+
const load = async () => {
|
|
12878
|
+
const res = await fetch("/api/api-keys");
|
|
12879
|
+
if (!res.ok) {
|
|
12880
|
+
setLoadError("Could not load your API keys. Try again.");
|
|
12881
|
+
return;
|
|
12882
|
+
}
|
|
12883
|
+
const data = await res.json();
|
|
12884
|
+
setKeys(data.keys ?? []);
|
|
12885
|
+
};
|
|
12886
|
+
|
|
12887
|
+
useEffect(() => { load(); }, []);
|
|
12888
|
+
}`
|
|
12889
|
+
},
|
|
12890
|
+
QA010: {
|
|
12891
|
+
path: "app/checklist/page.tsx",
|
|
12892
|
+
language: "tsx",
|
|
12893
|
+
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.",
|
|
12894
|
+
vulnerable: `"use client";
|
|
12895
|
+
|
|
12896
|
+
export function Checklist() {
|
|
12897
|
+
const [overrides, setOverrides] = useState({});
|
|
12898
|
+
const [stateLoaded, setStateLoaded] = useState(false);
|
|
12899
|
+
|
|
12900
|
+
useEffect(() => {
|
|
12901
|
+
(async () => {
|
|
12902
|
+
try {
|
|
12903
|
+
const res = await fetch("/api/checklist");
|
|
12904
|
+
const data = await res.json();
|
|
12905
|
+
if (data.state) setOverrides(data.state.overrides);
|
|
12906
|
+
} catch {
|
|
12907
|
+
// Non-critical
|
|
12908
|
+
} finally {
|
|
12909
|
+
setStateLoaded(true);
|
|
12910
|
+
}
|
|
12911
|
+
})();
|
|
12912
|
+
}, []);
|
|
12913
|
+
|
|
12914
|
+
const saveState = useCallback(() => {
|
|
12915
|
+
fetch("/api/checklist", { method: "POST", body: JSON.stringify({ overrides }) });
|
|
12916
|
+
}, [overrides]);
|
|
12917
|
+
|
|
12918
|
+
useEffect(() => {
|
|
12919
|
+
if (stateLoaded) saveState();
|
|
12920
|
+
}, [overrides, stateLoaded, saveState]);
|
|
12921
|
+
}`,
|
|
12922
|
+
safe: `"use client";
|
|
12923
|
+
|
|
12924
|
+
export function Checklist() {
|
|
12925
|
+
const [overrides, setOverrides] = useState({});
|
|
12926
|
+
const [stateLoaded, setStateLoaded] = useState(false);
|
|
12927
|
+
const [stateLoadFailed, setStateLoadFailed] = useState(false);
|
|
12928
|
+
|
|
12929
|
+
useEffect(() => {
|
|
12930
|
+
(async () => {
|
|
12931
|
+
try {
|
|
12932
|
+
const res = await fetch("/api/checklist");
|
|
12933
|
+
if (!res.ok) throw new Error(\`checklist load failed: HTTP \${res.status}\`);
|
|
12934
|
+
const data = await res.json();
|
|
12935
|
+
if (data.state) setOverrides(data.state.overrides);
|
|
12936
|
+
setStateLoaded(true);
|
|
12937
|
+
} catch {
|
|
12938
|
+
setStateLoadFailed(true);
|
|
12939
|
+
}
|
|
12940
|
+
})();
|
|
12941
|
+
}, []);
|
|
12942
|
+
|
|
12943
|
+
const saveState = useCallback(() => {
|
|
12944
|
+
fetch("/api/checklist", { method: "POST", body: JSON.stringify({ overrides }) });
|
|
12945
|
+
}, [overrides]);
|
|
12946
|
+
|
|
12947
|
+
useEffect(() => {
|
|
12948
|
+
if (stateLoaded && !stateLoadFailed) saveState();
|
|
12949
|
+
}, [overrides, stateLoaded, stateLoadFailed, saveState]);
|
|
12950
|
+
}`
|
|
12629
12951
|
},
|
|
12630
12952
|
QA002: {
|
|
12631
12953
|
path: "app/settings/ApiKeyRow.tsx",
|
|
@@ -12833,7 +13155,9 @@ var qualityRules = [
|
|
|
12833
13155
|
mouseOnlyControl,
|
|
12834
13156
|
undeclaredModal,
|
|
12835
13157
|
unboundedList,
|
|
12836
|
-
unassociatedLabel
|
|
13158
|
+
unassociatedLabel,
|
|
13159
|
+
failedReadEmptyState,
|
|
13160
|
+
autosaveArmedByFailedLoad
|
|
12837
13161
|
];
|
|
12838
13162
|
function scanQuality(files, rules = qualityRules) {
|
|
12839
13163
|
const ctx = buildQualityContext(files);
|