xploitscan-shared-rules 2.4.32 → 2.4.34

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
@@ -236,7 +236,7 @@ __export(index_exports, {
236
236
  module.exports = __toCommonJS(index_exports);
237
237
 
238
238
  // src/version.ts
239
- var SCANNER_VERSION = "2.4.32";
239
+ var SCANNER_VERSION = "2.4.34";
240
240
 
241
241
  // src/snippet.ts
242
242
  var cacheContent = null;
@@ -10453,9 +10453,8 @@ if (!(perCallTimeoutMs(AI_FILTER_BUDGET_MS) < AI_FILTER_BUDGET_MS)) {
10453
10453
  throw new Error("per-call AI timeout must be strictly below the filter budget");
10454
10454
  }
10455
10455
  function isAbortError(err) {
10456
- if (!err || typeof err !== "object") return false;
10457
- const name = "name" in err ? String(err.name) : "";
10458
- return name === "AbortError" || name === "APIUserAbortError";
10456
+ const { category } = classifyModelError(err);
10457
+ return category === "abort" || category === "timeout";
10459
10458
  }
10460
10459
  function isTimeoutError(err) {
10461
10460
  if (!err || typeof err !== "object") return false;
@@ -10469,11 +10468,71 @@ function errorHttpStatus(err) {
10469
10468
  const status = err.status;
10470
10469
  return typeof status === "number" && Number.isFinite(status) ? Math.trunc(status) : null;
10471
10470
  }
10472
- function callWasAborted(err) {
10473
- if (!err || typeof err !== "object") return false;
10474
- const name = "name" in err ? String(err.name) : "";
10475
- return name === "AbortError" || name === "APIUserAbortError";
10471
+ var SAFE_ERROR_IDENT = /^[A-Za-z][A-Za-z0-9_]{0,63}$/;
10472
+ var SAFE_ERROR_CODE = /^[A-Z][A-Z0-9_]{1,47}$/;
10473
+ function safeIdent(value) {
10474
+ return typeof value === "string" && SAFE_ERROR_IDENT.test(value) ? value : null;
10476
10475
  }
10476
+ function errorClassChain(err) {
10477
+ const names = [];
10478
+ let proto = Object.getPrototypeOf(err);
10479
+ for (let depth = 0; proto && depth < 8; depth++) {
10480
+ const ctor = proto.constructor;
10481
+ const name = safeIdent(ctor?.name);
10482
+ if (name && name !== "Object") names.push(name);
10483
+ proto = Object.getPrototypeOf(proto);
10484
+ }
10485
+ return names;
10486
+ }
10487
+ function causeInfo(err) {
10488
+ const cause = err.cause;
10489
+ if (!cause || typeof cause !== "object") return { label: null, code: null };
10490
+ const chain = errorClassChain(cause);
10491
+ const name = chain[0] ?? safeIdent(cause.name);
10492
+ let code = null;
10493
+ let node = cause;
10494
+ for (let depth = 0; node && typeof node === "object" && depth < 3 && !code; depth++) {
10495
+ const raw = node.code;
10496
+ if (typeof raw === "string" && SAFE_ERROR_CODE.test(raw)) code = raw;
10497
+ node = node.cause;
10498
+ }
10499
+ if (!name && !code) return { label: null, code: null };
10500
+ return { label: code ? `${name ?? "unknown"}/${code}` : name, code };
10501
+ }
10502
+ function classifyModelError(err) {
10503
+ if (!err || typeof err !== "object") {
10504
+ return { name: "unknown", category: "unknown", status: null, cause: null };
10505
+ }
10506
+ const chain = errorClassChain(err);
10507
+ const ownName = safeIdent(err.name);
10508
+ const specific = chain.find((name2) => name2 !== "Error");
10509
+ const name = specific ?? (ownName && ownName !== "Error" ? ownName : chain[0] ?? ownName ?? "unknown");
10510
+ const status = errorHttpStatus(err);
10511
+ const names = new Set(chain);
10512
+ if (ownName) names.add(ownName);
10513
+ const { label: cause, code: causeCode } = causeInfo(err);
10514
+ let category = "unknown";
10515
+ if (status === 401 || status === 403) category = "auth";
10516
+ else if (status === 400) category = "bad_request";
10517
+ else if (status === 429) category = "rate_limit";
10518
+ else if (status != null && status >= 500) category = "server";
10519
+ else if (status != null && status >= 400) category = "client";
10520
+ else if (names.has("APIConnectionTimeoutError") || names.has("TimeoutError") || causeCode === "ETIMEDOUT") {
10521
+ category = "timeout";
10522
+ } else if (names.has("APIUserAbortError") || names.has("AbortError")) category = "abort";
10523
+ else if (names.has("APIConnectionError")) category = "connection";
10524
+ if (category === "unknown" && causeCode) category = "connection";
10525
+ return { name, category, status, cause };
10526
+ }
10527
+ function isRetryableModelError(info) {
10528
+ if (info.category === "timeout" || info.category === "abort") return false;
10529
+ if (info.status == null) return true;
10530
+ if (info.status === 408 || info.status === 409 || info.status === 429) return true;
10531
+ return info.status >= 500;
10532
+ }
10533
+ var MODEL_RETRY_DELAY_MS = 300;
10534
+ var HOSTED_RETRY_SURFACES = /* @__PURE__ */ new Set(["api", "web", "app"]);
10535
+ var MAX_MODEL_THROW_LOGS = 3;
10477
10536
  var SEVERITY_PRIORITY = {
10478
10537
  critical: 0,
10479
10538
  high: 1,
@@ -10765,6 +10824,7 @@ function parseReviewResponse(text) {
10765
10824
  }
10766
10825
  async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER_BUDGET_MS, options) {
10767
10826
  const surface = options?.surface ?? "cli";
10827
+ const retryTransient = options?.retryTransient === true && HOSTED_RETRY_SURFACES.has(surface);
10768
10828
  const emptyMask = { prompt_batches: 0, prompt_chars: 0, masked_count: 0, had_secret_pattern: false, raw_after_mask: 0 };
10769
10829
  const early = (status2, reason2, extra) => ({
10770
10830
  findings,
@@ -10818,7 +10878,7 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
10818
10878
  const totalBefore = findings.length;
10819
10879
  let client;
10820
10880
  try {
10821
- client = new import_sdk.default();
10881
+ client = retryTransient ? new import_sdk.default({ maxRetries: 1 }) : new import_sdk.default();
10822
10882
  } catch {
10823
10883
  if (budgetTimer) clearTimeout(budgetTimer);
10824
10884
  return early("failed", "client_init", {
@@ -10840,6 +10900,14 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
10840
10900
  let fenceDropCount = 0;
10841
10901
  let timedOutCount = 0;
10842
10902
  let missingContentCount = 0;
10903
+ let modelThrowLogs = 0;
10904
+ const logModelThrow = (info, action) => {
10905
+ if (modelThrowLogs >= MAX_MODEL_THROW_LOGS) return;
10906
+ modelThrowLogs += 1;
10907
+ console.error(
10908
+ `[ai-fp-filter] model call threw: class ${info.name}, category ${info.category}, status ${info.status ?? "none"}, cause ${info.cause ?? "none"}, model ${MODEL_TRIAGE}; ${action}`
10909
+ );
10910
+ };
10843
10911
  const detectionCaches = /* @__PURE__ */ new Map();
10844
10912
  const cacheFor = (file) => {
10845
10913
  let cache2 = detectionCaches.get(file);
@@ -10910,6 +10978,7 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
10910
10978
  return;
10911
10979
  }
10912
10980
  let active = targets;
10981
+ let callAborted = false;
10913
10982
  try {
10914
10983
  const remaining = Math.max(1, deadlineMs - Date.now());
10915
10984
  const fences = assignFileFences(targets, fileContents);
@@ -10972,34 +11041,52 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
10972
11041
  );
10973
11042
  }
10974
11043
  const perCall = perCallTimeoutMs(budgetMs);
10975
- const callTimeout = Math.min(perCall, remaining);
10976
- const callAbort = new AbortController();
10977
- const onParentAbort = () => callAbort.abort();
10978
- if (abortController.signal.aborted) callAbort.abort();
10979
- else abortController.signal.addEventListener("abort", onParentAbort, { once: true });
10980
- const callTimer = setTimeout(() => callAbort.abort(), callTimeout);
11044
+ const send = async (callTimeout) => {
11045
+ const callAbort = new AbortController();
11046
+ const onParentAbort = () => callAbort.abort();
11047
+ if (abortController.signal.aborted) callAbort.abort();
11048
+ else abortController.signal.addEventListener("abort", onParentAbort, { once: true });
11049
+ const callTimer = setTimeout(() => callAbort.abort(), callTimeout);
11050
+ try {
11051
+ return await client.messages.create(
11052
+ {
11053
+ model: MODEL_TRIAGE,
11054
+ max_tokens: 2048,
11055
+ // Haiku 5.5 rejects non-default sampling params with HTTP 400
11056
+ // ("temperature is deprecated for this model"). Omit temperature /
11057
+ // top_p / top_k entirely — do NOT send MODEL_TEMPERATURE (0).
11058
+ // Adaptive thinking is on by default for 5.5; disable it here: this
11059
+ // is binary FP classification, not open-ended analysis, and thinking
11060
+ // burns the shared AI budget (Isobar rescan: every call 400'd on
11061
+ // temperature, then timed_out with aiReviewed:false → grade D).
11062
+ thinking: { type: "disabled" },
11063
+ system: REVIEW_SYSTEM_PROMPT,
11064
+ messages: [{ role: "user", content: prompt }]
11065
+ },
11066
+ { timeout: callTimeout, signal: callAbort.signal }
11067
+ );
11068
+ } finally {
11069
+ callAborted = callAbort.signal.aborted;
11070
+ clearTimeout(callTimer);
11071
+ abortController.signal.removeEventListener("abort", onParentAbort);
11072
+ }
11073
+ };
10981
11074
  let response;
10982
11075
  try {
10983
- response = await client.messages.create(
10984
- {
10985
- model: MODEL_TRIAGE,
10986
- max_tokens: 2048,
10987
- // Haiku 5.5 rejects non-default sampling params with HTTP 400
10988
- // ("temperature is deprecated for this model"). Omit temperature /
10989
- // top_p / top_k entirely — do NOT send MODEL_TEMPERATURE (0).
10990
- // Adaptive thinking is on by default for 5.5; disable it here: this
10991
- // is binary FP classification, not open-ended analysis, and thinking
10992
- // burns the shared AI budget (Isobar rescan: every call 400'd on
10993
- // temperature, then timed_out with aiReviewed:false → grade D).
10994
- thinking: { type: "disabled" },
10995
- system: REVIEW_SYSTEM_PROMPT,
10996
- messages: [{ role: "user", content: prompt }]
10997
- },
10998
- { timeout: callTimeout, signal: callAbort.signal }
10999
- );
11000
- } finally {
11001
- clearTimeout(callTimer);
11002
- abortController.signal.removeEventListener("abort", onParentAbort);
11076
+ response = await send(Math.min(perCall, remaining));
11077
+ } catch (err) {
11078
+ const info = classifyModelError(err);
11079
+ const budgetAbort = abortController.signal.aborted;
11080
+ const retry = retryTransient && !callAborted && !budgetAbort && !authFailed && isRetryableModelError(info) && deadlineMs - Date.now() - MODEL_RETRY_DELAY_MS >= perCall;
11081
+ if (!budgetAbort) logModelThrow(info, retry ? "retrying once" : "not retried");
11082
+ if (!retry) throw err;
11083
+ await new Promise((resolve) => setTimeout(resolve, MODEL_RETRY_DELAY_MS));
11084
+ try {
11085
+ response = await send(Math.min(perCall, Math.max(1, deadlineMs - Date.now())));
11086
+ } catch (retryErr) {
11087
+ if (!abortController.signal.aborted) logModelThrow(classifyModelError(retryErr), "retry failed");
11088
+ throw retryErr;
11089
+ }
11003
11090
  }
11004
11091
  const text = response.content.filter((b) => b.type === "text").map((b) => b.text).join("");
11005
11092
  const results = parseReviewResponse(text);
@@ -11058,7 +11145,7 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
11058
11145
  console.error(`[ai-fp-filter] AI review failed: auth status ${status2}`);
11059
11146
  return;
11060
11147
  }
11061
- const callTimedOut = isAbortError(err) || isTimeoutError(err) || abortController.signal.aborted || callWasAborted(err);
11148
+ const callTimedOut = callAborted || isAbortError(err) || isTimeoutError(err) || abortController.signal.aborted;
11062
11149
  if (callTimedOut) {
11063
11150
  timedOutCount += active.length;
11064
11151
  budgetExpired = true;