delete-guard-agent 0.0.0-stage → 0.5.0

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.
@@ -0,0 +1,88 @@
1
+ """命令行入口:command / path 两种模式,--json 机器可读输出。
2
+
3
+ 退出码:0 = allow,1 = warn,2 = block。与 0.1.0 的 guard.py 保持一致。
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ import argparse
9
+ import json
10
+ import sys
11
+ from collections.abc import Iterable
12
+
13
+ from .core import judge_command, judge_path
14
+
15
+
16
+ def render_text(result: dict) -> str:
17
+ icon = {"allow": "ALLOW", "warn": "WARN ", "block": "BLOCK"}
18
+ lines = [f"[{icon[result['verdict']]}]"]
19
+
20
+ if result["hazards"]:
21
+ lines.append("")
22
+ for h in result["hazards"]:
23
+ lines.append(f" {h['severity'].upper():5} {h['code']}")
24
+ lines.append(f" {h['why']}")
25
+ if h["detail"]:
26
+ lines.append(f" 实测:{h['detail']}")
27
+ lines.append(f" 依据:{h['evidence']}")
28
+
29
+ if result["blast_radius"]:
30
+ lines.append("")
31
+ lines.append(" 爆炸半径:")
32
+ for r in result["blast_radius"]:
33
+ lines.append(
34
+ f" {r['target']} -> {r['files']} 文件 / {r['dirs']} 目录 / "
35
+ f"{r['bytes_human']},链接 {r['reparse_points']} 个"
36
+ )
37
+ if r["truncated"]:
38
+ lines.append(" (统计已截断,实际更大)")
39
+ if r["note"]:
40
+ lines.append(f" 注:{r['note']}")
41
+
42
+ if result["unknowns"]:
43
+ lines.append("")
44
+ lines.append(" 未能验证(不构成安全保证):")
45
+ for u in result["unknowns"]:
46
+ lines.append(f" - {u}")
47
+
48
+ if result["verdict"] == "block":
49
+ lines.append("")
50
+ lines.append(" 拒绝执行。先改写成单层、显式、绝对路径的命令,或改用隔离区 rename。")
51
+
52
+ return "\n".join(lines)
53
+
54
+
55
+ def main(argv: Iterable[str] | None = None) -> int:
56
+ ap = argparse.ArgumentParser(
57
+ prog="guard.py",
58
+ description="判定破坏性命令/路径在当前平台上的真实爆炸半径",
59
+ )
60
+ sub = ap.add_subparsers(dest="mode", required=True)
61
+
62
+ c = sub.add_parser("command", help="分析一条 shell 命令")
63
+ c.add_argument("text", help="命令原文")
64
+ c.add_argument("--cwd", default=None)
65
+ c.add_argument("--json", action="store_true")
66
+
67
+ p = sub.add_parser("path", help="分析一个目标路径")
68
+ p.add_argument("path")
69
+ p.add_argument("--cwd", default=None)
70
+ p.add_argument("--json", action="store_true")
71
+
72
+ args = ap.parse_args(list(argv) if argv is not None else None)
73
+
74
+ if args.mode == "command":
75
+ result = judge_command(args.text, args.cwd)
76
+ else:
77
+ result = judge_path(args.path, args.cwd)
78
+
79
+ if args.json:
80
+ print(json.dumps(result, ensure_ascii=False, indent=2))
81
+ else:
82
+ print(render_text(result))
83
+
84
+ return 0 if result["verdict"] == "allow" else (1 if result["verdict"] == "warn" else 2)
85
+
86
+
87
+ if __name__ == "__main__":
88
+ sys.exit(main())