Guanghu Domestic Migration d1e47f4565
Some checks are pending
自动更新代码和重启 / update-and-restart (push) Waiting to run
CI检查 + 自动部署 / check (push) Waiting to run
CI检查 + 自动部署 / deploy (push) Blocked by required conditions
重启聊天服务 / restart (push) Waiting to run
chore: import sanitized domestic snapshot for REPO-002
Source snapshot: ca48d3ddf926d79aa138306164169baf764bb829
2026-07-17 15:54:41 +08:00

40 lines
1.0 KiB
JavaScript

/**
* @capability shell.run
* @description 运行 shell 命令 (sync, 默认拒绝危险命令)
* @signature run(cmd, args=[], options={}) → {status, stdout, stderr}
* @sovereign TCS-0002∞
* @copyright 国作登字-2026-A-00037559
*/
'use strict';
const { spawnSync } = require('child_process');
const DANGEROUS_PATTERNS = [
/\brm\s+-rf\s+\//,
/:\(\)\{:\|:&\};:/, // fork bomb
/\bdd\s+if=\/dev\//,
/\bmkfs\b/,
];
module.exports = function shellRun(cmd, args = [], options = {}) {
const full = [cmd, ...args].join(' ');
const checkTargets = [full, cmd, ...args];
for (const re of DANGEROUS_PATTERNS) {
for (const t of checkTargets) {
if (typeof t === 'string' && re.test(t)) {
throw new Error(`shell.run refused dangerous pattern in: ${t}`);
}
}
}
const r = spawnSync(cmd, args, {
encoding: 'utf8',
timeout: options.timeoutMs || 60000,
cwd: options.cwd,
env: options.env || process.env,
});
return {
status: r.status,
stdout: r.stdout || '',
stderr: r.stderr || '',
};
};