src / safety.ts
import { normalize } from "path";
export const MAX_FILE_SIZE = 102400;
export const MAX_WRITE_SIZE = MAX_FILE_SIZE * 10;
export const BLOCKED_DIRS = [
"C:\\Windows",
"C:\\Windows\\System32",
"C:\\Windows\\System",
"C:\\Windows\\SysWOW64",
"C:\\ProgramData",
"C:\\Program Files",
"C:\\Program Files (x86)",
"/etc",
"/usr",
"/bin",
"/boot",
"/dev",
"/proc",
"/sys",
"/sbin",
];
export function isPathSafe(resolved: string, workingDir: string): { safe: boolean; reason?: string } {
const normalized = normalize(resolved);
const wd = normalize(workingDir);
for (const blocked of BLOCKED_DIRS) {
if (normalized.toLowerCase().startsWith(blocked.toLowerCase())) {
return { safe: false, reason: `Access to system directory '${blocked}' is not allowed` };
}
}
if (!normalized.toLowerCase().startsWith(wd.toLowerCase())) {
return { safe: false, reason: "Path is outside the working directory" };
}
return { safe: true };
}