@node9/policy-engine 2.54.0 → 2.54.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.d.mts CHANGED
@@ -169,6 +169,12 @@ interface FileAccess {
169
169
  recursive?: boolean;
170
170
  /** A write that adds to the file (`>>`, `tee -a`): its content stays. */
171
171
  append?: true;
172
+ /** A shell write whose whole content is literal (a heredoc, `echo`, `printf`
173
+ * redirected to the file): what the file will hold (node9Firewall#385). */
174
+ text?: string;
175
+ /** An in-place edit made only of substitutions (`sed -i 's/a/b/'`): the
176
+ * replacement texts, judged as an edit tool's new text would be. */
177
+ edits?: string[];
172
178
  /** This basename-joined candidate applies only if this operand is a directory. */
173
179
  directoryTarget?: string;
174
180
  candidates?: string[];
@@ -2449,7 +2455,7 @@ declare const CANONICAL_EXTRACTOR_VERSION = "canonical-v39";
2449
2455
  * files changed, this hash must change too, and you must consciously
2450
2456
  * decide whether to bump CANONICAL_EXTRACTOR_VERSION."
2451
2457
  */
2452
- declare const CANONICAL_EXTRACTOR_HASH = "5ec00461a49e1858";
2458
+ declare const CANONICAL_EXTRACTOR_HASH = "9b120b2f71641789";
2453
2459
  declare function extractCanonicalFindings(call: ToolCallEntry, ctx: ExtractContext): CanonicalFinding[];
2454
2460
  declare function extractSessionLevelFindings(calls: ReadonlyArray<SessionToolCall>, ctx: SessionExtractContext): CanonicalFinding[];
2455
2461
  /**
package/dist/index.d.ts CHANGED
@@ -169,6 +169,12 @@ interface FileAccess {
169
169
  recursive?: boolean;
170
170
  /** A write that adds to the file (`>>`, `tee -a`): its content stays. */
171
171
  append?: true;
172
+ /** A shell write whose whole content is literal (a heredoc, `echo`, `printf`
173
+ * redirected to the file): what the file will hold (node9Firewall#385). */
174
+ text?: string;
175
+ /** An in-place edit made only of substitutions (`sed -i 's/a/b/'`): the
176
+ * replacement texts, judged as an edit tool's new text would be. */
177
+ edits?: string[];
172
178
  /** This basename-joined candidate applies only if this operand is a directory. */
173
179
  directoryTarget?: string;
174
180
  candidates?: string[];
@@ -2449,7 +2455,7 @@ declare const CANONICAL_EXTRACTOR_VERSION = "canonical-v39";
2449
2455
  * files changed, this hash must change too, and you must consciously
2450
2456
  * decide whether to bump CANONICAL_EXTRACTOR_VERSION."
2451
2457
  */
2452
- declare const CANONICAL_EXTRACTOR_HASH = "5ec00461a49e1858";
2458
+ declare const CANONICAL_EXTRACTOR_HASH = "9b120b2f71641789";
2453
2459
  declare function extractCanonicalFindings(call: ToolCallEntry, ctx: ExtractContext): CanonicalFinding[];
2454
2460
  declare function extractSessionLevelFindings(calls: ReadonlyArray<SessionToolCall>, ctx: SessionExtractContext): CanonicalFinding[];
2455
2461
  /**
package/dist/index.js CHANGED
@@ -4282,12 +4282,46 @@ function wordValues(word, state) {
4282
4282
  if (!dynamic) return out.map(asWord);
4283
4283
  return out.filter((piece) => piece.value !== "").map((piece) => ({ value: piece.value, pattern: piece.pattern + "*", dynamic: true }));
4284
4284
  }
4285
+ function sedSubstitutions(script) {
4286
+ const out = [];
4287
+ for (const raw of script.split(/\r?\n|;/)) {
4288
+ const cmd = raw.trim();
4289
+ if (cmd === "") continue;
4290
+ const m = /^(?:(?:\d+|\$|\/(?:\\.|[^\\/])*\/)(?:,(?:\d+|\$|\/(?:\\.|[^\\/])*\/))?)?s(.)/.exec(
4291
+ cmd
4292
+ );
4293
+ if (!m) return null;
4294
+ const delimiter = m[1];
4295
+ if (delimiter === "\\") return null;
4296
+ const parts = [];
4297
+ let cur = "";
4298
+ let i = m[0].length;
4299
+ for (; i < cmd.length && parts.length < 2; i++) {
4300
+ const ch = cmd[i];
4301
+ if (ch === "\\" && i + 1 < cmd.length) {
4302
+ cur += ch + cmd[++i];
4303
+ continue;
4304
+ }
4305
+ if (ch === delimiter) {
4306
+ parts.push(cur);
4307
+ cur = "";
4308
+ continue;
4309
+ }
4310
+ cur += ch;
4311
+ }
4312
+ if (parts.length < 2) return null;
4313
+ if (!/^[gpiImM0-9]*$/.test(cmd.slice(i))) return null;
4314
+ out.push(parts[1].replace(/\\(.)/g, "$1"));
4315
+ }
4316
+ return out.length > 0 ? out : null;
4317
+ }
4285
4318
  function collectShellFileAccesses(command, context = {}) {
4286
4319
  const out = [];
4287
4320
  let payloads = 256;
4288
4321
  const functions = /* @__PURE__ */ new Map();
4289
4322
  const seenCalls = /* @__PURE__ */ new Set();
4290
4323
  const pipedStdin = /* @__PURE__ */ new WeakMap();
4324
+ let currentStdin = null;
4291
4325
  let callWalks2 = MAX_CALL_WALKS;
4292
4326
  const initial = {
4293
4327
  cwd: [context.cwd ?? ""],
@@ -4382,8 +4416,9 @@ function collectShellFileAccesses(command, context = {}) {
4382
4416
  }
4383
4417
  const locate = (p) => tokens.find((w) => w?.value === p) ?? { value: p };
4384
4418
  if (name === "sed") {
4385
- let inplace = false, hasScript = false, ended = false;
4419
+ let inplace = false, hasScript = false, ended = false, scriptUnknown = false;
4386
4420
  const files = [];
4421
+ const scriptParts = [];
4387
4422
  for (let i = 0; i < args.length; i++) {
4388
4423
  const token = args[i];
4389
4424
  if (!token) {
@@ -4401,7 +4436,11 @@ function collectShellFileAccesses(command, context = {}) {
4401
4436
  hasScript = true;
4402
4437
  const eq = value.indexOf("=");
4403
4438
  const script = eq < 0 ? args[++i] : { value: value.slice(eq + 1) };
4404
- if (value.startsWith("--file") && script) add("read", script, state);
4439
+ if (value.startsWith("--file")) {
4440
+ scriptUnknown = true;
4441
+ if (script) add("read", script, state);
4442
+ } else if (script?.value) scriptParts.push(script.value);
4443
+ else scriptUnknown = true;
4405
4444
  }
4406
4445
  continue;
4407
4446
  }
@@ -4416,7 +4455,11 @@ function collectShellFileAccesses(command, context = {}) {
4416
4455
  if (flag === "e" || flag === "f") {
4417
4456
  hasScript = true;
4418
4457
  const script = value.slice(j + 1) ? { value: value.slice(j + 1) } : args[++i];
4419
- if (flag === "f" && script) add("read", script, state);
4458
+ if (flag === "f") {
4459
+ scriptUnknown = true;
4460
+ if (script) add("read", script, state);
4461
+ } else if (script?.value) scriptParts.push(script.value);
4462
+ else scriptUnknown = true;
4420
4463
  break;
4421
4464
  }
4422
4465
  if (flag === "l") {
@@ -4426,10 +4469,14 @@ function collectShellFileAccesses(command, context = {}) {
4426
4469
  }
4427
4470
  continue;
4428
4471
  }
4429
- if (!hasScript) hasScript = true;
4430
- else files.push(token);
4472
+ if (!hasScript) {
4473
+ hasScript = true;
4474
+ scriptParts.push(value);
4475
+ } else files.push(token);
4431
4476
  }
4432
- for (const file of files) add(inplace ? "write" : "read", file, state);
4477
+ const edits = inplace && !scriptUnknown ? sedSubstitutions(scriptParts.join("\n")) : null;
4478
+ for (const file of files)
4479
+ add(inplace ? "write" : "read", file, state, edits ? { edits } : {});
4433
4480
  return;
4434
4481
  }
4435
4482
  if (FS_READ_TOOLS.has(name) && name !== "dd") {
@@ -4556,9 +4603,11 @@ function collectShellFileAccesses(command, context = {}) {
4556
4603
  continue;
4557
4604
  const remove = name === "rm" || name === "unlink";
4558
4605
  const append = name === "tee" && strings.some((s) => s === "--append" || /^-[^-]*a/.test(s));
4606
+ const teed = name === "tee" && !append ? currentStdin : null;
4559
4607
  add(remove ? "remove" : name === "touch" ? "create" : "write", token, state, {
4560
4608
  recursive: remove && strings.some((s) => /^-[^-]*[rR]/.test(s) || s === "--recursive"),
4561
- ...append ? { append: true } : {}
4609
+ ...append ? { append: true } : {},
4610
+ ...teed !== null ? { text: teed } : {}
4562
4611
  });
4563
4612
  }
4564
4613
  return;
@@ -4880,11 +4929,18 @@ function collectShellFileAccesses(command, context = {}) {
4880
4929
  if (stmt.Background) state = clone(state);
4881
4930
  const headWord = stmt.Cmd && stmt.Cmd.kind === "CallExpr" ? base(resolveWordLiteral(stmt.Cmd.Args?.[0])) : "";
4882
4931
  const sends = SENDING_STDIN.has(headWord);
4932
+ let produced;
4883
4933
  for (const redir of stmt.Redirs ?? []) {
4884
4934
  substitutions(redir.Word, state, depth);
4885
4935
  for (const word of wordValues(redir.Word, state)) {
4886
- if (ops().WRITES.has(redir.Op))
4887
- add("write", word, state, ops().APPENDS.has(redir.Op) ? { append: true } : {});
4936
+ if (ops().WRITES.has(redir.Op)) {
4937
+ if (ops().APPENDS.has(redir.Op)) add("write", word, state, { append: true });
4938
+ else {
4939
+ const stdout = redir.N == null || redir.N.Value === "1";
4940
+ if (stdout && produced === void 0) produced = producedText(stmt);
4941
+ add("write", word, state, stdout && produced ? { text: produced } : {});
4942
+ }
4943
+ }
4888
4944
  if (ops().READS.has(redir.Op)) add("read", word, state, sends ? { upload: true } : {});
4889
4945
  if (redir.Op === ops().DUP_OUT && word.value && !/^\d+$/.test(word.value))
4890
4946
  add("write", word, state);
@@ -4896,6 +4952,7 @@ function collectShellFileAccesses(command, context = {}) {
4896
4952
  stdinScript(stmt, state, depth);
4897
4953
  const fedText = stdinText(stmt);
4898
4954
  if (fedText !== null) nestedPayload(xargsPayload(stmt, fedText), clone(state), depth);
4955
+ currentStdin = fedText;
4899
4956
  switch (cmd.kind) {
4900
4957
  case "CallExpr":
4901
4958
  call(cmd, state, depth);
@@ -15159,8 +15216,16 @@ function evaluateGitHooksFileAccess(args, accesses, context, settings, shell) {
15159
15216
  disarms = true;
15160
15217
  how = "removed";
15161
15218
  } else if (texts === null) {
15162
- disarms = true;
15163
- how = "overwritten";
15219
+ if (access.edits) {
15220
+ disarms = access.edits.some(editDisarms);
15221
+ how = "edited to exit";
15222
+ } else if (access.text !== void 0) {
15223
+ disarms = hookTextRunsNothing(access.text);
15224
+ how = "emptied";
15225
+ } else {
15226
+ disarms = true;
15227
+ how = "overwritten";
15228
+ }
15164
15229
  } else if (texts.whole.length > 0) {
15165
15230
  disarms = texts.whole.some(hookTextRunsNothing);
15166
15231
  how = "emptied";
@@ -20628,7 +20693,7 @@ function matchCanaryArgs(args, values) {
20628
20693
  // src/scan/canonical.ts
20629
20694
  var LONG_OUTPUT_THRESHOLD_BYTES = 100 * 1024;
20630
20695
  var CANONICAL_EXTRACTOR_VERSION = "canonical-v39";
20631
- var CANONICAL_EXTRACTOR_HASH = "5ec00461a49e1858";
20696
+ var CANONICAL_EXTRACTOR_HASH = "9b120b2f71641789";
20632
20697
  var DEDUPE_PREVIEW_LEN = 120;
20633
20698
  function extractCanonicalFindings(call, ctx) {
20634
20699
  const out = [];
package/dist/index.mjs CHANGED
@@ -4019,12 +4019,46 @@ function wordValues(word, state) {
4019
4019
  if (!dynamic) return out.map(asWord);
4020
4020
  return out.filter((piece) => piece.value !== "").map((piece) => ({ value: piece.value, pattern: piece.pattern + "*", dynamic: true }));
4021
4021
  }
4022
+ function sedSubstitutions(script) {
4023
+ const out = [];
4024
+ for (const raw of script.split(/\r?\n|;/)) {
4025
+ const cmd = raw.trim();
4026
+ if (cmd === "") continue;
4027
+ const m = /^(?:(?:\d+|\$|\/(?:\\.|[^\\/])*\/)(?:,(?:\d+|\$|\/(?:\\.|[^\\/])*\/))?)?s(.)/.exec(
4028
+ cmd
4029
+ );
4030
+ if (!m) return null;
4031
+ const delimiter = m[1];
4032
+ if (delimiter === "\\") return null;
4033
+ const parts = [];
4034
+ let cur = "";
4035
+ let i = m[0].length;
4036
+ for (; i < cmd.length && parts.length < 2; i++) {
4037
+ const ch = cmd[i];
4038
+ if (ch === "\\" && i + 1 < cmd.length) {
4039
+ cur += ch + cmd[++i];
4040
+ continue;
4041
+ }
4042
+ if (ch === delimiter) {
4043
+ parts.push(cur);
4044
+ cur = "";
4045
+ continue;
4046
+ }
4047
+ cur += ch;
4048
+ }
4049
+ if (parts.length < 2) return null;
4050
+ if (!/^[gpiImM0-9]*$/.test(cmd.slice(i))) return null;
4051
+ out.push(parts[1].replace(/\\(.)/g, "$1"));
4052
+ }
4053
+ return out.length > 0 ? out : null;
4054
+ }
4022
4055
  function collectShellFileAccesses(command, context = {}) {
4023
4056
  const out = [];
4024
4057
  let payloads = 256;
4025
4058
  const functions = /* @__PURE__ */ new Map();
4026
4059
  const seenCalls = /* @__PURE__ */ new Set();
4027
4060
  const pipedStdin = /* @__PURE__ */ new WeakMap();
4061
+ let currentStdin = null;
4028
4062
  let callWalks2 = MAX_CALL_WALKS;
4029
4063
  const initial = {
4030
4064
  cwd: [context.cwd ?? ""],
@@ -4119,8 +4153,9 @@ function collectShellFileAccesses(command, context = {}) {
4119
4153
  }
4120
4154
  const locate = (p) => tokens.find((w) => w?.value === p) ?? { value: p };
4121
4155
  if (name === "sed") {
4122
- let inplace = false, hasScript = false, ended = false;
4156
+ let inplace = false, hasScript = false, ended = false, scriptUnknown = false;
4123
4157
  const files = [];
4158
+ const scriptParts = [];
4124
4159
  for (let i = 0; i < args.length; i++) {
4125
4160
  const token = args[i];
4126
4161
  if (!token) {
@@ -4138,7 +4173,11 @@ function collectShellFileAccesses(command, context = {}) {
4138
4173
  hasScript = true;
4139
4174
  const eq = value.indexOf("=");
4140
4175
  const script = eq < 0 ? args[++i] : { value: value.slice(eq + 1) };
4141
- if (value.startsWith("--file") && script) add("read", script, state);
4176
+ if (value.startsWith("--file")) {
4177
+ scriptUnknown = true;
4178
+ if (script) add("read", script, state);
4179
+ } else if (script?.value) scriptParts.push(script.value);
4180
+ else scriptUnknown = true;
4142
4181
  }
4143
4182
  continue;
4144
4183
  }
@@ -4153,7 +4192,11 @@ function collectShellFileAccesses(command, context = {}) {
4153
4192
  if (flag === "e" || flag === "f") {
4154
4193
  hasScript = true;
4155
4194
  const script = value.slice(j + 1) ? { value: value.slice(j + 1) } : args[++i];
4156
- if (flag === "f" && script) add("read", script, state);
4195
+ if (flag === "f") {
4196
+ scriptUnknown = true;
4197
+ if (script) add("read", script, state);
4198
+ } else if (script?.value) scriptParts.push(script.value);
4199
+ else scriptUnknown = true;
4157
4200
  break;
4158
4201
  }
4159
4202
  if (flag === "l") {
@@ -4163,10 +4206,14 @@ function collectShellFileAccesses(command, context = {}) {
4163
4206
  }
4164
4207
  continue;
4165
4208
  }
4166
- if (!hasScript) hasScript = true;
4167
- else files.push(token);
4209
+ if (!hasScript) {
4210
+ hasScript = true;
4211
+ scriptParts.push(value);
4212
+ } else files.push(token);
4168
4213
  }
4169
- for (const file of files) add(inplace ? "write" : "read", file, state);
4214
+ const edits = inplace && !scriptUnknown ? sedSubstitutions(scriptParts.join("\n")) : null;
4215
+ for (const file of files)
4216
+ add(inplace ? "write" : "read", file, state, edits ? { edits } : {});
4170
4217
  return;
4171
4218
  }
4172
4219
  if (FS_READ_TOOLS.has(name) && name !== "dd") {
@@ -4293,9 +4340,11 @@ function collectShellFileAccesses(command, context = {}) {
4293
4340
  continue;
4294
4341
  const remove = name === "rm" || name === "unlink";
4295
4342
  const append = name === "tee" && strings.some((s) => s === "--append" || /^-[^-]*a/.test(s));
4343
+ const teed = name === "tee" && !append ? currentStdin : null;
4296
4344
  add(remove ? "remove" : name === "touch" ? "create" : "write", token, state, {
4297
4345
  recursive: remove && strings.some((s) => /^-[^-]*[rR]/.test(s) || s === "--recursive"),
4298
- ...append ? { append: true } : {}
4346
+ ...append ? { append: true } : {},
4347
+ ...teed !== null ? { text: teed } : {}
4299
4348
  });
4300
4349
  }
4301
4350
  return;
@@ -4617,11 +4666,18 @@ function collectShellFileAccesses(command, context = {}) {
4617
4666
  if (stmt.Background) state = clone(state);
4618
4667
  const headWord = stmt.Cmd && stmt.Cmd.kind === "CallExpr" ? base(resolveWordLiteral(stmt.Cmd.Args?.[0])) : "";
4619
4668
  const sends = SENDING_STDIN.has(headWord);
4669
+ let produced;
4620
4670
  for (const redir of stmt.Redirs ?? []) {
4621
4671
  substitutions(redir.Word, state, depth);
4622
4672
  for (const word of wordValues(redir.Word, state)) {
4623
- if (ops().WRITES.has(redir.Op))
4624
- add("write", word, state, ops().APPENDS.has(redir.Op) ? { append: true } : {});
4673
+ if (ops().WRITES.has(redir.Op)) {
4674
+ if (ops().APPENDS.has(redir.Op)) add("write", word, state, { append: true });
4675
+ else {
4676
+ const stdout = redir.N == null || redir.N.Value === "1";
4677
+ if (stdout && produced === void 0) produced = producedText(stmt);
4678
+ add("write", word, state, stdout && produced ? { text: produced } : {});
4679
+ }
4680
+ }
4625
4681
  if (ops().READS.has(redir.Op)) add("read", word, state, sends ? { upload: true } : {});
4626
4682
  if (redir.Op === ops().DUP_OUT && word.value && !/^\d+$/.test(word.value))
4627
4683
  add("write", word, state);
@@ -4633,6 +4689,7 @@ function collectShellFileAccesses(command, context = {}) {
4633
4689
  stdinScript(stmt, state, depth);
4634
4690
  const fedText = stdinText(stmt);
4635
4691
  if (fedText !== null) nestedPayload(xargsPayload(stmt, fedText), clone(state), depth);
4692
+ currentStdin = fedText;
4636
4693
  switch (cmd.kind) {
4637
4694
  case "CallExpr":
4638
4695
  call(cmd, state, depth);
@@ -14896,8 +14953,16 @@ function evaluateGitHooksFileAccess(args, accesses, context, settings, shell) {
14896
14953
  disarms = true;
14897
14954
  how = "removed";
14898
14955
  } else if (texts === null) {
14899
- disarms = true;
14900
- how = "overwritten";
14956
+ if (access.edits) {
14957
+ disarms = access.edits.some(editDisarms);
14958
+ how = "edited to exit";
14959
+ } else if (access.text !== void 0) {
14960
+ disarms = hookTextRunsNothing(access.text);
14961
+ how = "emptied";
14962
+ } else {
14963
+ disarms = true;
14964
+ how = "overwritten";
14965
+ }
14901
14966
  } else if (texts.whole.length > 0) {
14902
14967
  disarms = texts.whole.some(hookTextRunsNothing);
14903
14968
  how = "emptied";
@@ -20365,7 +20430,7 @@ function matchCanaryArgs(args, values) {
20365
20430
  // src/scan/canonical.ts
20366
20431
  var LONG_OUTPUT_THRESHOLD_BYTES = 100 * 1024;
20367
20432
  var CANONICAL_EXTRACTOR_VERSION = "canonical-v39";
20368
- var CANONICAL_EXTRACTOR_HASH = "5ec00461a49e1858";
20433
+ var CANONICAL_EXTRACTOR_HASH = "9b120b2f71641789";
20369
20434
  var DEDUPE_PREVIEW_LEN = 120;
20370
20435
  function extractCanonicalFindings(call, ctx) {
20371
20436
  const out = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@node9/policy-engine",
3
- "version": "2.54.0",
3
+ "version": "2.54.1",
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",