@yottameta/yotta-guardian 0.1.5 → 0.1.6

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/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # 更新日志
2
2
 
3
+ ## v0.1.6 (2026-09-25)
4
+
5
+ 安全修复:复合命令拆分,堵住分隔符后的危险段绕过。
6
+
7
+ - 背景:v0.1.5 只解决了包装命令(`sh -c` / `sudo` / `env` …)绕过,整条命令仍只按第一个词做 argv 分析——`echo ok && rm -rf /`、`ls; rm -rf /`、`true || rm -rf /` 里的危险段藏在分隔符之后,整条命令被判放行(本轮复现)。
8
+ - 修复:新增受控复合命令拆分(`;` `&&` `||` `|` `&` 换行,引号 / 转义内不拆)+ 命令替换展开(`$(...)` / 反引号),逐段跑同一套文本 / argv 规则;放行规则只对单一简单命令生效;引号未闭合时出 `CMD-UNPARSED`(medium,fail-closed)。
9
+ - 安装器加固:拒绝对符号链接目标写入、目标不是目录时拒绝、不做整目录删除。
10
+ - 回归:新增 11 项复合命令用例,测试 83/83 通过。
11
+
3
12
  ## v0.1.5 (2026-09-19)
4
13
 
5
14
  安全修复:包装命令解包,堵住 argv 级分析的绕过路径。
package/SKILL.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: yotta-guardian
3
- version: 0.1.5
3
+ version: 0.1.6
4
4
  description: 元盾 —— 跨智能体的危险调用拦截护栏:确定性规则引擎 + 可插拔意图验证(不绑模型),拦截危险 exec / write / edit / read / run / shell 工具调用,提供审计日志。触发:代理要执行高风险命令(递归删除、磁盘格式化、提权、防火墙改动、反向 shell、下载即执行等)、要写入系统敏感路径或修改系统配置、要在执行危险操作前做安全检查、或用户说 护栏/拦截/危险操作/安全检查 等。边界:默认只读评估,不自动执行也不放行危险操作;不替代用户决策;不隐藏审计记录;规则可配置。
5
5
  license: MIT
6
6
  ---
package/install.sh CHANGED
@@ -61,10 +61,29 @@ resolve_user() {
61
61
  }
62
62
 
63
63
  install_to() {
64
- mkdir -p "$1/$SKILL_NAME"
65
- cp -r "$SOURCE_DIR/." "$1/$SKILL_NAME/"
66
- rm -rf "$1/$SKILL_NAME/.git"
67
- echo "installed -> $1/$SKILL_NAME"
64
+ local base="$1"
65
+ local dest
66
+ case "$base" in
67
+ ""|"/") echo "安装失败:拒绝不安全的目标目录:'$base'" >&2; return 1 ;;
68
+ esac
69
+ if [ -L "$base" ]; then
70
+ echo "安装失败:目标目录是符号链接,拒绝跟随:$base" >&2; return 1
71
+ fi
72
+ mkdir -p "$base"
73
+ dest="$base/$SKILL_NAME"
74
+ if [ -L "$dest" ]; then
75
+ echo "安装失败:技能目录是符号链接,拒绝跟随:$dest" >&2; return 1
76
+ fi
77
+ if [ -e "$dest" ] && [ ! -d "$dest" ]; then
78
+ echo "安装失败:技能路径已存在且不是目录:$dest" >&2; return 1
79
+ fi
80
+ mkdir -p "$dest"
81
+ cp -RP "$SOURCE_DIR/." "$dest/"
82
+ # 只清理副本内部的开发残留(固定子路径),不做整目录删除、不跟随符号链接
83
+ if [ -d "$dest/.git" ] && [ ! -L "$dest/.git" ]; then rm -rf "$dest/.git"; fi
84
+ find "$dest" -type d -name '__pycache__' -prune -exec rm -rf {} + 2>/dev/null || true
85
+ find "$dest" -type f -name '*.pyc' -delete 2>/dev/null || true
86
+ echo "installed -> $dest"
68
87
  }
69
88
 
70
89
  list() {
@@ -129,4 +148,4 @@ main() {
129
148
  fi
130
149
  }
131
150
 
132
- main "$@"
151
+ main "$@"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yottameta/yotta-guardian",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Yuandun (元盾) — cross-agent dangerous tool-call guardrail: a deterministic rule engine + pluggable intent verifier (model-agnostic) that evaluates exec/write/edit/read/run/shell calls and provides audit logs. Triggers when an agent is about to run a high-risk command, write sensitive paths, or change system config. Boundaries: read-only evaluation by default, configurable allowances.",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -51,7 +51,7 @@ _HERE = Path(__file__).resolve().parent
51
51
  sys.path.insert(0, str(_HERE))
52
52
  import guardian_rules as GR # noqa: E402
53
53
 
54
- VERSION = "0.1.5"
54
+ VERSION = "0.1.6"
55
55
  TOOL_NAME = "yotta-guardian"
56
56
  TOOL_CN = "元盾"
57
57
 
@@ -279,14 +279,15 @@ class RuleEngine:
279
279
  def _argv_findings(self, call):
280
280
  if call.tool not in ("exec", "run", "shell"):
281
281
  return []
282
- argv = _tokenize(call.cmd)
283
- if not argv:
284
- return []
285
282
  findings = []
286
- # v0.1.5:先做包装解包(sh -c / cmd /c / powershell -Command / sudo / env …),
287
- # 否则只看 argv[0] 会漏掉内层真正的危险动词。
288
- for sub in _unwrap_argv(argv):
289
- findings += self._argv_findings_one(sub)
283
+ # v0.1.6:先拆复合命令(; && || | & 换行)与命令替换,再逐段解包 +
284
+ # 跑同一套 argv 规则;v0.1.5 的包装解包对每段继续生效。
285
+ for seg in _command_segments(call.cmd):
286
+ argv = _tokenize(seg)
287
+ if not argv:
288
+ continue
289
+ for sub in _unwrap_argv(argv):
290
+ findings += self._argv_findings_one(sub)
290
291
  return findings
291
292
 
292
293
  def _argv_findings_one(self, argv):
@@ -326,6 +327,12 @@ class RuleEngine:
326
327
 
327
328
  def _allow_hit(self, call):
328
329
  if call.tool in ("exec", "run", "shell"):
330
+ # v0.1.6:放行规则只覆盖「单一简单命令」。复合命令或命令替换不允许被
331
+ # 整条放行——否则 `echo ok && <危险命令>` 会借放行规则绕过风险判定。
332
+ if (len(_split_compound(call.cmd)) > 1
333
+ or _extract_substitutions(call.cmd)
334
+ or _scan_quotes(call.cmd)):
335
+ return False
329
336
  for rx in self.allow_text + self.allow_patterns:
330
337
  if rx.search(call.cmd):
331
338
  return True
@@ -343,6 +350,14 @@ class RuleEngine:
343
350
  findings = []
344
351
  if call.tool in ("exec", "run", "shell"):
345
352
  findings += self._text_findings(call.cmd, "command")
353
+ if _scan_quotes(call.cmd):
354
+ findings.append(Finding("CMD-UNPARSED", "medium", "command",
355
+ "命令引号未闭合,无法可靠拆分,按未知风险处理",
356
+ 80))
357
+ segments = _command_segments(call.cmd)
358
+ if len(segments) > 1:
359
+ for seg in segments:
360
+ findings += self._text_findings(seg, "command")
346
361
  findings += self._argv_findings(call)
347
362
  elif call.tool in ("write", "edit"):
348
363
  findings += self._path_findings(call.path)
@@ -546,6 +561,144 @@ def _unwrap_argv(argv, depth=0):
546
561
  return [argv]
547
562
 
548
563
 
564
+ # ── 复合命令拆分(v0.1.6)─────────────────────────────────────────────────
565
+ # v0.1.5 解决了包装命令(sh -c / sudo / env …)绕过,但整条命令仍然只按第一个词
566
+ # 做 argv 分析:`echo ok && rm -rf /`、`ls; rm -rf /`、`true || rm -rf /` 里的
567
+ # 危险段藏在分隔符之后,整条命令因此被判放行。现在先按顶层分隔符拆分、再逐段跑
568
+ # 同一套文本 / argv 规则;命令替换($(...) / `...`)同样展开;拆分不安全时 fail-closed。
569
+ _COMPOUND_SEPARATORS = (";", "|", "&", "\n", "\r")
570
+ _SEGMENT_MAX = 32
571
+ _SEGMENT_DEPTH = 3
572
+ _SUBST_MAX = 16
573
+ _QUOTES = ("'", '"', "`")
574
+
575
+
576
+ def _scan_quotes(cmd):
577
+ """扫描引号闭合情况,返回未闭合的引号字符(全部闭合返回 None)。"""
578
+ quote = None
579
+ i = 0
580
+ n = len(cmd)
581
+ while i < n:
582
+ ch = cmd[i]
583
+ if quote:
584
+ if quote != "'" and ch == "\\" and i + 1 < n:
585
+ i += 2
586
+ continue
587
+ if ch == quote:
588
+ quote = None
589
+ i += 1
590
+ continue
591
+ if ch in _QUOTES:
592
+ quote = ch
593
+ elif ch == "\\" and i + 1 < n:
594
+ i += 2
595
+ continue
596
+ i += 1
597
+ return quote
598
+
599
+
600
+ def _split_compound(cmd):
601
+ """按顶层分隔符(; && || | & 换行)拆分命令,引号 / 转义内不拆。"""
602
+ segs = []
603
+ cur = []
604
+ quote = None
605
+ i = 0
606
+ n = len(cmd)
607
+ while i < n:
608
+ ch = cmd[i]
609
+ if quote:
610
+ cur.append(ch)
611
+ if quote != "'" and ch == "\\" and i + 1 < n:
612
+ cur.append(cmd[i + 1])
613
+ i += 2
614
+ continue
615
+ if ch == quote:
616
+ quote = None
617
+ i += 1
618
+ continue
619
+ if ch in _QUOTES:
620
+ quote = ch
621
+ cur.append(ch)
622
+ i += 1
623
+ continue
624
+ if ch == "\\" and i + 1 < n:
625
+ cur.append(ch)
626
+ cur.append(cmd[i + 1])
627
+ i += 2
628
+ continue
629
+ if ch in _COMPOUND_SEPARATORS:
630
+ seg = "".join(cur).strip()
631
+ if seg:
632
+ segs.append(seg)
633
+ cur = []
634
+ i += 1
635
+ continue
636
+ cur.append(ch)
637
+ i += 1
638
+ seg = "".join(cur).strip()
639
+ if seg:
640
+ segs.append(seg)
641
+ return segs
642
+
643
+
644
+ def _extract_substitutions(text):
645
+ """取出 $(...) 与 `...` 里的内层命令文本(数量上限,防构造爆炸)。"""
646
+ out = []
647
+ i = 0
648
+ n = len(text)
649
+ while i < n and len(out) < _SUBST_MAX:
650
+ ch = text[i]
651
+ if ch == "\\" and i + 1 < n:
652
+ i += 2
653
+ continue
654
+ if ch == "$" and i + 1 < n and text[i + 1] == "(":
655
+ depth = 0
656
+ j = i + 1
657
+ while j < n:
658
+ if text[j] == "(":
659
+ depth += 1
660
+ elif text[j] == ")":
661
+ depth -= 1
662
+ if depth == 0:
663
+ break
664
+ j += 1
665
+ if j < n:
666
+ out.append(text[i + 2:j])
667
+ i = j + 1
668
+ continue
669
+ break
670
+ if ch == "`":
671
+ j = i + 1
672
+ while j < n and text[j] != "`":
673
+ if text[j] == "\\":
674
+ j += 1
675
+ j += 1
676
+ if j < n:
677
+ out.append(text[i + 1:j])
678
+ i = j + 1
679
+ continue
680
+ break
681
+ i += 1
682
+ return out
683
+
684
+
685
+ def _command_segments(cmd, depth=0):
686
+ """展开复合命令与命令替换,返回需要逐段分析的命令文本(去重、保序)。"""
687
+ segs = _split_compound(cmd)
688
+ expanded = list(segs)
689
+ if depth < _SEGMENT_DEPTH:
690
+ for seg in segs:
691
+ for sub in _extract_substitutions(seg):
692
+ expanded += _command_segments(sub, depth + 1)
693
+ out = []
694
+ for seg in expanded:
695
+ if seg and seg not in out:
696
+ out.append(seg)
697
+ if len(out) >= _SEGMENT_MAX:
698
+ break
699
+ return out
700
+
701
+
549
702
  # ── argv 级规则(按动词分组)───────────────────────────────────────────────
550
703
 
551
704
  _RM_VERBS = {"rm", "rmdir", "del", "erase", "unlink", "shred", "remove-item"}
@@ -3,7 +3,7 @@
3
3
  "slug": "yotta-guardian",
4
4
  "name": "元盾",
5
5
  "package": "@yottameta/yotta-guardian",
6
- "version": "0.1.5",
6
+ "version": "0.1.6",
7
7
  "trust": "yottameta",
8
8
  "install": {
9
9
  "idempotent": true