xploitscan-shared-rules 1.35.2 → 1.36.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/README.md CHANGED
@@ -2,12 +2,12 @@
2
2
 
3
3
  Shared XploitScan rule definitions, helpers, compliance mappings, the AI false-positive filter, and the entropy-based secret scanner.
4
4
 
5
- This package is the source of truth for XploitScan's detection logic. It's consumed by the [XploitScan CLI](https://www.npmjs.com/package/xploitscan), the [XploitScan MCP server](https://www.npmjs.com/package/xploitscan-mcp), the [VS Code extension](https://marketplace.visualstudio.com/items?itemName=XploitScan.xploitscan-vscode), the web scan API at [xploitscan.com](https://xploitscan.com), and the [XploitScan GitHub Action](https://github.com/marketplace/actions/xploitscan-security-scanner) — so detection behavior can't drift between surfaces.
5
+ This package is the source of truth for XploitScan's detection logic. It's consumed by the [XploitScan CLI](https://www.npmjs.com/package/xploitscan), the [XploitScan MCP server](https://www.npmjs.com/package/xploitscan-mcp), the [VS Code extension](https://marketplace.visualstudio.com/items?itemName=XploitScan.xploitscan-vscode), the web scan API at [xploitscan.com](https://xploitscan.com), and the [XploitScan GitHub Action](https://github.com/marketplace/actions/xploitscan-security-scanner) — one source of detection logic behind every surface.
6
6
 
7
7
  ## What's in here
8
8
 
9
- - **210+ security rules** — pattern + AST-based detection for hardcoded secrets, SQL injection, XSS, SSRF, prototype pollution, crypto misuse, deserialization, JWT alg confusion, and more.
10
- - **Compliance mappings** — each rule tagged with the SOC2, ISO 27001, and OWASP Top 10 controls it covers.
9
+ - **214 rules** — pattern + AST-based detection for hardcoded secrets, SQL injection, XSS, SSRF, prototype pollution, crypto misuse, deserialization, JWT alg confusion, and more.
10
+ - **Compliance mappings** — each rule tagged with its OWASP Top 10 (2021) category and CWE ID.
11
11
  - **AI false-positive filter** — optional Claude Haiku integration that re-evaluates findings to suppress benign matches before reporting.
12
12
  - **Entropy-based secret scanner** — detects high-entropy strings that look like credentials but don't match a known service-key pattern.
13
13
 
@@ -22,11 +22,11 @@ pnpm add xploitscan-shared-rules
22
22
  ## Usage
23
23
 
24
24
  ```ts
25
- import { getAllRules, scanContent, calculateGrade } from "xploitscan-shared-rules";
25
+ import { runCustomRules, proOnlyRules, calculateGrade } from "xploitscan-shared-rules";
26
26
 
27
- const rules = getAllRules();
28
- const findings = scanContent(sourceCode, filePath, rules);
29
- const { grade, score } = calculateGrade(findings);
27
+ // "pro" + proOnlyRules runs every rule (free + Pro)
28
+ const findings = runCustomRules(sourceCode, filePath, [], "pro", proOnlyRules);
29
+ const { grade, score } = calculateGrade(findings, 1);
30
30
  ```
31
31
 
32
32
  For the full API surface, see the TypeScript definitions in `dist/index.d.ts` (shipped with the package).
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,
@@ -2733,7 +2735,7 @@ var evalUsage = {
2733
2735
  title: "Use of eval() or Function Constructor",
2734
2736
  severity: "high",
2735
2737
  category: "Injection",
2736
- description: "eval() and new Function() execute arbitrary code, creating severe injection risks. Common in AI-generated code.",
2738
+ description: "eval() and new Function() execute arbitrary code, creating severe injection risks.",
2737
2739
  check(content, filePath) {
2738
2740
  if (filePath.includes("node_modules") || filePath.includes(".min.")) return [];
2739
2741
  if (filePath.match(/(?:webpack|rollup|vite|jest|babel|tsup|esbuild)\.config/i)) return [];
@@ -3397,7 +3399,7 @@ var missingHTTPS = {
3397
3399
  title: "Missing HTTPS Enforcement",
3398
3400
  severity: "high",
3399
3401
  category: "Configuration",
3400
- description: "HTTP URLs in production code, missing HSTS headers, or insecure redirect configurations expose data to man-in-the-middle attacks.",
3402
+ description: "Hardcoded http:// URLs (other than localhost and private-network addresses) send data unencrypted, exposing it to man-in-the-middle attacks.",
3401
3403
  check(content, filePath) {
3402
3404
  if (filePath.endsWith(".example") || filePath.includes("test") || filePath.includes("README")) return [];
3403
3405
  if (filePath.match(/\.(md|txt)$/)) return [];
@@ -3982,7 +3984,7 @@ var weakPasswordRequirements = {
3982
3984
  p,
3983
3985
  weakPasswordRequirements,
3984
3986
  filePath,
3985
- () => "Minimum password length is too low. Require at least 8 characters with complexity (upper+lower+digit+symbol) or 12+ characters without complexity. OWASP ASVS L1 requires 8+; NIST 800-63B recommends 12+."
3987
+ () => "Minimum password length is too low. Require at least 8 characters, and at least 15 when the password is the only authentication factor (NIST SP 800-63B)."
3986
3988
  ));
3987
3989
  }
3988
3990
  if (matches.length > 0) return matches;
@@ -4931,7 +4933,7 @@ var corsServerless = {
4931
4933
  title: "CORS Wildcard in Serverless Config",
4932
4934
  severity: "high",
4933
4935
  category: "Configuration",
4934
- description: "Setting Access-Control-Allow-Origin: * in serverless.yml, vercel.json, or similar configs allows any website to make authenticated requests to your API.",
4936
+ description: "Setting Access-Control-Allow-Origin: * in serverless.yml, vercel.json, or similar configs lets any website read your API's responses.",
4935
4937
  check(content, filePath) {
4936
4938
  if (!filePath.match(/serverless\.(yml|yaml)|vercel\.json|netlify\.toml|amplify\.yml/i)) return [];
4937
4939
  const matches = [];
@@ -5721,7 +5723,7 @@ var complianceMap = {
5721
5723
  VC022: { owasp: "A03:2021", cwe: "CWE-79" },
5722
5724
  VC023: { owasp: "A08:2021", cwe: "CWE-1321" },
5723
5725
  VC024: { owasp: "A04:2021", cwe: "CWE-770" },
5724
- VC025: { owasp: "A03:2021", cwe: "CWE-22" },
5726
+ VC025: { owasp: "A01:2021", cwe: "CWE-22" },
5725
5727
  VC026: { owasp: "A05:2021", cwe: "CWE-693" },
5726
5728
  VC027: { owasp: "A05:2021", cwe: "CWE-693" },
5727
5729
  VC028: { owasp: "A07:2021", cwe: "CWE-20" },
@@ -5749,7 +5751,7 @@ var complianceMap = {
5749
5751
  VC050: { owasp: "A02:2021", cwe: "CWE-319" },
5750
5752
  VC051: { owasp: "A05:2021", cwe: "CWE-200" },
5751
5753
  VC052: { owasp: "A04:2021", cwe: "CWE-770" },
5752
- VC053: { owasp: "A05:2021", cwe: "CWE-798" },
5754
+ VC053: { owasp: "A05:2021", cwe: "CWE-547" },
5753
5755
  VC054: { owasp: "A07:2021", cwe: "CWE-922" },
5754
5756
  VC055: { owasp: "A05:2021", cwe: "CWE-540" },
5755
5757
  VC056: { owasp: "A05:2021", cwe: "CWE-1021" },
@@ -5807,13 +5809,13 @@ var complianceMap = {
5807
5809
  VC108: { owasp: "A01:2021", cwe: "CWE-284" },
5808
5810
  VC109: { owasp: "A01:2021", cwe: "CWE-284" },
5809
5811
  VC110: { owasp: "A09:2021", cwe: "CWE-778" },
5810
- VC111: { owasp: "A05:2021", cwe: "CWE-16" },
5812
+ VC111: { owasp: "A05:2021", cwe: "CWE-653" },
5811
5813
  VC112: { owasp: "A06:2021", cwe: "CWE-1104" },
5812
5814
  VC113: { owasp: "A05:2021", cwe: "CWE-200" },
5813
5815
  VC114: { owasp: "A05:2021", cwe: "CWE-16" },
5814
5816
  VC115: { owasp: "A02:2021", cwe: "CWE-311" },
5815
5817
  VC116: { owasp: "A05:2021", cwe: "CWE-770" },
5816
- VC117: { owasp: "A03:2021", cwe: "CWE-22" },
5818
+ VC117: { owasp: "A01:2021", cwe: "CWE-22" },
5817
5819
  VC118: { owasp: "A09:2021", cwe: "CWE-532" },
5818
5820
  VC119: { owasp: "A07:2021", cwe: "CWE-798" },
5819
5821
  VC120: { owasp: "A07:2021", cwe: "CWE-352" },
@@ -5856,7 +5858,7 @@ var complianceMap = {
5856
5858
  VC154: { owasp: "A03:2021", cwe: "CWE-20" },
5857
5859
  VC155: { owasp: "A04:2021", cwe: "CWE-770" },
5858
5860
  VC156: { owasp: "A04:2021", cwe: "CWE-770" },
5859
- VC157: { owasp: "A05:2021", cwe: "CWE-16" },
5861
+ VC157: { owasp: "A05:2021", cwe: "CWE-1327" },
5860
5862
  VC158: { owasp: "A01:2021", cwe: "CWE-639" },
5861
5863
  // VC159–VC183: Additional service-specific API key detection.
5862
5864
  // All map to A07:2021 (Identification & Auth Failures) / CWE-798 (Hardcoded
@@ -6535,7 +6537,7 @@ var piiInLogs = {
6535
6537
  title: "Personally Identifiable Information in Logs",
6536
6538
  severity: "high",
6537
6539
  category: "Information Leakage",
6538
- description: "Logging statements that include email addresses, passwords, SSNs, credit card numbers, or other PII can leak sensitive data to log aggregation systems.",
6540
+ description: "Logging statements that include passwords, secrets, tokens, authorization headers, SSNs, credit card numbers or CVVs can leak sensitive data to log aggregation systems.",
6539
6541
  check(content, filePath) {
6540
6542
  if (isTestFile(filePath)) return [];
6541
6543
  if (!filePath.match(/\.(js|ts|jsx|tsx|py|rb|go|java|php)$/)) return [];
@@ -6978,7 +6980,7 @@ var vulnerableDependencies = {
6978
6980
  title: "Potentially Vulnerable Dependency",
6979
6981
  severity: "high",
6980
6982
  category: "Supply Chain",
6981
- description: "Dependencies with known security issues that are commonly found in AI-generated codebases.",
6983
+ description: "Dependency versions with known security issues.",
6982
6984
  check(content, filePath) {
6983
6985
  if (!filePath.endsWith("package.json")) return [];
6984
6986
  if (/node_modules|\.next|dist|build/.test(filePath)) return [];
@@ -6987,7 +6989,7 @@ var vulnerableDependencies = {
6987
6989
  [/"jsonwebtoken"\s*:\s*"[\^~]?[0-7]\./g, "jsonwebtoken < 8.x has signature bypass vulnerabilities"],
6988
6990
  [/"lodash"\s*:\s*"[\^~]?[0-3]\./g, "lodash < 4.x has prototype pollution vulnerabilities"],
6989
6991
  [/"minimist"\s*:\s*"[\^~]?[01]\.[01]\./g, "minimist < 1.2.6 has prototype pollution"],
6990
- [/"node-fetch"\s*:\s*"[\^~]?[12]\./g, "node-fetch < 3.x has data exposure issues"],
6992
+ [/"node-fetch"\s*:\s*"[\^~]?[12]\./g, "node-fetch before 2.6.7 (and 3.x before 3.1.1) forwards cookie and authorization headers on cross-origin redirects (CVE-2022-0235); check that this 1.x/2.x range resolves to 2.6.7 or later"],
6991
6993
  [/"express"\s*:\s*"[\^~]?[0-3]\./g, "express < 4.x has multiple known vulnerabilities"],
6992
6994
  [/"axios"\s*:\s*"[\^~]?0\.[0-9]\./g, "axios < 0.21 has SSRF vulnerabilities"],
6993
6995
  [/"tar"\s*:\s*"[\^~]?[0-5]\./g, "tar < 6.x has path traversal vulnerabilities"],
@@ -7924,7 +7926,7 @@ var ghaThirdPartyActionWithSecrets = {
7924
7926
  title: "GitHub Actions: secrets passed to third-party action",
7925
7927
  severity: "medium",
7926
7928
  category: "Supply Chain",
7927
- description: "Passing repository secrets (`${{ secrets.* }}`) via the `with:` block to a third-party action means the action's source code \u2014 and any transitive dependencies it pulls \u2014 has access to those secrets. The 2025 tj-actions/changed-files supply-chain attack stole secrets exactly this way. Common when intentional (AWS, Cloudflare, GCP credential actions) but worth flagging for review.",
7929
+ description: "Passing repository secrets (`${{ secrets.* }}`) via the `with:` block to a third-party action means the action's source code \u2014 and any transitive dependencies it pulls \u2014 has access to those secrets. A compromised action can read every secret its job has access to: in the March 2025 tj-actions/changed-files compromise, the hijacked action dumped secrets from runner memory into build logs. Common when intentional (AWS, Cloudflare, GCP credential actions) but worth flagging for review.",
7928
7930
  check(content, filePath) {
7929
7931
  if (!GHA_WORKFLOW_RE.test(filePath)) return [];
7930
7932
  const findings = [];
@@ -8986,7 +8988,7 @@ var secretInURLParam = {
8986
8988
  title: "Secret Passed in URL Query Parameter",
8987
8989
  severity: "critical",
8988
8990
  category: "Secrets",
8989
- description: "API keys or tokens in URL query parameters are logged in server access logs, browser history, referrer headers, and proxy logs. Secrets in URLs are one of the most common causes of credential exposure.",
8991
+ description: "API keys or tokens in URL query parameters are logged in server access logs, browser history, referrer headers, and proxy logs.",
8990
8992
  check(content, filePath) {
8991
8993
  if (isTestFile(filePath)) return [];
8992
8994
  if (!filePath.match(/\.(js|ts|jsx|tsx|py|rb|go|java|php)$/)) return [];
@@ -10339,7 +10341,7 @@ function assessHeaders(headers) {
10339
10341
  out.push({
10340
10342
  header: "Strict-Transport-Security",
10341
10343
  severity: "medium",
10342
- message: "No HSTS. A first visit over http can be intercepted before the redirect to https."
10344
+ message: "No HSTS. Any visit that starts over http can be intercepted before the redirect to https."
10343
10345
  });
10344
10346
  }
10345
10347
  if (!h["x-content-type-options"]) {
@@ -10353,7 +10355,7 @@ function assessHeaders(headers) {
10353
10355
  out.push({
10354
10356
  header: "Referrer-Policy",
10355
10357
  severity: "low",
10356
- message: "No referrer policy. Full URLs \u2014 including any ids in the path \u2014 leak to third parties."
10358
+ message: "No referrer policy, so the browser default applies. Current browsers send only your origin to other sites, but older ones send full URLs \u2014 including any ids in the path."
10357
10359
  });
10358
10360
  }
10359
10361
  return out;
@@ -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,