xploitscan-shared-rules 1.35.0 → 1.35.1

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 CHANGED
@@ -51,6 +51,7 @@ __export(index_exports, {
51
51
  assessHeaders: () => assessHeaders,
52
52
  blockingMainThread: () => blockingMainThread,
53
53
  buildProjectContext: () => buildProjectContext,
54
+ buildQualityContext: () => buildQualityContext,
54
55
  buildTaintMap: () => buildTaintMap,
55
56
  calculateGrade: () => calculateGrade,
56
57
  callSpreads: () => callSpreads,
@@ -12643,6 +12644,61 @@ function setterName(callee) {
12643
12644
  function isPropertyRead(node) {
12644
12645
  return node.type === "MemberExpression" || node.type === "OptionalMemberExpression";
12645
12646
  }
12647
+ function parsedBodyBindings(unit, code) {
12648
+ const names = /* @__PURE__ */ new Set();
12649
+ unit.traverse({
12650
+ VariableDeclarator(decl) {
12651
+ const init = decl.node.init;
12652
+ if (!init || init.type !== "AwaitExpression") return;
12653
+ const arg = init.argument;
12654
+ if (arg.type !== "CallExpression" && arg.type !== "OptionalCallExpression") return;
12655
+ const callee = arg.callee;
12656
+ if (callee.type !== "MemberExpression" && callee.type !== "OptionalMemberExpression")
12657
+ return;
12658
+ if (callee.property.type !== "Identifier") return;
12659
+ const method = callee.property.name;
12660
+ let isBody = method === "json";
12661
+ if (!isBody && method === "then") {
12662
+ const cb = arg.arguments[0];
12663
+ if (cb && (cb.type === "ArrowFunctionExpression" || cb.type === "FunctionExpression")) {
12664
+ isBody = /\.json\s*\(/.test(code.slice(nodeStart(cb), nodeEnd(cb)));
12665
+ }
12666
+ }
12667
+ if (!isBody) return;
12668
+ if (decl.node.id.type === "Identifier") names.add(decl.node.id.name);
12669
+ },
12670
+ // The promise-chain spelling, which is the shipped defect's own:
12671
+ // fetch(u).then(r => r.json()).then(data => setRows(data.rows || []))
12672
+ // `data` is a callback parameter rather than a binding, and it is just as
12673
+ // much a body this unit parsed. Keyed on the RECEIVER containing `.json(`,
12674
+ // so `somePromise.then(x => …)` does not qualify.
12675
+ CallExpression(call) {
12676
+ const callee = call.node.callee;
12677
+ if (callee.type !== "MemberExpression" && callee.type !== "OptionalMemberExpression")
12678
+ return;
12679
+ if (callee.property.type !== "Identifier" || callee.property.name !== "then") return;
12680
+ const receiver = callee.object;
12681
+ if (!/\.json\s*\(/.test(code.slice(nodeStart(receiver), nodeEnd(receiver)))) return;
12682
+ const cb = call.node.arguments[0];
12683
+ if (!cb) return;
12684
+ if (cb.type !== "ArrowFunctionExpression" && cb.type !== "FunctionExpression") return;
12685
+ const param = cb.params[0];
12686
+ if (param && param.type === "Identifier") names.add(param.name);
12687
+ }
12688
+ });
12689
+ return names;
12690
+ }
12691
+ function derivesFromParsedBody(node, bodies, code) {
12692
+ let cur = node;
12693
+ while (cur.type === "MemberExpression" || cur.type === "OptionalMemberExpression") {
12694
+ cur = cur.object;
12695
+ }
12696
+ if (cur.type === "Identifier") return bodies.has(cur.name);
12697
+ if (cur.type === "AwaitExpression" || cur.type === "CallExpression" || cur.type === "OptionalCallExpression") {
12698
+ return /\.json\s*\(/.test(code.slice(nodeStart(cur), nodeEnd(cur)));
12699
+ }
12700
+ return false;
12701
+ }
12646
12702
  function emptyFallbackArgument(node) {
12647
12703
  if (node.type !== "LogicalExpression") return null;
12648
12704
  if (node.operator !== "||" && node.operator !== "??") return null;
@@ -12683,7 +12739,7 @@ var failedReadEmptyState = {
12683
12739
  category: "Correctness",
12684
12740
  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.",
12685
12741
  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.",
12686
- 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.",
12742
+ 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. The defaulted value must come from a body the unit itself parsed \u2014 `await res.json()`, a `.then` off a receiver containing `.json(`, or the inline `(await res.json()).rows` \u2014 because a unit can hold more than one read, and a nearby real fetch was found satisfying the clause for a fallback belonging to a throwing helper. Measured at 6 fires over 5 sites in 437 .tsx files with zero false positives, then across 168 third-party repositories and 16,601 files: 8 fires over 5 repositories, every one re-read in full and every one real.",
12687
12743
  check(ctx) {
12688
12744
  const hits = [];
12689
12745
  for (const file of ctx.files) {
@@ -12703,6 +12759,7 @@ var failedReadEmptyState = {
12703
12759
  if (reported.has(start)) return;
12704
12760
  const unitSource = file.code.slice(start, nodeEnd(unit.node));
12705
12761
  if (READ_ERROR_PATH_SIGNALS.some((re) => re.test(unitSource))) return;
12762
+ const bodies = parsedBodyBindings(unit, file.code);
12706
12763
  let hit = null;
12707
12764
  unit.traverse({
12708
12765
  CallExpression(inner) {
@@ -12713,6 +12770,7 @@ var failedReadEmptyState = {
12713
12770
  if (!arg) return;
12714
12771
  const fallback = emptyFallbackArgument(arg);
12715
12772
  if (!fallback) return;
12773
+ if (!derivesFromParsedBody(fallback.left, bodies, file.code)) return;
12716
12774
  hit = { line: lineAt(file.content, nodeStart(fallback)), setter: name };
12717
12775
  }
12718
12776
  });
@@ -12747,6 +12805,26 @@ function isEffectCall2(node) {
12747
12805
  const name = calleeIdentifier(node);
12748
12806
  return name === "useEffect" || name === "useLayoutEffect";
12749
12807
  }
12808
+ function sameLiteral(a, b) {
12809
+ if (!a || !b || a.type !== b.type) return false;
12810
+ switch (a.type) {
12811
+ case "BooleanLiteral":
12812
+ return a.value === b.value;
12813
+ case "NumericLiteral":
12814
+ return a.value === b.value;
12815
+ case "StringLiteral":
12816
+ return a.value === b.value;
12817
+ case "NullLiteral":
12818
+ return true;
12819
+ default:
12820
+ return false;
12821
+ }
12822
+ }
12823
+ function setsBackToInitial(call, initial) {
12824
+ if (call.type !== "CallExpression") return false;
12825
+ if (call.arguments.length !== 1) return false;
12826
+ return sameLiteral(call.arguments[0], initial);
12827
+ }
12750
12828
  function stateFromSetter(setter) {
12751
12829
  const m = /^set([A-Z][A-Za-z0-9_]*)$/.exec(setter);
12752
12830
  if (!m) return null;
@@ -12770,7 +12848,7 @@ var autosaveArmedByFailedLoad = {
12770
12848
  category: "Correctness",
12771
12849
  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.",
12772
12850
  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.",
12773
- 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.',
12851
+ 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. The value matters too \u2014 a `finally` that sets the flag back to the literal `useState` started it as is a reset and cannot arm anything, which is what separates this from the hundreds of correct `finally` blocks around it. Measured across 168 third-party repositories and 16,601 files: 0 fires.',
12774
12852
  check(ctx) {
12775
12853
  const hits = [];
12776
12854
  for (const file of ctx.files) {
@@ -12780,6 +12858,22 @@ var autosaveArmedByFailedLoad = {
12780
12858
  const parsed = file.parsed;
12781
12859
  if (!parsed) continue;
12782
12860
  if (!EMPTY_INITIAL.test(file.code)) continue;
12861
+ const initialValues = /* @__PURE__ */ new Map();
12862
+ traverse5(parsed.ast, {
12863
+ VariableDeclarator(decl) {
12864
+ const id = decl.node.id;
12865
+ const init = decl.node.init;
12866
+ if (!id || id.type !== "ArrayPattern") return;
12867
+ if (!init || init.type !== "CallExpression") return;
12868
+ const callee = init.callee;
12869
+ const name = callee.type === "Identifier" ? callee.name : callee.type === "MemberExpression" && callee.property.type === "Identifier" ? callee.property.name : null;
12870
+ if (name !== "useState") return;
12871
+ const first = id.elements[0];
12872
+ const arg = init.arguments[0];
12873
+ if (!first || first.type !== "Identifier" || !arg) return;
12874
+ initialValues.set(first.name, arg);
12875
+ }
12876
+ });
12783
12877
  const armedInFinally = /* @__PURE__ */ new Map();
12784
12878
  traverse5(parsed.ast, {
12785
12879
  TryStatement(path) {
@@ -12791,6 +12885,7 @@ var autosaveArmedByFailedLoad = {
12791
12885
  if (!name) return;
12792
12886
  const state = stateFromSetter(name);
12793
12887
  if (!state) return;
12888
+ if (setsBackToInitial(call.node, initialValues.get(state))) return;
12794
12889
  armedInFinally.set(state, nodeStart(call.node));
12795
12890
  }
12796
12891
  });
@@ -13704,6 +13799,7 @@ function scanQuality(files, rules = qualityRules) {
13704
13799
  assessHeaders,
13705
13800
  blockingMainThread,
13706
13801
  buildProjectContext,
13802
+ buildQualityContext,
13707
13803
  buildTaintMap,
13708
13804
  calculateGrade,
13709
13805
  callSpreads,