xploitscan-shared-rules 2.4.1 → 2.4.3

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
@@ -52,6 +52,7 @@ __export(index_exports, {
52
52
  astMatch: () => astMatch,
53
53
  buildProjectContext: () => buildProjectContext,
54
54
  buildQualityContext: () => buildQualityContext,
55
+ buildReviewPrompt: () => buildReviewPrompt,
55
56
  buildTaintMap: () => buildTaintMap,
56
57
  calculateGrade: () => calculateGrade,
57
58
  callSpreads: () => callSpreads,
@@ -124,6 +125,7 @@ __export(index_exports, {
124
125
  nextPublicSecret: () => nextPublicSecret,
125
126
  noRateLimiting: () => noRateLimiting,
126
127
  normalizeScanPath: () => normalizeScanPath,
128
+ packReviewBatches: () => packReviewBatches,
127
129
  parseFile: () => parseFile,
128
130
  parseScanTarget: () => parseScanTarget,
129
131
  parseSupabaseConfig: () => parseSupabaseConfig,
@@ -2830,7 +2832,7 @@ var stripeWebhookUnprotected = {
2830
2832
  if (!/stripe/i.test(code) && !pathSaysStripeWebhook) return [];
2831
2833
  const hasStripeWebhookHandler = pathSaysStripeWebhook || /(?:stripe.{0,200}webhook|webhook.{0,200}stripe)/i.test(code) || /(?:checkout\.session\.completed|invoice\.paid|payment_intent|customer\.subscription)/i.test(code);
2832
2834
  if (!hasStripeWebhookHandler) return [];
2833
- if (/constructEvent|verifyHeader|stripe[_-]?signature|webhook[_-]?secret|STRIPE_WEBHOOK_SECRET/i.test(code) || /\b\w*verif\w*\s*\(/i.test(code) && /\bverif\w*(?:stripe|webhook|signature|sig)\w*\s*\(/i.test(code)) return [];
2835
+ if (/constructEvent|verifyHeader|stripe[_-]?signature|webhook[_-]?secret|STRIPE_WEBHOOK_SECRET/i.test(code) || /\b\w{0,40}verif\w{0,40}\s*\(/i.test(code) && /\bverif\w{0,40}(?:stripe|webhook|signature|sig)\w{0,40}\s*\(/i.test(code)) return [];
2834
2836
  const handlerPatterns = [
2835
2837
  // POST handler that processes Stripe events
2836
2838
  /export\s+(?:async\s+)?function\s+POST\s*\(/g,
@@ -2838,7 +2840,10 @@ var stripeWebhookUnprotected = {
2838
2840
  // export const POST = async (req) => { … }
2839
2841
  // Added alongside the same shape in VC003 (#648); without it an App
2840
2842
  // Router webhook written in the arrow style was invisible.
2841
- /export\s+const\s+POST\s*(?::[^=]+)?=\s*(?:async\s*)?(?:\([^)]*\)|[A-Za-z_$][\w$]*)\s*=>/g,
2843
+ // Bound the optional TypeScript type annotation: `(?::[^=]+)?` walks an
2844
+ // entire 500KB line of non-`=` characters on every near-miss. Real
2845
+ // annotations (`: NextRequest`, `: RouteHandler`) sit well under 200.
2846
+ /export\s+const\s+POST\s*(?::[^=\n]{0,200})?=\s*(?:async\s*)?(?:\([^)]*\)|[A-Za-z_$][\w$]*)\s*=>/g,
2842
2847
  // Express-style Stripe webhook route
2843
2848
  /\.(post|all)\s*\(\s*["'`][^"'`]*(?:stripe|webhook)[^"'`]*["'`]/gi,
2844
2849
  // Event type checking without prior verification
@@ -4668,6 +4673,9 @@ For each finding, respond ONLY with a JSON array. No other text.
4668
4673
  Each element: {"index": <number>, "verdict": "real" or "fp", "reason": "<1 sentence>"}`;
4669
4674
  var MAX_FINDINGS_PER_BATCH = 15;
4670
4675
  var MAX_CONTEXT_LINES = 10;
4676
+ var MAX_CONTEXT_LINE_CHARS = 300;
4677
+ var MAX_BATCH_PROMPT_CHARS = 2e4;
4678
+ var MAX_FIELD_CHARS = 500;
4671
4679
  var DEFAULT_MAX_TOTAL_FINDINGS = 200;
4672
4680
  var MAX_TOTAL_FINDINGS = (() => {
4673
4681
  const raw = process.env.XPLOITSCAN_AI_FILTER_MAX;
@@ -4705,6 +4713,11 @@ var SEVERITY_PRIORITY = {
4705
4713
  low: 3,
4706
4714
  info: 4
4707
4715
  };
4716
+ function truncateChars(text, max) {
4717
+ if (text.length <= max) return text;
4718
+ if (max <= 1) return "\u2026";
4719
+ return `${text.slice(0, max - 1)}\u2026`;
4720
+ }
4708
4721
  function getExpandedContext(content, line, contextLines = MAX_CONTEXT_LINES) {
4709
4722
  const lines = content.split("\n");
4710
4723
  const start = Math.max(0, line - 1 - contextLines);
@@ -4712,7 +4725,7 @@ function getExpandedContext(content, line, contextLines = MAX_CONTEXT_LINES) {
4712
4725
  return lines.slice(start, end).map((l, i) => {
4713
4726
  const lineNum = start + i + 1;
4714
4727
  const marker = lineNum === line ? ">>>" : " ";
4715
- return `${marker} ${lineNum} | ${l}`;
4728
+ return `${marker} ${lineNum} | ${truncateChars(l, MAX_CONTEXT_LINE_CHARS)}`;
4716
4729
  }).join("\n");
4717
4730
  }
4718
4731
  function buildReviewPrompt(findings, fileContent) {
@@ -4725,8 +4738,8 @@ Rule: ${f.rule} (${f.title})
4725
4738
  Severity: ${f.severity}
4726
4739
  File: ${f.file}
4727
4740
  Line: ${f.line}
4728
- Description: ${f.description}
4729
- Suggested fix: ${f.fix || "N/A"}
4741
+ Description: ${truncateChars(f.description || "", MAX_FIELD_CHARS)}
4742
+ Suggested fix: ${truncateChars(f.fix || "N/A", MAX_FIELD_CHARS)}
4730
4743
 
4731
4744
  Code context:
4732
4745
  ${context}
@@ -4736,6 +4749,23 @@ ${context}
4736
4749
 
4737
4750
  ${parts.join("\n")}`;
4738
4751
  }
4752
+ function packReviewBatches(fileFindings, content) {
4753
+ const batches = [];
4754
+ let current = [];
4755
+ for (const f of fileFindings) {
4756
+ const candidate = [...current, f];
4757
+ const overCount = candidate.length > MAX_FINDINGS_PER_BATCH;
4758
+ const overChars = current.length > 0 && buildReviewPrompt(candidate, content).length > MAX_BATCH_PROMPT_CHARS;
4759
+ if (overCount || overChars) {
4760
+ if (current.length > 0) batches.push(current);
4761
+ current = [f];
4762
+ continue;
4763
+ }
4764
+ current = candidate;
4765
+ }
4766
+ if (current.length > 0) batches.push(current);
4767
+ return batches;
4768
+ }
4739
4769
  function parseReviewResponse(text) {
4740
4770
  try {
4741
4771
  const cleaned = text.replace(/```json\n?/g, "").replace(/```\n?/g, "").trim();
@@ -4789,8 +4819,8 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
4789
4819
  for (const [file, fileFindings] of byFile) {
4790
4820
  const content = fileContents.get(file);
4791
4821
  if (!content) continue;
4792
- for (let i = 0; i < fileFindings.length; i += MAX_FINDINGS_PER_BATCH) {
4793
- tasks.push({ batch: fileFindings.slice(i, i + MAX_FINDINGS_PER_BATCH), content });
4822
+ for (const batch of packReviewBatches(fileFindings, content)) {
4823
+ tasks.push({ batch, content });
4794
4824
  }
4795
4825
  }
4796
4826
  const reviewBatch = async (task) => {
@@ -4803,6 +4833,14 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
4803
4833
  try {
4804
4834
  const remaining = Math.max(1, deadlineMs - Date.now());
4805
4835
  const prompt = buildReviewPrompt(batch, content);
4836
+ console.info(
4837
+ `[ai-fp-filter] batch prompt ${prompt.length} chars (~${Math.ceil(prompt.length / 4)} tokens), ${batch.length} finding(s)`
4838
+ );
4839
+ if (prompt.length > MAX_BATCH_PROMPT_CHARS) {
4840
+ console.warn(
4841
+ `[ai-fp-filter] batch prompt ${prompt.length} chars exceeds ${MAX_BATCH_PROMPT_CHARS} cap (single oversized finding after truncation)`
4842
+ );
4843
+ }
4806
4844
  const response = await client.messages.create(
4807
4845
  {
4808
4846
  model: MODEL_TRIAGE,
@@ -9616,6 +9654,7 @@ function scanQuality(input, rules = qualityRules) {
9616
9654
  astMatch,
9617
9655
  buildProjectContext,
9618
9656
  buildQualityContext,
9657
+ buildReviewPrompt,
9619
9658
  buildTaintMap,
9620
9659
  calculateGrade,
9621
9660
  callSpreads,
@@ -9688,6 +9727,7 @@ function scanQuality(input, rules = qualityRules) {
9688
9727
  nextPublicSecret,
9689
9728
  noRateLimiting,
9690
9729
  normalizeScanPath,
9730
+ packReviewBatches,
9691
9731
  parseFile,
9692
9732
  parseScanTarget,
9693
9733
  parseSupabaseConfig,