@myassis/gateway 1.0.44 → 1.0.46
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/dist/services/tools/exec.js +35 -23
- package/package.json +1 -1
|
@@ -10,7 +10,6 @@ const path_1 = __importDefault(require("path"));
|
|
|
10
10
|
const os_1 = __importDefault(require("os"));
|
|
11
11
|
const shared_1 = require("@myassis/shared");
|
|
12
12
|
const crypto_1 = __importDefault(require("crypto"));
|
|
13
|
-
const ServiceManager_js_1 = require("../ServiceManager.js");
|
|
14
13
|
const logger = (0, shared_1.getLogger)('exec');
|
|
15
14
|
const execAsync = (0, util_1.promisify)(child_process_1.exec);
|
|
16
15
|
// 待批准的命令缓存:token -> { command, cwd, timeout, sessionId, expiresAt }
|
|
@@ -70,6 +69,39 @@ exports.clearSessionCwd = clearSessionCwd;
|
|
|
70
69
|
function generateApprovalToken() {
|
|
71
70
|
return crypto_1.default.randomBytes(16).toString('hex');
|
|
72
71
|
}
|
|
72
|
+
function tokenizeCommand(command) {
|
|
73
|
+
return command.match(/"[^"]*"|'[^']*'|&&|\|\||[;&|\r\n]|\S+/g) ?? [];
|
|
74
|
+
}
|
|
75
|
+
function buildCommandSafetyView(command) {
|
|
76
|
+
const textCommands = new Set(['echo', 'findstr', 'grep']);
|
|
77
|
+
const separators = new Set(['&&', '||', '&', '|', ';', '\r', '\n']);
|
|
78
|
+
const tokens = tokenizeCommand(command);
|
|
79
|
+
const view = [];
|
|
80
|
+
let skipTextArgs = false;
|
|
81
|
+
const getCommandName = (token) => {
|
|
82
|
+
const normalized = token
|
|
83
|
+
.replace(/^["']|["']$/g, '')
|
|
84
|
+
.replace(/\\/g, '/')
|
|
85
|
+
.toLowerCase();
|
|
86
|
+
return (normalized.split('/').pop() ?? normalized)
|
|
87
|
+
.replace(/\.(exe|cmd|bat|ps1|sh)$/i, '');
|
|
88
|
+
};
|
|
89
|
+
for (const token of tokens) {
|
|
90
|
+
if (separators.has(token)) {
|
|
91
|
+
view.push(token);
|
|
92
|
+
skipTextArgs = false;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (skipTextArgs) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
view.push(token);
|
|
99
|
+
if (textCommands.has(getCommandName(token))) {
|
|
100
|
+
skipTextArgs = true;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return view.join(' ');
|
|
104
|
+
}
|
|
73
105
|
/** 检查命令是否危险 */
|
|
74
106
|
function isDangerousCommand(command) {
|
|
75
107
|
// 命令分隔符(起始位置、空格、shell 操作符)隔离的危险关键字
|
|
@@ -82,11 +114,8 @@ function isDangerousCommand(command) {
|
|
|
82
114
|
/(^|[\s;|&])mkfs\b/i,
|
|
83
115
|
/(^|[\s;|&])dd\s+if=.*of=\/dev\//i,
|
|
84
116
|
];
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
return false;
|
|
117
|
+
const safetyCommand = buildCommandSafetyView(command);
|
|
118
|
+
return dangerousPatterns.some(pattern => pattern.test(safetyCommand));
|
|
90
119
|
}
|
|
91
120
|
/** 添加待批准命令,返回 token */
|
|
92
121
|
function addPendingApproval(command, cwd, timeout, sessionId) {
|
|
@@ -176,23 +205,6 @@ exports.execTool = {
|
|
|
176
205
|
return;
|
|
177
206
|
}
|
|
178
207
|
}
|
|
179
|
-
// Windows 下通过 Helper 以登录用户身份执行
|
|
180
|
-
if (process.platform === 'win32') {
|
|
181
|
-
try {
|
|
182
|
-
const result = await (0, ServiceManager_js_1.execAsUser)(command, cwd);
|
|
183
|
-
resolve({
|
|
184
|
-
success: result.exitCode === 0,
|
|
185
|
-
output: result.stdout.substring(0, 100000),
|
|
186
|
-
errorMessage: result.stderr.substring(0, 100000),
|
|
187
|
-
exitCode: result.exitCode,
|
|
188
|
-
});
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
catch (e) {
|
|
192
|
-
resolve({ success: false, errorMessage: e?.message || String(e) });
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
208
|
const options = {
|
|
197
209
|
cwd,
|
|
198
210
|
timeout,
|