@2501-ai/cli
Version:
[](https://www.npmjs.com/package/@2501-ai/cli) [](https://www.2501.ai/research/full-humaneval-benchmark) [![Lic
101 lines (100 loc) • 3.12 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isDirUnsafe = exports.UNIX_UNSAFE_DIRS = void 0;
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const WINDOWS_UNSAFE_DIRS = [
'C:\\',
'C:\\Windows',
'C:\\Program Files',
'C:\\Program Files (x86)',
'C:\\ProgramData',
'C:\\Users',
];
const MACOS_ALLOWED_SUBS = [
'/private/tmp/',
'/Library/Application Support/',
'/Library/Preferences/',
'/usr/local/',
'/Users/',
'/Volumes/',
];
const LINUX_ALLOWED_SUBS = [
'/home/',
'/tmp/',
'/var/tmp/',
'/var/cache/',
'/var/log/',
'/var/lib/',
'/var/spool/',
'/var/run/',
'/var/mail/',
'/usr',
'/mnt/',
'/media/',
'/opt/',
'/srv/',
'/data/',
];
exports.UNIX_UNSAFE_DIRS = [
'/',
'/bin',
'/boot',
'/dev',
'/etc',
'/lib',
'/root',
'/proc',
'/run',
'/snap',
'/sys',
'/tmp',
'/var',
'/System/',
'/Applications/',
'/Library/',
'/Network/',
'/System/',
'/Volumes/',
...LINUX_ALLOWED_SUBS,
...MACOS_ALLOWED_SUBS,
];
const WINDOWS_EXCEPTIONS = ['C:\\Users\\'];
const UNIX_EXCEPTIONS = [...LINUX_ALLOWED_SUBS, ...MACOS_ALLOWED_SUBS];
function isDirUnsafe(dir, throwOnly = false) {
const isWindows = process.platform === 'win32';
const unsafeDirectories = isWindows ? WINDOWS_UNSAFE_DIRS : exports.UNIX_UNSAFE_DIRS;
const exceptions = isWindows ? WINDOWS_EXCEPTIONS : UNIX_EXCEPTIONS;
let realWorkspace;
try {
realWorkspace = fs_1.default.realpathSync(dir);
}
catch (err) {
throw new Error('Failed to resolve the workspace directory.');
}
const normalizedWorkspace = path_1.default.resolve(realWorkspace);
const unsafe = isUnsafe(normalizedWorkspace, unsafeDirectories, exceptions);
if (throwOnly && unsafe) {
throw new Error(`Cannot use system directory "${normalizedWorkspace}" as workspace, please choose another one (e.g. "/tmp/workspace" or "C:\\\\workspace)"`);
}
return unsafe;
}
exports.isDirUnsafe = isDirUnsafe;
const isUnsafe = (normalizedWorkspace, unsafeDirs, exceptions = []) => {
const homeDir = process.env.HOME || process.env.USERPROFILE;
if (homeDir) {
if (normalizedWorkspace === path_1.default.resolve(homeDir)) {
return true;
}
}
const resolvedUnsafeDirs = unsafeDirs.map((dir) => path_1.default.resolve(dir) + path_1.default.sep);
const resolvedExceptions = exceptions.map((exc) => path_1.default.resolve(exc) + path_1.default.sep);
const isInUnsafeDir = resolvedUnsafeDirs.some((dir) => normalizedWorkspace.startsWith(dir) ||
normalizedWorkspace === dir.slice(0, -1));
const isInExceptionDir = resolvedExceptions.some((exc) => normalizedWorkspace.startsWith(exc) &&
normalizedWorkspace !== exc.slice(0, -1));
return isInUnsafeDir && !isInExceptionDir;
};