fix: 安全—路径沙箱+命令字符过滤+移除unused spire
- safePath(): resolve→startsWith(BASE_DIR)防路径遍历
- sanitizeCommand(): 拒绝;&|$(){}等shell元字符
- 移除unused import(spawn,DeviceIdentity)
This commit is contained in:
@@ -7,15 +7,24 @@
|
||||
|
||||
import * as fs from "node:fs";
|
||||
import * as path from "node:path";
|
||||
import { execSync, spawn } from "node:child_process";
|
||||
import { execSync } from "node:child_process";
|
||||
import * as os from "node:os";
|
||||
|
||||
/** Sandbox: restrict file ops to this base directory or env GCA_WORKSPACE */
|
||||
const BASE_DIR = path.resolve(process.env.GCA_WORKSPACE ?? process.cwd());
|
||||
|
||||
function safePath(p: string): string {
|
||||
const resolved = path.resolve(BASE_DIR, p);
|
||||
if (!resolved.startsWith(BASE_DIR)) throw new Error(`Path traversal blocked: ${p}`);
|
||||
return resolved;
|
||||
}
|
||||
|
||||
// ── C-002: file_list ────────────────────────────────────────
|
||||
export async function file_list(args: {
|
||||
path: string;
|
||||
recursive?: boolean;
|
||||
}): Promise<any> {
|
||||
const dir = args.path || ".";
|
||||
const dir = safePath(args.path || ".");
|
||||
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
||||
const result = entries.map(e => ({
|
||||
name: e.name,
|
||||
@@ -66,11 +75,12 @@ export async function file_move(args: {
|
||||
}
|
||||
|
||||
// ── C-008: exec ─────────────────────────────────────────────
|
||||
export async function exec(args: {
|
||||
command: string;
|
||||
cwd?: string;
|
||||
timeout?: number;
|
||||
}): Promise<any> {
|
||||
function sanitizeCommand(cmd: string): string {
|
||||
if (/[;&|`$(){}[\]<>!\\\n\r]/.test(cmd)) throw new Error(`Dangerous chars in: ${cmd.slice(0,50)}`);
|
||||
return cmd;
|
||||
}
|
||||
export async function exec(args: { command: string; cwd?: string; timeout?: number }): Promise<any> {
|
||||
const command = sanitizeCommand(args.command);
|
||||
const timeout = args.timeout ?? 30000;
|
||||
try {
|
||||
const stdout = execSync(args.command, {
|
||||
|
||||
Reference in New Issue
Block a user