xploitscan-shared-rules 2.4.24 → 2.4.25

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.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var SCANNER_VERSION = "2.4.24";
2
+ var SCANNER_VERSION = "2.4.25";
3
3
 
4
4
  // src/snippet.ts
5
5
  var cacheContent = null;
@@ -1945,7 +1945,7 @@ var PUBLIC_LAST = /^(?:login|logout|signup|register|forgot(?:-password)?|reset(?
1945
1945
  var SIDE_EFFECT_RE = /\.(?:save|deleteMany|deleteOne|updateMany|updateOne|createMany|findMany|findOne|findUnique|findFirst|findByIdAndDelete|findByIdAndUpdate|remove|destroy|delete|update|create|insert|insertOne|upsert)\s*\(|\.(?:query|execute|exec|rm)\s*\(|\b(?:kv|redis)\s*\.\s*(?:set|del)\s*\(|\bsql\s*`|\bfetch\s*\(|\b(?:readFileSync|readFile|writeFileSync|writeFile|createReadStream|unlinkSync|unlink|mkdirSync|mkdir|execFileSync|execFile|spawn)\s*\(|\bcookies\s*\(\s*\)\s*\.\s*set\s*\(|\bres\s*\.\s*(?:json|send|redirect|end)\s*\(|\bres\s*\.\s*status\s*\([^)]*\)\s*\.\s*(?:json|send|end)\s*\(|\breturn\b/g;
1946
1946
  function blankComments(content) {
1947
1947
  let open = content.indexOf("/*");
1948
- let blocked = content;
1948
+ let blocked2 = content;
1949
1949
  if (open !== -1) {
1950
1950
  const parts = [];
1951
1951
  let copied = 0;
@@ -1957,14 +1957,18 @@ function blankComments(content) {
1957
1957
  open = content.indexOf("/*", copied);
1958
1958
  }
1959
1959
  parts.push(content.slice(copied));
1960
- blocked = parts.join("");
1960
+ blocked2 = parts.join("");
1961
1961
  }
1962
- return blocked.replace(/(^|[^:])\/\/[^\n]*/gm, (m, lead) => lead + " ".repeat(m.length - lead.length));
1962
+ return blocked2.replace(/(^|[^:])\/\/[^\n]*/gm, (m, lead) => lead + " ".repeat(m.length - lead.length));
1963
1963
  }
1964
- function matchingCloser(text, open, openCh, closeCh) {
1964
+ var ARG_CAP = 300;
1965
+ var IF_BODY_CAP = 2e3;
1966
+ var BRACE_CAP = 2e4;
1967
+ function matchingCloser(text, open, openCh, closeCh, maxLen = Infinity) {
1965
1968
  let depth = 0;
1966
1969
  let quote = "";
1967
- for (let i = open; i < text.length; i++) {
1970
+ const end = maxLen === Infinity ? text.length : Math.min(text.length, open + maxLen);
1971
+ for (let i = open; i < end; i++) {
1968
1972
  const c = text[i];
1969
1973
  if (quote) {
1970
1974
  if (c === "\\") {
@@ -1986,6 +1990,237 @@ function matchingCloser(text, open, openCh, closeCh) {
1986
1990
  }
1987
1991
  return -1;
1988
1992
  }
1993
+ function escapeRegExp(value) {
1994
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1995
+ }
1996
+ function maskStrings(content) {
1997
+ const chars = content.split("");
1998
+ let i = 0;
1999
+ while (i < chars.length) {
2000
+ const c = chars[i];
2001
+ if (c === "'" || c === '"') {
2002
+ const quote = c;
2003
+ chars[i] = " ";
2004
+ i++;
2005
+ while (i < chars.length) {
2006
+ if (chars[i] === "\\") {
2007
+ chars[i] = " ";
2008
+ if (i + 1 < chars.length && chars[i + 1] !== "\n") chars[i + 1] = " ";
2009
+ i += 2;
2010
+ continue;
2011
+ }
2012
+ if (chars[i] === "\n") break;
2013
+ if (chars[i] === quote) {
2014
+ chars[i] = " ";
2015
+ i++;
2016
+ break;
2017
+ }
2018
+ chars[i] = " ";
2019
+ i++;
2020
+ }
2021
+ continue;
2022
+ }
2023
+ if (c === "`") {
2024
+ chars[i] = " ";
2025
+ i++;
2026
+ let expr = 0;
2027
+ while (i < chars.length) {
2028
+ if (expr === 0 && chars[i] === "\\") {
2029
+ chars[i] = " ";
2030
+ if (i + 1 < chars.length && chars[i + 1] !== "\n") chars[i + 1] = " ";
2031
+ i += 2;
2032
+ continue;
2033
+ }
2034
+ if (expr === 0 && chars[i] === "`") {
2035
+ chars[i] = " ";
2036
+ i++;
2037
+ break;
2038
+ }
2039
+ if (expr === 0 && chars[i] === "$" && chars[i + 1] === "{") {
2040
+ chars[i] = " ";
2041
+ chars[i + 1] = " ";
2042
+ expr = 1;
2043
+ i += 2;
2044
+ continue;
2045
+ }
2046
+ if (expr > 0) {
2047
+ if (chars[i] === "{") expr++;
2048
+ else if (chars[i] === "}") expr--;
2049
+ i++;
2050
+ continue;
2051
+ }
2052
+ if (chars[i] !== "\n") chars[i] = " ";
2053
+ i++;
2054
+ }
2055
+ continue;
2056
+ }
2057
+ i++;
2058
+ }
2059
+ return chars.join("");
2060
+ }
2061
+ var REJECT_RE = /\bthrow\b|\bredirect\s*\(|\b(?:status(?:Code)?|sendStatus)\s*[:(=]\s*40[13]\b/;
2062
+ function localImportedNames(clause) {
2063
+ const names = [];
2064
+ for (const part of clause.split(",")) {
2065
+ const bits = part.trim().split(/\s+as\s+/i);
2066
+ const local = (bits[1] ?? bits[0] ?? "").replace(/^\s*type\s+/, "").trim();
2067
+ const ident = /^([A-Za-z_$][\w$]*)$/.exec(local);
2068
+ if (ident) names.push(ident[1]);
2069
+ }
2070
+ return names;
2071
+ }
2072
+ function isImportedName(content, name) {
2073
+ const id = escapeRegExp(name);
2074
+ if (new RegExp(`\\bimport\\s+${id}\\s+from\\b`).test(content)) return true;
2075
+ if (new RegExp(`\\b(?:const|let|var)\\s+${id}\\s*=\\s*require\\s*\\(`).test(content)) return true;
2076
+ for (const match of content.matchAll(/\bimport\s+(?:type\s+)?\{([^}]+)\}\s*from/g)) {
2077
+ if (localImportedNames(match[1]).includes(name)) return true;
2078
+ }
2079
+ for (const match of content.matchAll(/\b(?:const|let|var)\s*\{([^}]+)\}\s*=\s*require\s*\(/g)) {
2080
+ if (localImportedNames(match[1]).includes(name)) return true;
2081
+ }
2082
+ return false;
2083
+ }
2084
+ function jwtBindingSpan(file, name) {
2085
+ const id = escapeRegExp(name);
2086
+ const patterns = [
2087
+ new RegExp(`\\bimport\\s+${id}\\s+from\\s+["']jsonwebtoken["']`),
2088
+ new RegExp(`\\bimport\\s*\\*\\s*as\\s+${id}\\s+from\\s+["']jsonwebtoken["']`),
2089
+ new RegExp(
2090
+ `(?:const|let|var)\\s+${id}\\s*=\\s*require\\(\\s*["']jsonwebtoken["']\\s*\\)\\s*;?(?=\\s*(?:$|\\n|\\r))`
2091
+ )
2092
+ ];
2093
+ for (const pattern of patterns) {
2094
+ const match = pattern.exec(file);
2095
+ if (!match || match.index === void 0) continue;
2096
+ return [match.index, match.index + match[0].length];
2097
+ }
2098
+ return null;
2099
+ }
2100
+ function bindingIsShadowed(file, name, span) {
2101
+ const id = escapeRegExp(name);
2102
+ const checks = [
2103
+ new RegExp(`\\b(?:const|let|var)\\s+${id}\\b`, "g"),
2104
+ new RegExp(`\\b${id}\\s*=(?!=)`, "g"),
2105
+ new RegExp(`[(,]\\s*${id}\\s*[,)=:]`, "g")
2106
+ ];
2107
+ for (const pattern of checks) {
2108
+ let match;
2109
+ while (match = pattern.exec(file)) {
2110
+ if (match.index >= span[0] && match.index < span[1]) continue;
2111
+ return true;
2112
+ }
2113
+ }
2114
+ return false;
2115
+ }
2116
+ function identifierIsObjectLiteral(file, name) {
2117
+ const id = escapeRegExp(name);
2118
+ return new RegExp(`(?:\\b(?:const|let|var)\\s+|(?<![\\w$.]))${id}\\s*=(?!=)\\s*\\{`).test(file);
2119
+ }
2120
+ function verifyCallForm(source, callIndex, file) {
2121
+ const open = source.indexOf("(", callIndex);
2122
+ if (open < 0) return "none";
2123
+ const close = matchingCloser(source, open, "(", ")", ARG_CAP);
2124
+ if (close < 0) return "none";
2125
+ const args = splitArgs(source.slice(open + 1, close)).map((part) => part.trim()).filter(Boolean);
2126
+ if (args.length === 2) return "sync";
2127
+ if (args.length >= 4) return "callback";
2128
+ if (args.length !== 3) return "none";
2129
+ const third = args[2];
2130
+ if (/=>/.test(third) || /^\s*(?:async\s+)?function\b/.test(third)) return "callback";
2131
+ if (/^\s*\{/.test(third)) return "sync";
2132
+ const ident = /^\s*([A-Za-z_$][\w$]*)\s*$/.exec(third);
2133
+ if (!ident) return "callback";
2134
+ return identifierIsObjectLiteral(file, ident[1]) ? "sync" : "callback";
2135
+ }
2136
+ function syncJwtCredits(body, file) {
2137
+ const masked = maskStrings(body);
2138
+ const re = /\b([A-Za-z_$][\w$]*)\s*\.\s*verify\s*\(/g;
2139
+ let match;
2140
+ while (match = re.exec(masked)) {
2141
+ const span = jwtBindingSpan(file, match[1]);
2142
+ if (!span || bindingIsShadowed(file, match[1], span)) continue;
2143
+ if (verifyCallForm(masked, match.index, file) !== "sync") continue;
2144
+ if (!errorPathRejects(masked, match.index)) continue;
2145
+ return true;
2146
+ }
2147
+ return false;
2148
+ }
2149
+ var fnCacheContent = null;
2150
+ var fnCacheSpans = [];
2151
+ function headerIsFunction(content, brace) {
2152
+ const header = content.slice(Math.max(0, brace - 180), brace);
2153
+ const last = header.slice(header.lastIndexOf("\n") + 1);
2154
+ if (/=>\s*$/.test(last) || /\bfunction\b/.test(last)) return true;
2155
+ const method = /^\s*(?:(?:public|private|protected|static|async|override)\s+)*([A-Za-z_$][\w$]*)\s*\([^)]*\)\s*(?::\s*[^{]+)?$/.exec(last);
2156
+ if (!method) return false;
2157
+ return !/^(?:if|for|while|switch|catch|function|return|else|try)$/.test(method[1]);
2158
+ }
2159
+ function buildFnSpans(content) {
2160
+ const spans = [];
2161
+ const stack = [];
2162
+ let quote = "";
2163
+ for (let i = 0; i < content.length; i++) {
2164
+ const c = content[i];
2165
+ if (quote) {
2166
+ if (c === "\\" && quote !== "`") {
2167
+ i++;
2168
+ continue;
2169
+ }
2170
+ if (c === quote) quote = "";
2171
+ else if (quote !== "`" && c === "\n") quote = "";
2172
+ continue;
2173
+ }
2174
+ if (c === "'" || c === '"' || c === "`") {
2175
+ quote = c;
2176
+ continue;
2177
+ }
2178
+ if (c === "{") stack.push({ open: i, isFn: headerIsFunction(content, i) });
2179
+ else if (c === "}" && stack.length) {
2180
+ const frame = stack.pop();
2181
+ if (frame?.isFn) spans.push({ open: frame.open, close: i });
2182
+ }
2183
+ }
2184
+ for (const frame of stack) {
2185
+ if (frame.isFn) spans.push({ open: frame.open, close: content.length });
2186
+ }
2187
+ return spans;
2188
+ }
2189
+ var fnPrefixMax = [];
2190
+ function prepareFnCache(content) {
2191
+ if (fnCacheContent === content) return;
2192
+ fnCacheContent = content;
2193
+ fnCacheSpans = buildFnSpans(content).sort((a, b) => a.open - b.open || a.close - b.close);
2194
+ fnPrefixMax = new Array(fnCacheSpans.length);
2195
+ for (let i = 0; i < fnCacheSpans.length; i++) {
2196
+ fnPrefixMax[i] = i === 0 ? fnCacheSpans[i].close : Math.max(fnPrefixMax[i - 1], fnCacheSpans[i].close);
2197
+ }
2198
+ }
2199
+ function fnKeyAt(content, index) {
2200
+ prepareFnCache(content);
2201
+ const spans = fnCacheSpans;
2202
+ let lo = 0;
2203
+ let hi = spans.length - 1;
2204
+ let found = -1;
2205
+ while (lo <= hi) {
2206
+ const mid = lo + hi >> 1;
2207
+ if (spans[mid].open < index) {
2208
+ found = mid;
2209
+ lo = mid + 1;
2210
+ } else {
2211
+ hi = mid - 1;
2212
+ }
2213
+ }
2214
+ for (let i = found; i >= 0; i--) {
2215
+ if (fnPrefixMax[i] < index) break;
2216
+ const span = spans[i];
2217
+ if (index <= span.close) {
2218
+ if (index - span.open > BRACE_CAP) return "exhausted";
2219
+ return "fn@" + span.open;
2220
+ }
2221
+ }
2222
+ return "top";
2223
+ }
1989
2224
  function firstSideEffect(handler) {
1990
2225
  SIDE_EFFECT_RE.lastIndex = 0;
1991
2226
  const match = SIDE_EFFECT_RE.exec(handler);
@@ -1996,39 +2231,51 @@ function beforeSideEffect(handler, index) {
1996
2231
  return at === -1 || index < at;
1997
2232
  }
1998
2233
  function localGuardBody(content, name) {
1999
- const fn = new RegExp(`\\b(?:async\\s+)?function\\s+${name}\\s*\\([^)]*\\)\\s*\\{`).exec(content);
2234
+ const id = escapeRegExp(name);
2235
+ const fn = new RegExp(`\\b(?:async\\s+)?function\\s+${id}\\s*\\([^)]*\\)\\s*\\{`).exec(content);
2000
2236
  if (fn && fn.index !== void 0) {
2001
2237
  const brace = content.indexOf("{", fn.index);
2002
- const end = matchingCloser(content, brace, "{", "}");
2238
+ const end = matchingCloser(content, brace, "{", "}", BRACE_CAP);
2003
2239
  return end < 0 ? "" : content.slice(brace + 1, end);
2004
2240
  }
2005
2241
  const arrow = new RegExp(
2006
- `\\b(?:const|let|var)\\s+${name}\\s*=\\s*(?:async\\s*)?(?:function\\s*\\([^)]*\\)|(?:\\([^)]*\\)|[A-Za-z_]\\w*)\\s*=>)`
2242
+ `\\b(?:const|let|var)\\s+${id}\\s*=\\s*(?:async\\s*)?(?:function\\s*\\([^)]*\\)|(?:\\([^)]*\\)|[A-Za-z_]\\w*)\\s*=>)`
2007
2243
  ).exec(content);
2008
2244
  if (!arrow || arrow.index === void 0) return null;
2009
2245
  const after = arrow.index + arrow[0].length;
2010
2246
  const rest = content.slice(after);
2011
2247
  if (/^\s*\{/.test(rest)) {
2012
2248
  const brace = content.indexOf("{", after);
2013
- const end = matchingCloser(content, brace, "{", "}");
2249
+ const end = matchingCloser(content, brace, "{", "}", BRACE_CAP);
2014
2250
  return end < 0 ? "" : content.slice(brace + 1, end);
2015
2251
  }
2016
2252
  const semi = content.indexOf(";", after);
2017
2253
  return content.slice(after, semi === -1 ? content.length : semi);
2018
2254
  }
2019
- function guardBodyQualifies(body) {
2020
- const flat = body.replace(/\s+/g, " ").trim();
2255
+ function guardBodyQualifies(body, file) {
2256
+ const masked = maskStrings(body);
2257
+ const flat = masked.replace(/\s+/g, " ").trim();
2021
2258
  if (!flat) return false;
2022
2259
  if (/^(?:return\s+)?next\s*\(\s*\)\s*;?$/.test(flat)) return false;
2023
- return /\bthrow\b|\bredirect\s*\(|\bauth\s*\(|\bgetServerSession\s*\(|\bgetUser\s*\(|\bjwt\s*\.\s*verify\s*\(|\b(?:status(?:Code)?|sendStatus)\s*[:(]\s*40[13]\b/.test(body);
2260
+ if (REJECT_RE.test(masked)) return true;
2261
+ return syncJwtCredits(body, file);
2024
2262
  }
2263
+ var guardCacheContent = null;
2264
+ var guardCache = /* @__PURE__ */ new Map();
2025
2265
  function guardNameCounts(content, name) {
2266
+ if (guardCacheContent !== content) {
2267
+ guardCacheContent = content;
2268
+ guardCache.clear();
2269
+ }
2270
+ const cached = guardCache.get(name);
2271
+ if (cached !== void 0) return cached;
2026
2272
  const body = localGuardBody(content, name);
2027
- if (body === null) return true;
2028
- return guardBodyQualifies(body);
2273
+ const result = body === null ? isImportedName(content, name) : guardBodyQualifies(body, content);
2274
+ guardCache.set(name, result);
2275
+ return result;
2029
2276
  }
2030
2277
  function guardMentioned(content, args) {
2031
- return GUARD_NAMES.some((name) => new RegExp(`\\b${name}\\b`).test(args) && guardNameCounts(content, name));
2278
+ return GUARD_NAMES.some((name) => new RegExp(`\\b${escapeRegExp(name)}\\b`).test(args) && guardNameCounts(content, name));
2032
2279
  }
2033
2280
  function handlerBodyOpen(handler) {
2034
2281
  const arrow = handler.search(/=>\s*\{/);
@@ -2069,16 +2316,38 @@ function guardStatementCounts(handler, callIndex) {
2069
2316
  else if (c === ")") paren--;
2070
2317
  else if (c === "[") bracket++;
2071
2318
  else if (c === "]") bracket--;
2072
- else if (c === ";" && brace === 1 && paren === 0 && bracket === 0) stmtStart = i + 1;
2319
+ else if (c === "\n" && brace === 1 && paren === 0 && bracket === 0) {
2320
+ let j = i - 1;
2321
+ while (j > bodyOpen && /[ \t\r]/.test(handler[j])) j--;
2322
+ const prev = handler[j] ?? "";
2323
+ if (!",([{+-*/=&|?:.".includes(prev)) stmtStart = i + 1;
2324
+ } else if (c === ";" && brace === 1 && paren === 0 && bracket === 0) stmtStart = i + 1;
2073
2325
  }
2074
2326
  if (brace !== 1 || paren !== 0 || bracket !== 0) return false;
2075
2327
  const stmt = handler.slice(stmtStart, callIndex);
2076
2328
  if (/&&|\|\||\?\?|\?(?!\.)/.test(stmt)) return false;
2077
- if (/\b(?:if|while|for|switch)\b/.test(stmt)) return false;
2329
+ if (/\b(?:if|else|while|for|switch|catch|try)\b/.test(stmt)) return false;
2078
2330
  if (!/\b(?:const|let|var)\b|=/.test(stmt)) return true;
2079
- const assigned = /\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)/.exec(stmt)?.[1] ?? /([A-Za-z_$][\w$]*)\s*=\s*(?:await\s*)?$/.exec(stmt)?.[1];
2331
+ const assigned = lastAssignedName(stmt);
2080
2332
  if (!assigned) return false;
2081
- return new RegExp(`\\b${assigned}\\b`).test(handler.slice(callIndex + 1));
2333
+ return new RegExp(`\\b${escapeRegExp(assigned)}\\b`).test(handler.slice(callIndex + 1));
2334
+ }
2335
+ function lastAssignedName(stmt) {
2336
+ let name;
2337
+ let at = -1;
2338
+ for (const match of stmt.matchAll(/\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)/g)) {
2339
+ if (match.index !== void 0 && match.index >= at) {
2340
+ at = match.index;
2341
+ name = match[1];
2342
+ }
2343
+ }
2344
+ for (const match of stmt.matchAll(/([A-Za-z_$][\w$]*)\s*=(?!=)/g)) {
2345
+ if (match.index !== void 0 && match.index >= at) {
2346
+ at = match.index;
2347
+ name = match[1];
2348
+ }
2349
+ }
2350
+ return name;
2082
2351
  }
2083
2352
  function guardCallBeforeSideEffect(content, handler) {
2084
2353
  const re = new RegExp(`\\b(${GUARD_NAMES.join("|")})\\s*\\(`, "g");
@@ -2104,6 +2373,34 @@ function authPrefixCovers(prefix, path) {
2104
2373
  const normalized = prefix.length > 1 ? prefix.replace(/\/+$/, "") : prefix;
2105
2374
  return path === normalized || path.startsWith(normalized + "/");
2106
2375
  }
2376
+ var useCacheContent = null;
2377
+ var useCacheByKey = /* @__PURE__ */ new Map();
2378
+ function usesInScope(content, key) {
2379
+ if (useCacheContent === content) return useCacheByKey.get(key) ?? [];
2380
+ const byKey = /* @__PURE__ */ new Map();
2381
+ const re = /\b([A-Za-z_$][\w$]*)\s*\.\s*use\s*\(/g;
2382
+ let match;
2383
+ while (match = re.exec(content)) {
2384
+ const open = match.index + match[0].length - 1;
2385
+ const close = matchingCloser(content, open, "(", ")", ARG_CAP);
2386
+ if (close < 0) continue;
2387
+ const args = content.slice(open + 1, close);
2388
+ const siteKey = fnKeyAt(content, match.index);
2389
+ const site = {
2390
+ index: match.index,
2391
+ receiver: match[1],
2392
+ args,
2393
+ first: (splitArgs(args)[0] ?? "").trim(),
2394
+ key: siteKey
2395
+ };
2396
+ const list = byKey.get(siteKey);
2397
+ if (list) list.push(site);
2398
+ else byKey.set(siteKey, [site]);
2399
+ }
2400
+ useCacheContent = content;
2401
+ useCacheByKey = byKey;
2402
+ return byKey.get(key) ?? [];
2403
+ }
2107
2404
  function useCoversRoute(content, index) {
2108
2405
  const lineStart = content.lastIndexOf("\n", Math.max(0, index - 1)) + 1;
2109
2406
  const lineEnd = content.indexOf("\n", index);
@@ -2112,17 +2409,17 @@ function useCoversRoute(content, index) {
2112
2409
  if (!route) return false;
2113
2410
  const receiver = route[1];
2114
2411
  const path = route[2];
2115
- const useRe = new RegExp(`\\b${receiver}\\s*\\.\\s*use\\s*\\(`, "g");
2116
- let match;
2117
- while (match = useRe.exec(content)) {
2118
- if (match.index >= lineStart) continue;
2119
- const open = content.indexOf("(", match.index);
2120
- const close = matchingCloser(content, open, "(", ")");
2121
- if (close < 0) continue;
2122
- const args = content.slice(open + 1, close);
2123
- const prefix = /^\s*["'`]([^"'`]+)["'`]/.exec(args)?.[1] ?? "";
2412
+ const routeKey = fnKeyAt(content, index);
2413
+ if (routeKey === "exhausted") return false;
2414
+ for (const site of usesInScope(content, routeKey)) {
2415
+ if (site.receiver !== receiver || site.index >= lineStart) continue;
2416
+ const stringPrefix = /^\s*["'`]([^"'`]*)["'`]/.exec(site.first)?.[1];
2417
+ const firstIsGuard = /^[A-Za-z_$][\w$]*$/.test(site.first) && GUARD_NAMES.includes(site.first) && guardNameCounts(content, site.first);
2418
+ if (stringPrefix === void 0 && !firstIsGuard) continue;
2419
+ const prefix = stringPrefix ?? "";
2124
2420
  if (prefix && !authPrefixCovers(prefix, path)) continue;
2125
- if (guardMentioned(content, args)) return true;
2421
+ if (!guardMentioned(content, site.args) && !firstIsGuard) continue;
2422
+ return true;
2126
2423
  }
2127
2424
  return false;
2128
2425
  }
@@ -2185,17 +2482,29 @@ function splitArgs(args) {
2185
2482
  parts.push(args.slice(start));
2186
2483
  return parts;
2187
2484
  }
2485
+ function unwrapSecret(value) {
2486
+ let bare = value.trim().replace(/!+$/, "").trim();
2487
+ for (let n = 0; n < 4; n++) {
2488
+ if (!bare.startsWith("(")) break;
2489
+ const end = matchingCloser(bare, 0, "(", ")", ARG_CAP);
2490
+ if (end < 0 || end !== bare.length - 1) break;
2491
+ bare = bare.slice(1, -1).trim().replace(/!+$/, "").trim();
2492
+ }
2493
+ return bare;
2494
+ }
2495
+ function secretValueReal(value) {
2496
+ const bare = unwrapSecret(value);
2497
+ if (bare === '""' || bare === "''" || bare === "``" || bare === "undefined" || bare === "") return false;
2498
+ if (/(\|\||\?\?)\s*(["'`])[^"'`]*\2\s*$/.test(bare)) return false;
2499
+ return true;
2500
+ }
2188
2501
  function secretArgOk(args) {
2189
2502
  const parts = splitArgs(args).map((part) => part.trim()).filter(Boolean);
2190
- const empty = (value) => {
2191
- const bare = value.replace(/!+$/, "").trim();
2192
- return bare === '""' || bare === "''" || bare === "``" || bare === "undefined" || bare === "";
2193
- };
2194
- if (parts.length >= 3) return !empty(parts[2]);
2503
+ if (parts.length >= 3) return secretValueReal(parts[2]);
2195
2504
  if (parts.length === 1 && parts[0].startsWith("{")) {
2196
2505
  const prop = parts[0].match(/\b(?:secret|signingKey|currentSigningKey|token)\s*:\s*([^,}\n]+)/i);
2197
2506
  if (!prop) return false;
2198
- return !empty(prop[1]);
2507
+ return secretValueReal(prop[1]);
2199
2508
  }
2200
2509
  return false;
2201
2510
  }
@@ -2214,7 +2523,7 @@ function errorPathRejects(handler, callIndex) {
2214
2523
  if (!caught) return true;
2215
2524
  const brace = handler.indexOf("{", caught.index);
2216
2525
  if (brace < 0) return false;
2217
- const end = matchingCloser(handler, brace, "{", "}");
2526
+ const end = matchingCloser(handler, brace, "{", "}", BRACE_CAP);
2218
2527
  if (end < 0) return false;
2219
2528
  const body = handler.slice(brace, end);
2220
2529
  return /\bthrow\b/.test(body) || /\breturn\b/.test(body) && /\b4\d\d\b/.test(body);
@@ -2224,7 +2533,7 @@ function signatureCheckQualifies(handler) {
2224
2533
  let match;
2225
2534
  while (match = callRe.exec(handler)) {
2226
2535
  const open = match.index + match[0].length - 1;
2227
- const close = matchingCloser(handler, open, "(", ")");
2536
+ const close = matchingCloser(handler, open, "(", ")", ARG_CAP);
2228
2537
  if (close < 0) continue;
2229
2538
  const args = handler.slice(open + 1, close);
2230
2539
  if (!/\.text\s*\(/.test(handler) && !/\.text\s*\(/.test(args)) continue;
@@ -2236,37 +2545,551 @@ function signatureCheckQualifies(handler) {
2236
2545
  }
2237
2546
  return false;
2238
2547
  }
2548
+ function ifBranchBody(handler, closeParen) {
2549
+ let i = closeParen + 1;
2550
+ while (i < handler.length && /\s/.test(handler[i])) i++;
2551
+ if (i >= handler.length) return null;
2552
+ if (handler[i] === "{") {
2553
+ const end = matchingCloser(handler, i, "{", "}", IF_BODY_CAP);
2554
+ if (end < 0) return null;
2555
+ return handler.slice(i + 1, end);
2556
+ }
2557
+ const limit = Math.min(handler.length, i + IF_BODY_CAP);
2558
+ const semi = handler.indexOf(";", i);
2559
+ if (semi < 0 || semi >= limit) return null;
2560
+ return handler.slice(i, semi);
2561
+ }
2562
+ function branchRejects401(body) {
2563
+ return /\breturn\b/.test(body) && /\b401\b/.test(body) || /\b(?:status|sendStatus)\s*\(\s*401\b/.test(body);
2564
+ }
2239
2565
  function cronSecretGates(handler) {
2240
2566
  const re = /\bif\s*\(/g;
2241
2567
  let match;
2242
2568
  while (match = re.exec(handler)) {
2243
2569
  const open = match.index + match[0].length - 1;
2244
- const close = matchingCloser(handler, open, "(", ")");
2570
+ const close = matchingCloser(handler, open, "(", ")", ARG_CAP);
2245
2571
  if (close < 0) continue;
2246
2572
  const cond = handler.slice(open + 1, close);
2247
2573
  if (!/\bCRON_SECRET\b/.test(cond)) continue;
2574
+ if (/CRON_SECRET\s*(?:&&|\?)/.test(cond)) continue;
2248
2575
  if (!/(?:!==|!=(?!=))/.test(cond)) continue;
2249
2576
  if (!beforeSideEffect(handler, match.index)) continue;
2250
- const after = handler.slice(close, close + 400);
2251
- if (/\breturn\b/.test(after) && /\b401\b/.test(after)) return true;
2252
- if (/\bstatus\s*\(\s*401\b/.test(after)) return true;
2577
+ const body = ifBranchBody(handler, close);
2578
+ if (body && branchRejects401(body)) return true;
2253
2579
  }
2254
2580
  return false;
2255
2581
  }
2256
- function widenedAuthSuppresses(content, filePath, index, handler) {
2582
+ function handlerVerb(content, index, _handler) {
2583
+ const lineStart = content.lastIndexOf("\n", Math.max(0, index - 1)) + 1;
2584
+ const lineEnd = content.indexOf("\n", Math.max(index, lineStart));
2585
+ const line = content.slice(lineStart, lineEnd === -1 ? content.length : lineEnd);
2586
+ const registered = line.match(/\b[A-Za-z_$][\w$]*\s*\.\s*(get|head|post|put|patch|delete|all)\s*\(\s*["'`]/i);
2587
+ if (registered) return registered[1].toLowerCase();
2588
+ const windowStart = Math.max(0, index - 120);
2589
+ const window = content.slice(windowStart, index + 180);
2590
+ const re = /export\s+(?:async\s+)?function\s+(GET|HEAD|POST|PUT|PATCH|DELETE)\b|export\s+const\s+(GET|HEAD|POST|PUT|PATCH|DELETE)\s*(?::[^=\n]{0,120})?=/g;
2591
+ let best = null;
2592
+ let match;
2593
+ while (match = re.exec(window)) {
2594
+ const at = windowStart + match.index;
2595
+ if (at > index) continue;
2596
+ const verb = (match[1] || match[2]).toLowerCase();
2597
+ if (!best || Math.abs(at - index) < Math.abs(best.at - index)) best = { at, verb };
2598
+ }
2599
+ return best ? best.verb : null;
2600
+ }
2601
+ var authFaultForTest = false;
2602
+ function widenedAuthSuppressesBody(content, filePath, index, handler) {
2257
2603
  if (!handler) return false;
2258
2604
  const masked = blankComments(content);
2259
2605
  const maskedHandler = blankComments(handler);
2606
+ prepareFnCache(masked);
2260
2607
  if (guardCallBeforeSideEffect(masked, maskedHandler)) return true;
2261
2608
  if (routeArgsQualify(masked, maskedHandler)) return true;
2262
2609
  if (useCoversRoute(masked, index)) return true;
2263
2610
  const routePath = routePathFromFile(filePath) ?? expressRoutePath(maskedHandler);
2264
2611
  if (routePath && isAnchoredPublicAuthPath(routePath) && !mutatesKeyedOnParams(maskedHandler)) return true;
2265
- if (routePath && isOgRoutePath(routePath)) return true;
2612
+ if (routePath && isOgRoutePath(routePath)) {
2613
+ const verb = handlerVerb(masked, index, maskedHandler);
2614
+ if (verb === "get" || verb === "head") return true;
2615
+ }
2266
2616
  if (signatureCheckQualifies(maskedHandler)) return true;
2267
2617
  if (cronSecretGates(maskedHandler)) return true;
2268
2618
  return false;
2269
2619
  }
2620
+ function widenedAuthSuppresses(content, filePath, index, handler) {
2621
+ try {
2622
+ if (authFaultForTest) throw new Error("injected");
2623
+ return widenedAuthSuppressesBody(content, filePath, index, handler);
2624
+ } catch {
2625
+ return false;
2626
+ }
2627
+ }
2628
+
2629
+ // src/weak-hash.ts
2630
+ var ARG_CAP2 = 300;
2631
+ var PF_RE = /password|passwd|passphrase|pwd/;
2632
+ var TOKEN_WORDS = ["token", "code", "otp", "nonce"];
2633
+ var RESET_WORDS = ["reset", "forgot", "recover"];
2634
+ var HASH_VERBS = ["hash", "encrypt", "digest", "verify"];
2635
+ var KEY_BLOCK = ["token", "code", "otp", "nonce", "reset", "forgot", "recover"];
2636
+ function escapeRegExp2(value) {
2637
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2638
+ }
2639
+ function nameTokens(text) {
2640
+ const out = [];
2641
+ for (const part of text.split(/[^A-Za-z0-9]+/)) {
2642
+ if (!part) continue;
2643
+ const bits = part.split(
2644
+ /(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|(?<=[A-Za-z])(?=\d)|(?<=\d)(?=[A-Za-z])/
2645
+ );
2646
+ for (const bit of bits) if (bit) out.push(bit.toLowerCase());
2647
+ const lower = part.toLowerCase();
2648
+ if (PF_RE.test(lower) && !lower.includes("passwordless") && !bits.some((bit) => bit && PF_RE.test(bit.toLowerCase()))) {
2649
+ out.push(lower);
2650
+ }
2651
+ }
2652
+ return out;
2653
+ }
2654
+ function isPf(token) {
2655
+ return PF_RE.test(token) && !token.includes("passwordless");
2656
+ }
2657
+ function wordIn(tokens, word) {
2658
+ if (tokens.some((token) => token === word)) return true;
2659
+ return tokens.some((token) => isPf(token) && token.includes(word));
2660
+ }
2661
+ function hasPf(text) {
2662
+ return nameTokens(text).some(isPf);
2663
+ }
2664
+ function isTokenValue(expr) {
2665
+ const tokens = nameTokens(expr);
2666
+ const pf = tokens.filter(isPf);
2667
+ if (pf.some((token) => !TOKEN_WORDS.some((word) => token.includes(word)))) return false;
2668
+ if (TOKEN_WORDS.some((word) => tokens.includes(word))) return true;
2669
+ return TOKEN_WORDS.some((word) => wordIn(tokens, word));
2670
+ }
2671
+ function blocked(text, words) {
2672
+ const tokens = nameTokens(text);
2673
+ return words.some((word) => wordIn(tokens, word));
2674
+ }
2675
+ function namedHasher(name, expr) {
2676
+ if (!hasPf(name)) return false;
2677
+ if (blocked(name, RESET_WORDS) && isTokenValue(expr)) return false;
2678
+ return true;
2679
+ }
2680
+ function hasHashVerb(name) {
2681
+ const tokens = nameTokens(name);
2682
+ return HASH_VERBS.some((word) => wordIn(tokens, word));
2683
+ }
2684
+ function perCallKeeps(name, expr) {
2685
+ if (isTokenValue(expr)) return namedHasher(name, expr) && hasHashVerb(name);
2686
+ return hasPf(expr) || namedHasher(name, expr);
2687
+ }
2688
+ function hashedExprAt(content, index) {
2689
+ const slice = content.slice(index, index + ARG_CAP2);
2690
+ const updateAt = slice.search(/\.update\s*\(/);
2691
+ if (updateAt >= 0) {
2692
+ const open = index + slice.indexOf("(", updateAt);
2693
+ const close = matchingCloser(content, open, "(", ")", ARG_CAP2);
2694
+ return close < 0 ? "" : content.slice(open + 1, close);
2695
+ }
2696
+ const libAt = slice.search(/hashlib\s*\.\s*(?:md5|sha1|sha256)\s*\(/);
2697
+ if (libAt >= 0) {
2698
+ const open = index + slice.indexOf("(", libAt);
2699
+ const close = matchingCloser(content, open, "(", ")", ARG_CAP2);
2700
+ return close < 0 ? "" : content.slice(open + 1, close);
2701
+ }
2702
+ return slice.slice(0, 160);
2703
+ }
2704
+ var METHOD_RE = /^\s*(?:(?:public|private|protected|static|async|override)\s+)*([A-Za-z_$][\w$]*)\s*\([^)]*\)\s*(?::\s*[^{]+)?$/;
2705
+ var METHOD_SKIP = /* @__PURE__ */ new Set(["if", "for", "while", "switch", "catch", "function", "return"]);
2706
+ function methodHeaderName(header) {
2707
+ const last = header.slice(header.lastIndexOf("\n") + 1);
2708
+ const method = METHOD_RE.exec(last);
2709
+ if (!method || METHOD_SKIP.has(method[1])) return "";
2710
+ return method[1];
2711
+ }
2712
+ var memo = null;
2713
+ var helperFault = false;
2714
+ function lineStartsOf(content) {
2715
+ const starts = [0];
2716
+ for (let i = 0; i < content.length; i++) if (content[i] === "\n") starts.push(i + 1);
2717
+ return starts;
2718
+ }
2719
+ function lineNoAt(content, index) {
2720
+ const starts = memo && memo.content === content ? memo.lineStarts : lineStartsOf(content);
2721
+ let lo = 0;
2722
+ let hi = starts.length - 1;
2723
+ while (lo < hi) {
2724
+ const mid = lo + hi + 1 >> 1;
2725
+ if (starts[mid] <= index) lo = mid;
2726
+ else hi = mid - 1;
2727
+ }
2728
+ return lo + 1;
2729
+ }
2730
+ function maskCallSites(content, py) {
2731
+ const chars = content.split("");
2732
+ let i = 0;
2733
+ const blankUntil = (pred) => {
2734
+ while (i < chars.length && !pred(i)) {
2735
+ if (chars[i] !== "\n") chars[i] = " ";
2736
+ i++;
2737
+ }
2738
+ };
2739
+ while (i < chars.length) {
2740
+ const c = chars[i];
2741
+ if (!py && c === "/" && chars[i + 1] === "/") {
2742
+ while (i < chars.length && chars[i] !== "\n") {
2743
+ chars[i] = " ";
2744
+ i++;
2745
+ }
2746
+ continue;
2747
+ }
2748
+ if (!py && c === "/" && chars[i + 1] === "*") {
2749
+ chars[i] = " ";
2750
+ chars[i + 1] = " ";
2751
+ i += 2;
2752
+ blankUntil((n) => chars[n] === "*" && chars[n + 1] === "/");
2753
+ if (i < chars.length) {
2754
+ chars[i] = " ";
2755
+ chars[i + 1] = " ";
2756
+ i += 2;
2757
+ }
2758
+ continue;
2759
+ }
2760
+ if (py && c === "#") {
2761
+ while (i < chars.length && chars[i] !== "\n") {
2762
+ chars[i] = " ";
2763
+ i++;
2764
+ }
2765
+ continue;
2766
+ }
2767
+ if ((c === '"' || c === "'") && chars[i + 1] === c && chars[i + 2] === c) {
2768
+ const q = c;
2769
+ chars[i] = chars[i + 1] = chars[i + 2] = " ";
2770
+ i += 3;
2771
+ blankUntil((n) => chars[n] === q && chars[n + 1] === q && chars[n + 2] === q);
2772
+ if (i < chars.length) {
2773
+ chars[i] = chars[i + 1] = chars[i + 2] = " ";
2774
+ i += 3;
2775
+ }
2776
+ continue;
2777
+ }
2778
+ if (c === "'" || c === '"') {
2779
+ const q = c;
2780
+ chars[i] = " ";
2781
+ i++;
2782
+ while (i < chars.length) {
2783
+ if (chars[i] === "\\") {
2784
+ chars[i] = " ";
2785
+ if (i + 1 < chars.length && chars[i + 1] !== "\n") chars[i + 1] = " ";
2786
+ i += 2;
2787
+ continue;
2788
+ }
2789
+ if (chars[i] === "\n") break;
2790
+ if (chars[i] === q) {
2791
+ chars[i] = " ";
2792
+ i++;
2793
+ break;
2794
+ }
2795
+ chars[i] = " ";
2796
+ i++;
2797
+ }
2798
+ continue;
2799
+ }
2800
+ if (c === "`") {
2801
+ chars[i] = " ";
2802
+ i++;
2803
+ let expr = 0;
2804
+ while (i < chars.length) {
2805
+ if (expr === 0 && chars[i] === "\\") {
2806
+ chars[i] = " ";
2807
+ if (i + 1 < chars.length && chars[i + 1] !== "\n") chars[i + 1] = " ";
2808
+ i += 2;
2809
+ continue;
2810
+ }
2811
+ if (expr === 0 && chars[i] === "`") {
2812
+ chars[i] = " ";
2813
+ i++;
2814
+ break;
2815
+ }
2816
+ if (expr === 0 && chars[i] === "$" && chars[i + 1] === "{") {
2817
+ chars[i] = " ";
2818
+ chars[i + 1] = " ";
2819
+ expr = 1;
2820
+ i += 2;
2821
+ continue;
2822
+ }
2823
+ if (expr > 0) {
2824
+ if (chars[i] === "{") expr++;
2825
+ else if (chars[i] === "}") expr--;
2826
+ i++;
2827
+ continue;
2828
+ }
2829
+ if (chars[i] !== "\n") chars[i] = " ";
2830
+ i++;
2831
+ }
2832
+ continue;
2833
+ }
2834
+ i++;
2835
+ }
2836
+ return chars.join("");
2837
+ }
2838
+ function remember(content, filePath) {
2839
+ const py = filePath.endsWith(".py");
2840
+ if (memo && memo.content === content && memo.py === py) return memo;
2841
+ memo = {
2842
+ content,
2843
+ py,
2844
+ masked: maskCallSites(content, py),
2845
+ lineStarts: lineStartsOf(content)
2846
+ };
2847
+ return memo;
2848
+ }
2849
+ function argIsParam(expr, param) {
2850
+ const trimmed = expr.trim();
2851
+ const id = escapeRegExp2(param);
2852
+ return new RegExp(`^${id}$`).test(trimmed) || new RegExp(`^String\\(${id}\\)$`).test(trimmed) || new RegExp(`^Buffer\\.from\\(${id}\\)$`).test(trimmed) || new RegExp(`^${id}\\.encode\\([\\s\\S]*\\)$`).test(trimmed);
2853
+ }
2854
+ function argumentOf(content, chain, base) {
2855
+ const update = /createHash\s*\(\s*["'`](?:md5|sha1|sha256)["'`]\s*\)/.exec(chain);
2856
+ if (update) {
2857
+ const after = chain.slice(update.index + update[0].length);
2858
+ const upd = /\.update\s*\(/.exec(after);
2859
+ if (!upd) return null;
2860
+ const open2 = base + update.index + update[0].length + upd.index + upd[0].lastIndexOf("(");
2861
+ const close2 = matchingCloser(content, open2, "(", ")", ARG_CAP2);
2862
+ if (close2 < 0) return null;
2863
+ return { expr: content.slice(open2 + 1, close2), at: base + update.index };
2864
+ }
2865
+ const lib = /hashlib\s*\.\s*(?:md5|sha1|sha256)\s*\(/.exec(chain);
2866
+ if (!lib) return null;
2867
+ const open = base + lib.index + lib[0].lastIndexOf("(");
2868
+ const close = matchingCloser(content, open, "(", ")", ARG_CAP2);
2869
+ if (close < 0) return null;
2870
+ return { expr: content.slice(open + 1, close), at: base + lib.index };
2871
+ }
2872
+ function onlyReturn(body) {
2873
+ const lines = body.split("\n").map((line) => line.trim()).filter(Boolean);
2874
+ if (lines.length !== 1) return null;
2875
+ const match = /^return\s+([\s\S]+?);?$/.exec(lines[0]);
2876
+ return match ? match[1].replace(/;$/, "").trim() : null;
2877
+ }
2878
+ function qualifyingHelpers(content) {
2879
+ const found = [];
2880
+ const counts = /* @__PURE__ */ new Map();
2881
+ const note = (name) => counts.set(name, (counts.get(name) ?? 0) + 1);
2882
+ const arrow = /(?:^|[\n;])\s*(?:export\s+)?(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*(?:async\s+)?(?:\(\s*([A-Za-z_$][\w$]*)\s*(?::\s*[^)=]+)?\s*\)|([A-Za-z_$][\w$]*))\s*=>/g;
2883
+ let match;
2884
+ while (match = arrow.exec(content)) {
2885
+ const name = match[1];
2886
+ const param = match[2] || match[3];
2887
+ note(name);
2888
+ if (!param) continue;
2889
+ const at = match.index + match[0].length;
2890
+ const rest = content.slice(at);
2891
+ let chain = "";
2892
+ let base = at;
2893
+ if (/^\s*\{/.test(rest)) {
2894
+ const brace = content.indexOf("{", at);
2895
+ const end = matchingCloser(content, brace, "{", "}", 2e4);
2896
+ if (end < 0) continue;
2897
+ const body = onlyReturn(content.slice(brace + 1, end));
2898
+ if (body === null) continue;
2899
+ chain = body;
2900
+ base = brace + 1 + content.slice(brace + 1, end).indexOf(body);
2901
+ } else {
2902
+ const lineEnd = content.indexOf("\n", at);
2903
+ const semi = content.indexOf(";", at);
2904
+ let stop = lineEnd < 0 ? content.length : lineEnd;
2905
+ if (semi >= 0 && semi < stop) stop = semi;
2906
+ chain = content.slice(at, stop);
2907
+ base = at;
2908
+ }
2909
+ const hashed = argumentOf(content, chain, base);
2910
+ if (hashed && argIsParam(hashed.expr, param)) found.push({ name, at: hashed.at });
2911
+ }
2912
+ const fn = /(?:^|[\n;])\s*(?:export\s+)?(?:async\s+)?function\s+([A-Za-z_$][\w$]*)\s*\(\s*([A-Za-z_$][\w$]*)\s*(?::\s*[^)=]+)?\s*\)\s*\{/g;
2913
+ while (match = fn.exec(content)) {
2914
+ const name = match[1];
2915
+ const param = match[2];
2916
+ note(name);
2917
+ const brace = content.indexOf("{", match.index);
2918
+ const end = matchingCloser(content, brace, "{", "}", 2e4);
2919
+ if (end < 0) continue;
2920
+ const body = onlyReturn(content.slice(brace + 1, end));
2921
+ if (body === null) continue;
2922
+ const base = brace + 1 + content.slice(brace + 1, end).indexOf(body);
2923
+ const hashed = argumentOf(content, body, base);
2924
+ if (hashed && argIsParam(hashed.expr, param)) found.push({ name, at: hashed.at });
2925
+ }
2926
+ const lines = content.split("\n");
2927
+ let offset = 0;
2928
+ for (let i = 0; i < lines.length; i++) {
2929
+ const line = lines[i];
2930
+ const same = /^(\s*)def\s+([A-Za-z_]\w*)\s*\(\s*([A-Za-z_]\w*)\s*\)\s*:\s*return\s+(.+)$/.exec(line);
2931
+ if (same) {
2932
+ note(same[2]);
2933
+ const chainAt = offset + line.indexOf(same[4]);
2934
+ const hashed = argumentOf(content, same[4], chainAt);
2935
+ if (hashed && argIsParam(hashed.expr, same[3])) found.push({ name: same[2], at: hashed.at });
2936
+ } else {
2937
+ const head = /^(\s*)def\s+([A-Za-z_]\w*)\s*\(\s*([A-Za-z_]\w*)\s*\)\s*:\s*$/.exec(line);
2938
+ if (head) {
2939
+ note(head[2]);
2940
+ const next = lines[i + 1] ?? "";
2941
+ const body = /^(\s+)return\s+(.+)$/.exec(next);
2942
+ if (body && body[1].length > head[1].length) {
2943
+ let more = false;
2944
+ for (let j = i + 2; j < lines.length; j++) {
2945
+ if (!lines[j].trim()) continue;
2946
+ const ind = /^[ \t]*/.exec(lines[j])?.[0].length ?? 0;
2947
+ if (ind > head[1].length) more = true;
2948
+ break;
2949
+ }
2950
+ if (!more) {
2951
+ const chainAt = offset + line.length + 1 + next.indexOf(body[2]);
2952
+ const hashed = argumentOf(content, body[2], chainAt);
2953
+ if (hashed && argIsParam(hashed.expr, head[3])) found.push({ name: head[2], at: hashed.at });
2954
+ }
2955
+ }
2956
+ }
2957
+ }
2958
+ offset += line.length + 1;
2959
+ }
2960
+ const good = /* @__PURE__ */ new Map();
2961
+ for (const helper of found) good.set(helper.name, (good.get(helper.name) ?? 0) + 1);
2962
+ const kept = /* @__PURE__ */ new Set();
2963
+ for (const [name, count] of counts) {
2964
+ if (good.get(name) === count) kept.add(name);
2965
+ }
2966
+ const seen = /* @__PURE__ */ new Set();
2967
+ return found.filter((helper) => {
2968
+ if (!kept.has(helper.name) || seen.has(helper.name)) return false;
2969
+ seen.add(helper.name);
2970
+ return true;
2971
+ });
2972
+ }
2973
+ function hasConditionalQuestion(window) {
2974
+ let depth = 0;
2975
+ let quote = "";
2976
+ let open = false;
2977
+ for (let i = 0; i < window.length; i++) {
2978
+ const c = window[i];
2979
+ if (quote) {
2980
+ if (c === "\\") {
2981
+ i++;
2982
+ continue;
2983
+ }
2984
+ if (c === quote) quote = "";
2985
+ continue;
2986
+ }
2987
+ if (c === "'" || c === '"' || c === "`") {
2988
+ quote = c;
2989
+ continue;
2990
+ }
2991
+ if (c === "(" || c === "[" || c === "{") depth++;
2992
+ else if (c === ")" || c === "]" || c === "}") depth = Math.max(0, depth - 1);
2993
+ else if (depth === 0 && (c === ";" || c === "\n")) open = false;
2994
+ else if (c === "?" && depth === 0 && window[i + 1] !== "?" && window[i + 1] !== "." && window[i - 1] !== "?") {
2995
+ open = true;
2996
+ }
2997
+ }
2998
+ return open;
2999
+ }
3000
+ function rawNames(content, callIndex) {
3001
+ const start = Math.max(0, callIndex - 200);
3002
+ const slice = content.slice(start, callIndex);
3003
+ const nl = slice.lastIndexOf("\n");
3004
+ const window = nl < 0 ? slice : slice.slice(nl + 1);
3005
+ const names = [];
3006
+ if (!hasConditionalQuestion(window)) {
3007
+ const key = /(["']?)([A-Za-z_$][\w$]*)\1\s*:\s*$/.exec(window);
3008
+ if (key) names.push(key[2]);
3009
+ }
3010
+ const assigned = /([A-Za-z_$][\w$.]*)(?:\s*:\s*[^=]{1,80}?)?\s*=\s*$/.exec(window);
3011
+ if (assigned) names.push(assigned[1]);
3012
+ const sub = /\[\s*(["'])([A-Za-z_$][\w$]*)\1\s*\]\s*=\s*$/.exec(window);
3013
+ if (sub) names.push(sub[2]);
3014
+ return names;
3015
+ }
3016
+ function firstArg(content, open) {
3017
+ const close = matchingCloser(content, open, "(", ")", ARG_CAP2);
3018
+ if (close < 0) return null;
3019
+ let depth = 0;
3020
+ let quote = "";
3021
+ for (let i = open + 1; i < close; i++) {
3022
+ const c = content[i];
3023
+ if (quote) {
3024
+ if (c === "\\") {
3025
+ i++;
3026
+ continue;
3027
+ }
3028
+ if (c === quote) quote = "";
3029
+ continue;
3030
+ }
3031
+ if (c === "'" || c === '"' || c === "`") {
3032
+ quote = c;
3033
+ continue;
3034
+ }
3035
+ if (c === "(" || c === "{" || c === "[") depth++;
3036
+ else if (c === ")" || c === "}" || c === "]") depth--;
3037
+ else if (c === "," && depth === 0) return content.slice(open + 1, i);
3038
+ }
3039
+ return content.slice(open + 1, close);
3040
+ }
3041
+ function callQualifies(content, masked, name) {
3042
+ const re = new RegExp(`(?<![\\w$.])${escapeRegExp2(name)}\\(`, "g");
3043
+ let match;
3044
+ while (match = re.exec(masked)) {
3045
+ const before = masked.slice(Math.max(0, match.index - 24), match.index);
3046
+ if (/\b(?:function|def)\s+$/.test(before)) continue;
3047
+ if (/\b(?:const|let|var)\s+$/.test(before)) continue;
3048
+ const arg = firstArg(masked, match.index + name.length);
3049
+ if (arg === null) continue;
3050
+ const names = rawNames(content, match.index);
3051
+ const argOk = hasPf(arg) && !blocked(arg, TOKEN_WORDS);
3052
+ const nameOk = names.some((label) => hasPf(label) && !blocked(label, KEY_BLOCK));
3053
+ if (argOk || nameOk) return true;
3054
+ }
3055
+ return false;
3056
+ }
3057
+ function helperWeakIndexes(content, filePath) {
3058
+ try {
3059
+ if (helperFault) throw new Error("injected");
3060
+ const slot = remember(content, filePath);
3061
+ const helpers = qualifyingHelpers(content);
3062
+ const indexes = [];
3063
+ for (const helper of helpers) {
3064
+ if (callQualifies(content, slot.masked, helper.name)) indexes.push(helper.at);
3065
+ }
3066
+ return indexes;
3067
+ } catch {
3068
+ remember(content, filePath);
3069
+ return [];
3070
+ }
3071
+ }
3072
+ function pythonDefs(content) {
3073
+ const defs = [];
3074
+ const re = /^([ \t]*)def\s+([A-Za-z_]\w*)\s*\(/gm;
3075
+ let match;
3076
+ while (match = re.exec(content)) {
3077
+ defs.push({ name: match[2], indent: match[1].length, at: match.index });
3078
+ }
3079
+ return defs;
3080
+ }
3081
+ function pythonDefName(content, index, defs) {
3082
+ const lineStart = content.lastIndexOf("\n", Math.max(0, index - 1)) + 1;
3083
+ const indent = /^[ \t]*/.exec(content.slice(lineStart))?.[0].length ?? 0;
3084
+ let name = "";
3085
+ let defIndent = -1;
3086
+ for (const def of defs) {
3087
+ if (def.at >= index) break;
3088
+ name = def.name;
3089
+ defIndent = def.indent;
3090
+ }
3091
+ return name && indent > defIndent ? name : "";
3092
+ }
2270
3093
 
2271
3094
  // src/public-env.ts
2272
3095
  var PUBLIC_ENV_PREFIX_RE = /^(?:NEXT_PUBLIC_|NUXT_PUBLIC_|EXPO_PUBLIC_|REACT_APP_|GATSBY_|VITE_|PUBLIC_)/i;
@@ -4119,10 +4942,10 @@ function unauthenticatedTanStackHandlers(masked, filePath) {
4119
4942
  return hasAuthMiddleware(handlersAt === -1 ? block : block.slice(0, handlersAt));
4120
4943
  })();
4121
4944
  if (!routeLevelAuth) {
4122
- const METHOD_RE = /\b(GET|POST|PUT|PATCH|DELETE)\s*:\s*(?:async\s+)?(?:\(|function\b|\{|[A-Za-z_$][\w$]{0,40}\s*=>)/g;
4945
+ const METHOD_RE2 = /\b(GET|POST|PUT|PATCH|DELETE)\s*:\s*(?:async\s+)?(?:\(|function\b|\{|[A-Za-z_$][\w$]{0,40}\s*=>)/g;
4123
4946
  const methodStarts = [];
4124
4947
  let m;
4125
- while ((m = METHOD_RE.exec(masked)) !== null && methodStarts.length < 200) methodStarts.push(m.index);
4948
+ while ((m = METHOD_RE2.exec(masked)) !== null && methodStarts.length < 200) methodStarts.push(m.index);
4126
4949
  for (let k = 0; k < methodStarts.length; k++) {
4127
4950
  const start = methodStarts[k];
4128
4951
  const limit = k + 1 < methodStarts.length ? methodStarts[k + 1] : Math.min(masked.length, start + 4e4);
@@ -5653,8 +6476,8 @@ var weakHashing = {
5653
6476
  () => "Use bcrypt, scrypt, or argon2 for password hashing \u2014 they're intentionally slow. Example: const hash = await bcrypt.hash(password, 12);"
5654
6477
  ));
5655
6478
  }
5656
- const passwordValue = /password|passwd|passphrase|pwd/i;
5657
- const resetName = /forgot|reset|recover/i;
6479
+ const defs = pythonDefs(content);
6480
+ const nameAt = /* @__PURE__ */ new Map();
5658
6481
  const weakCallPatterns = [
5659
6482
  /createHash\s*\(\s*["'`](?:md5|sha1|sha256)["'`]\s*\)/gi,
5660
6483
  /hashlib\.(?:md5|sha1|sha256)\s*\(/gi,
@@ -5669,58 +6492,49 @@ var weakHashing = {
5669
6492
  }
5670
6493
  return at;
5671
6494
  };
5672
- const hashedExpr = (index) => {
5673
- const slice = content.slice(index, index + 300);
5674
- const update = slice.match(/\.update\s*\(\s*([^)]{0,200})\)/);
5675
- if (update) return update[1];
5676
- const lib = slice.match(/hashlib\s*\.\s*(?:md5|sha1|sha256)\s*\(\s*([^)]{0,200})\)/);
5677
- if (lib) return lib[1];
5678
- return slice.slice(0, 160);
5679
- };
5680
- const tokenValue = (expr) => {
5681
- if (passwordValue.test(expr)) return false;
5682
- return /token|otp|(?:^|[^A-Za-z])code(?:[^A-Za-z]|$)/i.test(expr);
5683
- };
5684
6495
  const braceEnclosingName = (index) => {
5685
- let searchFrom = index;
5686
- for (let level = 0; level < 8; level++) {
5687
- let depth = 0;
5688
- let open = -1;
5689
- for (let i = searchFrom; i >= 0; i--) {
5690
- const c = content[i];
5691
- if (c === "}") depth++;
5692
- else if (c === "{") {
5693
- if (depth === 0) {
5694
- open = i;
5695
- break;
6496
+ const cached = nameAt.get(index);
6497
+ if (cached !== void 0) return cached;
6498
+ let name = "";
6499
+ try {
6500
+ let searchFrom = index;
6501
+ for (let level = 0; level < 8 && index - searchFrom < 2e4; level++) {
6502
+ let depth = 0;
6503
+ let open = -1;
6504
+ const limit = Math.max(0, index - 2e4);
6505
+ for (let i = searchFrom; i >= limit; i--) {
6506
+ const c = content[i];
6507
+ if (c === "}") depth++;
6508
+ else if (c === "{") {
6509
+ if (depth === 0) {
6510
+ open = i;
6511
+ break;
6512
+ }
6513
+ depth--;
5696
6514
  }
5697
- depth--;
5698
6515
  }
6516
+ if (open < 0) break;
6517
+ const header = content.slice(Math.max(0, open - 180), open);
6518
+ const named = /(?:function|def)\s+([A-Za-z_]\w*)\s*\([^)]*\)\s*$/.exec(header) || /(?:const|let|var)\s+([A-Za-z_]\w*)\s*=\s*(?:async\s*)?(?:function\b|\([^)]*\)\s*=>|[A-Za-z_]\w*\s*=>)\s*$/.exec(header);
6519
+ if (named?.[1]) {
6520
+ name = named[1];
6521
+ break;
6522
+ }
6523
+ const method = methodHeaderName(header);
6524
+ if (method) {
6525
+ name = method;
6526
+ break;
6527
+ }
6528
+ searchFrom = open - 1;
5699
6529
  }
5700
- if (open < 0) return "";
5701
- const header = content.slice(Math.max(0, open - 180), open);
5702
- const named = /(?:function|def)\s+([A-Za-z_]\w*)\s*\([^)]*\)\s*$/.exec(header) || /(?:const|let|var)\s+([A-Za-z_]\w*)\s*=\s*(?:async\s*)?(?:function\b|\([^)]*\)\s*=>|[A-Za-z_]\w*\s*=>)\s*$/.exec(header);
5703
- if (named?.[1]) return named[1];
5704
- searchFrom = open - 1;
5705
- }
5706
- return "";
5707
- };
5708
- const pythonDefName = (index) => {
5709
- const lineStart = content.lastIndexOf("\n", Math.max(0, index - 1)) + 1;
5710
- const indent = /^[ \t]*/.exec(content.slice(lineStart))?.[0].length ?? 0;
5711
- const re = /^([ \t]*)def\s+([A-Za-z_]\w*)\s*\(/gm;
5712
- re.lastIndex = 0;
5713
- const before = content.slice(0, index);
5714
- let name = "";
5715
- let defIndent = -1;
5716
- let found;
5717
- while (found = re.exec(before)) {
5718
- name = found[2];
5719
- defIndent = found[1].length;
6530
+ if (!name) name = pythonDefName(content, index, defs);
6531
+ } catch {
6532
+ name = "";
5720
6533
  }
5721
- return name && indent > defIndent ? name : "";
6534
+ nameAt.set(index, name);
6535
+ return name;
5722
6536
  };
5723
- const enclosingFunctionName = (index) => braceEnclosingName(index) || pythonDefName(index);
6537
+ const rawHits = [];
5724
6538
  for (const p of weakCallPatterns) {
5725
6539
  const raw = findMatches(
5726
6540
  content,
@@ -5729,16 +6543,22 @@ var weakHashing = {
5729
6543
  filePath,
5730
6544
  () => "Use bcrypt, scrypt, or argon2 for password hashing. These algorithms are intentionally slow so brute-force attacks are infeasible."
5731
6545
  );
6546
+ rawHits.push(...raw);
5732
6547
  for (const m of raw) {
5733
6548
  if (matches.some((existing) => existing.line === m.line)) continue;
5734
6549
  const lineStart = indexOfLineNo(m.line);
5735
- const expr = hashedExpr(lineStart);
5736
- const name = enclosingFunctionName(lineStart);
5737
- const namedHasher = /password/i.test(name) && !(resetName.test(name) && tokenValue(expr));
5738
- if (!passwordValue.test(expr) && !namedHasher) continue;
6550
+ const expr = hashedExprAt(content, lineStart);
6551
+ const name = braceEnclosingName(lineStart);
6552
+ if (!perCallKeeps(name, expr)) continue;
5739
6553
  matches.push(m);
5740
6554
  }
5741
6555
  }
6556
+ for (const at of helperWeakIndexes(content, filePath)) {
6557
+ const line = lineNoAt(content, at);
6558
+ if (matches.some((existing) => existing.line === line)) continue;
6559
+ const hit = rawHits.find((m) => m.line === line);
6560
+ if (hit) matches.push(hit);
6561
+ }
5742
6562
  return matches;
5743
6563
  }
5744
6564
  };