xploitscan-shared-rules 2.4.30 → 2.4.32

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
@@ -94,6 +94,7 @@ __export(index_exports, {
94
94
  classifyPublicEnvVar: () => classifyPublicEnvVar,
95
95
  classifyStatement: () => classifyStatement,
96
96
  clientSideAuth: () => clientSideAuth,
97
+ collectRouteClaims: () => collectRouteClaims,
97
98
  complianceMap: () => complianceMap,
98
99
  consoleLogProduction: () => consoleLogProduction,
99
100
  corsWildcard: () => corsWildcard,
@@ -109,6 +110,7 @@ __export(index_exports, {
109
110
  emptyCatchBlock: () => emptyCatchBlock,
110
111
  enclosingHandler: () => enclosingHandler,
111
112
  envNotGitignored: () => envNotGitignored,
113
+ envVarNamesUsed: () => envVarNamesUsed,
112
114
  evalUsage: () => evalUsage,
113
115
  exposedAuthSecret: () => exposedAuthSecret,
114
116
  exposedDebugMode: () => exposedDebugMode,
@@ -165,6 +167,8 @@ __export(index_exports, {
165
167
  maskSecretValue: () => maskSecretValue,
166
168
  maskStringLiterals: () => maskStringLiterals,
167
169
  matchIgnore: () => matchIgnore,
170
+ mergeIntentStatements: () => mergeIntentStatements,
171
+ mergeRouteClaims: () => mergeRouteClaims,
168
172
  missingAuthMiddleware: () => missingAuthMiddleware,
169
173
  missingCSP: () => missingCSP,
170
174
  missingErrorBoundary: () => missingErrorBoundary,
@@ -193,6 +197,8 @@ __export(index_exports, {
193
197
  refineDirectObjectFindings: () => refineDirectObjectFindings,
194
198
  replaceBlockComments: () => replaceBlockComments,
195
199
  resetStructuralState: () => resetStructuralState,
200
+ resolveSecretGuardedRoutes: () => resolveSecretGuardedRoutes,
201
+ routeDelegationFacts: () => routeDelegationFacts,
196
202
  routeForFile: () => routeForFile,
197
203
  routePathFromFile: () => routePathFromFile,
198
204
  runCustomRules: () => runCustomRules,
@@ -230,7 +236,7 @@ __export(index_exports, {
230
236
  module.exports = __toCommonJS(index_exports);
231
237
 
232
238
  // src/version.ts
233
- var SCANNER_VERSION = "2.4.30";
239
+ var SCANNER_VERSION = "2.4.32";
234
240
 
235
241
  // src/snippet.ts
236
242
  var cacheContent = null;
@@ -1502,6 +1508,121 @@ function calleeIsGuarded(fn, route, files) {
1502
1508
  const open = defs[0].index + defs[0][0].length - 1;
1503
1509
  return opensWithGuard(targets[0].content.slice(open, open + 4e3));
1504
1510
  }
1511
+ var DEF_HEAD_RE = /\b(?:function|const|let)\s+([A-Za-z_$][\w$]*)/g;
1512
+ var GENERIC_DEF_RE = new RegExp(
1513
+ `\\bfunction\\s+([A-Za-z_$][\\w$]*)\\s*\\([^()]{0,200}\\)[^{;]{0,80}\\{|\\b(?:const|let)\\s+([A-Za-z_$][\\w$]*)\\s*(?::[^=;\\n]{0,80})?=\\s*(?:async\\s*)?\\([^()]{0,200}\\)[^{;=]{0,80}=>\\s*\\{`,
1514
+ "y"
1515
+ );
1516
+ var ENV_READ = "process.env";
1517
+ var MAX_GUARDED_CANDIDATES = 50;
1518
+ function importSpec(fn, route) {
1519
+ for (const m of route.matchAll(/\bimport\s*\{([^{}]{0,1000})\}\s*from\s*["']([^"'\n]{1,200})["']/g)) {
1520
+ if (m[1].split(",").some((n) => n.trim() === fn)) return m[2];
1521
+ }
1522
+ return null;
1523
+ }
1524
+ function definesGuarded(fn, content) {
1525
+ const F = esc(fn);
1526
+ const defRe = new RegExp(
1527
+ `\\bfunction\\s+${F}\\s*\\([^()]{0,200}\\)[^{;]{0,80}\\{|\\b(?:const|let)\\s+${F}\\s*(?::[^=;\\n]{0,80})?=\\s*(?:async\\s*)?\\([^()]{0,200}\\)[^{;=]{0,80}=>\\s*\\{`,
1528
+ "g"
1529
+ );
1530
+ const defs = [...content.matchAll(defRe)];
1531
+ if (defs.length !== 1) return false;
1532
+ const open = defs[0].index + defs[0][0].length - 1;
1533
+ return opensWithGuard(content.slice(open, open + 4e3));
1534
+ }
1535
+ function envReads(content) {
1536
+ const out = [];
1537
+ for (let i = content.indexOf(ENV_READ); i !== -1; i = content.indexOf(ENV_READ, i + 1)) out.push(i);
1538
+ return out;
1539
+ }
1540
+ function anyIn(sorted, from, to) {
1541
+ let lo = 0;
1542
+ let hi = sorted.length;
1543
+ while (lo < hi) {
1544
+ const mid = lo + hi >>> 1;
1545
+ if (sorted[mid] < from) lo = mid + 1;
1546
+ else hi = mid;
1547
+ }
1548
+ return lo < sorted.length && sorted[lo] < to;
1549
+ }
1550
+ function guardedHelperNames(content) {
1551
+ const reads = envReads(content);
1552
+ if (reads.length === 0) return [];
1553
+ const candidates = [];
1554
+ const seen = /* @__PURE__ */ new Set();
1555
+ DEF_HEAD_RE.lastIndex = 0;
1556
+ for (let m = DEF_HEAD_RE.exec(content); m !== null; m = DEF_HEAD_RE.exec(content)) {
1557
+ if (seen.has(m[1])) continue;
1558
+ GENERIC_DEF_RE.lastIndex = m.index;
1559
+ const def = GENERIC_DEF_RE.exec(content);
1560
+ if (def === null || (def[1] ?? def[2]) !== m[1]) continue;
1561
+ const open = m.index + def[0].length - 1;
1562
+ if (!anyIn(reads, open, open + 4e3)) continue;
1563
+ if (!opensWithGuard(content.slice(open, open + 4e3))) continue;
1564
+ seen.add(m[1]);
1565
+ candidates.push(m[1]);
1566
+ if (candidates.length >= MAX_GUARDED_CANDIDATES) break;
1567
+ }
1568
+ DEF_HEAD_RE.lastIndex = 0;
1569
+ return candidates.filter((name) => definesGuarded(name, content));
1570
+ }
1571
+ function routeDelegationFacts(files) {
1572
+ const routes = [];
1573
+ const helpers = [];
1574
+ for (const f of files) {
1575
+ if (/\.[jt]sx?$/.test(f.path)) {
1576
+ const names = guardedHelperNames(f.content);
1577
+ if (names.length > 0) helpers.push({ path: f.path, names });
1578
+ }
1579
+ if (!ROUTE_FILE_RE.test(f.path) || OTHER_EXPORT_RE.test(f.content)) continue;
1580
+ const exported = firstMatches(f.content, HANDLER_EXPORT_RE, 8);
1581
+ if (exported.length === 0 || exported.length > 7) continue;
1582
+ const callees = /* @__PURE__ */ new Set();
1583
+ let readable = true;
1584
+ for (const e of exported) {
1585
+ DELEGATING_HANDLER_RE.lastIndex = e.index;
1586
+ const h = DELEGATING_HANDLER_RE.exec(f.content);
1587
+ if (h !== null) {
1588
+ callees.add(h[2]);
1589
+ continue;
1590
+ }
1591
+ HANDLER_HEAD_RE.lastIndex = e.index;
1592
+ const head = HANDLER_HEAD_RE.exec(f.content);
1593
+ const open = head === null ? -1 : e.index + head[0].length - 1;
1594
+ if (open === -1 || !opensWithGuard(f.content.slice(open, open + 4e3))) {
1595
+ readable = false;
1596
+ break;
1597
+ }
1598
+ }
1599
+ if (!readable) continue;
1600
+ routes.push({ path: f.path, callees: [...callees].map((fn) => ({ fn, spec: importSpec(fn, f.content) })) });
1601
+ }
1602
+ return { routes, helpers };
1603
+ }
1604
+ function resolveSecretGuardedRoutes(routes, helpers, paths) {
1605
+ const all = [...new Set(paths)];
1606
+ const guardedBy = /* @__PURE__ */ new Map();
1607
+ for (const h of helpers) {
1608
+ const set = guardedBy.get(h.path) ?? /* @__PURE__ */ new Set();
1609
+ for (const name of h.names) set.add(name);
1610
+ guardedBy.set(h.path, set);
1611
+ }
1612
+ const out = /* @__PURE__ */ new Set();
1613
+ for (const route of routes) {
1614
+ const ok = route.callees.every(({ fn, spec }) => {
1615
+ if (spec === null) return false;
1616
+ const tail = spec.replace(/^(?:@|~)\//, "").replace(/^(?:\.\.?\/)+/, "");
1617
+ const tailRe = new RegExp(`(?:^|/)${esc(tail)}(?:\\.[jt]sx?|/index\\.[jt]sx?)$`);
1618
+ const targets = all.filter((p) => tailRe.test(p));
1619
+ if (targets.length !== 1) return false;
1620
+ return guardedBy.get(targets[0])?.has(fn) === true;
1621
+ });
1622
+ if (ok) out.add(route.path);
1623
+ }
1624
+ return out;
1625
+ }
1505
1626
 
1506
1627
  // src/project-context.ts
1507
1628
  var TOML_FN_SECTION_RE = /^\s*\[functions\.(?:"([^"]+)"|([A-Za-z0-9._-]+))\]\s*$/;
@@ -1769,12 +1890,34 @@ function normalize2(p) {
1769
1890
  return p;
1770
1891
  }
1771
1892
  function extractRoutes(files) {
1893
+ const claims = collectRouteClaims(files);
1894
+ return assembleRoutes([...claims.manifest?.routes ?? [], ...claims.declared]);
1895
+ }
1896
+ function mergeRouteClaims(parts) {
1897
+ const manifest = parts.find((part) => part.manifest !== null)?.manifest ?? null;
1898
+ return assembleRoutes([...manifest?.routes ?? [], ...parts.flatMap((part) => part.declared)]);
1899
+ }
1900
+ function assembleRoutes(claims) {
1772
1901
  const routes = /* @__PURE__ */ new Map();
1773
1902
  const frameworks = /* @__PURE__ */ new Set();
1774
- const add = (raw, framework, source, kindOverride) => {
1903
+ for (const claim of claims) {
1904
+ if (routes.has(claim.path)) continue;
1905
+ routes.set(claim.path, claim);
1906
+ frameworks.add(claim.framework);
1907
+ }
1908
+ return {
1909
+ frameworks: [...frameworks],
1910
+ routes: [...routes.values()].sort((a, b) => a.path.localeCompare(b.path))
1911
+ };
1912
+ }
1913
+ function collectRouteClaims(files) {
1914
+ const declaredPaths = /* @__PURE__ */ new Set();
1915
+ const declared = [];
1916
+ let manifest = null;
1917
+ const claim = (raw, framework, source, kindOverride) => {
1775
1918
  const path = normalize2(raw);
1776
- if (!path || routes.has(path)) return;
1777
- routes.set(path, {
1919
+ if (!path) return null;
1920
+ return {
1778
1921
  path,
1779
1922
  // The URL prefix is a fallback, not the authority. A Next `route.ts` is a
1780
1923
  // handler wherever it lives: /free-trust-page/claim/start/route.ts is an
@@ -1784,8 +1927,13 @@ function extractRoutes(files) {
1784
1927
  dynamic: DYNAMIC_RE.test(path),
1785
1928
  framework,
1786
1929
  source
1787
- });
1788
- frameworks.add(framework);
1930
+ };
1931
+ };
1932
+ const add = (raw, framework, source, kindOverride) => {
1933
+ const route = claim(raw, framework, source, kindOverride);
1934
+ if (!route || declaredPaths.has(route.path)) return;
1935
+ declaredPaths.add(route.path);
1936
+ declared.push(route);
1789
1937
  };
1790
1938
  const isNext = hasDep(files, "next");
1791
1939
  const isExpress = hasDep(files, "express") || hasDep(files, "hono") || hasDep(files, "koa");
@@ -1793,10 +1941,16 @@ function extractRoutes(files) {
1793
1941
  if (isTanstack) {
1794
1942
  const gen = files.find((f) => f.path.endsWith("routeTree.gen.ts"));
1795
1943
  if (gen) {
1944
+ const listed = [];
1945
+ const seen = /* @__PURE__ */ new Set();
1796
1946
  for (const m of gen.content.matchAll(/'(\/[A-Za-z0-9_\-/$.]*)'/g)) {
1797
1947
  if (m[1].split("/").some((seg) => seg.startsWith("_"))) continue;
1798
- add(m[1], "tanstack-router", gen.path);
1948
+ const route = claim(m[1], "tanstack-router", gen.path);
1949
+ if (!route || seen.has(route.path)) continue;
1950
+ seen.add(route.path);
1951
+ listed.push(route);
1799
1952
  }
1953
+ manifest = { source: gen.path, routes: listed };
1800
1954
  }
1801
1955
  }
1802
1956
  for (const file of files) {
@@ -1844,10 +1998,7 @@ function extractRoutes(files) {
1844
1998
  }
1845
1999
  }
1846
2000
  }
1847
- return {
1848
- frameworks: [...frameworks],
1849
- routes: [...routes.values()].sort((a, b) => a.path.localeCompare(b.path))
1850
- };
2001
+ return { manifest, declared };
1851
2002
  }
1852
2003
  function routeForFile(table, filePath) {
1853
2004
  const norm = filePath.replace(/^\.\//, "");
@@ -2101,10 +2252,24 @@ var CHECKABLE_CLAIMS = [
2101
2252
  { id: "verified-webhooks", label: "which webhooks are verified", example: "Stripe webhooks are signature verified." },
2102
2253
  { id: "rate-limited", label: "what is rate limited", example: "The public API is rate limited." }
2103
2254
  ];
2104
- function checkIntent(files, findings) {
2105
- const statements = harvestIntent(files);
2255
+ function mergeIntentStatements(parts) {
2256
+ const claimed = /* @__PURE__ */ new Set();
2257
+ const out = [];
2258
+ for (const part of parts) {
2259
+ const own = /* @__PURE__ */ new Set();
2260
+ for (const statement of part) {
2261
+ if (claimed.has(statement.source)) continue;
2262
+ own.add(statement.source);
2263
+ out.push(statement);
2264
+ }
2265
+ for (const source of own) claimed.add(source);
2266
+ }
2267
+ return out;
2268
+ }
2269
+ function checkIntent(files, findings, inputs = {}) {
2270
+ const statements = inputs.statements ?? harvestIntent(files);
2106
2271
  const sources = [...new Set(statements.map((s) => s.source))];
2107
- const routes = extractRoutes(files).routes;
2272
+ const routes = inputs.routes ?? extractRoutes(files).routes;
2108
2273
  const mismatches = [];
2109
2274
  const unchallenged = [];
2110
2275
  const uncheckable = [];
@@ -10585,13 +10750,12 @@ function parseReviewResponse(text) {
10585
10750
  file = Number(candidate.file);
10586
10751
  }
10587
10752
  const index = Number(candidate.index);
10753
+ const reason = !("reason" in candidate) || candidate.reason === void 0 ? "" : candidate.reason;
10588
10754
  valid.push({
10589
10755
  index,
10590
10756
  file,
10591
10757
  verdict: candidate.verdict,
10592
- // Non-strings stay non-strings. The verdict loop masks, then marks
10593
- // reviewed. A reason that cannot be masked throws and stays unreviewed.
10594
- reason: candidate.reason
10758
+ reason
10595
10759
  });
10596
10760
  }
10597
10761
  return valid;
@@ -11806,9 +11970,24 @@ function getSnippet5(content, line) {
11806
11970
  return `${prefix} ${String(lineNum).padStart(5)} | ${l}`;
11807
11971
  }).join("\n");
11808
11972
  }
11809
- function scanMultiFile(files) {
11973
+ function collectEnvNames(files, into) {
11974
+ for (const f of files) {
11975
+ if (/\.(ts|js|tsx|jsx)$/i.test(f.path)) {
11976
+ const matches = f.content.matchAll(/process\.env\.([A-Z_][A-Z0-9_]*)/g);
11977
+ for (const m of matches) {
11978
+ into.add(m[1]);
11979
+ }
11980
+ }
11981
+ }
11982
+ }
11983
+ function envVarNamesUsed(files) {
11984
+ const names = /* @__PURE__ */ new Set();
11985
+ collectEnvNames(files.filter((f) => !isSbomPath(f.path)), names);
11986
+ return [...names];
11987
+ }
11988
+ function scanMultiFile(files, opts = {}) {
11810
11989
  files = files.filter((f) => !isSbomPath(f.path));
11811
- const guarded = secretGuardedRoutes(files);
11990
+ const guarded = opts.guardedRoutes ?? secretGuardedRoutes(files);
11812
11991
  const findings = [];
11813
11992
  const fileMap = /* @__PURE__ */ new Map();
11814
11993
  for (const f of files) {
@@ -11926,15 +12105,8 @@ function scanMultiFile(files) {
11926
12105
  }
11927
12106
  const envExample = files.find((f) => /\.env\.example$|\.env\.sample$|\.env\.template$/.test(f.path));
11928
12107
  if (envExample) {
11929
- const envVarsUsed = /* @__PURE__ */ new Set();
11930
- for (const f of files) {
11931
- if (/\.(ts|js|tsx|jsx)$/i.test(f.path)) {
11932
- const matches = f.content.matchAll(/process\.env\.([A-Z_][A-Z0-9_]*)/g);
11933
- for (const m of matches) {
11934
- envVarsUsed.add(m[1]);
11935
- }
11936
- }
11937
- }
12108
+ const envVarsUsed = new Set(opts.priorEnvNames ?? []);
12109
+ collectEnvNames(files, envVarsUsed);
11938
12110
  const envVarsDocumented = /* @__PURE__ */ new Set();
11939
12111
  const exampleMatches = envExample.content.matchAll(/^([A-Z_][A-Z0-9_]*)=/gm);
11940
12112
  for (const m of exampleMatches) {
@@ -15738,6 +15910,7 @@ function scanQuality(input, rules = qualityRules) {
15738
15910
  classifyPublicEnvVar,
15739
15911
  classifyStatement,
15740
15912
  clientSideAuth,
15913
+ collectRouteClaims,
15741
15914
  complianceMap,
15742
15915
  consoleLogProduction,
15743
15916
  corsWildcard,
@@ -15753,6 +15926,7 @@ function scanQuality(input, rules = qualityRules) {
15753
15926
  emptyCatchBlock,
15754
15927
  enclosingHandler,
15755
15928
  envNotGitignored,
15929
+ envVarNamesUsed,
15756
15930
  evalUsage,
15757
15931
  exposedAuthSecret,
15758
15932
  exposedDebugMode,
@@ -15809,6 +15983,8 @@ function scanQuality(input, rules = qualityRules) {
15809
15983
  maskSecretValue,
15810
15984
  maskStringLiterals,
15811
15985
  matchIgnore,
15986
+ mergeIntentStatements,
15987
+ mergeRouteClaims,
15812
15988
  missingAuthMiddleware,
15813
15989
  missingCSP,
15814
15990
  missingErrorBoundary,
@@ -15837,6 +16013,8 @@ function scanQuality(input, rules = qualityRules) {
15837
16013
  refineDirectObjectFindings,
15838
16014
  replaceBlockComments,
15839
16015
  resetStructuralState,
16016
+ resolveSecretGuardedRoutes,
16017
+ routeDelegationFacts,
15840
16018
  routeForFile,
15841
16019
  routePathFromFile,
15842
16020
  runCustomRules,