xploitscan-shared-rules 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 VibeCheck
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs CHANGED
@@ -158,6 +158,7 @@ __export(index_exports, {
158
158
  regexDos: () => regexDos,
159
159
  runCustomRules: () => runCustomRules,
160
160
  s3BucketNoEncryption: () => s3BucketNoEncryption,
161
+ scanEntropy: () => scanEntropy,
161
162
  secretInBundleConfig: () => secretInBundleConfig,
162
163
  secretInCLIArgument: () => secretInCLIArgument,
163
164
  secretInErrorResponse: () => secretInErrorResponse,
@@ -5424,6 +5425,163 @@ async function filterFalsePositives(findings, fileContents) {
5424
5425
  totalBefore
5425
5426
  };
5426
5427
  }
5428
+
5429
+ // src/entropy-scanner.ts
5430
+ function shannonEntropy(str) {
5431
+ const freq = {};
5432
+ for (const ch of str) {
5433
+ freq[ch] = (freq[ch] || 0) + 1;
5434
+ }
5435
+ const len = str.length;
5436
+ let entropy = 0;
5437
+ for (const count of Object.values(freq)) {
5438
+ const p = count / len;
5439
+ entropy -= p * Math.log2(p);
5440
+ }
5441
+ return entropy;
5442
+ }
5443
+ var SAFE_PATTERNS = [
5444
+ // UUIDs (v1-v5)
5445
+ /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
5446
+ // Git commit hashes (long and short)
5447
+ /^[0-9a-f]{40}$/i,
5448
+ /^[0-9a-f]{7,8}$/i,
5449
+ // Hex colors
5450
+ /^#?[0-9a-fA-F]{3,8}$/,
5451
+ // Small base64 (< 20 chars can't be a real key anyway)
5452
+ /^[A-Za-z0-9+/]{1,19}={0,2}$/,
5453
+ // Package versions
5454
+ /^\d+\.\d+\.\d+/,
5455
+ // Integrity-hash prefix (sha256-, sha512-, ...)
5456
+ /^sha\d+-/i,
5457
+ // URLs without credentials in them
5458
+ /^https?:\/\/[^:@]*$/,
5459
+ /^https?:\/\/registry\.npmjs\.org\//,
5460
+ /^https?:\/\/registry\.yarnpkg\.com\//,
5461
+ // Full package integrity hash (sha512-[base64]=)
5462
+ /^sha\d+-[A-Za-z0-9+/=]+$/,
5463
+ // Package tarball URLs
5464
+ /\.tgz$/,
5465
+ /registry.*\/-\/.*\.tgz$/,
5466
+ // ISO dates / times
5467
+ /^\d{4}-\d{2}-\d{2}/,
5468
+ // Locale tags
5469
+ /^[a-z]{2}-[A-Z]{2}$/,
5470
+ // Text encodings
5471
+ /^utf-?8|ascii|latin|iso-8859/i,
5472
+ // MIME types
5473
+ /^(?:application|text|image|audio|video)\//,
5474
+ // CSS keyword values
5475
+ /^(?:inherit|none|auto|block|flex|grid|absolute|relative|fixed|px|em|rem|%)/,
5476
+ // Developer-placeholder tokens
5477
+ /^(?:test|example|sample|demo|placeholder|temp|tmp|foo|bar|baz|lorem|ipsum)/i,
5478
+ // XML / DTD markers
5479
+ /DTD|DOCTYPE|w3\.org|apple\.com\/DTDs/i,
5480
+ /xmlns|schema|xsd|xsi/i,
5481
+ // NEW — added in Wave 3.2 for context-aware FP reduction
5482
+ // ──────────────────────────────────────────────────────
5483
+ // Tailwind JIT / CSS-in-JS class-name fingerprints, e.g. "css-2kx3yr8",
5484
+ // "tw-abc12def", "jss-1a2b3c4d". Typically prefix + 6-12 hex-ish chars.
5485
+ /^(?:css|tw|jss|emotion|styled|mui|chakra)-[a-z0-9]{4,14}$/i,
5486
+ // SVG path data — starts with a path command letter followed by coords.
5487
+ // These can get very long and high-entropy but are never secrets.
5488
+ /^[MmLlHhVvCcSsQqTtAaZz][\d.,\s\-MmLlHhVvCcSsQqTtAaZz]{10,}$/,
5489
+ // Next.js / Vite / webpack content-addressed asset filenames, e.g.
5490
+ // "main.4e5f6a78.js", "chunk-2a3b.js", "_next/static/chunks/pages-xyz".
5491
+ /\.[0-9a-f]{6,16}\.(?:js|css|mjs|woff2?|ttf|png|jpg|svg)(?:\?.*)?$/i,
5492
+ /^_next\//,
5493
+ // Publishable / client-side keys from common vendors. Flagged by their
5494
+ // own service-specific rules if they look wrong, but entropy should NOT
5495
+ // double-flag these. They are designed to ship to the browser.
5496
+ /^pk_(?:live|test|[a-z0-9]+)_/,
5497
+ // Stripe / Clerk publishable
5498
+ /^NEXT_PUBLIC_|^VITE_|^REACT_APP_/,
5499
+ // Build-time public env vars
5500
+ /^pub_/,
5501
+ // Segment etc.
5502
+ /^ey[A-Za-z0-9_-]+\.ey[A-Za-z0-9_-]+\./,
5503
+ // JWT header.payload prefix — don't flag solely on entropy
5504
+ // Content-Security-Policy hashes / nonces in HTML/JSON
5505
+ /^'sha\d+-/,
5506
+ /^nonce-/i,
5507
+ // Embedded data URIs
5508
+ /^data:[a-z]+\//i
5509
+ ];
5510
+ var SKIP_FILES = /\.(css|scss|less|svg|md|txt|html?|xml|yml|yaml|toml|lock|map|woff2?|ttf|eot|ico|png|jpg|gif|webp)$/i;
5511
+ var SKIP_FILENAMES = /(?:package-lock\.json|pnpm-lock\.yaml|yarn\.lock|composer\.lock|Gemfile\.lock|Cargo\.lock|poetry\.lock|Pipfile\.lock|shrinkwrap\.json)$/i;
5512
+ 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|locale|translation|copy|prose|markdown|slug|handle)/i;
5513
+ 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;
5514
+ var HASH_PREFIX_RE = /^(?:sha\d+|md5|crc32|base64|bcrypt|argon2|pbkdf2|blake2b?)[:_]/i;
5515
+ var SECRET_VAR_NAMES = /(?:^|[^a-z])(?:key|secret|token|password|passwd|pwd|api[_-]?key|auth|credential|private|signing|bearer|access[_-]?token|refresh[_-]?token|session[_-]?id)(?:$|[^a-z])/i;
5516
+ function getSnippet2(content, line) {
5517
+ const lines = content.split("\n");
5518
+ const start = Math.max(0, line - 2);
5519
+ const end = Math.min(lines.length, line + 2);
5520
+ return lines.slice(start, end).map((l, i) => {
5521
+ const lineNum = start + i + 1;
5522
+ const prefix = lineNum === line ? ">" : " ";
5523
+ return `${prefix} ${String(lineNum).padStart(5)} | ${l}`;
5524
+ }).join("\n");
5525
+ }
5526
+ function scanEntropy(files) {
5527
+ const findings = [];
5528
+ for (const { path: filePath, content } of files) {
5529
+ if (SKIP_FILES.test(filePath)) continue;
5530
+ if (SKIP_FILENAMES.test(filePath)) continue;
5531
+ const basename = filePath.split("/").pop() || "";
5532
+ if (SKIP_FILENAMES.test(basename)) continue;
5533
+ if (filePath.includes("node_modules")) continue;
5534
+ if (filePath.includes(".min.")) continue;
5535
+ if (/pro-rules-bundle|\.bundle\.|\.chunk\./i.test(filePath)) continue;
5536
+ if (/(?:\.test\.|\.spec\.|__tests__|__mocks__|fixtures?\/)/i.test(filePath)) continue;
5537
+ const lines = content.split("\n");
5538
+ for (let i = 0; i < lines.length; i++) {
5539
+ const line = lines[i];
5540
+ const trimmed = line.trimStart();
5541
+ if (trimmed.startsWith("//") || trimmed.startsWith("#") || trimmed.startsWith("*") || trimmed.startsWith("/*")) continue;
5542
+ const stringPattern = /(?:[:=]\s*)(["'`])([^"'`\n]{20,120})\1/g;
5543
+ let match;
5544
+ while ((match = stringPattern.exec(line)) !== null) {
5545
+ const value = match[2];
5546
+ if (SAFE_PATTERNS.some((p) => p.test(value))) continue;
5547
+ if (HASH_PREFIX_RE.test(value)) continue;
5548
+ const beforeAssign = line.substring(0, match.index);
5549
+ if (SAFE_VAR_NAMES.test(beforeAssign)) continue;
5550
+ if (/^https?:\/\/[^:@]*$/.test(value)) continue;
5551
+ if ((value.match(/\s/g) || []).length > 2) continue;
5552
+ const isHex = /^[0-9a-fA-F]+$/.test(value);
5553
+ const isBase64 = /^[A-Za-z0-9+/]+=*$/.test(value);
5554
+ let threshold = 4;
5555
+ if (isHex) threshold = 3;
5556
+ else if (isBase64) threshold = 4.5;
5557
+ if (value.length < 20) continue;
5558
+ const entropy = shannonEntropy(value);
5559
+ if (entropy < threshold) continue;
5560
+ const varName = beforeAssign.match(/(\w+)\s*[:=]\s*$/)?.[1] || "";
5561
+ if (HASH_LIKE_VAR_NAMES.test(varName) && (isHex || isBase64)) continue;
5562
+ const isLikelySecret = SECRET_VAR_NAMES.test(varName);
5563
+ if (entropy < 4.5 && !isLikelySecret) continue;
5564
+ const masked = value.substring(0, 6) + "..." + value.substring(value.length - 4);
5565
+ findings.push({
5566
+ id: `ENTROPY-${filePath}:${i + 1}`,
5567
+ rule: "ENTROPY",
5568
+ severity: isLikelySecret ? "critical" : "high",
5569
+ title: "High-Entropy String Detected (Possible Secret)",
5570
+ description: `Found a high-entropy string (${entropy.toFixed(1)} bits) that may be a hardcoded secret or API key: "${masked}"`,
5571
+ file: filePath,
5572
+ line: i + 1,
5573
+ snippet: getSnippet2(content, i + 1),
5574
+ fix: "If this is a secret, move it to an environment variable. If it's not a secret (e.g., hash, encoded data), add it to .xploitscanignore.",
5575
+ category: "Secrets",
5576
+ source: "custom",
5577
+ owasp: "A02:2021",
5578
+ cwe: "CWE-798"
5579
+ });
5580
+ }
5581
+ }
5582
+ }
5583
+ return findings;
5584
+ }
5427
5585
  // Annotate the CommonJS export names for ESM import in node:
5428
5586
  0 && (module.exports = {
5429
5587
  allCustomRules,
@@ -5554,6 +5712,7 @@ async function filterFalsePositives(findings, fileContents) {
5554
5712
  regexDos,
5555
5713
  runCustomRules,
5556
5714
  s3BucketNoEncryption,
5715
+ scanEntropy,
5557
5716
  secretInBundleConfig,
5558
5717
  secretInCLIArgument,
5559
5718
  secretInErrorResponse,