@kenhkl/agent-moss 0.17.38 → 0.17.40

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/README.md CHANGED
@@ -64,6 +64,11 @@ const result = await resp.json();
64
64
  ├── 完全安全 (15 bash 模式 + 6 action types) → 跳过 L2+L3
65
65
  └── 只读敏感 (15 bash 模式 + 8 action types) → 跳过 L3
66
66
 
67
+ 专项引擎
68
+ ├── script-engine: Shell/Python 后端 → 可达内容切片 + 递归脚本链追踪
69
+ │ (深度/循环/字节预算;解析失败回退全文本,fail-closed)
70
+ └── formal-engine: 多 session 行为图 + 跨会话组合链/数据流验证 + 有状态增量
71
+
67
72
  层1: 特征匹配静态检测 (<1ms)
68
73
  ├── 危险命令正则: 48 个模式(rm 分级检测,sudo 只拦截危险命令,nmap/masscan/zmap 检测)
69
74
  ├── → high/critical → 直接 Deny
@@ -154,6 +159,8 @@ ts/src/
154
159
  ├── logic-rules.ts # 层2: 8 条规则 + 20 敏感路径
155
160
  ├── llm-analyzer.ts # 层3: LLM 分析 (缓存 + 重试 + fail-closed)
156
161
  ├── script-analyzer.ts # 脚本内容预扫描 (18 个可疑模式)
162
+ ├── script-engine/ # 脚本分析引擎 (Shell/Python → IR + 递归脚本链)
163
+ ├── formal-engine/ # 形式化引擎 (L2.2 组合链 + 有状态增量 + 链状态)
157
164
  ├── skill-loader.ts # Skill 加载与关键词匹配 (12 个 Skill)
158
165
  └── template-loader.ts # Prompt 模板加载 (3 个模板)
159
166
  ```
package/dist/cli.js CHANGED
@@ -5982,7 +5982,19 @@ function checkHomeDeletion(actionType, actionDetail) {
5982
5982
  }
5983
5983
  }
5984
5984
  const assigned = {};
5985
- for (const m of actionDetail.matchAll(HOME_ASSIGN_RE)) assigned[m[1]] = m[2];
5985
+ const anchored = new RegExp(HOME_ASSIGN_RE.source, "iy");
5986
+ const nameRe = /[A-Za-z_][A-Za-z0-9_]*/gi;
5987
+ let nm;
5988
+ while ((nm = nameRe.exec(actionDetail)) !== null) {
5989
+ anchored.lastIndex = nm.index;
5990
+ const m = anchored.exec(actionDetail);
5991
+ if (m) {
5992
+ assigned[m[1]] = m[2];
5993
+ nameRe.lastIndex = anchored.lastIndex;
5994
+ } else {
5995
+ nameRe.lastIndex = nm.index + nm[0].length;
5996
+ }
5997
+ }
5986
5998
  for (const [varName, path9] of Object.entries(assigned)) {
5987
5999
  if (!isHomeValue(path9)) continue;
5988
6000
  const varRefRe = new RegExp(`(?:${escapeRegExp(varName)}|\\$\\{${escapeRegExp(varName)}\\})`);
@@ -6249,7 +6261,7 @@ var init_heuristic = __esm({
6249
6261
  SEARCH_ACTION_PREFIXES = HEURISTIC_RULES.search_action_prefixes ?? [];
6250
6262
  SEARCH_ACTION_EXACT = HEURISTIC_RULES.search_action_exact ?? [];
6251
6263
  HOME_TARGET_RE = /(?:^|[\s="'($])(?:~|(?:\$home|\$\{home\}))(?:\/(?:\.\*|\*|\.)?)?(?=[\s"';}|&)]|$)/i;
6252
- HOME_ASSIGN_RE = /([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(~[A-Za-z0-9_./-]*|\$HOME[A-Za-z0-9_./-]*|\$\{HOME\}[A-Za-z0-9_./-]*|\/home\/[^\s;|&]+|\$\((?:[^()]|\([^()]*\))*\))(?=\s|;|$|\||&)/gi;
6264
+ HOME_ASSIGN_RE = /([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(~[A-Za-z0-9_./-]*|\$HOME[A-Za-z0-9_./-]*|\$\{HOME\}[A-Za-z0-9_./-]*|\/home\/[^\s;|&]+|\$\((?:[^()]|\([^()]*\))*\))(?=\s|;|$|\||&)/i;
6253
6265
  }
6254
6266
  });
6255
6267
 
@@ -12047,8 +12059,10 @@ function cleanupChainArchives(dir, maxBytes = envInt3("AGENT_MOSS_CHAIN_ARCHIVE_
12047
12059
  }
12048
12060
  }, 0);
12049
12061
  if (maxBytes > 0) {
12050
- for (const p of files) {
12062
+ for (let i = 0; i < files.length; i++) {
12051
12063
  if (total <= maxBytes) break;
12064
+ if (i === files.length - 1) break;
12065
+ const p = files[i];
12052
12066
  try {
12053
12067
  const size = fs5.statSync(p).size;
12054
12068
  fs5.unlinkSync(p);
@@ -14584,8 +14598,10 @@ async function analyzeRoute(c) {
14584
14598
  }
14585
14599
  const slowMs = Number(process.env.AGENT_MOSS_SLOW_REQUEST_MS || "1000");
14586
14600
  if (Number.isFinite(slowMs) && slowMs > 0 && durationMs >= slowMs) {
14601
+ const recvAt = c.get?.("recvAt");
14602
+ const queueMs = typeof recvAt === "number" ? Math.max(0, t0 - recvAt) : 0;
14587
14603
  console.warn(
14588
- `[AgentMoss] \u6162\u8BF7\u6C42: ${Math.round(durationMs)}ms\uFF08\u9608\u503C ${slowMs}ms\uFF09 action_type=${req.a_next.action_type} agent=${agentId || "-"} decision=${result.decision}`
14604
+ `[AgentMoss] \u6162\u8BF7\u6C42: \u603B ${Math.round(durationMs)}ms = \u6392\u961F ${Math.round(queueMs)}ms + \u8BA1\u7B97 ${Math.round(durationMs - queueMs)}ms\uFF08\u9608\u503C ${slowMs}ms\uFF09 action_type=${req.a_next.action_type} agent=${agentId || "-"} decision=${result.decision}`
14589
14605
  );
14590
14606
  }
14591
14607
  try {
@@ -15447,6 +15463,10 @@ function createApp() {
15447
15463
  enforceRulesContract();
15448
15464
  hardenExistingPaths();
15449
15465
  const app = new Hono2();
15466
+ app.use("*", async (c, next) => {
15467
+ c.set("recvAt", performance.now());
15468
+ await next();
15469
+ });
15450
15470
  app.use("*", cors({
15451
15471
  origin: (o) => allowedOrigins().includes(o) ? o : null,
15452
15472
  allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
@@ -5103,7 +5103,19 @@ function checkHomeDeletion(actionType, actionDetail) {
5103
5103
  }
5104
5104
  }
5105
5105
  const assigned = {};
5106
- for (const m of actionDetail.matchAll(HOME_ASSIGN_RE)) assigned[m[1]] = m[2];
5106
+ const anchored = new RegExp(HOME_ASSIGN_RE.source, "iy");
5107
+ const nameRe = /[A-Za-z_][A-Za-z0-9_]*/gi;
5108
+ let nm;
5109
+ while ((nm = nameRe.exec(actionDetail)) !== null) {
5110
+ anchored.lastIndex = nm.index;
5111
+ const m = anchored.exec(actionDetail);
5112
+ if (m) {
5113
+ assigned[m[1]] = m[2];
5114
+ nameRe.lastIndex = anchored.lastIndex;
5115
+ } else {
5116
+ nameRe.lastIndex = nm.index + nm[0].length;
5117
+ }
5118
+ }
5107
5119
  for (const [varName, path8] of Object.entries(assigned)) {
5108
5120
  if (!isHomeValue(path8)) continue;
5109
5121
  const varRefRe = new RegExp(`(?:${escapeRegExp(varName)}|\\$\\{${escapeRegExp(varName)}\\})`);
@@ -5370,7 +5382,7 @@ var init_heuristic = __esm({
5370
5382
  SEARCH_ACTION_PREFIXES = HEURISTIC_RULES.search_action_prefixes ?? [];
5371
5383
  SEARCH_ACTION_EXACT = HEURISTIC_RULES.search_action_exact ?? [];
5372
5384
  HOME_TARGET_RE = /(?:^|[\s="'($])(?:~|(?:\$home|\$\{home\}))(?:\/(?:\.\*|\*|\.)?)?(?=[\s"';}|&)]|$)/i;
5373
- HOME_ASSIGN_RE = /([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(~[A-Za-z0-9_./-]*|\$HOME[A-Za-z0-9_./-]*|\$\{HOME\}[A-Za-z0-9_./-]*|\/home\/[^\s;|&]+|\$\((?:[^()]|\([^()]*\))*\))(?=\s|;|$|\||&)/gi;
5385
+ HOME_ASSIGN_RE = /([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(~[A-Za-z0-9_./-]*|\$HOME[A-Za-z0-9_./-]*|\$\{HOME\}[A-Za-z0-9_./-]*|\/home\/[^\s;|&]+|\$\((?:[^()]|\([^()]*\))*\))(?=\s|;|$|\||&)/i;
5374
5386
  }
5375
5387
  });
5376
5388
 
@@ -1,25 +1,25 @@
1
1
  {
2
2
  "dangerous_commands": [
3
3
  {
4
- "pattern": "\\brm\\s+-[a-zA-Z]*r[a-zA-Z]*f[a-zA-Z]*\\s+/(?!\\w)",
4
+ "pattern": "\\brm[ \\t]+-[a-zA-Z]*r[a-zA-Z]*f[a-zA-Z]*(?:[ \\t]+(?:-[a-zA-Z]+|--[\\w=-]*))*[ \\t]+[\\\"']?/(?!\\w)",
5
5
  "risk_level": "critical",
6
6
  "risk_type": "script_execution",
7
7
  "reason": "检测到递归强制删除根目录 (rm -rf /)"
8
8
  },
9
9
  {
10
- "pattern": "\\brm\\s+-[a-zA-Z]*[rf][a-zA-Z]*\\s+/(?:etc|var(?!/tmp)|usr|boot|root|opt|srv|sys|proc|home(?:/|/[A-Za-z0-9_.-]+/?)?(?=[\\s;|&\\\"']|$))",
10
+ "pattern": "\\brm[ \\t]+-[a-zA-Z]*[rf][a-zA-Z]*(?:[ \\t]+(?:-[a-zA-Z]+|--[\\w=-]*))*[ \\t]+[\\\"']?/(?:etc|var(?!/tmp)|usr|boot|root|opt|srv|sys|proc|home(?:/|/[A-Za-z0-9_.-]+/?)?(?=[\\s;|&\\\"']|$))",
11
11
  "risk_level": "critical",
12
12
  "risk_type": "script_execution",
13
13
  "reason": "检测到删除关键系统目录"
14
14
  },
15
15
  {
16
- "pattern": "\\brm\\s+-[a-zA-Z]*r[a-zA-Z]*\\s+/(?!tmp|var/tmp|run|dev/shm|mnt|media)(?:/|\\b)",
16
+ "pattern": "\\brm[ \\t]+-[a-zA-Z]*r[a-zA-Z]*(?:[ \\t]+(?:-[a-zA-Z]+|--[\\w=-]*))*[ \\t]+[\\\"']?/(?!tmp|var/tmp|run|dev/shm|mnt|media)(?:/|\\b)",
17
17
  "risk_level": "medium",
18
18
  "risk_type": "script_execution",
19
19
  "reason": "检测到递归删除操作 (rm -r /...)"
20
20
  },
21
21
  {
22
- "pattern": "\\brm\\s+-[a-zA-Z]*f[a-zA-Z]*\\s+/(?!tmp/|var/tmp/|run/|dev/shm/)",
22
+ "pattern": "\\brm[ \\t]+-[a-zA-Z]*f[a-zA-Z]*(?:[ \\t]+(?:-[a-zA-Z]+|--[\\w=-]*))*[ \\t]+[\\\"']?/(?!tmp/|var/tmp/|run/|dev/shm/)",
23
23
  "risk_level": "medium",
24
24
  "risk_type": "script_execution",
25
25
  "reason": "检测到强制删除文件 (rm -f /...)"
@@ -228,7 +228,7 @@
228
228
  "\\bcurl\\b[^;&|\\n]*(?:-X\\s+POST\\s+|-d\\s+|-F\\s+|--data|--upload-file|--data-binary)[^;&|\\n]*(?:/etc/shadow|/etc/passwd|id_rsa|credentials|[^\\s]*\\.db|secret[^\\s]*)[^;&|\\n]*https?://(?!127\\.\\d+\\.\\d+\\.\\d+|0\\.0\\.0\\.0|localhost|\\[?::1\\]?)",
229
229
  "\\bcurl\\b[^;&|\\n]*(?:-X\\s+POST\\s+|-d\\s+|-F\\s+|--data|--upload-file|--data-binary)[^;&|\\n]*https?://(?!127\\.\\d+\\.\\d+\\.\\d+|0\\.0\\.0\\.0|localhost|\\[?::1\\]?)",
230
230
  "\\b(?:curl|wget)\\b[^;&|\\n]*\\s(?:-T|--upload-file)\\s+[^;&|\\n]*https?://(?!localhost|127\\.0\\.0\\.1)",
231
- "[^;&|\\n]*\\|\\s*nc\\s+[^;&|\\n]*\\d{1,5}\\b",
231
+ "\\|\\s*nc\\s+[^;&|\\n]*\\d{1,5}\\b",
232
232
  "\\b(?:scp|sftp|rsync)\\s+[^;&|\\n]*(?:/etc/shadow|/etc/passwd|id_rsa|id_dsa|credentials|\\S*secret\\S*|\\.ssh/)[^;&|\\n]*\\S+@\\S+:",
233
233
  "\\bpython3?\\s+-c\\s+['\"][^'\"]*(?:requests|urllib(?:\\.request)?|aiohttp)\\.(?:post|put|request|send)\\s*\\([^'\"]*['\"]https?://(?!127\\.\\d+\\.\\d+\\.\\d+|0\\.0\\.0\\.0|localhost|\\[?::1\\]?)"
234
234
  ],
@@ -13,7 +13,7 @@
13
13
  "network_egress": [
14
14
  "\\bcurl\\b[^;&|\\n]*(?:-X\\s+POST\\s+|-d\\s+|-F\\s+|--data|--upload-file|--data-binary|-T\\s+)[^;&|\\n]*https?://(?!127\\.\\d+\\.\\d+\\.\\d+|0\\.0\\.0\\.0|localhost|\\[?::1\\]?)",
15
15
  "\\b(?:curl|wget|ftp)\\b[^;&|\\n]*\\s(?:-T|--upload-file|--post-file|--post-data|put)\\b[^;&|\\n]*https?://(?!localhost|127\\.0\\.0\\.1)",
16
- "[^;&|\\n]*\\|\\s*(?:nc|netcat)\\s+\\S+\\s+\\d{1,5}\\b",
16
+ "\\|\\s*(?:nc|netcat)\\s+\\S+\\s+\\d{1,5}\\b",
17
17
  "(?:requests|urllib(?:\\.request)?|aiohttp|httpx)\\.(?:post|put|request|send)\\s*\\(",
18
18
  "\\b(?:scp|sftp|rsync)\\s+[^;&|\\n]*(?:/etc/shadow|/etc/passwd|id_rsa|id_dsa|credentials|\\S*secret\\S*|\\.ssh/)[^;&|\\n]*\\S+@\\S+:",
19
19
  "\\b(?:smtplib|ftplib|paramiko)\\s*\\."
package/dist/server.js CHANGED
@@ -5658,7 +5658,19 @@ function checkHomeDeletion(actionType, actionDetail) {
5658
5658
  }
5659
5659
  }
5660
5660
  const assigned = {};
5661
- for (const m of actionDetail.matchAll(HOME_ASSIGN_RE)) assigned[m[1]] = m[2];
5661
+ const anchored = new RegExp(HOME_ASSIGN_RE.source, "iy");
5662
+ const nameRe = /[A-Za-z_][A-Za-z0-9_]*/gi;
5663
+ let nm;
5664
+ while ((nm = nameRe.exec(actionDetail)) !== null) {
5665
+ anchored.lastIndex = nm.index;
5666
+ const m = anchored.exec(actionDetail);
5667
+ if (m) {
5668
+ assigned[m[1]] = m[2];
5669
+ nameRe.lastIndex = anchored.lastIndex;
5670
+ } else {
5671
+ nameRe.lastIndex = nm.index + nm[0].length;
5672
+ }
5673
+ }
5662
5674
  for (const [varName, path9] of Object.entries(assigned)) {
5663
5675
  if (!isHomeValue(path9)) continue;
5664
5676
  const varRefRe = new RegExp(`(?:${escapeRegExp(varName)}|\\$\\{${escapeRegExp(varName)}\\})`);
@@ -5925,7 +5937,7 @@ var init_heuristic = __esm({
5925
5937
  SEARCH_ACTION_PREFIXES = HEURISTIC_RULES.search_action_prefixes ?? [];
5926
5938
  SEARCH_ACTION_EXACT = HEURISTIC_RULES.search_action_exact ?? [];
5927
5939
  HOME_TARGET_RE = /(?:^|[\s="'($])(?:~|(?:\$home|\$\{home\}))(?:\/(?:\.\*|\*|\.)?)?(?=[\s"';}|&)]|$)/i;
5928
- HOME_ASSIGN_RE = /([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(~[A-Za-z0-9_./-]*|\$HOME[A-Za-z0-9_./-]*|\$\{HOME\}[A-Za-z0-9_./-]*|\/home\/[^\s;|&]+|\$\((?:[^()]|\([^()]*\))*\))(?=\s|;|$|\||&)/gi;
5940
+ HOME_ASSIGN_RE = /([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(~[A-Za-z0-9_./-]*|\$HOME[A-Za-z0-9_./-]*|\$\{HOME\}[A-Za-z0-9_./-]*|\/home\/[^\s;|&]+|\$\((?:[^()]|\([^()]*\))*\))(?=\s|;|$|\||&)/i;
5929
5941
  }
5930
5942
  });
5931
5943
 
@@ -11458,8 +11470,10 @@ function cleanupChainArchives(dir, maxBytes = envInt3("AGENT_MOSS_CHAIN_ARCHIVE_
11458
11470
  }
11459
11471
  }, 0);
11460
11472
  if (maxBytes > 0) {
11461
- for (const p of files) {
11473
+ for (let i = 0; i < files.length; i++) {
11462
11474
  if (total <= maxBytes) break;
11475
+ if (i === files.length - 1) break;
11476
+ const p = files[i];
11463
11477
  try {
11464
11478
  const size = fs5.statSync(p).size;
11465
11479
  fs5.unlinkSync(p);
@@ -14242,8 +14256,10 @@ async function analyzeRoute(c) {
14242
14256
  }
14243
14257
  const slowMs = Number(process.env.AGENT_MOSS_SLOW_REQUEST_MS || "1000");
14244
14258
  if (Number.isFinite(slowMs) && slowMs > 0 && durationMs >= slowMs) {
14259
+ const recvAt = c.get?.("recvAt");
14260
+ const queueMs = typeof recvAt === "number" ? Math.max(0, t0 - recvAt) : 0;
14245
14261
  console.warn(
14246
- `[AgentMoss] \u6162\u8BF7\u6C42: ${Math.round(durationMs)}ms\uFF08\u9608\u503C ${slowMs}ms\uFF09 action_type=${req.a_next.action_type} agent=${agentId || "-"} decision=${result.decision}`
14262
+ `[AgentMoss] \u6162\u8BF7\u6C42: \u603B ${Math.round(durationMs)}ms = \u6392\u961F ${Math.round(queueMs)}ms + \u8BA1\u7B97 ${Math.round(durationMs - queueMs)}ms\uFF08\u9608\u503C ${slowMs}ms\uFF09 action_type=${req.a_next.action_type} agent=${agentId || "-"} decision=${result.decision}`
14247
14263
  );
14248
14264
  }
14249
14265
  try {
@@ -15104,6 +15120,10 @@ function createApp() {
15104
15120
  enforceRulesContract();
15105
15121
  hardenExistingPaths();
15106
15122
  const app = new Hono2();
15123
+ app.use("*", async (c, next) => {
15124
+ c.set("recvAt", performance.now());
15125
+ await next();
15126
+ });
15107
15127
  app.use("*", cors({
15108
15128
  origin: (o) => allowedOrigins().includes(o) ? o : null,
15109
15129
  allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kenhkl/agent-moss",
3
- "version": "0.17.38",
3
+ "version": "0.17.40",
4
4
  "description": "Multi-layer security analysis engine for AI agents (TypeScript)",
5
5
  "type": "module",
6
6
  "main": "dist/server.js",