xploitscan-shared-rules 2.4.15 → 2.4.17

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
@@ -162,7 +162,7 @@ __export(index_exports, {
162
162
  module.exports = __toCommonJS(index_exports);
163
163
 
164
164
  // src/version.ts
165
- var SCANNER_VERSION = "2.4.15";
165
+ var SCANNER_VERSION = "2.4.17";
166
166
 
167
167
  // src/snippet.ts
168
168
  var cacheContent = null;
@@ -5217,6 +5217,41 @@ var KEY_VALUE_CONFIG_RE = /\.(?:ini|properties|cfg|conf)$/i;
5217
5217
  var XML_CONFIG_RE = /\.xml$/i;
5218
5218
  var JWT_RE = /^ey[A-Za-z0-9_-]+\.ey[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/;
5219
5219
  var SAFE_VAR_NAMES = /(?:description|message|text|label|title|content|template|html|svg|css|style|class(?:Name)?|query|mutation|schema|regex|pattern|format|placeholder|comment|url|path|route|endpoint|href|src|alt|name|type|version|encoding|charset|chars|alphabet|locale|translation|copy|prose|markdown|slug|handle)/i;
5220
+ var SAFE_VAR_NAMES_SCAN = new RegExp(SAFE_VAR_NAMES.source, "gi");
5221
+ var SAFE_VAR_NAMES_AT = new RegExp(SAFE_VAR_NAMES.source, "iy");
5222
+ function buildSafeWordIndex(line) {
5223
+ const starts = [];
5224
+ const ends = [];
5225
+ SAFE_VAR_NAMES_SCAN.lastIndex = 0;
5226
+ let m;
5227
+ while ((m = SAFE_VAR_NAMES_SCAN.exec(line)) !== null) {
5228
+ const s = m.index;
5229
+ let end = s + m[0].length;
5230
+ for (let len = 1; len < m[0].length; len++) {
5231
+ SAFE_VAR_NAMES_AT.lastIndex = 0;
5232
+ if (SAFE_VAR_NAMES_AT.test(line.substring(s, s + len))) {
5233
+ end = s + len;
5234
+ break;
5235
+ }
5236
+ }
5237
+ starts.push(s);
5238
+ ends.push(end);
5239
+ SAFE_VAR_NAMES_SCAN.lastIndex = s + 1;
5240
+ }
5241
+ const minEnd = ends.slice();
5242
+ for (let j = minEnd.length - 2; j >= 0; j--) minEnd[j] = Math.min(minEnd[j], minEnd[j + 1]);
5243
+ return { starts, minEnd };
5244
+ }
5245
+ function safeWordIn(index, from, to) {
5246
+ let lo = 0;
5247
+ let hi = index.starts.length;
5248
+ while (lo < hi) {
5249
+ const mid = lo + hi >>> 1;
5250
+ if (index.starts[mid] < from) lo = mid + 1;
5251
+ else hi = mid;
5252
+ }
5253
+ return lo < index.minEnd.length && index.minEnd[lo] <= to;
5254
+ }
5220
5255
  var HASH_LIKE_VAR_NAMES = /(?:^|[^a-z])(?:hash|digest|checksum|fingerprint|etag|crc|md5|sha1|sha256|sha512|contenthash|buildid|revision|commit|sri|integrity|cacheKey|fileHash|assetId|versionId)(?:$|[^a-z])/i;
5221
5256
  var HASH_PREFIX_RE = /^(?:sha\d+|md5|crc32|base64|bcrypt|argon2|pbkdf2|blake2b?)[:_]/i;
5222
5257
  var VERIFICATION_BLOCK_RE = /\bverification\s*:\s*\{/;
@@ -5253,8 +5288,7 @@ function assignmentNameBefore(beforeAssign) {
5253
5288
  return s.slice(i + 1, end);
5254
5289
  }
5255
5290
  var MAX_QUOTED_VALUE = 256;
5256
- function getSnippet2(content, line) {
5257
- const lines = content.split("\n");
5291
+ function getSnippet2(lines, line) {
5258
5292
  const start = Math.max(0, line - 2);
5259
5293
  const end = Math.min(lines.length, line + 2);
5260
5294
  return lines.slice(start, end).map((l, i) => {
@@ -5356,22 +5390,38 @@ function scanEntropyFiles(files, collect) {
5356
5390
  if (adjacent && joined.length >= 20 && joined.length <= MAX_QUOTED_VALUE) {
5357
5391
  candidates.push({
5358
5392
  value: joined,
5359
- beforeAssign: line.substring(0, assignmentMatch.index)
5393
+ beforeAssign: line.substring(0, assignmentMatch.index),
5394
+ contextFrom: 0,
5395
+ contextTo: assignmentMatch.index
5360
5396
  });
5361
5397
  }
5362
5398
  }
5363
5399
  }
5364
5400
  const stringPattern = /(?:[:=]\s*)(["'`])([^"'`\n]{20,256})\1/g;
5365
5401
  let match;
5402
+ let separatorScan = 0;
5403
+ let lastSeparator = -1;
5366
5404
  while ((match = stringPattern.exec(line)) !== null) {
5367
5405
  if (match[1] === "`" && /(?:^|[\s(]):[a-z][\w-]{0,20}:$/.test(line.slice(Math.max(0, match.index - 23), match.index + 1))) continue;
5368
5406
  let beforeAssign = line.substring(0, match.index);
5369
- if (structured) beforeAssign = beforeAssign.slice(Math.max(beforeAssign.lastIndexOf(","), beforeAssign.lastIndexOf("{")) + 1);
5370
- candidates.push({ value: match[2], beforeAssign });
5407
+ let contextFrom = 0;
5408
+ if (structured) {
5409
+ for (; separatorScan < match.index; separatorScan++) {
5410
+ const c = line.charCodeAt(separatorScan);
5411
+ if (c === 44 || c === 123) lastSeparator = separatorScan;
5412
+ }
5413
+ contextFrom = lastSeparator + 1;
5414
+ beforeAssign = beforeAssign.slice(contextFrom);
5415
+ }
5416
+ candidates.push({ value: match[2], beforeAssign, contextFrom, contextTo: match.index });
5371
5417
  }
5372
5418
  }
5419
+ let safeWords;
5420
+ let verificationMeta;
5421
+ let silenced;
5422
+ let snippet;
5373
5423
  const seenValues = /* @__PURE__ */ new Set();
5374
- for (const { value, beforeAssign, dotenvName, variableName } of candidates) {
5424
+ for (const { value, beforeAssign, dotenvName, variableName, contextFrom, contextTo } of candidates) {
5375
5425
  if (seenValues.has(value)) continue;
5376
5426
  seenValues.add(value);
5377
5427
  if (value.includes("${")) continue;
@@ -5382,7 +5432,10 @@ function scanEntropyFiles(files, collect) {
5382
5432
  const isUuidShape = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
5383
5433
  if (!isUuidShape && SAFE_PATTERNS.some((p) => p.test(value))) continue;
5384
5434
  if (HASH_PREFIX_RE.test(value)) continue;
5385
- if (SAFE_VAR_NAMES.test(beforeAssign)) continue;
5435
+ if (contextTo !== void 0) {
5436
+ if (safeWords === void 0) safeWords = buildSafeWordIndex(line);
5437
+ if (safeWordIn(safeWords, contextFrom ?? 0, contextTo)) continue;
5438
+ } else if (SAFE_VAR_NAMES.test(beforeAssign)) continue;
5386
5439
  if (/^https?:\/\/[^:@]*$/.test(value)) continue;
5387
5440
  if ((value.match(/\s/g) || []).length > 2) continue;
5388
5441
  const isHex = /^[0-9a-fA-F]+$/.test(value);
@@ -5399,7 +5452,8 @@ function scanEntropyFiles(files, collect) {
5399
5452
  continue;
5400
5453
  }
5401
5454
  if (inVerificationBlock && VERIFICATION_KEY_NAMES.test(varName)) continue;
5402
- if (VERIFICATION_META_RE.test(line)) continue;
5455
+ if (verificationMeta === void 0) verificationMeta = VERIFICATION_META_RE.test(line);
5456
+ if (verificationMeta) continue;
5403
5457
  if (HASH_LIKE_VAR_NAMES.test(varName) && (isHex || isBase64)) continue;
5404
5458
  if (JWT_RE.test(value) && !isSecretVariableName(varName)) continue;
5405
5459
  if (/^DOTENV_PUBLIC_KEY(?:_\w{1,60})?$/.test(varName) && /^0[23][0-9a-fA-F]{64}$/.test(value)) continue;
@@ -5408,9 +5462,11 @@ function scanEntropyFiles(files, collect) {
5408
5462
  if (dotenvName && /(?:^|_)IDS?$/i.test(dotenvName) && !isSecretVariableName(dotenvName)) continue;
5409
5463
  const isLikelySecret = isSecretVariableName(varName) || publicEnv?.secret === true;
5410
5464
  if (entropy < 4.5 && !isLikelySecret) continue;
5411
- if (isLineSilenced(lines, i, "ENTROPY")) continue;
5465
+ if (silenced === void 0) silenced = isLineSilenced(lines, i, "ENTROPY");
5466
+ if (silenced) continue;
5412
5467
  collect?.push(value);
5413
5468
  const masked = maskSecretValue(value);
5469
+ if (snippet === void 0) snippet = getSnippet2(lines, i + 1);
5414
5470
  findings.push({
5415
5471
  ...vendored ? { vendored: true } : {},
5416
5472
  id: `ENTROPY-${filePath}:${i + 1}`,
@@ -5420,7 +5476,7 @@ function scanEntropyFiles(files, collect) {
5420
5476
  description: `Found a high-entropy string (${entropy.toFixed(1)} bits) that may be a hardcoded secret or API key: "${masked}"`,
5421
5477
  file: filePath,
5422
5478
  line: i + 1,
5423
- snippet: getSnippet2(content, i + 1),
5479
+ snippet,
5424
5480
  // The generic advice ("move it to an environment variable") is
5425
5481
  // wrong for a value that is already in one, so dotenv findings get
5426
5482
  // advice about the file, and public-prefixed ones about the prefix.
@@ -6827,15 +6883,17 @@ function isExcludedPath(path) {
6827
6883
  if (/(?:^|\/)(?:migrations?|seeds?|scripts?)\//i.test(path)) return true;
6828
6884
  return false;
6829
6885
  }
6886
+ var USE_CLIENT_DIRECTIVE = /^[^\S\n\r\u2028\u2029]*["']use client["']/m;
6887
+ var USE_SERVER_DIRECTIVE = /^[^\S\n\r\u2028\u2029]*["']use server["']/m;
6830
6888
  function looksClient(code) {
6831
- if (/^\s*["']use client["']/m.test(code)) return true;
6889
+ if (USE_CLIENT_DIRECTIVE.test(code)) return true;
6832
6890
  return /\b(?:useState|useEffect|useTransition|useReducer)\s*\(/.test(code);
6833
6891
  }
6834
6892
  function looksServer(path, code) {
6835
6893
  if (/(?:^|\/)route\.(?:ts|js)$/.test(path)) return true;
6836
6894
  if (/(?:^|\/)middleware\.(?:ts|js)$/.test(path)) return true;
6837
6895
  if (/(?:^|\/)(?:api|server|functions|edge-functions)\//.test(path)) return true;
6838
- if (/^\s*["']use server["']/m.test(code)) return true;
6896
+ if (USE_SERVER_DIRECTIVE.test(code)) return true;
6839
6897
  if (/\bDeno\s*\.\s*serve\s*\(/.test(code)) return true;
6840
6898
  if (/\bserve\s*\(\s*(?:async\s*)?\(/.test(code) && /deno\.land|@std\/http/i.test(code)) return true;
6841
6899
  if (/\bexport\s+(?:async\s+)?function\s+(?:GET|POST|PUT|PATCH|DELETE|HEAD)\s*\(/.test(code)) return true;
@@ -7319,7 +7377,7 @@ var silentFailedWrite = {
7319
7377
  if (!file.isClient) continue;
7320
7378
  if (file.isServer) continue;
7321
7379
  if (NEVER_CLIENT_PATH.test(file.path)) continue;
7322
- if (/^\s*["']use server["']/m.test(file.code)) continue;
7380
+ if (USE_SERVER_DIRECTIVE.test(file.code)) continue;
7323
7381
  const parsed = file.parsed;
7324
7382
  if (!parsed) continue;
7325
7383
  const { reachable } = collectFunctions(file);
@@ -9273,7 +9331,7 @@ var failedReadEmptyState = {
9273
9331
  if (file.excluded) continue;
9274
9332
  if (!file.isClient) continue;
9275
9333
  if (file.isServer) continue;
9276
- if (/^\s*["']use server["']/m.test(file.code)) continue;
9334
+ if (USE_SERVER_DIRECTIVE.test(file.code)) continue;
9277
9335
  if (!file.code.includes("fetch")) continue;
9278
9336
  const parsed = file.parsed;
9279
9337
  if (!parsed) continue;