work-agent 0.1.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.
- package/README.md +234 -0
- package/app/(admin)/approvals/page.tsx +16 -0
- package/app/(admin)/audit/page.tsx +18 -0
- package/app/(admin)/layout.tsx +47 -0
- package/app/(admin)/scheduled-tasks/page.tsx +17 -0
- package/app/(admin)/settings/page.tsx +46 -0
- package/app/(admin)/skills/[name]/page.tsx +378 -0
- package/app/(admin)/skills/page.tsx +406 -0
- package/app/(admin)/statistics/page.tsx +416 -0
- package/app/(admin)/tickets/[id]/page.tsx +348 -0
- package/app/(admin)/tickets/new/page.tsx +309 -0
- package/app/(admin)/tickets/page.tsx +27 -0
- package/app/api/audit/route.ts +30 -0
- package/app/api/auth/feishu/callback/route.ts +72 -0
- package/app/api/auth/feishu/login/route.ts +17 -0
- package/app/api/auth/feishu/sso/route.ts +78 -0
- package/app/api/auth/login/route.ts +85 -0
- package/app/api/auth/oauth/route.ts +168 -0
- package/app/api/config/providers/route.ts +105 -0
- package/app/api/config/route.ts +115 -0
- package/app/api/config/status/route.ts +56 -0
- package/app/api/config/test/route.ts +212 -0
- package/app/api/documents/[id]/route.ts +88 -0
- package/app/api/documents/route.ts +53 -0
- package/app/api/health/route.ts +32 -0
- package/app/api/knowledge/[id]/route.ts +152 -0
- package/app/api/knowledge/from-session/route.ts +27 -0
- package/app/api/knowledge/route.ts +100 -0
- package/app/api/market/knowledge/[id]/route.ts +92 -0
- package/app/api/market/knowledge/route.ts +130 -0
- package/app/api/marketplace/skills/[id]/approve/route.ts +68 -0
- package/app/api/marketplace/skills/[id]/certify/route.ts +54 -0
- package/app/api/marketplace/skills/[id]/install/route.ts +180 -0
- package/app/api/marketplace/skills/[id]/promote-to-system/route.ts +219 -0
- package/app/api/marketplace/skills/[id]/rate/route.ts +90 -0
- package/app/api/marketplace/skills/[id]/ratings/route.ts +55 -0
- package/app/api/marketplace/skills/[id]/reject/route.ts +68 -0
- package/app/api/marketplace/skills/[id]/route.ts +177 -0
- package/app/api/marketplace/skills/route.ts +235 -0
- package/app/api/memory/route.ts +40 -0
- package/app/api/my/files/[id]/route.ts +52 -0
- package/app/api/my/files/route.ts +230 -0
- package/app/api/my/knowledge/route.ts +36 -0
- package/app/api/pi-chat/route.ts +443 -0
- package/app/api/recommend/route.ts +38 -0
- package/app/api/scheduled-tasks/[id]/execute/route.ts +132 -0
- package/app/api/scheduled-tasks/[id]/route.ts +165 -0
- package/app/api/scheduled-tasks/[id]/toggle/route.ts +53 -0
- package/app/api/scheduled-tasks/route.ts +101 -0
- package/app/api/sessions/[id]/messages/route.ts +212 -0
- package/app/api/sessions/route.ts +101 -0
- package/app/api/share/file/[id]/route.ts +37 -0
- package/app/api/skills/[name]/execute/route.ts +121 -0
- package/app/api/skills/[name]/route.ts +167 -0
- package/app/api/skills/create/route.ts +65 -0
- package/app/api/skills/generate/route.ts +405 -0
- package/app/api/skills/installed/route.ts +151 -0
- package/app/api/skills/route.ts +174 -0
- package/app/api/skills/translate/route.ts +40 -0
- package/app/api/skills/user/[name]/route.ts +159 -0
- package/app/api/skills/user/route.ts +90 -0
- package/app/api/statistics/route.ts +94 -0
- package/app/api/task-executions/[id]/route.ts +34 -0
- package/app/api/task-executions/route.ts +29 -0
- package/app/api/tickets/[id]/approve/route.ts +129 -0
- package/app/api/tickets/[id]/execute/route.ts +201 -0
- package/app/api/tickets/[id]/route.ts +127 -0
- package/app/api/tickets/route.ts +103 -0
- package/app/api/user/skills/route.ts +175 -0
- package/app/api/users/route.ts +80 -0
- package/app/chat/page.tsx +5 -0
- package/app/globals.css +84 -0
- package/app/h5/layout.tsx +5 -0
- package/app/h5/mobile-approvals-page.tsx +167 -0
- package/app/h5/mobile-chat-page.tsx +951 -0
- package/app/h5/mobile-profile-page.tsx +147 -0
- package/app/h5/mobile-tickets-page.tsx +121 -0
- package/app/h5/page.tsx +23 -0
- package/app/h5/ticket-action-buttons.tsx +80 -0
- package/app/layout.tsx +26 -0
- package/app/login/page.tsx +318 -0
- package/app/market/knowledge/[id]/page.tsx +77 -0
- package/app/market/knowledge/page.tsx +358 -0
- package/app/market/layout.tsx +29 -0
- package/app/market/page.tsx +18 -0
- package/app/market/skills/page.tsx +397 -0
- package/app/my/files/page.tsx +511 -0
- package/app/my/knowledge/[id]/page.tsx +271 -0
- package/app/my/knowledge/new/page.tsx +234 -0
- package/app/my/knowledge/page.tsx +248 -0
- package/app/my/layout.tsx +32 -0
- package/app/my/memory/page.tsx +164 -0
- package/app/my/page.tsx +18 -0
- package/app/my/scheduled-tasks/[id]/edit/page.tsx +290 -0
- package/app/my/scheduled-tasks/[id]/executions/page.tsx +275 -0
- package/app/my/scheduled-tasks/[id]/page.tsx +284 -0
- package/app/my/scheduled-tasks/new/page.tsx +230 -0
- package/app/my/scheduled-tasks/page.tsx +27 -0
- package/app/my/skills/[name]/page.tsx +320 -0
- package/app/my/skills/new/page.tsx +394 -0
- package/app/my/skills/page.tsx +303 -0
- package/app/page.tsx +2288 -0
- package/app/share/[sessionId]/page.tsx +226 -0
- package/app/share/file/[id]/page.tsx +140 -0
- package/bin/README.md +63 -0
- package/bin/generate-api-system +300 -0
- package/bin/postinstall.js +95 -0
- package/bin/work-agent.js +173 -0
- package/components/ai-elements/agent.tsx +142 -0
- package/components/ai-elements/artifact.tsx +149 -0
- package/components/ai-elements/attachments.tsx +427 -0
- package/components/ai-elements/audio-player.tsx +232 -0
- package/components/ai-elements/canvas.tsx +26 -0
- package/components/ai-elements/chain-of-thought.tsx +223 -0
- package/components/ai-elements/checkpoint.tsx +72 -0
- package/components/ai-elements/code-block.tsx +555 -0
- package/components/ai-elements/commit.tsx +449 -0
- package/components/ai-elements/confirmation.tsx +173 -0
- package/components/ai-elements/connection.tsx +28 -0
- package/components/ai-elements/context.tsx +410 -0
- package/components/ai-elements/controls.tsx +19 -0
- package/components/ai-elements/conversation.tsx +167 -0
- package/components/ai-elements/edge.tsx +144 -0
- package/components/ai-elements/environment-variables.tsx +325 -0
- package/components/ai-elements/file-tree.tsx +298 -0
- package/components/ai-elements/image.tsx +25 -0
- package/components/ai-elements/inline-citation.tsx +294 -0
- package/components/ai-elements/jsx-preview.tsx +250 -0
- package/components/ai-elements/message.tsx +367 -0
- package/components/ai-elements/mic-selector.tsx +372 -0
- package/components/ai-elements/model-selector.tsx +214 -0
- package/components/ai-elements/node.tsx +72 -0
- package/components/ai-elements/open-in-chat.tsx +367 -0
- package/components/ai-elements/package-info.tsx +235 -0
- package/components/ai-elements/panel.tsx +16 -0
- package/components/ai-elements/persona.tsx +280 -0
- package/components/ai-elements/plan.tsx +144 -0
- package/components/ai-elements/prompt-input.tsx +1341 -0
- package/components/ai-elements/queue.tsx +275 -0
- package/components/ai-elements/reasoning.tsx +355 -0
- package/components/ai-elements/sandbox.tsx +133 -0
- package/components/ai-elements/schema-display.tsx +473 -0
- package/components/ai-elements/shimmer.tsx +78 -0
- package/components/ai-elements/snippet.tsx +141 -0
- package/components/ai-elements/sources.tsx +78 -0
- package/components/ai-elements/speech-input.tsx +324 -0
- package/components/ai-elements/stack-trace.tsx +531 -0
- package/components/ai-elements/suggestion.tsx +58 -0
- package/components/ai-elements/task.tsx +88 -0
- package/components/ai-elements/terminal.tsx +277 -0
- package/components/ai-elements/test-results.tsx +497 -0
- package/components/ai-elements/tool.tsx +174 -0
- package/components/ai-elements/toolbar.tsx +17 -0
- package/components/ai-elements/transcription.tsx +126 -0
- package/components/ai-elements/voice-selector.tsx +525 -0
- package/components/ai-elements/web-preview.tsx +282 -0
- package/components/audit-log-list.tsx +114 -0
- package/components/chat/EmptyPreviewState.tsx +12 -0
- package/components/chat/KnowledgePickerDialog.tsx +464 -0
- package/components/chat/KnowledgePreview.tsx +70 -0
- package/components/chat/KnowledgePreviewPanel.tsx +86 -0
- package/components/chat/MentionInput.tsx +309 -0
- package/components/chat/OrganizeDialog.tsx +258 -0
- package/components/chat/RecommendationBanner.tsx +94 -0
- package/components/chat/SaveToKnowledgeDialog.tsx +193 -0
- package/components/chat/SkillSelector.tsx +305 -0
- package/components/chat/SkillSwitcher.tsx +163 -0
- package/components/client-layout.tsx +15 -0
- package/components/knowledge/KnowledgeMetadataPanel.tsx +293 -0
- package/components/layout-wrapper.tsx +18 -0
- package/components/mobile-layout.tsx +62 -0
- package/components/scheduled-task-list.tsx +356 -0
- package/components/setup-guide.tsx +484 -0
- package/components/sub-nav.tsx +54 -0
- package/components/ticket-detail-content.tsx +383 -0
- package/components/ticket-list.tsx +366 -0
- package/components/top-nav.tsx +132 -0
- package/components/ui/accordion.tsx +58 -0
- package/components/ui/alert.tsx +59 -0
- package/components/ui/avatar.tsx +50 -0
- package/components/ui/badge.tsx +36 -0
- package/components/ui/button-group.tsx +83 -0
- package/components/ui/button.tsx +57 -0
- package/components/ui/card.tsx +91 -0
- package/components/ui/carousel.tsx +262 -0
- package/components/ui/collapsible.tsx +11 -0
- package/components/ui/command.tsx +153 -0
- package/components/ui/dialog.tsx +122 -0
- package/components/ui/dropdown-menu.tsx +200 -0
- package/components/ui/hover-card.tsx +29 -0
- package/components/ui/input-group.tsx +170 -0
- package/components/ui/input.tsx +22 -0
- package/components/ui/label.tsx +26 -0
- package/components/ui/popover.tsx +31 -0
- package/components/ui/progress.tsx +28 -0
- package/components/ui/scroll-area.tsx +48 -0
- package/components/ui/select.tsx +174 -0
- package/components/ui/separator.tsx +31 -0
- package/components/ui/spinner.tsx +16 -0
- package/components/ui/switch.tsx +29 -0
- package/components/ui/table.tsx +120 -0
- package/components/ui/tabs.tsx +55 -0
- package/components/ui/textarea.tsx +22 -0
- package/components/ui/tooltip.tsx +30 -0
- package/components/welcome-guide.tsx +182 -0
- package/components.json +24 -0
- package/lib/command-parser.ts +331 -0
- package/lib/dangerous-commands.ts +672 -0
- package/lib/db.ts +2250 -0
- package/lib/feishu-auth.ts +135 -0
- package/lib/file-storage.ts +306 -0
- package/lib/file-tool.ts +583 -0
- package/lib/knowledge-tool.ts +152 -0
- package/lib/knowledge-types.ts +66 -0
- package/lib/market-client.ts +313 -0
- package/lib/market-db.ts +736 -0
- package/lib/market-types.ts +51 -0
- package/lib/memory-tool.ts +211 -0
- package/lib/memory.ts +197 -0
- package/lib/pi-config.ts +436 -0
- package/lib/pi-session.ts +799 -0
- package/lib/pinyin.ts +13 -0
- package/lib/recommendation.ts +227 -0
- package/lib/risk-estimator.ts +350 -0
- package/lib/scheduled-task-tool.ts +184 -0
- package/lib/scheduler-init.ts +43 -0
- package/lib/scheduler.ts +416 -0
- package/lib/secure-bash-tool.ts +413 -0
- package/lib/skill-engine.ts +396 -0
- package/lib/skill-generator.ts +269 -0
- package/lib/skill-loader.ts +234 -0
- package/lib/skill-tool.ts +188 -0
- package/lib/skill-types.ts +82 -0
- package/lib/skills-init.ts +58 -0
- package/lib/ticket-tool.ts +246 -0
- package/lib/user-skill-types.ts +30 -0
- package/lib/user-skills.ts +362 -0
- package/lib/utils.ts +6 -0
- package/lib/workflow.ts +154 -0
- package/lib/zip-tool.ts +191 -0
- package/next.config.js +8 -0
- package/package.json +106 -0
- package/public/.gitkeep +1 -0
- package/public/icon.svg +1 -0
- package/tsconfig.json +42 -0
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 危险命令检测模块
|
|
3
|
+
*
|
|
4
|
+
* 提供可配置的危险命令检测功能,用于识别需要审批的命令。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { readFileSync, existsSync } from 'fs';
|
|
8
|
+
import { join } from 'path';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 危险级别
|
|
12
|
+
*/
|
|
13
|
+
export type DangerLevel = 'safe' | 'low' | 'medium' | 'high' | 'critical';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 危险命令规则
|
|
17
|
+
*/
|
|
18
|
+
export interface DangerousRule {
|
|
19
|
+
pattern: RegExp;
|
|
20
|
+
description: string;
|
|
21
|
+
riskLevel: 'medium' | 'high' | 'critical';
|
|
22
|
+
category?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 安全命令规则(不需要审批)
|
|
27
|
+
*/
|
|
28
|
+
export interface SafeRule {
|
|
29
|
+
pattern: RegExp;
|
|
30
|
+
description: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 危险命令配置
|
|
35
|
+
*/
|
|
36
|
+
export interface DangerousCommandsConfig {
|
|
37
|
+
rules: DangerousRule[];
|
|
38
|
+
safeRules: SafeRule[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* CRITICAL 级别危险规则 - 系统状态修改/资源删除
|
|
43
|
+
*/
|
|
44
|
+
const CRITICAL_RULES: DangerousRule[] = [
|
|
45
|
+
{
|
|
46
|
+
pattern: /shutdown\b/i,
|
|
47
|
+
description: '系统关机',
|
|
48
|
+
riskLevel: 'critical',
|
|
49
|
+
category: 'system-state',
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
pattern: /reboot\b/i,
|
|
53
|
+
description: '系统重启',
|
|
54
|
+
riskLevel: 'critical',
|
|
55
|
+
category: 'system-state',
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
pattern: /halt\b/i,
|
|
59
|
+
description: '系统停止',
|
|
60
|
+
riskLevel: 'critical',
|
|
61
|
+
category: 'system-state',
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
pattern: /init\s+[06]/,
|
|
65
|
+
description: '系统关机/重启',
|
|
66
|
+
riskLevel: 'critical',
|
|
67
|
+
category: 'system-state',
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
pattern: /systemctl\s+(start|stop|restart|reload|enable|disable|mask|unmask)\b/i,
|
|
71
|
+
description: '系统服务管理',
|
|
72
|
+
riskLevel: 'critical',
|
|
73
|
+
category: 'service-management',
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
pattern: /service\s+\S+\s+(start|stop|restart|reload)\b/i,
|
|
77
|
+
description: '服务启停',
|
|
78
|
+
riskLevel: 'critical',
|
|
79
|
+
category: 'service-management',
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
pattern: /\bkill\s+(-9\s+|-KILL\s+)/,
|
|
83
|
+
description: '强制终止进程',
|
|
84
|
+
riskLevel: 'critical',
|
|
85
|
+
category: 'process-management',
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
pattern: /\bpkill\s+(-9\s+|-KILL\s+)/,
|
|
89
|
+
description: '强制终止进程组',
|
|
90
|
+
riskLevel: 'critical',
|
|
91
|
+
category: 'process-management',
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
pattern: /\bkillall\s+(-9\s+|-KILL\s+)/,
|
|
95
|
+
description: '强制终止所有同名进程',
|
|
96
|
+
riskLevel: 'critical',
|
|
97
|
+
category: 'process-management',
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
pattern: /rm\s+(-[rf]+\s+|.*\s+-[rf]+)/,
|
|
101
|
+
description: '递归/强制删除',
|
|
102
|
+
riskLevel: 'critical',
|
|
103
|
+
category: 'file-deletion',
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
pattern: /rm\s+.*\s\*/,
|
|
107
|
+
description: '通配符删除',
|
|
108
|
+
riskLevel: 'critical',
|
|
109
|
+
category: 'file-deletion',
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
pattern: /\bmv\s+.*\s+\/dev\/null/i,
|
|
113
|
+
description: '清空文件',
|
|
114
|
+
riskLevel: 'critical',
|
|
115
|
+
category: 'file-deletion',
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
pattern: />\s*\/dev\/sd[a-z]/i,
|
|
119
|
+
description: '写入磁盘设备',
|
|
120
|
+
riskLevel: 'critical',
|
|
121
|
+
category: 'disk-operations',
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
pattern: /mkfs\./i,
|
|
125
|
+
description: '格式化文件系统',
|
|
126
|
+
riskLevel: 'critical',
|
|
127
|
+
category: 'disk-operations',
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
pattern: /dd\s+.*of=\/dev\//i,
|
|
131
|
+
description: 'DD 写入设备',
|
|
132
|
+
riskLevel: 'critical',
|
|
133
|
+
category: 'disk-operations',
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
pattern: /kubectl\s+delete\b/i,
|
|
137
|
+
description: '删除K8s资源',
|
|
138
|
+
riskLevel: 'critical',
|
|
139
|
+
category: 'k8s-operations',
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
pattern: /docker\s+rm\b/i,
|
|
143
|
+
description: '删除Docker容器',
|
|
144
|
+
riskLevel: 'critical',
|
|
145
|
+
category: 'docker-operations',
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
pattern: /docker\s+rmi\b/i,
|
|
149
|
+
description: '删除Docker镜像',
|
|
150
|
+
riskLevel: 'critical',
|
|
151
|
+
category: 'docker-operations',
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
pattern: /docker\s+system\s+prune/i,
|
|
155
|
+
description: 'Docker清理系统',
|
|
156
|
+
riskLevel: 'critical',
|
|
157
|
+
category: 'docker-operations',
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
pattern: /docker\s+(run|create).*--privileged/i,
|
|
161
|
+
description: 'Docker特权容器',
|
|
162
|
+
riskLevel: 'critical',
|
|
163
|
+
category: 'docker-operations',
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
pattern: /helm\s+(uninstall|delete)\b/i,
|
|
167
|
+
description: 'Helm卸载',
|
|
168
|
+
riskLevel: 'critical',
|
|
169
|
+
category: 'helm-operations',
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
pattern: /DROP\s+(TABLE|DATABASE|SCHEMA)/i,
|
|
173
|
+
description: '数据库删除',
|
|
174
|
+
riskLevel: 'critical',
|
|
175
|
+
category: 'database-operations',
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
pattern: /TRUNCATE\s+TABLE/i,
|
|
179
|
+
description: '数据库截断',
|
|
180
|
+
riskLevel: 'critical',
|
|
181
|
+
category: 'database-operations',
|
|
182
|
+
},
|
|
183
|
+
];
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* HIGH 级别危险规则 - 配置修改/部署操作
|
|
187
|
+
*/
|
|
188
|
+
const HIGH_RULES: DangerousRule[] = [
|
|
189
|
+
{
|
|
190
|
+
pattern: /kubectl\s+(apply|create|scale|patch|edit|replace|rollout|exec|run)\b/i,
|
|
191
|
+
description: 'K8s修改操作',
|
|
192
|
+
riskLevel: 'high',
|
|
193
|
+
category: 'k8s-operations',
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
pattern: /helm\s+(install|upgrade)\b/i,
|
|
197
|
+
description: 'Helm部署',
|
|
198
|
+
riskLevel: 'high',
|
|
199
|
+
category: 'helm-operations',
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
pattern: /(apt|apt-get)\s+(install|remove|purge)/i,
|
|
203
|
+
description: 'APT包管理',
|
|
204
|
+
riskLevel: 'high',
|
|
205
|
+
category: 'package-management',
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
pattern: /yum\s+(install|remove)/i,
|
|
209
|
+
description: 'YUM包管理',
|
|
210
|
+
riskLevel: 'high',
|
|
211
|
+
category: 'package-management',
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
pattern: /npm\s+(install|uninstall|update)\s+(-g|--global)/i,
|
|
215
|
+
description: 'NPM全局包操作',
|
|
216
|
+
riskLevel: 'high',
|
|
217
|
+
category: 'package-management',
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
pattern: /pip\s+(install|uninstall)\s+(-\S*\s+)?(--user|--system)/i,
|
|
221
|
+
description: 'PIP包操作',
|
|
222
|
+
riskLevel: 'high',
|
|
223
|
+
category: 'package-management',
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
pattern: /iptables\b/i,
|
|
227
|
+
description: '防火墙规则修改',
|
|
228
|
+
riskLevel: 'high',
|
|
229
|
+
category: 'network-config',
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
pattern: /ufw\b/i,
|
|
233
|
+
description: 'UFW防火墙操作',
|
|
234
|
+
riskLevel: 'high',
|
|
235
|
+
category: 'network-config',
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
pattern: /firewall-cmd\b/i,
|
|
239
|
+
description: 'Firewalld操作',
|
|
240
|
+
riskLevel: 'high',
|
|
241
|
+
category: 'network-config',
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
pattern: /chmod\s+[0-7]*7[0-7]{2}/,
|
|
245
|
+
description: '设置完全权限',
|
|
246
|
+
riskLevel: 'high',
|
|
247
|
+
category: 'permissions',
|
|
248
|
+
},
|
|
249
|
+
{
|
|
250
|
+
pattern: /chown\s+.*\s+\//i,
|
|
251
|
+
description: '修改目录所有者',
|
|
252
|
+
riskLevel: 'high',
|
|
253
|
+
category: 'permissions',
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
pattern: /useradd|userdel|usermod/i,
|
|
257
|
+
description: '用户管理',
|
|
258
|
+
riskLevel: 'high',
|
|
259
|
+
category: 'user-management',
|
|
260
|
+
},
|
|
261
|
+
{
|
|
262
|
+
pattern: /passwd\b/i,
|
|
263
|
+
description: '修改密码',
|
|
264
|
+
riskLevel: 'high',
|
|
265
|
+
category: 'user-management',
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
pattern: />\s*\/etc\//,
|
|
269
|
+
description: '修改系统配置',
|
|
270
|
+
riskLevel: 'high',
|
|
271
|
+
category: 'system-config',
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
pattern: />\s*.*\/\.ssh\//,
|
|
275
|
+
description: '修改SSH配置',
|
|
276
|
+
riskLevel: 'high',
|
|
277
|
+
category: 'system-config',
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
pattern: />\s*.*\/\.bashrc$/,
|
|
281
|
+
description: '修改Shell配置',
|
|
282
|
+
riskLevel: 'high',
|
|
283
|
+
category: 'system-config',
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
pattern: />\s*.*\/\.zshrc$/,
|
|
287
|
+
description: '修改Shell配置',
|
|
288
|
+
riskLevel: 'high',
|
|
289
|
+
category: 'system-config',
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
pattern: /DELETE\s+FROM/i,
|
|
293
|
+
description: '数据库删除记录',
|
|
294
|
+
riskLevel: 'high',
|
|
295
|
+
category: 'database-operations',
|
|
296
|
+
},
|
|
297
|
+
{
|
|
298
|
+
pattern: /ALTER\s+(TABLE|DATABASE|USER)/i,
|
|
299
|
+
description: '数据库修改',
|
|
300
|
+
riskLevel: 'high',
|
|
301
|
+
category: 'database-operations',
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
pattern: /CREATE\s+(TABLE|DATABASE|USER|INDEX)/i,
|
|
305
|
+
description: '数据库创建',
|
|
306
|
+
riskLevel: 'high',
|
|
307
|
+
category: 'database-operations',
|
|
308
|
+
},
|
|
309
|
+
{
|
|
310
|
+
pattern: /GRANT\s+/i,
|
|
311
|
+
description: '数据库授权',
|
|
312
|
+
riskLevel: 'high',
|
|
313
|
+
category: 'database-operations',
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
pattern: /REVOKE\s+/i,
|
|
317
|
+
description: '数据库撤销权限',
|
|
318
|
+
riskLevel: 'high',
|
|
319
|
+
category: 'database-operations',
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
pattern: /\bkill\s+\d+/,
|
|
323
|
+
description: '终止进程',
|
|
324
|
+
riskLevel: 'high',
|
|
325
|
+
category: 'process-management',
|
|
326
|
+
},
|
|
327
|
+
{
|
|
328
|
+
pattern: /\bpkill\s+\S+/,
|
|
329
|
+
description: '终止进程组',
|
|
330
|
+
riskLevel: 'high',
|
|
331
|
+
category: 'process-management',
|
|
332
|
+
},
|
|
333
|
+
{
|
|
334
|
+
pattern: /\bkillall\s+\S+/,
|
|
335
|
+
description: '终止所有同名进程',
|
|
336
|
+
riskLevel: 'high',
|
|
337
|
+
category: 'process-management',
|
|
338
|
+
},
|
|
339
|
+
];
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* MEDIUM 级别警告规则 - 文件写入等需关注操作
|
|
343
|
+
*/
|
|
344
|
+
const MEDIUM_RULES: DangerousRule[] = [
|
|
345
|
+
{
|
|
346
|
+
pattern: /\brm\s+\S+/,
|
|
347
|
+
description: '删除文件',
|
|
348
|
+
riskLevel: 'medium',
|
|
349
|
+
category: 'file-deletion',
|
|
350
|
+
},
|
|
351
|
+
{
|
|
352
|
+
pattern: /(?<!echo\s)">>\s*\/(?!tmp|var\/tmp)/,
|
|
353
|
+
description: '追加写入文件',
|
|
354
|
+
riskLevel: 'medium',
|
|
355
|
+
category: 'file-write',
|
|
356
|
+
},
|
|
357
|
+
{
|
|
358
|
+
pattern: /(?<!echo\s)">\s*\/(?!tmp|var\/tmp|dev\/null)/,
|
|
359
|
+
description: '覆盖写入文件',
|
|
360
|
+
riskLevel: 'medium',
|
|
361
|
+
category: 'file-write',
|
|
362
|
+
},
|
|
363
|
+
{
|
|
364
|
+
pattern: /sed\s+.*-i/,
|
|
365
|
+
description: '就地修改文件',
|
|
366
|
+
riskLevel: 'medium',
|
|
367
|
+
category: 'file-write',
|
|
368
|
+
},
|
|
369
|
+
{
|
|
370
|
+
pattern: /\btee\s+/,
|
|
371
|
+
description: 'Tee写入文件',
|
|
372
|
+
riskLevel: 'medium',
|
|
373
|
+
category: 'file-write',
|
|
374
|
+
},
|
|
375
|
+
{
|
|
376
|
+
pattern: /\bcp\s+.*\s+\/(?!tmp|var\/tmp|home)/,
|
|
377
|
+
description: '复制到系统目录',
|
|
378
|
+
riskLevel: 'medium',
|
|
379
|
+
category: 'file-write',
|
|
380
|
+
},
|
|
381
|
+
{
|
|
382
|
+
pattern: /\bmv\s+.*\s+\/(?!tmp|var\/tmp|home)/,
|
|
383
|
+
description: '移动到系统目录',
|
|
384
|
+
riskLevel: 'medium',
|
|
385
|
+
category: 'file-write',
|
|
386
|
+
},
|
|
387
|
+
{
|
|
388
|
+
pattern: /docker\s+(start|stop|restart|pause|unpause)\b/i,
|
|
389
|
+
description: 'Docker容器启停',
|
|
390
|
+
riskLevel: 'medium',
|
|
391
|
+
category: 'docker-operations',
|
|
392
|
+
},
|
|
393
|
+
{
|
|
394
|
+
pattern: /docker\s+(build|push|tag|commit)\b/i,
|
|
395
|
+
description: 'Docker镜像操作',
|
|
396
|
+
riskLevel: 'medium',
|
|
397
|
+
category: 'docker-operations',
|
|
398
|
+
},
|
|
399
|
+
{
|
|
400
|
+
pattern: /UPDATE\s+\S+\s+SET/i,
|
|
401
|
+
description: '数据库更新',
|
|
402
|
+
riskLevel: 'medium',
|
|
403
|
+
category: 'database-operations',
|
|
404
|
+
},
|
|
405
|
+
{
|
|
406
|
+
pattern: /INSERT\s+INTO/i,
|
|
407
|
+
description: '数据库插入',
|
|
408
|
+
riskLevel: 'medium',
|
|
409
|
+
category: 'database-operations',
|
|
410
|
+
},
|
|
411
|
+
{
|
|
412
|
+
pattern: /git\s+(push|commit|reset|rebase|checkout\s+-[bf]|merge|cherry-pick)/i,
|
|
413
|
+
description: 'Git修改操作',
|
|
414
|
+
riskLevel: 'medium',
|
|
415
|
+
category: 'git-operations',
|
|
416
|
+
},
|
|
417
|
+
{
|
|
418
|
+
pattern: /crontab\s+(-[er])/i,
|
|
419
|
+
description: '修改定时任务',
|
|
420
|
+
riskLevel: 'medium',
|
|
421
|
+
category: 'scheduled-tasks',
|
|
422
|
+
},
|
|
423
|
+
{
|
|
424
|
+
pattern: /at\s+(-[fr])/i,
|
|
425
|
+
description: '修改定时任务',
|
|
426
|
+
riskLevel: 'medium',
|
|
427
|
+
category: 'scheduled-tasks',
|
|
428
|
+
},
|
|
429
|
+
];
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* 默认危险命令规则
|
|
433
|
+
*/
|
|
434
|
+
const DEFAULT_DANGEROUS_RULES: DangerousRule[] = [
|
|
435
|
+
...CRITICAL_RULES,
|
|
436
|
+
...HIGH_RULES,
|
|
437
|
+
...MEDIUM_RULES,
|
|
438
|
+
];
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* 默认安全命令规则(不需要审批)
|
|
442
|
+
*/
|
|
443
|
+
const DEFAULT_SAFE_RULES: SafeRule[] = [
|
|
444
|
+
// 容器编排只读操作
|
|
445
|
+
{
|
|
446
|
+
pattern: /kubectl\s+(get|describe|logs|top|explain|api-resources|version|cluster-info)/i,
|
|
447
|
+
description: '只读查询',
|
|
448
|
+
},
|
|
449
|
+
{
|
|
450
|
+
pattern: /kubectl\s+.*--dry-run(=client|=server)?/i,
|
|
451
|
+
description: '模拟运行',
|
|
452
|
+
},
|
|
453
|
+
|
|
454
|
+
// 文件只读操作
|
|
455
|
+
{
|
|
456
|
+
pattern: /^(ls|cat|head|tail|less|more|file|stat|wc|find|grep|awk|sed)\s/i,
|
|
457
|
+
description: '文件只读操作',
|
|
458
|
+
},
|
|
459
|
+
{
|
|
460
|
+
pattern: /^echo\s/i,
|
|
461
|
+
description: 'Echo 命令',
|
|
462
|
+
},
|
|
463
|
+
{
|
|
464
|
+
pattern: /^pwd$/i,
|
|
465
|
+
description: '显示当前目录',
|
|
466
|
+
},
|
|
467
|
+
{
|
|
468
|
+
pattern: /^whoami$/i,
|
|
469
|
+
description: '显示当前用户',
|
|
470
|
+
},
|
|
471
|
+
{
|
|
472
|
+
pattern: /^date$/i,
|
|
473
|
+
description: '显示日期',
|
|
474
|
+
},
|
|
475
|
+
{
|
|
476
|
+
pattern: /^which\s/i,
|
|
477
|
+
description: '查找命令路径',
|
|
478
|
+
},
|
|
479
|
+
{
|
|
480
|
+
pattern: /^type\s/i,
|
|
481
|
+
description: '显示命令类型',
|
|
482
|
+
},
|
|
483
|
+
|
|
484
|
+
// 网络只读操作
|
|
485
|
+
{
|
|
486
|
+
pattern: /^(ping|traceroute|tracepath|nslookup|dig|host)\s/i,
|
|
487
|
+
description: '网络诊断',
|
|
488
|
+
},
|
|
489
|
+
{
|
|
490
|
+
pattern: /^(curl|wget)\s+.*--head/i,
|
|
491
|
+
description: 'HTTP 头查询',
|
|
492
|
+
},
|
|
493
|
+
{
|
|
494
|
+
pattern: /^netstat\s/i,
|
|
495
|
+
description: '网络状态查询',
|
|
496
|
+
},
|
|
497
|
+
{
|
|
498
|
+
pattern: /^ss\s/i,
|
|
499
|
+
description: 'Socket 统计',
|
|
500
|
+
},
|
|
501
|
+
{
|
|
502
|
+
pattern: /^ip\s+(addr|link|route|neigh)\s+(show|list)/i,
|
|
503
|
+
description: 'IP 配置查询',
|
|
504
|
+
},
|
|
505
|
+
|
|
506
|
+
// 进程只读操作
|
|
507
|
+
{
|
|
508
|
+
pattern: /^ps\s/i,
|
|
509
|
+
description: '进程状态',
|
|
510
|
+
},
|
|
511
|
+
{
|
|
512
|
+
pattern: /^top$/i,
|
|
513
|
+
description: '系统监控',
|
|
514
|
+
},
|
|
515
|
+
{
|
|
516
|
+
pattern: /^htop$/i,
|
|
517
|
+
description: '系统监控',
|
|
518
|
+
},
|
|
519
|
+
{
|
|
520
|
+
pattern: /^free\s/i,
|
|
521
|
+
description: '内存状态',
|
|
522
|
+
},
|
|
523
|
+
{
|
|
524
|
+
pattern: /^df\s/i,
|
|
525
|
+
description: '磁盘使用',
|
|
526
|
+
},
|
|
527
|
+
{
|
|
528
|
+
pattern: /^du\s/i,
|
|
529
|
+
description: '目录大小',
|
|
530
|
+
},
|
|
531
|
+
|
|
532
|
+
// Git 只读操作
|
|
533
|
+
{
|
|
534
|
+
pattern: /git\s+(status|log|diff|show|branch|tag|remote)/i,
|
|
535
|
+
description: 'Git 只读操作',
|
|
536
|
+
},
|
|
537
|
+
];
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* 全局配置实例
|
|
541
|
+
*/
|
|
542
|
+
let configInstance: DangerousCommandsConfig | null = null;
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* 加载配置
|
|
546
|
+
*/
|
|
547
|
+
function loadConfig(): DangerousCommandsConfig {
|
|
548
|
+
if (configInstance) {
|
|
549
|
+
return configInstance;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// 尝试从配置文件加载
|
|
553
|
+
const configPath = join(process.cwd(), '.pi', 'dangerous-commands.json');
|
|
554
|
+
|
|
555
|
+
if (existsSync(configPath)) {
|
|
556
|
+
try {
|
|
557
|
+
const content = readFileSync(configPath, 'utf-8');
|
|
558
|
+
const userConfig = JSON.parse(content);
|
|
559
|
+
|
|
560
|
+
configInstance = {
|
|
561
|
+
rules: [
|
|
562
|
+
...DEFAULT_DANGEROUS_RULES,
|
|
563
|
+
...(userConfig.rules || []).map((r: any) => ({
|
|
564
|
+
pattern: new RegExp(r.pattern, 'i'),
|
|
565
|
+
description: r.description,
|
|
566
|
+
riskLevel: r.riskLevel as 'medium' | 'high' | 'critical',
|
|
567
|
+
category: r.category,
|
|
568
|
+
})),
|
|
569
|
+
],
|
|
570
|
+
safeRules: [
|
|
571
|
+
...DEFAULT_SAFE_RULES,
|
|
572
|
+
...(userConfig.safeRules || []).map((r: any) => ({
|
|
573
|
+
pattern: new RegExp(r.pattern, 'i'),
|
|
574
|
+
description: r.description,
|
|
575
|
+
})),
|
|
576
|
+
],
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
console.log('[dangerous-commands] Loaded config from', configPath);
|
|
580
|
+
return configInstance;
|
|
581
|
+
} catch (error) {
|
|
582
|
+
console.error('[dangerous-commands] Failed to load config:', error);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// 使用默认配置
|
|
587
|
+
configInstance = {
|
|
588
|
+
rules: DEFAULT_DANGEROUS_RULES,
|
|
589
|
+
safeRules: DEFAULT_SAFE_RULES,
|
|
590
|
+
};
|
|
591
|
+
|
|
592
|
+
return configInstance;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* 重置配置(用于测试)
|
|
597
|
+
*/
|
|
598
|
+
export function resetConfig(): void {
|
|
599
|
+
configInstance = null;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* 检查命令是否安全(不需要审批)
|
|
604
|
+
*/
|
|
605
|
+
export function isSafeCommand(command: string): boolean {
|
|
606
|
+
const config = loadConfig();
|
|
607
|
+
|
|
608
|
+
for (const rule of config.safeRules) {
|
|
609
|
+
if (rule.pattern.test(command)) {
|
|
610
|
+
return true;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
return false;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* 检查命令是否危险
|
|
619
|
+
*/
|
|
620
|
+
export function checkDangerousCommand(command: string): {
|
|
621
|
+
isDangerous: boolean;
|
|
622
|
+
rule?: DangerousRule;
|
|
623
|
+
level: DangerLevel;
|
|
624
|
+
} {
|
|
625
|
+
// 首先检查是否是安全命令
|
|
626
|
+
if (isSafeCommand(command)) {
|
|
627
|
+
return { isDangerous: false, level: 'safe' };
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
const config = loadConfig();
|
|
631
|
+
|
|
632
|
+
// 检查危险规则
|
|
633
|
+
for (const rule of config.rules) {
|
|
634
|
+
if (rule.pattern.test(command)) {
|
|
635
|
+
return {
|
|
636
|
+
isDangerous: true,
|
|
637
|
+
rule,
|
|
638
|
+
level: rule.riskLevel,
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
// 未知命令,视为低风险
|
|
644
|
+
return { isDangerous: false, level: 'low' };
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* 评估命令风险等级
|
|
649
|
+
*/
|
|
650
|
+
export function estimateRiskLevel(command: string): 'low' | 'medium' | 'high' | 'critical' {
|
|
651
|
+
const result = checkDangerousCommand(command);
|
|
652
|
+
|
|
653
|
+
if (result.level === 'safe') return 'low';
|
|
654
|
+
if (result.level === 'low') return 'low';
|
|
655
|
+
if (result.level === 'medium') return 'medium';
|
|
656
|
+
if (result.level === 'high') return 'high';
|
|
657
|
+
return 'critical';
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* 获取所有危险规则(用于调试)
|
|
662
|
+
*/
|
|
663
|
+
export function getDangerousRules(): DangerousRule[] {
|
|
664
|
+
return loadConfig().rules;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/**
|
|
668
|
+
* 获取所有安全规则(用于调试)
|
|
669
|
+
*/
|
|
670
|
+
export function getSafeRules(): SafeRule[] {
|
|
671
|
+
return loadConfig().safeRules;
|
|
672
|
+
}
|