@node9/policy-engine 2.32.0 → 2.33.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/dist/index.mjs CHANGED
@@ -4520,36 +4520,129 @@ var BASH_TOOL_NAMES = /* @__PURE__ */ new Set([
4520
4520
  "shell",
4521
4521
  "exec_command"
4522
4522
  ]);
4523
- var MCP_EXECUTORS = /* @__PURE__ */ new Set([...BASH_TOOL_NAMES, "exec", "run_command", "terminal"]);
4523
+ var MCP_EXECUTORS = /* @__PURE__ */ new Set([
4524
+ ...BASH_TOOL_NAMES,
4525
+ "exec",
4526
+ "run_command",
4527
+ "terminal",
4528
+ "execute_command",
4529
+ "execute_shell",
4530
+ "execute_shell_command",
4531
+ "shell_execute",
4532
+ "run_shell",
4533
+ "run_terminal_command",
4534
+ "start_process"
4535
+ ]);
4536
+ var BUILTIN_FIELDS = ["command", "cmd", "script"];
4537
+ var ARGV_FIELDS = ["args", "argv"];
4538
+ var BARE_WORD = /^[A-Za-z0-9_./+:@%,-]+$/;
4539
+ var MAX_ARGV_CHARACTERS = 262144;
4524
4540
  function isBashTool(tool) {
4525
4541
  return BASH_TOOL_NAMES.has(tool.toLowerCase());
4526
4542
  }
4527
- function inspectionEntry(tool, inspection = {}) {
4543
+ function inspectionLayers(policy) {
4544
+ return { trusted: policy.toolInspection, project: policy.projectToolInspection };
4545
+ }
4546
+ function isShellEntry(value) {
4547
+ return typeof value === "object" && value !== null && value.kind === "shell" && typeof value.field === "string" && Object.keys(value).length === 2;
4548
+ }
4549
+ function isInspectionLayers(input) {
4550
+ const trusted = input.trusted;
4551
+ return typeof trusted === "object" && trusted !== null && !isShellEntry(trusted);
4552
+ }
4553
+ function layersOf(input) {
4554
+ return isInspectionLayers(input) ? { trusted: input.trusted, project: input.project ?? {} } : { trusted: input, project: {} };
4555
+ }
4556
+ function entryOf(tool, inspection) {
4528
4557
  const pattern = Object.keys(inspection).find((p) => matchesPattern(tool, p));
4529
4558
  return pattern === void 0 ? void 0 : inspection[pattern];
4530
4559
  }
4560
+ function inspectionEntry(tool, inspection = {}) {
4561
+ const { trusted, project } = layersOf(inspection);
4562
+ return entryOf(tool, trusted) ?? entryOf(tool, project);
4563
+ }
4564
+ function executorName(tool) {
4565
+ const name = tool.toLowerCase();
4566
+ if (!name.startsWith("mcp__")) return name;
4567
+ const at = name.lastIndexOf("__");
4568
+ return at >= "mcp__".length ? name.slice(at + 2) : name;
4569
+ }
4570
+ function entryFields(entry) {
4571
+ if (typeof entry === "object") return [entry.field];
4572
+ return entry === "command" ? ["command"] : null;
4573
+ }
4531
4574
  function shellFields(tool, inspection) {
4532
4575
  if (isBashTool(tool)) return ["command"];
4533
- const entry = inspectionEntry(tool, inspection);
4534
- if (typeof entry === "object") return [entry.field];
4535
- if (entry !== void 0) return entry === "command" ? [entry] : null;
4536
- const name = tool.toLowerCase().replace(/^mcp__[a-z0-9_-]+?__/, "");
4537
- return MCP_EXECUTORS.has(name) ? ["command", "cmd", "script"] : null;
4576
+ const { trusted, project } = layersOf(inspection);
4577
+ const trustedEntry = entryOf(tool, trusted);
4578
+ if (trustedEntry !== void 0) return entryFields(trustedEntry);
4579
+ const builtin = MCP_EXECUTORS.has(executorName(tool)) ? BUILTIN_FIELDS : null;
4580
+ const projectEntry = entryOf(tool, project);
4581
+ if (projectEntry === void 0) return builtin;
4582
+ const added = entryFields(projectEntry);
4583
+ if (!builtin) return added;
4584
+ if (!added) return builtin;
4585
+ return [.../* @__PURE__ */ new Set([...builtin, ...added])];
4538
4586
  }
4539
4587
  function isShellShapedTool(tool, inspection = {}) {
4540
4588
  return shellFields(tool, inspection) !== null;
4541
4589
  }
4590
+ function shellWord(word) {
4591
+ if (BARE_WORD.test(word)) return word;
4592
+ return word === "" ? "''" : `'${word.replace(/'/g, `'\\''`)}'`;
4593
+ }
4594
+ function isStringArray(value) {
4595
+ return Array.isArray(value) && value.every((item) => typeof item === "string");
4596
+ }
4597
+ function commandLine(args, fieldPath, value, native) {
4598
+ const missing = { reason: "Shell command field is missing or is not a string" };
4599
+ const unreadable = { reason: "Shell command arguments could not be read" };
4600
+ if (typeof value !== "string" && (native || !Array.isArray(value))) return missing;
4601
+ const dot = fieldPath.lastIndexOf(".");
4602
+ const parent = dot === -1 ? args : getNestedValue(args, fieldPath.slice(0, dot));
4603
+ const siblings = ARGV_FIELDS.map((key) => ({
4604
+ key,
4605
+ value: parent && typeof parent === "object" ? parent[key] : void 0
4606
+ })).filter(({ value: value2 }) => value2 !== void 0 && value2 !== null);
4607
+ let argv = siblings[0];
4608
+ if (native) argv = siblings.find(({ value: v }) => isStringArray(v));
4609
+ else if (siblings.length === 2 && JSON.stringify(siblings[0].value) !== JSON.stringify(siblings[1].value))
4610
+ return { reason: "Shell command arguments disagree" };
4611
+ let command;
4612
+ let source = fieldPath;
4613
+ if (Array.isArray(value)) {
4614
+ if (argv || !value.length || !isStringArray(value)) return unreadable;
4615
+ command = value.map(shellWord).join(" ");
4616
+ } else if (!argv) {
4617
+ return { command: value, fieldPath };
4618
+ } else {
4619
+ if (!isStringArray(argv.value)) return unreadable;
4620
+ if (!argv.value.length) return { command: value, fieldPath };
4621
+ command = `${value} ${argv.value.map(shellWord).join(" ")}`;
4622
+ source = `${fieldPath}+${argv.key}`;
4623
+ }
4624
+ if (command.length > MAX_ARGV_CHARACTERS)
4625
+ return { reason: "Shell command arguments exceed the inspectable size" };
4626
+ return { command, fieldPath: source };
4627
+ }
4542
4628
  function resolveShellInput(tool, args, inspection = {}) {
4543
4629
  const fields = shellFields(tool, inspection);
4544
4630
  if (!fields) return { kind: "not-shell" };
4545
4631
  const values = fields.map((fieldPath) => ({ fieldPath, value: getNestedValue(args, fieldPath) })).filter(({ value }) => value !== void 0 && value !== null && value !== "");
4546
- if (!values.length || values.some(({ value }) => typeof value !== "string")) {
4632
+ if (!values.length) {
4547
4633
  return { kind: "uninspectable", reason: "Shell command field is missing or is not a string" };
4548
4634
  }
4549
- if (new Set(values.map(({ value }) => value)).size !== 1) {
4635
+ const native = isBashTool(tool);
4636
+ const lines = values.map(({ fieldPath, value }) => commandLine(args, fieldPath, value, native));
4637
+ const failed = lines.find((line) => "reason" in line);
4638
+ if (failed) return { kind: "uninspectable", reason: failed.reason };
4639
+ const commands = lines.filter(
4640
+ (line) => "command" in line
4641
+ );
4642
+ if (new Set(commands.map(({ command }) => command)).size !== 1) {
4550
4643
  return { kind: "uninspectable", reason: "Multiple shell command fields disagree" };
4551
4644
  }
4552
- return { kind: "shell", command: values[0].value, fieldPath: values[0].fieldPath };
4645
+ return { kind: "shell", ...commands[0] };
4553
4646
  }
4554
4647
  function shellRuleArgs(tool, args, ruleTool, inspection = {}) {
4555
4648
  if (!ruleTool || !matchesPattern("bash", ruleTool) || matchesPattern(tool, ruleTool)) return args;
@@ -7434,20 +7527,26 @@ var BIP39_PUBLIC_VECTORS = /* @__PURE__ */ new Set([
7434
7527
  var indices = new Map(BIP39_ENGLISH.map((word, i) => [word, i]));
7435
7528
  var lengths = [24, 21, 18, 15, 12];
7436
7529
  var hash = (value) => createHash("sha256").update(value).digest();
7437
- function findMnemonic(text, budget = { candidates: 0, reasons: /* @__PURE__ */ new Set() }) {
7438
- const words = [...text.matchAll(/[a-zA-Z]+/g)];
7530
+ var MAX_MNEMONIC_HASHES = 25e3;
7531
+ function findMnemonic(text, budget = { hashes: 0, reasons: /* @__PURE__ */ new Set() }) {
7439
7532
  let run = [];
7533
+ let runIndex = [];
7440
7534
  let previousEnd = 0;
7441
7535
  const rejected = /* @__PURE__ */ new Set();
7442
7536
  const inspect = () => {
7537
+ if (run.length < 12) return false;
7538
+ const ascending = new Array(run.length).fill(0);
7539
+ for (let i = 1; i < run.length; i++)
7540
+ ascending[i] = ascending[i - 1] + (runIndex[i] > runIndex[i - 1] ? 1 : 0);
7443
7541
  for (let start = 0; start + 12 <= run.length; start++) {
7444
7542
  for (const count of lengths) {
7445
7543
  if (start + count > run.length) continue;
7544
+ if (ascending[start + count - 1] - ascending[start] === count - 1) continue;
7446
7545
  const candidate = run.slice(start, start + count);
7447
7546
  const sentence = candidate.join(" ");
7448
7547
  if (rejected.has(sentence)) continue;
7449
- if (++budget.candidates > 1024) {
7450
- budget.reasons.add("mnemonic-candidates");
7548
+ if (++budget.hashes > MAX_MNEMONIC_HASHES) {
7549
+ budget.reasons.add("mnemonic-budget");
7451
7550
  return false;
7452
7551
  }
7453
7552
  if (BIP39_PUBLIC_VECTORS.has(hash(sentence).toString("hex"))) {
@@ -7465,13 +7564,18 @@ function findMnemonic(text, budget = { candidates: 0, reasons: /* @__PURE__ */ n
7465
7564
  }
7466
7565
  return false;
7467
7566
  };
7468
- for (const word of words) {
7469
- if (!indices.has(word[0]) || run.length && !/^\s+$/.test(text.slice(previousEnd, word.index))) {
7567
+ for (const word of text.matchAll(/[a-zA-Z]+/g)) {
7568
+ const index = indices.get(word[0]);
7569
+ if (index === void 0 || run.length && !/^\s+$/.test(text.slice(previousEnd, word.index))) {
7470
7570
  if (inspect()) return true;
7471
- if (budget.reasons.has("mnemonic-candidates")) return false;
7571
+ if (budget.reasons.has("mnemonic-budget")) return false;
7472
7572
  run = [];
7573
+ runIndex = [];
7574
+ }
7575
+ if (index !== void 0) {
7576
+ run.push(word[0]);
7577
+ runIndex.push(index);
7473
7578
  }
7474
- if (indices.has(word[0])) run.push(word[0]);
7475
7579
  previousEnd = word.index + word[0].length;
7476
7580
  }
7477
7581
  return inspect();
@@ -7714,6 +7818,10 @@ function validateCask(raw) {
7714
7818
 
7715
7819
  // src/dlp/normalize.ts
7716
7820
  var INVISIBLE_RE = /[­͏؜᠎​-‏‪-‮⁠-⁤-︀-️]|[\u{E0000}-\u{E007F}]/gu;
7821
+ function isInvisibleChar(char) {
7822
+ return INVISIBLE_ONE.test(char);
7823
+ }
7824
+ var INVISIBLE_ONE = new RegExp(`^(?:${INVISIBLE_RE.source.split("|")[0]})$`, "u");
7717
7825
  function stripInvisible(text) {
7718
7826
  return text.replace(INVISIBLE_RE, "");
7719
7827
  }
@@ -7889,7 +7997,7 @@ function scanInjection(text, ctx = {}) {
7889
7997
  }
7890
7998
  }
7891
7999
  if (matched.length === 0) return null;
7892
- const untrusted = !!ctx.tool && UNTRUSTED_TOOLS.test(ctx.tool);
8000
+ const untrusted = ctx.untrusted ?? (!!ctx.tool && UNTRUSTED_TOOLS.test(ctx.tool));
7893
8001
  const score = matched.length + (untrusted ? 1 : 0);
7894
8002
  const confidence = score >= 3 ? "high" : score === 2 ? "medium" : "low";
7895
8003
  return { signals: untrusted ? [...matched, "untrusted-origin"] : matched, confidence };
@@ -8677,6 +8785,46 @@ function matchArgumentText(text, fieldPath, budget) {
8677
8785
  }
8678
8786
  return best;
8679
8787
  }
8788
+ var BATCH_SEPARATOR = "\n";
8789
+ function matchArgumentBatch(fragments, fieldPath, budget) {
8790
+ if (!fragments.length) return null;
8791
+ const text = fragments.join(BATCH_SEPARATOR);
8792
+ const lower2 = text.toLowerCase();
8793
+ let best = null;
8794
+ for (const pattern of DLP_PATTERNS) {
8795
+ if (pattern.keywords && !pattern.keywords.some((kw) => lower2.includes(kw.toLowerCase())))
8796
+ continue;
8797
+ const flags = pattern.regex.flags.replace(/[gmy]/g, "");
8798
+ if (!new RegExp(pattern.regex.source, flags + "m").test(text)) continue;
8799
+ for (const fragment of fragments) {
8800
+ if (firstAcceptedMatch(pattern, fragment) === null) continue;
8801
+ const severity = pattern.contextBoost && isAssignmentContext(fragment) ? "block" : pattern.severity;
8802
+ if (severity === "block")
8803
+ return {
8804
+ patternName: pattern.name,
8805
+ fieldPath,
8806
+ redactedSample: "[encoded secret]",
8807
+ severity
8808
+ };
8809
+ best ??= {
8810
+ patternName: pattern.name,
8811
+ fieldPath,
8812
+ redactedSample: "[encoded secret]",
8813
+ severity
8814
+ };
8815
+ if (!pattern.contextBoost) break;
8816
+ }
8817
+ }
8818
+ if (findMnemonic(text, budget)) {
8819
+ return {
8820
+ patternName: "BIP-39 Recovery Phrase",
8821
+ fieldPath,
8822
+ redactedSample: "[recovery phrase]",
8823
+ severity: "block"
8824
+ };
8825
+ }
8826
+ return best;
8827
+ }
8680
8828
  function scanArgs(args, depth = 0, fieldPath = "args") {
8681
8829
  if (depth > MAX_DEPTH2 || args === null || args === void 0) return null;
8682
8830
  if (Array.isArray(args)) {
@@ -10135,12 +10283,23 @@ function ssrfDestinationFloor(toolName, args, opts = {}) {
10135
10283
 
10136
10284
  // src/dlp/secret-views.ts
10137
10285
  var SECRET_VIEW_LIMITS = {
10286
+ /** A decoded text that itself decodes is followed this many layers; a deeper layer is reviewed. */
10138
10287
  maxDepth: 4,
10139
- maxViewsPerLeaf: 64,
10140
- maxViews: 256,
10141
- maxDerivedBytes: 1048576,
10142
- maxCandidates: 4096
10288
+ /** Nested views per parent view, and nested text per call; past either the call is reviewed. */
10289
+ maxNestedViewsPerLeaf: 64,
10290
+ maxNestedBytes: 1048576,
10291
+ /** Joined hostname-label text per call; past it the call is reviewed. */
10292
+ maxJoinedLabelBytes: 1048576,
10293
+ /** Decoded fragments are matched in batches of about this many characters. */
10294
+ batchCharacters: 65536,
10295
+ /** Only a valid DNS name has its labels joined. */
10296
+ maxHostLength: 253,
10297
+ maxLabelLength: 63,
10298
+ maxLabels: 127
10143
10299
  };
10300
+ function newSecretViewBudget() {
10301
+ return { hashes: 0, nestedBytes: 0, joinedLabelBytes: 0, reasons: /* @__PURE__ */ new Set() };
10302
+ }
10144
10303
  function textBytes(buf) {
10145
10304
  if (buf.length < 16) return null;
10146
10305
  const text = buf.toString("utf8");
@@ -10159,13 +10318,41 @@ function percentOnce(text) {
10159
10318
  }
10160
10319
  });
10161
10320
  }
10321
+ function* wrappedBase64(text) {
10322
+ if (!text.includes("\n")) return;
10323
+ const word = /^[A-Za-z0-9+/_-]+={0,2}$/;
10324
+ let run = [];
10325
+ let width = 0;
10326
+ const flush = function* () {
10327
+ if (run.length >= 2) yield run.join("");
10328
+ run = [];
10329
+ width = 0;
10330
+ };
10331
+ for (const raw of text.split("\n")) {
10332
+ const line = raw.trim();
10333
+ const full = line.length >= 16 && word.test(line) && !line.includes("=");
10334
+ if (full && (!run.length || line.length === width)) {
10335
+ run.push(line);
10336
+ width = line.length;
10337
+ continue;
10338
+ }
10339
+ if (run.length && word.test(line) && line.length >= 2 && line.length <= width) {
10340
+ run.push(line);
10341
+ yield* flush();
10342
+ continue;
10343
+ }
10344
+ yield* flush();
10345
+ if (full) {
10346
+ run.push(line);
10347
+ width = line.length;
10348
+ }
10349
+ }
10350
+ yield* flush();
10351
+ }
10162
10352
  function* encodedRegions(text, budget) {
10163
10353
  yield [text, false];
10354
+ for (const block of wrappedBase64(text)) yield [block, false];
10164
10355
  for (const match of text.matchAll(/(?<![a-z0-9+.-])[a-z][a-z0-9+.-]{0,30}:\/\/[^\s<>"']+/gi)) {
10165
- if (++budget.candidates > SECRET_VIEW_LIMITS.maxCandidates) {
10166
- budget.reasons.add("decode-candidates");
10167
- return;
10168
- }
10169
10356
  try {
10170
10357
  new URL(match[0]);
10171
10358
  } catch {
@@ -10185,6 +10372,8 @@ function* encodedRegions(text, budget) {
10185
10372
  ];
10186
10373
  for (const region of regions) if (region) yield [region, false];
10187
10374
  const labels = host2.split(".");
10375
+ if (host2.length > SECRET_VIEW_LIMITS.maxHostLength || labels.length > SECRET_VIEW_LIMITS.maxLabels || !labels.every((l) => l.length >= 1 && l.length <= SECRET_VIEW_LIMITS.maxLabelLength))
10376
+ continue;
10188
10377
  for (let start = 0; start < labels.length; start++) {
10189
10378
  let joined = labels[start];
10190
10379
  for (let end = start + 1; end < Math.min(labels.length, start + 8); end++) {
@@ -10192,11 +10381,13 @@ function* encodedRegions(text, budget) {
10192
10381
  if (!run.every((s) => /^[a-f0-9]{2,}$/i.test(s)) && !run.every((s) => /^[A-Z2-7]{4,}$/i.test(s)))
10193
10382
  break;
10194
10383
  joined += labels[end];
10195
- if (++budget.candidates > SECRET_VIEW_LIMITS.maxCandidates) {
10196
- budget.reasons.add("decode-candidates");
10384
+ if (joined.length < 16) continue;
10385
+ budget.joinedLabelBytes += joined.length;
10386
+ if (budget.joinedLabelBytes > SECRET_VIEW_LIMITS.maxJoinedLabelBytes) {
10387
+ budget.reasons.add("decode-labels");
10197
10388
  return;
10198
10389
  }
10199
- if (joined.length >= 16 && joined.length <= 253) yield [joined, true];
10390
+ yield [joined, true];
10200
10391
  }
10201
10392
  }
10202
10393
  }
@@ -10233,10 +10424,6 @@ function* decodedCandidates(text, budget) {
10233
10424
  if (joined) yield ["dns-labels", region];
10234
10425
  for (const [kind, re] of families) {
10235
10426
  for (const match of region.matchAll(re)) {
10236
- if (++budget.candidates > SECRET_VIEW_LIMITS.maxCandidates) {
10237
- budget.reasons.add("decode-candidates");
10238
- return;
10239
- }
10240
10427
  const raw = match[0];
10241
10428
  let buf;
10242
10429
  if (kind === "base64") {
@@ -10259,37 +10446,47 @@ function* decodedCandidates(text, budget) {
10259
10446
  }
10260
10447
  }
10261
10448
  }
10262
- function* secretViews(text, budget) {
10449
+ function secretViews(text, budget) {
10263
10450
  const seen = /* @__PURE__ */ new Set([text]);
10264
- const queue = [{ text, transforms: [] }];
10265
- for (let i = 0; i < queue.length; i++) {
10266
- const view = queue[i];
10267
- yield view;
10268
- for (const [kind, decoded] of decodedCandidates(view.text, budget)) {
10269
- if (seen.has(decoded)) continue;
10270
- if (view.transforms.length >= SECRET_VIEW_LIMITS.maxDepth) {
10271
- budget.reasons.add("decode-depth");
10272
- continue;
10273
- }
10274
- const bytes = Buffer.byteLength(decoded);
10275
- if (queue.length >= SECRET_VIEW_LIMITS.maxViewsPerLeaf || budget.views >= SECRET_VIEW_LIMITS.maxViews || budget.bytes + bytes > SECRET_VIEW_LIMITS.maxDerivedBytes) {
10276
- budget.reasons.add("decode-budget");
10277
- return;
10451
+ const out = [];
10452
+ let frontier = [{ text, transforms: [] }];
10453
+ for (let depth = 1; depth <= SECRET_VIEW_LIMITS.maxDepth; depth++) {
10454
+ const next = [];
10455
+ for (const parent of frontier) {
10456
+ let perLeaf = 0;
10457
+ for (const [kind, decoded] of decodedCandidates(parent.text, budget)) {
10458
+ if (seen.has(decoded)) continue;
10459
+ seen.add(decoded);
10460
+ const normalizing = kind === "invisibles" || kind === "percent";
10461
+ if (depth >= 2 && !normalizing) {
10462
+ budget.nestedBytes += Buffer.byteLength(decoded);
10463
+ if (++perLeaf > SECRET_VIEW_LIMITS.maxNestedViewsPerLeaf || budget.nestedBytes > SECRET_VIEW_LIMITS.maxNestedBytes) {
10464
+ budget.reasons.add("decode-nested");
10465
+ return out;
10466
+ }
10467
+ }
10468
+ const view = { text: decoded, transforms: [...parent.transforms, kind] };
10469
+ out.push(view);
10470
+ next.push(view);
10278
10471
  }
10279
- seen.add(decoded);
10280
- budget.views++;
10281
- budget.bytes += bytes;
10282
- queue.push({ text: decoded, transforms: [...view.transforms, kind] });
10283
10472
  }
10284
- if (budget.candidates > SECRET_VIEW_LIMITS.maxCandidates) return;
10473
+ frontier = next;
10474
+ if (!frontier.length) return out;
10475
+ }
10476
+ for (const view of frontier) {
10477
+ if (!decodedCandidates(view.text, budget).next().done) {
10478
+ budget.reasons.add("decode-nested");
10479
+ break;
10480
+ }
10285
10481
  }
10482
+ return out;
10286
10483
  }
10287
10484
 
10288
10485
  // src/dlp/arguments.ts
10289
10486
  var ARGUMENT_SCAN_LIMITS = {
10290
10487
  maxDepth: 12,
10291
10488
  maxNodes: 16384,
10292
- /** Raw text is matched in windows of this size; decoding views use the first window. */
10489
+ /** Text is matched and decoded in windows of this size. */
10293
10490
  maxStringCharacters: 1e5,
10294
10491
  /** Windows overlap by this much so a token on a window edge is still seen whole. */
10295
10492
  windowOverlap: 4096,
@@ -10299,17 +10496,46 @@ var ARGUMENT_SCAN_LIMITS = {
10299
10496
  maxTailBytes: 4194304,
10300
10497
  maxJsonCharacters: 1e4
10301
10498
  };
10499
+ var RUN_CHAR = /[A-Za-z0-9+/=_%\\.-]/;
10500
+ var runChar = (value, index) => {
10501
+ const char = value[index] ?? "";
10502
+ return RUN_CHAR.test(char) || char !== "" && isInvisibleChar(char);
10503
+ };
10504
+ function batches(views) {
10505
+ const out = [];
10506
+ let current = [];
10507
+ let size = 0;
10508
+ for (const { text } of views) {
10509
+ if (current.length && size + text.length > SECRET_VIEW_LIMITS.batchCharacters) {
10510
+ out.push(current);
10511
+ current = [];
10512
+ size = 0;
10513
+ }
10514
+ current.push(text);
10515
+ size += text.length;
10516
+ }
10517
+ if (current.length) out.push(current);
10518
+ return out;
10519
+ }
10302
10520
  function inspectArgumentSecrets(args) {
10303
- const budget = { views: 0, bytes: 0, candidates: 0, reasons: /* @__PURE__ */ new Set() };
10521
+ const budget = newSecretViewBudget();
10304
10522
  const ancestors = /* @__PURE__ */ new Set();
10305
10523
  let nodes = 0;
10306
10524
  let sourceBytes = 0;
10307
10525
  let tailBytes = 0;
10308
10526
  let best = null;
10527
+ const blocked = () => best?.severity === "block";
10309
10528
  const accept = (match) => {
10310
10529
  if (match && (!best || best.severity !== "block" && match.severity === "block")) best = match;
10311
10530
  };
10531
+ const decodeSegment = (segment, fieldPath) => {
10532
+ for (const batch of batches(secretViews(segment, budget))) {
10533
+ accept(matchArgumentBatch(batch, fieldPath, budget));
10534
+ if (blocked()) return;
10535
+ }
10536
+ };
10312
10537
  const visit = (value, depth, fieldPath) => {
10538
+ if (blocked()) return;
10313
10539
  if (++nodes > ARGUMENT_SCAN_LIMITS.maxNodes) {
10314
10540
  budget.reasons.add("argument-nodes");
10315
10541
  return;
@@ -10351,14 +10577,11 @@ function inspectArgumentSecrets(args) {
10351
10577
  }
10352
10578
  sourceBytes += Math.min(bytes, Math.max(0, remaining));
10353
10579
  if (!text) return;
10354
- for (const view of secretViews(text, budget)) {
10355
- const hit = matchArgumentText(view.text, fieldPath, budget);
10356
- if (hit)
10357
- accept(view.transforms.length ? { ...hit, redactedSample: "[encoded secret]" } : hit);
10358
- }
10580
+ accept(matchArgumentText(text, fieldPath, budget));
10581
+ let scannedEnd = text.length;
10359
10582
  if (!truncated && value.length > window) {
10360
- budget.reasons.add("decode-window");
10361
10583
  for (let start = window - overlap; start < value.length; start += window - overlap) {
10584
+ if (blocked()) return;
10362
10585
  const chunk = value.slice(start, start + window);
10363
10586
  const chunkBytes = Buffer.byteLength(chunk);
10364
10587
  if (tailBytes + chunkBytes > ARGUMENT_SCAN_LIMITS.maxTailBytes) {
@@ -10367,9 +10590,16 @@ function inspectArgumentSecrets(args) {
10367
10590
  }
10368
10591
  tailBytes += chunkBytes;
10369
10592
  accept(matchArgumentText(chunk, fieldPath, budget));
10593
+ scannedEnd = start + chunk.length;
10370
10594
  if (start + window >= value.length) break;
10371
10595
  }
10372
10596
  }
10597
+ for (let start = 0; start < scannedEnd && !blocked(); ) {
10598
+ let end = Math.min(start + window, scannedEnd);
10599
+ while (end < scannedEnd && runChar(value, end - 1) && runChar(value, end)) end++;
10600
+ decodeSegment(value.slice(start, end), fieldPath);
10601
+ start = end;
10602
+ }
10373
10603
  const trimmed = text.trim();
10374
10604
  if (value.length < ARGUMENT_SCAN_LIMITS.maxJsonCharacters && (trimmed.startsWith("{") || trimmed.startsWith("["))) {
10375
10605
  try {
@@ -10388,7 +10618,7 @@ function inspectArgumentSecrets(args) {
10388
10618
  function scanArgumentSecrets(args) {
10389
10619
  const result = inspectArgumentSecrets(args);
10390
10620
  if (result.match) return result.match;
10391
- return result.reasons.some((reason) => reason.startsWith("argument-")) ? {
10621
+ return result.coverage === "incomplete" ? {
10392
10622
  patternName: "Argument inspection incomplete",
10393
10623
  fieldPath: "args",
10394
10624
  redactedSample: "[uninspected content]",
@@ -10423,18 +10653,13 @@ function tokenize2(toolName) {
10423
10653
  return toolName.toLowerCase().split(/[_.\-\s]+/).filter(Boolean);
10424
10654
  }
10425
10655
  function extractShellCommand(toolName, args, toolInspection) {
10426
- const patterns = Object.keys(toolInspection);
10427
- const matchingPattern = patterns.find((p) => matchesPattern(toolName, p));
10428
- if (!matchingPattern) return null;
10429
- const fieldPath = toolInspection[matchingPattern];
10656
+ const fieldPath = inspectionEntry(toolName, toolInspection);
10657
+ if (fieldPath === void 0) return null;
10430
10658
  const value = getNestedValue(args, typeof fieldPath === "string" ? fieldPath : fieldPath.field);
10431
10659
  return typeof value === "string" ? value : null;
10432
10660
  }
10433
10661
  function isSqlTool(toolName, toolInspection) {
10434
- const patterns = Object.keys(toolInspection);
10435
- const matchingPattern = patterns.find((p) => matchesPattern(toolName, p));
10436
- if (!matchingPattern) return false;
10437
- const fieldName = toolInspection[matchingPattern];
10662
+ const fieldName = inspectionEntry(toolName, toolInspection);
10438
10663
  return fieldName === "sql" || fieldName === "query";
10439
10664
  }
10440
10665
  var SQL_DML_KEYWORDS = /* @__PURE__ */ new Set(["select", "insert", "update", "delete", "merge", "upsert"]);
@@ -10511,9 +10736,10 @@ function egressPolicyVerdict(eg) {
10511
10736
  }
10512
10737
  async function evaluatePolicy(config, toolName, args, context = {}, hooks = {}) {
10513
10738
  let protectedVerdict = null;
10739
+ const layers = inspectionLayers(config.policy);
10514
10740
  if (context.agent !== "Terminal") {
10515
10741
  try {
10516
- const accesses = context.accesses ?? collectFileAccesses(toolName, args, context, config.policy.toolInspection);
10742
+ const accesses = context.accesses ?? collectFileAccesses(toolName, args, context, layers);
10517
10743
  protectedVerdict = evaluateProtectedFileAccess(accesses, context, catalogSettings(config));
10518
10744
  } catch {
10519
10745
  protectedVerdict = incompleteFileVerdict();
@@ -10548,7 +10774,7 @@ async function evaluatePolicy(config, toolName, args, context = {}, hooks = {})
10548
10774
  hooks
10549
10775
  );
10550
10776
  if (result.decision === "block") return result;
10551
- if (context.agent !== "Terminal" && resolveShellInput(toolName, args, config.policy.toolInspection).kind === "uninspectable") {
10777
+ if (context.agent !== "Terminal" && resolveShellInput(toolName, args, layers).kind === "uninspectable") {
10552
10778
  return {
10553
10779
  decision: "review",
10554
10780
  ruleName: "shell-inspection-incomplete",
@@ -10564,13 +10790,14 @@ async function evaluatePolicyCore(config, toolName, args, context = {}, hooks =
10564
10790
  const { agent, cwd, activeEnvironment } = context;
10565
10791
  const { checkProvenance, isTrustedHost } = hooks;
10566
10792
  const settingsView = catalogSettings(config);
10793
+ const layers = inspectionLayers(config.policy);
10567
10794
  const cv = (id) => checkValue(settingsView, id);
10568
10795
  let loggedFinding;
10569
10796
  const noteLogged = (v) => {
10570
10797
  if (v?.logged && !loggedFinding) loggedFinding = v;
10571
10798
  };
10572
10799
  const allowed = () => loggedFinding ?? { decision: "allow" };
10573
- const shellInput = resolveShellInput(toolName, args, config.policy.toolInspection);
10800
+ const shellInput = resolveShellInput(toolName, args, layers);
10574
10801
  const wouldBeIgnored = matchesPattern(toolName, config.policy.ignoredTools);
10575
10802
  {
10576
10803
  const dest = ssrfDestinationFloor(toolName, args, {
@@ -10675,13 +10902,10 @@ async function evaluatePolicyCore(config, toolName, args, context = {}, hooks =
10675
10902
  return { id, value: cv(id) };
10676
10903
  };
10677
10904
  const matches = config.policy.smartRules.filter((rule) => {
10678
- if (!toolMatchesRule(toolName, rule.tool, config.policy.toolInspection)) return false;
10905
+ if (!toolMatchesRule(toolName, rule.tool, layers)) return false;
10679
10906
  if (astSuppressed(rule)) return false;
10680
10907
  if (rmCleanupWaiver && rule.name === "review-rm" && rule.verdict === "review") return false;
10681
- if (!evaluateSmartConditions(
10682
- shellRuleArgs(toolName, args, rule.tool, config.policy.toolInspection),
10683
- rule
10684
- ))
10908
+ if (!evaluateSmartConditions(shellRuleArgs(toolName, args, rule.tool, layers), rule))
10685
10909
  return false;
10686
10910
  const check = ruleCheck(rule);
10687
10911
  if (check?.value === "off") return false;
@@ -10728,7 +10952,7 @@ async function evaluatePolicyCore(config, toolName, args, context = {}, hooks =
10728
10952
  let allTokens = [];
10729
10953
  let pathTokens = [];
10730
10954
  if (pendingToolEgress) return pendingToolEgress;
10731
- const shellCommand = shellInput.kind === "shell" ? shellInput.command : shellInput.kind === "not-shell" ? extractShellCommand(toolName, args, config.policy.toolInspection) : null;
10955
+ const shellCommand = shellInput.kind === "shell" ? shellInput.command : shellInput.kind === "not-shell" ? extractShellCommand(toolName, args, layers) : null;
10732
10956
  if (shellCommand) {
10733
10957
  const analyzed = analyzeShellCommand(shellCommand);
10734
10958
  allTokens = analyzed.allTokens;
@@ -10805,7 +11029,7 @@ async function evaluatePolicyCore(config, toolName, args, context = {}, hooks =
10805
11029
  if (["ssh", "scp", "rsync"].includes(firstToken)) {
10806
11030
  const rawTokens = shellCommand.trim().split(/\s+/);
10807
11031
  const sshHosts = extractAllSshHosts(rawTokens.slice(1));
10808
- allTokens.push(...sshHosts);
11032
+ allTokens = allTokens.concat(sshHosts);
10809
11033
  }
10810
11034
  if (firstToken && firstToken.startsWith("/") && checkProvenance) {
10811
11035
  const prov = checkProvenance(firstToken, cwd);
@@ -10834,7 +11058,7 @@ async function evaluatePolicyCore(config, toolName, args, context = {}, hooks =
10834
11058
  };
10835
11059
  }
10836
11060
  }
10837
- if (isSqlTool(toolName, config.policy.toolInspection)) {
11061
+ if (isSqlTool(toolName, layers)) {
10838
11062
  allTokens = allTokens.filter((t) => !SQL_DML_KEYWORDS.has(t.toLowerCase()));
10839
11063
  }
10840
11064
  } else {
@@ -10842,7 +11066,7 @@ async function evaluatePolicyCore(config, toolName, args, context = {}, hooks =
10842
11066
  if (args && typeof args === "object") {
10843
11067
  const flattenedArgs = JSON.stringify(args).toLowerCase();
10844
11068
  const extraTokens = flattenedArgs.split(/[^a-zA-Z0-9]+/).filter((t) => t.length > 1);
10845
- allTokens.push(...extraTokens);
11069
+ allTokens = allTokens.concat(extraTokens);
10846
11070
  }
10847
11071
  }
10848
11072
  const isManual = agent === "Terminal";
@@ -12130,8 +12354,8 @@ function matchCanaryArgs(args, values) {
12130
12354
 
12131
12355
  // src/scan/canonical.ts
12132
12356
  var LONG_OUTPUT_THRESHOLD_BYTES = 100 * 1024;
12133
- var CANONICAL_EXTRACTOR_VERSION = "canonical-v25";
12134
- var CANONICAL_EXTRACTOR_HASH = "48ef4af5dd89198d";
12357
+ var CANONICAL_EXTRACTOR_VERSION = "canonical-v26";
12358
+ var CANONICAL_EXTRACTOR_HASH = "dec8c65145686360";
12135
12359
  var DEDUPE_PREVIEW_LEN = 120;
12136
12360
  function extractCanonicalFindings(call, ctx) {
12137
12361
  const out = [];
@@ -12608,6 +12832,8 @@ export {
12608
12832
  hostMatches,
12609
12833
  incompleteFileVerdict,
12610
12834
  inspectArgumentSecrets,
12835
+ inspectionEntry,
12836
+ inspectionLayers,
12611
12837
  isAgentInstructionPath,
12612
12838
  isBashTool,
12613
12839
  isDestructiveToolName,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@node9/policy-engine",
3
- "version": "2.32.0",
3
+ "version": "2.33.0",
4
4
  "description": "Shared policy evaluation engine for node9 — DLP, smart rules, AST shell parsing, shields, loop detection. Pure functions, no I/O. Used by both node9-proxy and the node9 SaaS firewall.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://node9.ai",