xploitscan-shared-rules 2.0.0 → 2.0.2

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
@@ -38,7 +38,7 @@ For the full API surface, see the TypeScript definitions in `dist/index.d.ts` (s
38
38
  If you're scanning code, install the CLI instead:
39
39
 
40
40
  ```bash
41
- npx xploitscan scan .
41
+ npx xploitscan@latest scan .
42
42
  ```
43
43
 
44
44
  This package is published as a building block for the official XploitScan surfaces and for advanced users who want to call the detection engine directly from their own tooling.
package/dist/index.cjs CHANGED
@@ -1726,6 +1726,16 @@ function tryParse(content, filePath) {
1726
1726
  parseCacheValid = true;
1727
1727
  return result;
1728
1728
  }
1729
+ function isNamespacedSlotKey(name, value) {
1730
+ if (CREDENTIAL_NAME.test(name)) return false;
1731
+ const words = value.split(/[.:/_-]/);
1732
+ if (words[words.length - 1] === "") words.pop();
1733
+ if (words.length < 2) return false;
1734
+ if (words.some((w) => CREDENTIAL_WORD.test(w))) return false;
1735
+ return words.every((w) => /^[a-z]+[0-9]*(?:[A-Z][a-z]+[0-9]*)*$/.test(w));
1736
+ }
1737
+ var CREDENTIAL_NAME = /SECRET|SIGN|ENCRYPT|CRYPT|CIPHER|HMAC|JWT|PRIVATE|API|ACCESS|AUTH|MASTER|ADMIN|SESSION|PASS|TOKEN|CLIENT|SALT|PEPPER|LICEN[CS]E|WEBHOOK|STRIPE|AWS|SERVICE/;
1738
+ var CREDENTIAL_WORD = /secret|passw|token|private|signing|apikey|credential/i;
1729
1739
  var hardcodedSecrets = {
1730
1740
  id: "VC001",
1731
1741
  title: "Hardcoded API Key or Secret",
@@ -1772,7 +1782,7 @@ var hardcodedSecrets = {
1772
1782
  "Supabase key detected. Service role keys bypass Row Level Security and grant full database read/write access. Move to a server-side environment variable immediately.",
1773
1783
  "OpenAI API key detected \u2014 grants full API access and can incur charges. Rotate at platform.openai.com \u2192 API Keys.",
1774
1784
  "Hardcoded token or password detected. Move to an environment variable and rotate the credential if it has been committed to version control.",
1775
- "Named secret constant detected (XXX_SECRET / XXX_TOKEN / XXX_KEY). Move to an environment variable: `const NAME = process.env.NAME;`. Rotate the credential if this file has been committed to version control.",
1785
+ "Named secret constant detected (XXX_SECRET / XXX_TOKEN / XXX_KEY). If it's a real credential, move it to server-side configuration \u2014 an environment variable read by server code (`process.env.NAME` in Node) \u2014 and rotate it if this file has been committed. Code that runs in the browser can't keep a secret: everything it loads is public, and `process` doesn't exist there without a bundler, so a browser-side credential has to move behind your own server. If the value is an identifier rather than a credential (a storage key, a cookie or cache name), there is nothing to rotate.",
1776
1786
  "Credential assigned to a config object property. Move to an environment variable \u2014 `apiKey: process.env.API_KEY` \u2014 and rotate the value if this file has been committed.",
1777
1787
  "Hardcoded password detected in a config object. Move to an environment variable (`password: process.env.DB_PASSWORD`) and rotate the password if this file has been committed.",
1778
1788
  "Private key found in source code. If this has been committed to version control, consider the key compromised \u2014 generate a new key pair and revoke the old one.",
@@ -1821,7 +1831,7 @@ var hardcodedSecrets = {
1821
1831
  if (valMatch && nameMatch) {
1822
1832
  const value = valMatch[1];
1823
1833
  const isKeySuffix = nameMatch[1].endsWith("_KEY");
1824
- const isSlotIdentifier = value.length < 40 && /^[a-z0-9]+([_-][a-z0-9]+)+$/.test(value);
1834
+ const isSlotIdentifier = value.length < 40 && (/^[a-z0-9]+([_-][a-z0-9]+)+$/.test(value) || isNamespacedSlotKey(nameMatch[1], value));
1825
1835
  if (isKeySuffix && isSlotIdentifier) continue;
1826
1836
  }
1827
1837
  }
@@ -2309,7 +2319,7 @@ var xssVulnerability = {
2309
2319
  pattern,
2310
2320
  xssVulnerability,
2311
2321
  filePath,
2312
- () => "Sanitize user input before rendering as HTML. Use a library like DOMPurify: DOMPurify.sanitize(userInput). If this site is intentional (JSON-LD structured data, a const boot script, a sandboxed preview), add an inline `// VC007-OK: <reason>` comment above the line to silence."
2322
+ () => "Sanitize user input before rendering as HTML. Use a library like DOMPurify: DOMPurify.sanitize(userInput). If this site is intentional (JSON-LD structured data, a const boot script, a sandboxed preview, markup built only from constants and numbers), add an inline `// VC007-OK: <reason>` comment above the line to silence."
2313
2323
  );
2314
2324
  for (const m of raw) {
2315
2325
  const lineText = (allLines[m.line - 1] || "").slice(0, 4e3);
@@ -2367,7 +2377,8 @@ var noRateLimiting = {
2367
2377
  check(content, filePath) {
2368
2378
  const isEntryFile = /(?:server|app|index|main)\.[jt]sx?$/.test(filePath) || filePath.includes("middleware");
2369
2379
  if (!isEntryFile) return [];
2370
- const isServer = /(?:express|hono|fastify|koa|next|createServer|listen\()/i.test(content);
2380
+ const isServer = /(?:from\s+|require\s*\(\s*)["'`](?:express|hono|fastify|koa|koa-router|@koa\/[\w-]+|@hono\/[\w-]+|next(?:\/server)?|next-auth(?:\/[\w-]+)?|@clerk\/nextjs(?:\/server)?)["'`]/.test(content) || // Case-sensitive framework types — `adNext` and `nextLap` can't match.
2381
+ /\b(?:NextApiRequest|NextApiResponse|NextRequest|NextResponse)\b/.test(content) || /\b(?:express|fastify|createServer)\s*\(/.test(content) || /\bnew\s+(?:Hono|Koa)\s*\(/.test(content) || /\b(?:Deno|Bun)\.serve\s*\(/.test(content) || /\.listen\s*\(/.test(content);
2371
2382
  if (!isServer) return [];
2372
2383
  const hasRateLimit = /rate.?limit|throttle|express-rate-limit|@elysiajs\/rate-limit|hono.*limiter/i.test(content);
2373
2384
  if (hasRateLimit) return [];
@@ -2645,6 +2656,40 @@ var exposedAuthSecret = {
2645
2656
  return matches;
2646
2657
  }
2647
2658
  };
2659
+ function cspFixFor(content) {
2660
+ const html = content.slice(0, 2e5);
2661
+ const origins = /* @__PURE__ */ new Set();
2662
+ const addOrigin = (url) => {
2663
+ const m2 = url.match(/^(https?:)?\/\/([^/"'`\s>?#]+)/i);
2664
+ if (m2 && origins.size < 10) origins.add(`${(m2[1] ?? "https:").toLowerCase()}//${m2[2].toLowerCase()}`);
2665
+ };
2666
+ let inlineScripts = 0;
2667
+ const openTag = /<script\b([^>]{0,500})>/gi;
2668
+ let m;
2669
+ for (let seen = 0; (m = openTag.exec(html)) && seen < 100; seen++) {
2670
+ const attrs = m[1];
2671
+ const src = attrs.match(/\bsrc\s*=\s*["']?((?:https?:)?\/\/[^"'\s>]+)/i);
2672
+ if (src) {
2673
+ addOrigin(src[1]);
2674
+ continue;
2675
+ }
2676
+ if (/\bsrc\s*=/i.test(attrs)) continue;
2677
+ const close = html.indexOf("</script", openTag.lastIndex);
2678
+ const body = html.slice(openTag.lastIndex, close < 0 ? openTag.lastIndex + 5e4 : close);
2679
+ const type = (attrs.match(/\btype\s*=\s*["']?([^"'\s>]+)/i)?.[1] ?? "").toLowerCase();
2680
+ const runs = type === "" || type === "module" || type === "importmap" || /^(?:text|application)\/(?:x-)?(?:java|ecma)script$/.test(type);
2681
+ if (!runs || !body.trim()) continue;
2682
+ inlineScripts++;
2683
+ const urls = type === "importmap" ? body.matchAll(/["']((?:https?:)?\/\/[^"'\s]+)["']/g) : type === "module" ? body.matchAll(/(?:\bfrom|\bimport\s*\(?)\s*["'`]((?:https?:)?\/\/[^"'`\s]+)["'`]/g) : [];
2684
+ for (const u of urls) addOrigin(u[1]);
2685
+ }
2686
+ const scriptSrc = ["'self'", ...origins].join(" ");
2687
+ return [
2688
+ `Add a Content-Security-Policy. A starting point for this page: \`default-src 'self'; script-src ${scriptSrc}\`, plus any other origins it fetches from, frames or loads styles, fonts and images from.`,
2689
+ inlineScripts > 0 ? `This page has ${inlineScripts} inline <script> block${inlineScripts === 1 ? "" : "s"} (an import map counts), which that policy blocks: add each one's 'sha256-\u2026' hash to script-src, or move the code into a file.` : "",
2690
+ "Prefer sending it as an HTTP response header from your host; a <meta http-equiv> tag also works, but ignores frame-ancestors. Test it before shipping \u2014 a policy that's too strict stops the page from loading. As a header, Content-Security-Policy-Report-Only lets you see what it would block first."
2691
+ ].filter(Boolean).join(" ");
2692
+ }
2648
2693
  var missingCSP = {
2649
2694
  id: "VC020",
2650
2695
  title: "Missing Content Security Policy (CSP)",
@@ -2662,7 +2707,7 @@ var missingCSP = {
2662
2707
  file: filePath,
2663
2708
  line: 1,
2664
2709
  snippet: getSnippet(content, 1),
2665
- fix: `Add a CSP meta tag: <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">`
2710
+ fix: cspFixFor(content)
2666
2711
  }];
2667
2712
  }
2668
2713
  }