xploitscan-shared-rules 1.35.2 → 1.36.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 CHANGED
@@ -181,8 +181,10 @@ __export(index_exports, {
181
181
  insecureWebSocket: () => insecureWebSocket,
182
182
  ipcPathTraversal: () => ipcPathTraversal,
183
183
  isCalleeNamed: () => isCalleeNamed,
184
+ isClientTarget: () => isClientTarget,
184
185
  isMethodCall: () => isMethodCall,
185
186
  isPrivateAddress: () => isPrivateAddress,
187
+ isServerTarget: () => isServerTarget,
186
188
  isVendoredFinding: () => isVendoredFinding,
187
189
  isVendoredPath: () => isVendoredPath,
188
190
  javaDeserialization: () => javaDeserialization,
@@ -10413,6 +10415,12 @@ function buildQualityContext(files) {
10413
10415
  });
10414
10416
  return { files: built, byPath: new Map(built.map((f) => [f.path, f])) };
10415
10417
  }
10418
+ function isClientTarget(file) {
10419
+ return !file.excluded && file.isClient && !file.isServer;
10420
+ }
10421
+ function isServerTarget(file) {
10422
+ return !file.excluded && file.isServer;
10423
+ }
10416
10424
  function lineAt(content, index) {
10417
10425
  let line = 1;
10418
10426
  for (let i = 0; i < index && i < content.length; i++) {
@@ -12627,6 +12635,55 @@ var unassociatedLabel = {
12627
12635
 
12628
12636
  // src/quality/rules/failed-read-empty-state.ts
12629
12637
  var import_traverse5 = __toESM(require("@babel/traverse"), 1);
12638
+ var import_types = require("@babel/types");
12639
+
12640
+ // src/quality/rules/scope.ts
12641
+ function unwrap(node) {
12642
+ let cur = node;
12643
+ while (cur.type === "TSAsExpression" || cur.type === "TSNonNullExpression" || cur.type === "TSTypeAssertion" || cur.type === "TSSatisfiesExpression") {
12644
+ cur = cur.expression;
12645
+ }
12646
+ return cur;
12647
+ }
12648
+ function calleeName3(node) {
12649
+ if (node.type !== "CallExpression" && node.type !== "OptionalCallExpression") return null;
12650
+ const c = node.callee;
12651
+ if (c.type === "Identifier") return c.name;
12652
+ if ((c.type === "MemberExpression" || c.type === "OptionalMemberExpression") && c.property.type === "Identifier") {
12653
+ return c.property.name;
12654
+ }
12655
+ return null;
12656
+ }
12657
+ function useStateBinding(at, name) {
12658
+ const binding = at.scope.getBinding(name);
12659
+ if (!binding) return void 0;
12660
+ const decl = binding.path;
12661
+ if (!decl.isVariableDeclarator()) return void 0;
12662
+ const id = decl.node.id;
12663
+ const init = decl.node.init;
12664
+ if (id.type !== "ArrayPattern") return void 0;
12665
+ if (!init || calleeName3(init) !== "useState") return void 0;
12666
+ const first = id.elements[0];
12667
+ if (!first || first.type !== "Identifier" || first.name !== name) return void 0;
12668
+ const second = id.elements[1];
12669
+ return {
12670
+ initial: init.arguments[0],
12671
+ setter: second && second.type === "Identifier" ? second.name : null,
12672
+ decl
12673
+ };
12674
+ }
12675
+ function isEmptyInitial(node) {
12676
+ if (!node) return false;
12677
+ if (node.type === "ArrayExpression") return node.elements.length === 0;
12678
+ if (node.type === "ObjectExpression") return node.properties.length === 0;
12679
+ if (node.type === "StringLiteral") return node.value === "";
12680
+ if (node.type === "TemplateLiteral") {
12681
+ return node.quasis.length === 1 && node.quasis[0].value.raw === "";
12682
+ }
12683
+ return false;
12684
+ }
12685
+
12686
+ // src/quality/rules/failed-read-empty-state.ts
12630
12687
  var traverse4 = typeof import_traverse5.default === "function" ? import_traverse5.default : import_traverse5.default.default;
12631
12688
  var READ_ERROR_PATH_SIGNALS = ERROR_PATH_SIGNALS.filter(
12632
12689
  (re) => re.source !== /\.catch\s*\(/.source
@@ -12644,19 +12701,15 @@ function setterName(callee) {
12644
12701
  function isPropertyRead(node) {
12645
12702
  return node.type === "MemberExpression" || node.type === "OptionalMemberExpression";
12646
12703
  }
12647
- function unwrap(node) {
12648
- let cur = node;
12649
- while (cur && (cur.type === "TSAsExpression" || cur.type === "TSNonNullExpression" || cur.type === "TSTypeAssertion" || cur.type === "TSSatisfiesExpression" || cur.type === "ParenthesizedExpression")) {
12650
- cur = cur.expression;
12651
- }
12652
- return cur;
12704
+ function mentionsJsonParse(node, code) {
12705
+ return /\.json\s*\(/.test(code.slice(nodeStart(node), nodeEnd(node)));
12653
12706
  }
12654
12707
  function chainParsesBody(node, code) {
12655
12708
  let cur = unwrap(node);
12656
- while (cur) {
12709
+ for (; ; ) {
12657
12710
  if (cur.type === "CallExpression" || cur.type === "OptionalCallExpression") {
12658
12711
  const callee = unwrap(cur.callee);
12659
- if (callee && (callee.type === "MemberExpression" || callee.type === "OptionalMemberExpression") && callee.property.type === "Identifier") {
12712
+ if ((callee.type === "MemberExpression" || callee.type === "OptionalMemberExpression") && callee.property.type === "Identifier") {
12660
12713
  const method = callee.property.name;
12661
12714
  if (method === "json") return true;
12662
12715
  if (method === "then" || method === "catch") {
@@ -12668,7 +12721,7 @@ function chainParsesBody(node, code) {
12668
12721
  cur = unwrap(callee.object);
12669
12722
  continue;
12670
12723
  }
12671
- cur = unwrap(cur.callee);
12724
+ cur = callee;
12672
12725
  continue;
12673
12726
  }
12674
12727
  if (cur.type === "MemberExpression" || cur.type === "OptionalMemberExpression") {
@@ -12680,23 +12733,14 @@ function chainParsesBody(node, code) {
12680
12733
  return false;
12681
12734
  }
12682
12735
  function boundNames(id, into) {
12683
- if (id.type === "Identifier") {
12684
- into.add(id.name);
12685
- return;
12686
- }
12687
- if (id.type === "ObjectPattern") {
12688
- for (const prop of id.properties) {
12689
- if (prop.type === "ObjectProperty") boundNames(prop.value, into);
12690
- else if (prop.type === "RestElement") boundNames(prop.argument, into);
12691
- }
12692
- }
12736
+ for (const name of Object.keys((0, import_types.getBindingIdentifiers)(id))) into.add(name);
12693
12737
  }
12694
12738
  function parsedBodyBindings(unit, code) {
12695
12739
  const names = /* @__PURE__ */ new Set();
12696
12740
  const record = (id, init) => {
12697
- if (!id) return;
12741
+ if (!id || !init) return;
12698
12742
  const awaited = unwrap(init);
12699
- if (!awaited || awaited.type !== "AwaitExpression") return;
12743
+ if (awaited.type !== "AwaitExpression") return;
12700
12744
  if (!chainParsesBody(awaited.argument, code)) return;
12701
12745
  boundNames(id, names);
12702
12746
  };
@@ -12720,7 +12764,7 @@ function parsedBodyBindings(unit, code) {
12720
12764
  return;
12721
12765
  if (callee.property.type !== "Identifier" || callee.property.name !== "then") return;
12722
12766
  const receiver = callee.object;
12723
- if (!/\.json\s*\(/.test(code.slice(nodeStart(receiver), nodeEnd(receiver)))) return;
12767
+ if (!mentionsJsonParse(receiver, code)) return;
12724
12768
  const cb = call.node.arguments[0];
12725
12769
  if (!cb) return;
12726
12770
  if (cb.type !== "ArrowFunctionExpression" && cb.type !== "FunctionExpression") return;
@@ -12735,13 +12779,9 @@ function derivesFromParsedBody(node, bodies, code) {
12735
12779
  while (cur && (cur.type === "MemberExpression" || cur.type === "OptionalMemberExpression")) {
12736
12780
  cur = unwrap(cur.object);
12737
12781
  }
12738
- if (!cur) return false;
12739
12782
  if (cur.type === "Identifier") return bodies.has(cur.name);
12740
- if (cur.type === "AwaitExpression") return chainParsesBody(cur.argument, code);
12741
- if (cur.type === "CallExpression" || cur.type === "OptionalCallExpression") {
12742
- return chainParsesBody(cur, code);
12743
- }
12744
- return false;
12783
+ if (cur.type === "AwaitExpression") cur = unwrap(cur.argument);
12784
+ return chainParsesBody(cur, code);
12745
12785
  }
12746
12786
  function emptyFallbackArgument(node) {
12747
12787
  if (node.type !== "LogicalExpression") return null;
@@ -12791,6 +12831,7 @@ var failedReadEmptyState = {
12791
12831
  if (!file.isClient) continue;
12792
12832
  if (file.isServer) continue;
12793
12833
  if (/^\s*["']use server["']/m.test(file.code)) continue;
12834
+ if (!file.code.includes("fetch")) continue;
12794
12835
  const parsed = file.parsed;
12795
12836
  if (!parsed) continue;
12796
12837
  const examined = /* @__PURE__ */ new Set();
@@ -12836,6 +12877,20 @@ var failedReadEmptyState = {
12836
12877
  // src/quality/rules/autosave-armed-by-failed-load.ts
12837
12878
  var import_traverse6 = __toESM(require("@babel/traverse"), 1);
12838
12879
  var traverse5 = typeof import_traverse6.default === "function" ? import_traverse6.default : import_traverse6.default.default;
12880
+ function writerTouchesEmptyState(writers) {
12881
+ for (const writer of writers) {
12882
+ let found = false;
12883
+ writer.traverse({
12884
+ Identifier(id) {
12885
+ if (found) return;
12886
+ if (!id.isReferencedIdentifier()) return;
12887
+ if (isEmptyInitial(useStateBinding(id, id.node.name)?.initial)) found = true;
12888
+ }
12889
+ });
12890
+ if (found) return true;
12891
+ }
12892
+ return false;
12893
+ }
12839
12894
  var PERSISTENT_WRITE = /\bfetch\s*\([^)]{0,400}?method\s*:\s*["'`](?:POST|PUT|PATCH)["'`]|\blocalStorage\s*\.\s*setItem\s*\(|\bsessionStorage\s*\.\s*setItem\s*\(/is;
12840
12895
  var EMPTY_INITIAL = /\buseState\s*(?:<[^>]*>)?\s*\(\s*(?:\[\s*\]|\{\s*\}|""|''|``)\s*\)/;
12841
12896
  function calleeIdentifier(node) {
@@ -12849,22 +12904,6 @@ function isEffectCall2(node) {
12849
12904
  const name = calleeIdentifier(node);
12850
12905
  return name === "useEffect" || name === "useLayoutEffect";
12851
12906
  }
12852
- function initialFor(call, state) {
12853
- const binding = call.scope.getBinding(state);
12854
- if (!binding) return void 0;
12855
- const decl = binding.path;
12856
- if (!decl.isVariableDeclarator()) return void 0;
12857
- const id = decl.node.id;
12858
- const init = decl.node.init;
12859
- if (!id || id.type !== "ArrayPattern") return void 0;
12860
- if (!init || init.type !== "CallExpression") return void 0;
12861
- const callee = init.callee;
12862
- const name = callee.type === "Identifier" ? callee.name : callee.type === "MemberExpression" && callee.property.type === "Identifier" ? callee.property.name : null;
12863
- if (name !== "useState") return void 0;
12864
- const first = id.elements[0];
12865
- if (!first || first.type !== "Identifier" || first.name !== state) return void 0;
12866
- return init.arguments[0];
12867
- }
12868
12907
  function sameLiteral(a, b) {
12869
12908
  if (!a || !b || a.type !== b.type) return false;
12870
12909
  switch (a.type) {
@@ -12912,9 +12951,7 @@ var autosaveArmedByFailedLoad = {
12912
12951
  check(ctx) {
12913
12952
  const hits = [];
12914
12953
  for (const file of ctx.files) {
12915
- if (file.excluded) continue;
12916
- if (!file.isClient) continue;
12917
- if (file.isServer) continue;
12954
+ if (!isClientTarget(file)) continue;
12918
12955
  const parsed = file.parsed;
12919
12956
  if (!parsed) continue;
12920
12957
  if (!EMPTY_INITIAL.test(file.code)) continue;
@@ -12929,29 +12966,32 @@ var autosaveArmedByFailedLoad = {
12929
12966
  if (!name) return;
12930
12967
  const state = stateFromSetter(name);
12931
12968
  if (!state) return;
12932
- if (setsBackToInitial(call.node, initialFor(call, state))) return;
12969
+ if (setsBackToInitial(call.node, useStateBinding(call, state)?.initial)) return;
12933
12970
  armedInFinally.set(state, nodeStart(call.node));
12934
12971
  }
12935
12972
  });
12936
12973
  }
12937
12974
  });
12938
12975
  if (armedInFinally.size === 0) continue;
12939
- const writingFunctions = /* @__PURE__ */ new Set();
12976
+ const writingFunctions = /* @__PURE__ */ new Map();
12940
12977
  traverse5(parsed.ast, {
12941
12978
  Function(path) {
12942
12979
  const src = file.code.slice(nodeStart(path.node), nodeEnd(path.node));
12943
12980
  if (!PERSISTENT_WRITE.test(src)) return;
12981
+ const deps = [];
12944
12982
  let owner = path.parentPath;
12945
12983
  for (let hops = 0; owner && hops < 3; hops++, owner = owner.parentPath) {
12946
12984
  const node = owner.node;
12947
12985
  if (node.type === "VariableDeclarator" && node.id.type === "Identifier") {
12948
- writingFunctions.add(node.id.name);
12986
+ writingFunctions.set(node.id.name, [path, ...deps]);
12949
12987
  break;
12950
12988
  }
12951
12989
  if (node.type !== "CallExpression") break;
12990
+ const depArg = owner.get("arguments.1");
12991
+ if (depArg && depArg.node) deps.push(depArg);
12952
12992
  }
12953
12993
  const n = path.node;
12954
- if (n.id && n.id.name) writingFunctions.add(n.id.name);
12994
+ if (n.id && n.id.name) writingFunctions.set(n.id.name, [path, ...deps]);
12955
12995
  }
12956
12996
  });
12957
12997
  const reported = /* @__PURE__ */ new Set();
@@ -12963,8 +13003,15 @@ var autosaveArmedByFailedLoad = {
12963
13003
  const body = file.code.slice(nodeStart(cb.node), nodeEnd(cb.node));
12964
13004
  const names = identifiersIn(cb);
12965
13005
  const writesDirectly = PERSISTENT_WRITE.test(body);
12966
- const writesViaCallback = [...writingFunctions].some((fn) => names.has(fn));
12967
- if (!writesDirectly && !writesViaCallback) return;
13006
+ const calledWriters = [...writingFunctions.entries()].filter(([fn]) => names.has(fn)).flatMap(([, paths]) => paths);
13007
+ if (!writesDirectly && calledWriters.length === 0) return;
13008
+ const effectDeps = path.get("arguments.1");
13009
+ const writers = [
13010
+ ...writesDirectly ? [cb] : [],
13011
+ ...calledWriters,
13012
+ ...effectDeps && effectDeps.node ? [effectDeps] : []
13013
+ ];
13014
+ if (!writerTouchesEmptyState(writers)) return;
12968
13015
  for (const [state, armLine] of armedInFinally) {
12969
13016
  if (!names.has(state)) continue;
12970
13017
  if (reported.has(state)) continue;
@@ -12991,7 +13038,7 @@ var DURABLE_WRITE = /\b(?:INSERT\s+INTO\b|UPDATE\s+\w+\s+SET\b|DELETE\s+FROM\b)|
12991
13038
  var EXTERNAL_MUTATION = /\b\w+\s*\.\s*\w+\s*\.\s*(?:create|send|update|delete|cancel|charge|capture|refund)\s*\(|\bfetch\s*\([^)]{0,300}?method\s*:\s*["'`](?:POST|PUT|PATCH|DELETE)["'`]/is;
12992
13039
  var BOOKKEEPING = /analytics|telemetry|track|metric|event|beacon|log|audit|ping/i;
12993
13040
  var DIRECT_ENV_TEST = /\bprocess\s*\.\s*env\b|\bDeno\s*\.\s*env\s*\.\s*get\s*\(/;
12994
- function calleeName3(node) {
13041
+ function calleeName4(node) {
12995
13042
  if (node.type !== "CallExpression") return null;
12996
13043
  const c = node.callee;
12997
13044
  if (c.type === "Identifier") return c.name;
@@ -13013,11 +13060,11 @@ function successPayloadShape(ret, source, offset) {
13013
13060
  if (!arg || arg.type !== "CallExpression") return null;
13014
13061
  const text = source.slice(nodeStart(ret) - offset, nodeEnd(ret) - offset);
13015
13062
  if (/status\s*:\s*[45]\d\d|,\s*\{\s*status\s*:\s*[45]\d\d/.test(text)) return null;
13016
- const name = calleeName3(arg);
13063
+ const name = calleeName4(arg);
13017
13064
  if (name !== "json" && name !== "Response" && name !== "NextResponse") return null;
13018
13065
  const first = arg.arguments[0];
13019
13066
  if (!first) return null;
13020
- if (first.type === "CallExpression" && calleeName3(first) === "stringify") {
13067
+ if (first.type === "CallExpression" && calleeName4(first) === "stringify") {
13021
13068
  return keyShape(first.arguments[0]);
13022
13069
  }
13023
13070
  return keyShape(first);
@@ -13197,9 +13244,7 @@ var staleResponseWins = {
13197
13244
  check(ctx) {
13198
13245
  const hits = [];
13199
13246
  for (const file of ctx.files) {
13200
- if (file.excluded) continue;
13201
- if (!file.isClient) continue;
13202
- if (file.isServer) continue;
13247
+ if (!isClientTarget(file)) continue;
13203
13248
  const parsed = file.parsed;
13204
13249
  if (!parsed) continue;
13205
13250
  traverse7(parsed.ast, {
@@ -13244,7 +13289,10 @@ var staleResponseWins = {
13244
13289
  for (const ident2 of interpolated) {
13245
13290
  if (!derivesFromDep(ident2, deps, cb, file.code)) continue;
13246
13291
  if (new RegExp(`\\b${ident2}\\b`).test(afterCall)) continue;
13247
- if (![...deps].some((d) => userDriven(d, file.code))) continue;
13292
+ const driving = [...deps].filter(
13293
+ (d) => derivesFromDep(ident2, /* @__PURE__ */ new Set([d]), cb, file.code)
13294
+ );
13295
+ if (!driving.some((d) => userDriven(d, cb))) continue;
13248
13296
  hit = { line: lineAt(file.content, nodeStart(inner.node)), ident: ident2 };
13249
13297
  return;
13250
13298
  }
@@ -13263,10 +13311,26 @@ var staleResponseWins = {
13263
13311
  return hits;
13264
13312
  }
13265
13313
  };
13266
- function userDriven(dep, code) {
13267
- const setter = `set${dep.charAt(0).toUpperCase()}${dep.slice(1)}`;
13268
- if (!new RegExp(`\\[\\s*${dep}\\s*,\\s*${setter}\\s*\\]\\s*=\\s*useState`).test(code)) return false;
13269
- return new RegExp(`on(?:Change|Click|Input|Select)\\s*=\\s*\\{[^}]{0,200}\\b${setter}\\b`, "s").test(code);
13314
+ function userDriven(dep, at) {
13315
+ const binding = useStateBinding(at, dep);
13316
+ const setter = binding?.setter;
13317
+ if (!setter) return false;
13318
+ const owner = binding.decl.getFunctionParent() ?? binding.decl.scope.path;
13319
+ let wired = false;
13320
+ owner.traverse({
13321
+ JSXAttribute(attr) {
13322
+ if (wired) return;
13323
+ const name = attr.node.name;
13324
+ if (name.type !== "JSXIdentifier") return;
13325
+ if (!/^on(?:Change|Click|Input|Select)$/.test(name.name)) return;
13326
+ attr.traverse({
13327
+ Identifier(id) {
13328
+ if (id.node.name === setter) wired = true;
13329
+ }
13330
+ });
13331
+ }
13332
+ });
13333
+ return wired;
13270
13334
  }
13271
13335
 
13272
13336
  // src/quality/rules/lost-update-counter.ts
@@ -13787,8 +13851,8 @@ var qualityRules = [
13787
13851
  var unmeasuredQualityRules = [
13788
13852
  lostUpdateCounter
13789
13853
  ];
13790
- function scanQuality(files, rules = qualityRules, prebuilt) {
13791
- const ctx = prebuilt ?? buildQualityContext(files);
13854
+ function scanQuality(input, rules = qualityRules) {
13855
+ const ctx = Array.isArray(input) ? buildQualityContext(input) : input;
13792
13856
  const findings = [];
13793
13857
  for (const rule of rules) {
13794
13858
  let hits;
@@ -13973,8 +14037,10 @@ function scanQuality(files, rules = qualityRules, prebuilt) {
13973
14037
  insecureWebSocket,
13974
14038
  ipcPathTraversal,
13975
14039
  isCalleeNamed,
14040
+ isClientTarget,
13976
14041
  isMethodCall,
13977
14042
  isPrivateAddress,
14043
+ isServerTarget,
13978
14044
  isVendoredFinding,
13979
14045
  isVendoredPath,
13980
14046
  javaDeserialization,