Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
import fs from "node:fs";
import path from "node:path";
import { FsSafeError } from "./errors.js";
import { normalizeLowercaseStringOrEmpty } from "./string-coerce.js";
export { assertNoUnsafeDeviceReadPath, isUnsafeDeviceReadPath, matchUnsafeDeviceReadPath, } from "./device-path.js";
const NOT_FOUND_CODES = new Set(["ENOENT", "ENOTDIR"]);
const SYMLINK_OPEN_CODES = new Set(["ELOOP", "EINVAL", "ENOTSUP"]);
const POSIX_SEPARATOR_CHAR_CODE = 0x2f;
export function normalizeWindowsPathForComparison(input) {
let normalized = path.win32.normalize(input);
if (normalized.startsWith("\\\\?\\")) {
normalized = normalized.slice(4);
if (normalized.toUpperCase().startsWith("UNC\\")) {
normalized = `\\\\${normalized.slice(4)}`;
}
}
return normalizeLowercaseStringOrEmpty(normalized.replaceAll("/", "\\"));
}
export function isNodeError(value) {
return Boolean(value && typeof value === "object" && "code" in value);
}
export function hasNodeErrorCode(value, code) {
return isNodeError(value) && value.code === code;
}
export function assertNoNulPathInput(filePath, message = "path contains a NUL byte") {
if (filePath.includes("\0")) {
throw new FsSafeError("invalid-path", message);
}
}
export function isNotFoundPathError(value) {
return isNodeError(value) && typeof value.code === "string" && NOT_FOUND_CODES.has(value.code);
}
export function isSymlinkOpenError(value) {
return isNodeError(value) && typeof value.code === "string" && SYMLINK_OPEN_CODES.has(value.code);
}
export function isPathInside(root, target) {
if (process.platform === "win32") {
const rootForCompare = normalizeWindowsPathForComparison(path.win32.resolve(root));
const targetForCompare = normalizeWindowsPathForComparison(path.win32.resolve(target));
const relative = path.win32.relative(rootForCompare, targetForCompare);
const firstSegment = relative.split(path.win32.sep)[0];
return (relative === "" || (firstSegment !== ".." && !path.win32.isAbsolute(relative)));
}
if (root.length > 0 &&
root.charCodeAt(0) === POSIX_SEPARATOR_CHAR_CODE &&
target.length >= root.length &&
target.charCodeAt(0) === POSIX_SEPARATOR_CHAR_CODE &&
!target.includes("/..") &&
(target === root ||
(target.startsWith(root) && target.charCodeAt(root.length) === POSIX_SEPARATOR_CHAR_CODE))) {
return true;
}
const resolvedRoot = path.resolve(root);
const resolvedTarget = path.resolve(target);
const relative = path.relative(resolvedRoot, resolvedTarget);
const firstSegment = relative.split(path.posix.sep)[0];
return relative === "" || (firstSegment !== ".." && !path.isAbsolute(relative));
}
export function isPathRelativeEscape(relativePath) {
return relativePath === ".." || relativePath.startsWith(`..${path.sep}`) || path.isAbsolute(relativePath);
}
export function resolveSafeBaseDir(rootDir) {
const resolved = path.resolve(rootDir);
return resolved.endsWith(path.sep) ? resolved : `${resolved}${path.sep}`;
}
export function isWithinDir(rootDir, targetPath) {
return isPathInside(rootDir, targetPath);
}
export function safeRealpathSync(targetPath, cache) {
const cached = cache?.get(targetPath);
if (cached) {
return cached;
}
try {
const resolved = fs.realpathSync(targetPath);
cache?.set(targetPath, resolved);
cache?.set(resolved, resolved);
return resolved;
}
catch {
return null;
}
}
export function isPathInsideWithRealpath(basePath, candidatePath, opts) {
if (!isPathInside(basePath, candidatePath)) {
return false;
}
const baseReal = safeRealpathSync(basePath, opts?.cache);
const candidateReal = safeRealpathSync(candidatePath, opts?.cache);
if (!baseReal || !candidateReal) {
return opts?.requireRealpath === false;
}
return isPathInside(baseReal, candidateReal);
}
export function safeStatSync(targetPath) {
try {
return fs.statSync(targetPath);
}
catch {
return null;
}
}
export function splitSafeRelativePath(relativePath) {
if (relativePath.length === 0 || relativePath === ".") {
return [];
}
assertNoNulPathInput(relativePath, "relative path contains a NUL byte");
if (relativePath.includes("\\")) {
throw new FsSafeError("invalid-path", "relative path must use forward slashes");
}
if (path.posix.isAbsolute(relativePath) ||
path.win32.isAbsolute(relativePath) ||
relativePath.startsWith("//")) {
throw new FsSafeError("invalid-path", "relative path must not be absolute");
}
const segments = relativePath.split("/").filter((segment) => segment.length > 0 && segment !== ".");
for (const segment of segments) {
if (segment === "..") {
throw new FsSafeError("invalid-path", "relative path must not contain '..'");
}
}
return segments;
}
export function resolveSafeRelativePath(rootDir, relativePath) {
const root = path.resolve(rootDir);
const target = path.resolve(root, ...splitSafeRelativePath(relativePath));
if (!isPathInside(root, target)) {
throw new FsSafeError("outside-workspace", "relative path escapes root");
}
return target;
}