@node9/policy-engine 2.41.0 → 2.41.2

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
@@ -2251,7 +2251,7 @@ declare const LONG_OUTPUT_THRESHOLD_BYTES: number;
2251
2251
  * and fails CI when the hash drifts without a version bump — forgetting
2252
2252
  * is loud, not silent.
2253
2253
  */
2254
- declare const CANONICAL_EXTRACTOR_VERSION = "canonical-v30";
2254
+ declare const CANONICAL_EXTRACTOR_VERSION = "canonical-v31";
2255
2255
  /**
2256
2256
  * SHA-256 prefix of the detector-source files
2257
2257
  * (canonical.ts + pii.ts + destructive-regex.ts).
@@ -2262,7 +2262,7 @@ declare const CANONICAL_EXTRACTOR_VERSION = "canonical-v30";
2262
2262
  * files changed, this hash must change too, and you must consciously
2263
2263
  * decide whether to bump CANONICAL_EXTRACTOR_VERSION."
2264
2264
  */
2265
- declare const CANONICAL_EXTRACTOR_HASH = "d55f1f9e5bee7e41";
2265
+ declare const CANONICAL_EXTRACTOR_HASH = "60048df5a64eb7c0";
2266
2266
  declare function extractCanonicalFindings(call: ToolCallEntry, ctx: ExtractContext): CanonicalFinding[];
2267
2267
  declare function extractSessionLevelFindings(calls: ReadonlyArray<SessionToolCall>, ctx: SessionExtractContext): CanonicalFinding[];
2268
2268
  /**
package/dist/index.d.ts CHANGED
@@ -2251,7 +2251,7 @@ declare const LONG_OUTPUT_THRESHOLD_BYTES: number;
2251
2251
  * and fails CI when the hash drifts without a version bump — forgetting
2252
2252
  * is loud, not silent.
2253
2253
  */
2254
- declare const CANONICAL_EXTRACTOR_VERSION = "canonical-v30";
2254
+ declare const CANONICAL_EXTRACTOR_VERSION = "canonical-v31";
2255
2255
  /**
2256
2256
  * SHA-256 prefix of the detector-source files
2257
2257
  * (canonical.ts + pii.ts + destructive-regex.ts).
@@ -2262,7 +2262,7 @@ declare const CANONICAL_EXTRACTOR_VERSION = "canonical-v30";
2262
2262
  * files changed, this hash must change too, and you must consciously
2263
2263
  * decide whether to bump CANONICAL_EXTRACTOR_VERSION."
2264
2264
  */
2265
- declare const CANONICAL_EXTRACTOR_HASH = "d55f1f9e5bee7e41";
2265
+ declare const CANONICAL_EXTRACTOR_HASH = "60048df5a64eb7c0";
2266
2266
  declare function extractCanonicalFindings(call: ToolCallEntry, ctx: ExtractContext): CanonicalFinding[];
2267
2267
  declare function extractSessionLevelFindings(calls: ReadonlyArray<SessionToolCall>, ctx: SessionExtractContext): CanonicalFinding[];
2268
2268
  /**
package/dist/index.js CHANGED
@@ -3908,6 +3908,97 @@ function foldCommandWords(reading) {
3908
3908
  function commandReadingsImpl(command) {
3909
3909
  return cachedNormalize(command, () => normalizeCommandForPolicyImpl(command));
3910
3910
  }
3911
+ var STDIN_CODE_INTERPRETERS = /^(python[0-9.]*|node|nodejs|perl|ruby|php|lua)$/;
3912
+ function readsStdinAsCode(interp, rest) {
3913
+ const ok = (w) => {
3914
+ if (w === null) return false;
3915
+ if (w === "-") return true;
3916
+ if (/^python/.test(interp))
3917
+ return ["-u", "-B", "-E", "-I", "-s", "-q", "-O", "-OO"].includes(w) || /^-[WX]\S/.test(w);
3918
+ if (interp === "perl" || interp === "ruby") return w === "-w" || w === "-W";
3919
+ return false;
3920
+ };
3921
+ return rest.every(ok);
3922
+ }
3923
+ var RUNS_TEXT_ELSEWHERE = /* @__PURE__ */ new Set([
3924
+ ...SHELL_INTERPRETERS,
3925
+ "csh",
3926
+ "tcsh",
3927
+ "ash",
3928
+ "mksh",
3929
+ "busybox",
3930
+ "eval",
3931
+ "source",
3932
+ ".",
3933
+ "exec",
3934
+ "xargs",
3935
+ "ssh",
3936
+ "scp",
3937
+ "docker",
3938
+ "podman",
3939
+ "kubectl",
3940
+ "su",
3941
+ "nsenter",
3942
+ "at",
3943
+ "batch",
3944
+ "crontab",
3945
+ "flock",
3946
+ "psql",
3947
+ "mysql",
3948
+ "mongo",
3949
+ "mongosh",
3950
+ "redis-cli",
3951
+ "sqlite3"
3952
+ ]);
3953
+ function interpreterHeredocSpans(f, at) {
3954
+ const stmts = f?.Stmts || [];
3955
+ const candidates = [];
3956
+ let unsafe = false;
3957
+ syntax.Walk(f, (node) => {
3958
+ if (!node || unsafe) return false;
3959
+ const n = node;
3960
+ const t = syntax.NodeType(n);
3961
+ if (t === "FuncDecl") {
3962
+ unsafe = true;
3963
+ return false;
3964
+ }
3965
+ if (t !== "CallExpr") return true;
3966
+ const words = (n.Args || []).map((a) => resolveWordLiteral(a));
3967
+ const head = unwrapCommandHead(words);
3968
+ const raw = words[head];
3969
+ if (raw === null || raw === void 0) {
3970
+ unsafe = true;
3971
+ return false;
3972
+ }
3973
+ const name = raw.split("/").pop()?.toLowerCase() ?? "";
3974
+ if (raw.includes("/") && !STDIN_CODE_INTERPRETERS.test(name)) unsafe = true;
3975
+ if (RUNS_TEXT_ELSEWHERE.has(name)) unsafe = true;
3976
+ return !unsafe;
3977
+ });
3978
+ if (unsafe) return [];
3979
+ for (const st of stmts) {
3980
+ if (!st?.Cmd || syntax.NodeType(st.Cmd) !== "CallExpr") continue;
3981
+ const words = (st.Cmd.Args || []).map((a) => resolveWordLiteral(a));
3982
+ const name = (words[0] ?? "").toLowerCase();
3983
+ if (!STDIN_CODE_INTERPRETERS.test(name)) continue;
3984
+ if (!readsStdinAsCode(name, words.slice(1))) continue;
3985
+ candidates.push(st);
3986
+ }
3987
+ const spans = [];
3988
+ for (const st of candidates) {
3989
+ const heredocs = (st.Redirs || []).filter((r2) => r2?.Hdoc);
3990
+ const stdin = heredocs.filter((r2) => r2.N == null || r2.N.Value === "0");
3991
+ const others = heredocs.length - stdin.length;
3992
+ if (stdin.length !== 1 || others > 0) continue;
3993
+ const r = stdin[0];
3994
+ if (heredocText(r.Hdoc) === null) continue;
3995
+ const hs = at(r.Hdoc.Pos().Offset());
3996
+ const he = at(r.Hdoc.End().Offset());
3997
+ if (hs < 0 || he < 0 || he <= hs) continue;
3998
+ spans.push([hs, he]);
3999
+ }
4000
+ return spans;
4001
+ }
3911
4002
  function normalizeCommandForPolicyImpl(command) {
3912
4003
  const f = parseShared(command);
3913
4004
  if (f === PARSE_FAIL) return { posix: command, separator: command, folded: command };
@@ -3918,6 +4009,10 @@ function normalizeCommandForPolicyImpl(command) {
3918
4009
  const rewrites = [];
3919
4010
  const quoteOnlyRewrites = [];
3920
4011
  const msgSpans = /* @__PURE__ */ new Set();
4012
+ for (const [hs, he] of interpreterHeredocSpans(f, at)) {
4013
+ strips.push([hs, he]);
4014
+ msgSpans.add(`${hs}:${he}`);
4015
+ }
3921
4016
  syntax.Walk(f, (node) => {
3922
4017
  if (!node) return false;
3923
4018
  const n = node;
@@ -3971,7 +4066,12 @@ function normalizeCommandForPolicyImpl(command) {
3971
4066
  });
3972
4067
  const stripEdits = strips.map(([s, e]) => [s, e, '""']);
3973
4068
  const apply = (extra) => {
3974
- const edits = [...stripEdits, ...extra];
4069
+ const all = [...stripEdits, ...extra];
4070
+ const edits = all.filter(
4071
+ (e, i) => !all.some(
4072
+ (o, k) => k !== i && o[0] <= e[0] && e[1] <= o[1] && (o[0] < e[0] || e[1] < o[1] || k < i)
4073
+ )
4074
+ );
3975
4075
  if (edits.length === 0) return command;
3976
4076
  edits.sort((a, b) => b[0] - a[0]);
3977
4077
  let out = command;
@@ -15941,8 +16041,8 @@ function matchCanaryArgs(args, values) {
15941
16041
 
15942
16042
  // src/scan/canonical.ts
15943
16043
  var LONG_OUTPUT_THRESHOLD_BYTES = 100 * 1024;
15944
- var CANONICAL_EXTRACTOR_VERSION = "canonical-v30";
15945
- var CANONICAL_EXTRACTOR_HASH = "d55f1f9e5bee7e41";
16044
+ var CANONICAL_EXTRACTOR_VERSION = "canonical-v31";
16045
+ var CANONICAL_EXTRACTOR_HASH = "60048df5a64eb7c0";
15946
16046
  var DEDUPE_PREVIEW_LEN = 120;
15947
16047
  function extractCanonicalFindings(call, ctx) {
15948
16048
  const out = [];
package/dist/index.mjs CHANGED
@@ -3658,6 +3658,97 @@ function foldCommandWords(reading) {
3658
3658
  function commandReadingsImpl(command) {
3659
3659
  return cachedNormalize(command, () => normalizeCommandForPolicyImpl(command));
3660
3660
  }
3661
+ var STDIN_CODE_INTERPRETERS = /^(python[0-9.]*|node|nodejs|perl|ruby|php|lua)$/;
3662
+ function readsStdinAsCode(interp, rest) {
3663
+ const ok = (w) => {
3664
+ if (w === null) return false;
3665
+ if (w === "-") return true;
3666
+ if (/^python/.test(interp))
3667
+ return ["-u", "-B", "-E", "-I", "-s", "-q", "-O", "-OO"].includes(w) || /^-[WX]\S/.test(w);
3668
+ if (interp === "perl" || interp === "ruby") return w === "-w" || w === "-W";
3669
+ return false;
3670
+ };
3671
+ return rest.every(ok);
3672
+ }
3673
+ var RUNS_TEXT_ELSEWHERE = /* @__PURE__ */ new Set([
3674
+ ...SHELL_INTERPRETERS,
3675
+ "csh",
3676
+ "tcsh",
3677
+ "ash",
3678
+ "mksh",
3679
+ "busybox",
3680
+ "eval",
3681
+ "source",
3682
+ ".",
3683
+ "exec",
3684
+ "xargs",
3685
+ "ssh",
3686
+ "scp",
3687
+ "docker",
3688
+ "podman",
3689
+ "kubectl",
3690
+ "su",
3691
+ "nsenter",
3692
+ "at",
3693
+ "batch",
3694
+ "crontab",
3695
+ "flock",
3696
+ "psql",
3697
+ "mysql",
3698
+ "mongo",
3699
+ "mongosh",
3700
+ "redis-cli",
3701
+ "sqlite3"
3702
+ ]);
3703
+ function interpreterHeredocSpans(f, at) {
3704
+ const stmts = f?.Stmts || [];
3705
+ const candidates = [];
3706
+ let unsafe = false;
3707
+ syntax.Walk(f, (node) => {
3708
+ if (!node || unsafe) return false;
3709
+ const n = node;
3710
+ const t = syntax.NodeType(n);
3711
+ if (t === "FuncDecl") {
3712
+ unsafe = true;
3713
+ return false;
3714
+ }
3715
+ if (t !== "CallExpr") return true;
3716
+ const words = (n.Args || []).map((a) => resolveWordLiteral(a));
3717
+ const head = unwrapCommandHead(words);
3718
+ const raw = words[head];
3719
+ if (raw === null || raw === void 0) {
3720
+ unsafe = true;
3721
+ return false;
3722
+ }
3723
+ const name = raw.split("/").pop()?.toLowerCase() ?? "";
3724
+ if (raw.includes("/") && !STDIN_CODE_INTERPRETERS.test(name)) unsafe = true;
3725
+ if (RUNS_TEXT_ELSEWHERE.has(name)) unsafe = true;
3726
+ return !unsafe;
3727
+ });
3728
+ if (unsafe) return [];
3729
+ for (const st of stmts) {
3730
+ if (!st?.Cmd || syntax.NodeType(st.Cmd) !== "CallExpr") continue;
3731
+ const words = (st.Cmd.Args || []).map((a) => resolveWordLiteral(a));
3732
+ const name = (words[0] ?? "").toLowerCase();
3733
+ if (!STDIN_CODE_INTERPRETERS.test(name)) continue;
3734
+ if (!readsStdinAsCode(name, words.slice(1))) continue;
3735
+ candidates.push(st);
3736
+ }
3737
+ const spans = [];
3738
+ for (const st of candidates) {
3739
+ const heredocs = (st.Redirs || []).filter((r2) => r2?.Hdoc);
3740
+ const stdin = heredocs.filter((r2) => r2.N == null || r2.N.Value === "0");
3741
+ const others = heredocs.length - stdin.length;
3742
+ if (stdin.length !== 1 || others > 0) continue;
3743
+ const r = stdin[0];
3744
+ if (heredocText(r.Hdoc) === null) continue;
3745
+ const hs = at(r.Hdoc.Pos().Offset());
3746
+ const he = at(r.Hdoc.End().Offset());
3747
+ if (hs < 0 || he < 0 || he <= hs) continue;
3748
+ spans.push([hs, he]);
3749
+ }
3750
+ return spans;
3751
+ }
3661
3752
  function normalizeCommandForPolicyImpl(command) {
3662
3753
  const f = parseShared(command);
3663
3754
  if (f === PARSE_FAIL) return { posix: command, separator: command, folded: command };
@@ -3668,6 +3759,10 @@ function normalizeCommandForPolicyImpl(command) {
3668
3759
  const rewrites = [];
3669
3760
  const quoteOnlyRewrites = [];
3670
3761
  const msgSpans = /* @__PURE__ */ new Set();
3762
+ for (const [hs, he] of interpreterHeredocSpans(f, at)) {
3763
+ strips.push([hs, he]);
3764
+ msgSpans.add(`${hs}:${he}`);
3765
+ }
3671
3766
  syntax.Walk(f, (node) => {
3672
3767
  if (!node) return false;
3673
3768
  const n = node;
@@ -3721,7 +3816,12 @@ function normalizeCommandForPolicyImpl(command) {
3721
3816
  });
3722
3817
  const stripEdits = strips.map(([s, e]) => [s, e, '""']);
3723
3818
  const apply = (extra) => {
3724
- const edits = [...stripEdits, ...extra];
3819
+ const all = [...stripEdits, ...extra];
3820
+ const edits = all.filter(
3821
+ (e, i) => !all.some(
3822
+ (o, k) => k !== i && o[0] <= e[0] && e[1] <= o[1] && (o[0] < e[0] || e[1] < o[1] || k < i)
3823
+ )
3824
+ );
3725
3825
  if (edits.length === 0) return command;
3726
3826
  edits.sort((a, b) => b[0] - a[0]);
3727
3827
  let out = command;
@@ -15691,8 +15791,8 @@ function matchCanaryArgs(args, values) {
15691
15791
 
15692
15792
  // src/scan/canonical.ts
15693
15793
  var LONG_OUTPUT_THRESHOLD_BYTES = 100 * 1024;
15694
- var CANONICAL_EXTRACTOR_VERSION = "canonical-v30";
15695
- var CANONICAL_EXTRACTOR_HASH = "d55f1f9e5bee7e41";
15794
+ var CANONICAL_EXTRACTOR_VERSION = "canonical-v31";
15795
+ var CANONICAL_EXTRACTOR_HASH = "60048df5a64eb7c0";
15696
15796
  var DEDUPE_PREVIEW_LEN = 120;
15697
15797
  function extractCanonicalFindings(call, ctx) {
15698
15798
  const out = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@node9/policy-engine",
3
- "version": "2.41.0",
3
+ "version": "2.41.2",
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",