xploitscan-shared-rules 2.1.0 → 2.1.1

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
@@ -82,6 +82,7 @@ __export(index_exports, {
82
82
  filterFalsePositives: () => filterFalsePositives,
83
83
  filterSilenced: () => filterSilenced,
84
84
  findMatches: () => findMatches,
85
+ findMatchesAt: () => findMatchesAt,
85
86
  findSourceMapRefs: () => findSourceMapRefs,
86
87
  freeRules: () => freeRules,
87
88
  getObjectProperty: () => getObjectProperty,
@@ -1765,19 +1766,27 @@ function filterSilenced(matches, content, ruleId) {
1765
1766
  return matches.filter((m) => !isInlineSilenced(content, lineStartIndex(m.line), ruleId));
1766
1767
  }
1767
1768
  function findMatches(content, pattern, rule, filePath, fixTemplate) {
1768
- const matches = [];
1769
- let m;
1770
1769
  const re = new RegExp(pattern.source, pattern.flags.includes("g") ? pattern.flags : `${pattern.flags}g`);
1770
+ function* execAll() {
1771
+ let m;
1772
+ let lastMatchIndex = -1;
1773
+ while ((m = re.exec(content)) !== null) {
1774
+ if (m.index === lastMatchIndex && m[0].length === 0) {
1775
+ re.lastIndex++;
1776
+ continue;
1777
+ }
1778
+ lastMatchIndex = m.index;
1779
+ yield m;
1780
+ }
1781
+ }
1782
+ return findMatchesAt(content, execAll(), rule, filePath, fixTemplate);
1783
+ }
1784
+ function findMatchesAt(content, found, rule, filePath, fixTemplate) {
1785
+ const matches = [];
1771
1786
  let scanned = 0;
1772
1787
  let lineNum = 1;
1773
- let lastMatchIndex = -1;
1774
1788
  const MAX_MATCHES = 500;
1775
- while ((m = re.exec(content)) !== null) {
1776
- if (m.index === lastMatchIndex && m[0].length === 0) {
1777
- re.lastIndex++;
1778
- continue;
1779
- }
1780
- lastMatchIndex = m.index;
1789
+ for (const m of found) {
1781
1790
  if (isCommentLine(content, m.index)) continue;
1782
1791
  if (isInsideFixMessage(content, m.index)) continue;
1783
1792
  for (; scanned < m.index; scanned++) {
@@ -2719,6 +2728,105 @@ function namesPlaceholderRun(content) {
2719
2728
  }
2720
2729
  return false;
2721
2730
  }
2731
+ function isLineTerminator(c) {
2732
+ return c === 10 || c === 13 || c === 8232 || c === 8233;
2733
+ }
2734
+ function quotedLiterals(content) {
2735
+ const literals = [];
2736
+ const failedUpTo = /* @__PURE__ */ new Map();
2737
+ for (let i = 0; i < content.length; i++) {
2738
+ const quote = content.charCodeAt(i);
2739
+ if (quote !== 39 && quote !== 34 && quote !== 96) continue;
2740
+ if (i < (failedUpTo.get(quote) ?? -1)) continue;
2741
+ let j = i + 1;
2742
+ let closed = false;
2743
+ while (j < content.length) {
2744
+ const c = content.charCodeAt(j);
2745
+ if (c === quote) {
2746
+ closed = true;
2747
+ break;
2748
+ }
2749
+ if (c === 92) {
2750
+ if (j + 1 >= content.length || isLineTerminator(content.charCodeAt(j + 1))) break;
2751
+ j += 2;
2752
+ continue;
2753
+ }
2754
+ if (c === 10 && quote !== 96) break;
2755
+ j++;
2756
+ }
2757
+ if (!closed) {
2758
+ failedUpTo.set(quote, j);
2759
+ continue;
2760
+ }
2761
+ literals.push(content.slice(i, j + 1));
2762
+ i = j;
2763
+ }
2764
+ return literals;
2765
+ }
2766
+ var SQL_VERB_RE = /\b(?:SELECT|INSERT|UPDATE|DELETE)\b/i;
2767
+ var SQL_CLAUSE_RE = /\b(?:FROM|INTO|SET|VALUES|WHERE)\b/gi;
2768
+ function isSqlStatement(lit) {
2769
+ const verb = SQL_VERB_RE.exec(lit);
2770
+ if (!verb) return false;
2771
+ SQL_CLAUSE_RE.lastIndex = verb.index + verb[0].length;
2772
+ return SQL_CLAUSE_RE.test(lit);
2773
+ }
2774
+ function* interpolatedSqlLiterals(content) {
2775
+ const opener = /["'`]\s*(?:SELECT|INSERT|UPDATE|DELETE)\b/gi;
2776
+ const literalBreak = /["'`\n]/g;
2777
+ const clauseRe = /\b(?:FROM|INTO|SET|VALUES|WHERE|JOIN|RETURNING)\b/i;
2778
+ let lineEnds = null;
2779
+ let lastOnLine = [];
2780
+ const parameterizedLater = (x) => {
2781
+ if (lineEnds === null) {
2782
+ const ends = [];
2783
+ for (let i = 0; i < content.length; i++) {
2784
+ if (isLineTerminator(content.charCodeAt(i))) ends.push(i);
2785
+ }
2786
+ ends.push(content.length);
2787
+ lastOnLine = new Array(ends.length).fill(-1);
2788
+ const word = /parameterized/gi;
2789
+ let line = 0;
2790
+ for (let w = word.exec(content); w !== null; w = word.exec(content)) {
2791
+ while (ends[line] < w.index) line++;
2792
+ lastOnLine[line] = w.index;
2793
+ }
2794
+ lineEnds = ends;
2795
+ }
2796
+ let lo = 0;
2797
+ let hi = lineEnds.length - 1;
2798
+ while (lo < hi) {
2799
+ const mid = lo + hi >> 1;
2800
+ if (lineEnds[mid] < x) lo = mid + 1;
2801
+ else hi = mid;
2802
+ }
2803
+ return lastOnLine[lo] >= x;
2804
+ };
2805
+ for (let m = opener.exec(content); m !== null; m = opener.exec(content)) {
2806
+ const start = m.index;
2807
+ const runStart = opener.lastIndex;
2808
+ literalBreak.lastIndex = runStart;
2809
+ const runEnd = literalBreak.exec(content)?.index ?? content.length;
2810
+ const run = content.slice(runStart, runEnd);
2811
+ const clause = clauseRe.exec(run);
2812
+ let matchEnd = -1;
2813
+ if (clause) {
2814
+ const clauseEnd = clause.index + clause[0].length;
2815
+ for (let d = run.lastIndexOf("${"); d >= clauseEnd; d = run.lastIndexOf("${", d - 1)) {
2816
+ if (!parameterizedLater(runStart + d + 2)) {
2817
+ matchEnd = runStart + d + 2;
2818
+ break;
2819
+ }
2820
+ }
2821
+ }
2822
+ if (matchEnd === -1) {
2823
+ opener.lastIndex = start + 1;
2824
+ continue;
2825
+ }
2826
+ yield { index: start };
2827
+ opener.lastIndex = matchEnd;
2828
+ }
2829
+ }
2722
2830
  var sqlInjection = {
2723
2831
  id: "VC006",
2724
2832
  title: "Potential SQL Injection",
@@ -2741,35 +2849,22 @@ var sqlInjection = {
2741
2849
  // allows the opposite quote type inside (common in SQL — "WHERE x = 'a'").
2742
2850
  // The older `[^"']*` class bailed out at the first inner quote and
2743
2851
  // missed every realistic SQL-containing concat.
2744
- /(?:query|execute|raw|sql|queryRaw|queryRawUnsafe|executeRaw|executeRawUnsafe)\s*\(\s*(?:"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')\s*\+/gi,
2745
- // Direct variable interpolation in a SQL string literal. Requires the
2746
- // verb to START a quoted/backtick literal AND be followed — within the
2747
- // same literal — by a SQL structural keyword (FROM/SET/WHERE/…), then a
2748
- // `${` interpolation. The structural keyword is what distinguishes a
2749
- // real query from ordinary prose: the previous pattern matched any
2750
- // SELECT/UPDATE/… word anywhere on a line followed by `${`, so UI copy
2751
- // like `UPDATE your profile settings ${name}` flagged as a CRITICAL SQL
2752
- // injection. Real queries ("SELECT … FROM …", "UPDATE … SET …") always
2753
- // carry a second structural keyword; sentences almost never do.
2754
- /["'`]\s*(?:SELECT|INSERT|UPDATE|DELETE)\b[^"'`\n]*\b(?:FROM|INTO|SET|VALUES|WHERE|JOIN|RETURNING)\b[^"'`\n]*\$\{(?!.*parameterized)/gi
2852
+ /(?:query|execute|raw|sql|queryRaw|queryRawUnsafe|executeRaw|executeRawUnsafe)\s*\(\s*(?:"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')\s*\+/gi
2755
2853
  ];
2756
2854
  const matches = [];
2757
- const sqlLiterals = (content.match(/'(?:[^'\\\n]|\\.)*'|"(?:[^"\\\n]|\\.)*"|`(?:[^`\\]|\\.)*`/g) ?? []).filter(
2758
- (lit) => /\b(?:SELECT|INSERT|UPDATE|DELETE)\b[\s\S]*\b(?:FROM|INTO|SET|VALUES|WHERE)\b/i.test(lit)
2759
- );
2855
+ const sqlLiterals = quotedLiterals(content).filter(isSqlStatement);
2760
2856
  const usesParams = /\bprepare\b/i.test(content) || sqlLiterals.some((lit) => /\?\s*,|\?[^?,;]{1,20}\?|\$\d+|:[A-Za-z_]\w*|@[A-Za-z_]\w*|=\s*\?/.test(lit));
2761
- const code = content.replace(/\/\*[\s\S]*?\*\//g, " ").replace(/(^|[^:"'`\\])\/\/.*$/gm, "$1");
2857
+ const code = replaceBlockComments(content, () => " ").replace(/(^|[^:"'`\\])\/\/.*$/gm, "$1");
2762
2858
  if (usesParams || namesPlaceholderRun(code)) return [];
2763
- for (const pattern of patterns) {
2764
- const raw = findMatches(
2765
- content,
2766
- pattern,
2767
- sqlInjection,
2768
- filePath,
2769
- () => "Use parameterized queries or prepared statements instead of string interpolation. Example: db.query('SELECT * FROM users WHERE id = ?', [userId])"
2770
- );
2859
+ const fix = () => "Use parameterized queries or prepared statements instead of string interpolation. Example: db.query('SELECT * FROM users WHERE id = ?', [userId])";
2860
+ const raws = [
2861
+ ...patterns.map((pattern) => findMatches(content, pattern, sqlInjection, filePath, fix)),
2862
+ findMatchesAt(content, interpolatedSqlLiterals(content), sqlInjection, filePath, fix)
2863
+ ];
2864
+ let lines;
2865
+ for (const raw of raws) {
2771
2866
  for (const m of raw) {
2772
- const lineText = content.split("\n")[m.line - 1] || "";
2867
+ const lineText = (lines ??= content.split("\n"))[m.line - 1] || "";
2773
2868
  if (/\bPrisma\.sql\s*`/.test(lineText)) continue;
2774
2869
  if (/\bsql\s*`/.test(lineText) && !/\bsql\.raw\s*\(/.test(lineText)) continue;
2775
2870
  if (/\$queryRaw\s*\(\s*Prisma\.sql|\$executeRaw\s*\(\s*Prisma\.sql/.test(lineText)) continue;
@@ -3579,7 +3674,7 @@ var dangerousInnerHTML = {
3579
3674
  if (!filePath.match(/\.(jsx|tsx)$/)) return [];
3580
3675
  if (isTestFile(filePath)) return [];
3581
3676
  if (!/dangerouslySetInnerHTML/i.test(content)) return [];
3582
- const codeOnly = content.replace(/\/\*[\s\S]*?\*\//g, "").replace(/(^|[^:])\/\/.*$/gm, "$1").replace(/\{\s*\/\*[\s\S]*?\*\/\s*\}/g, "");
3677
+ const codeOnly = replaceBlockComments(content, () => "").replace(/(^|[^:])\/\/.*$/gm, "$1").replace(/\{\s*\/\*[\s\S]*?\*\/\s*\}/g, "");
3583
3678
  const hasSanitize = /\b(?:DOMPurify|sanitizeHtml|sanitize-html|isomorphic-dompurify)\b|\b(?:sanitize|purify|xss)\s*\(/i.test(codeOnly);
3584
3679
  if (hasSanitize) return [];
3585
3680
  const findings = [];
@@ -8383,6 +8478,7 @@ function scanQuality(input, rules = qualityRules) {
8383
8478
  filterFalsePositives,
8384
8479
  filterSilenced,
8385
8480
  findMatches,
8481
+ findMatchesAt,
8386
8482
  findSourceMapRefs,
8387
8483
  freeRules,
8388
8484
  getObjectProperty,