/** * Shared utilities for real-filesystem-backed IFileSystem implementations * (OverlayFs and ReadWriteFs). * * Security-critical path validation logic lives here so both implementations * stay consistent. * * Pure path utilities (normalizePath, validatePath, dirname, resolvePath, etc.) * live in path-utils.ts (no node:fs dependency, browser-safe) and are * re-exported here for convenience. */ export { normalizePath, validatePath } from "./path-utils.js"; /** * Check whether `resolved` is equal to, or a child of, `canonicalRoot`. * Uses a boundary-safe prefix check (appends the platform path separator) so * that `/data` does not match `/datastore`. * * Both arguments are REAL filesystem paths produced by `fs.realpathSync` / * `nodePath.resolve`, so on Windows they are backslash-separated * (`C:\root\child`), not POSIX forward-slash paths. Appending a hardcoded * `/` here made every child path fail this check on Windows (only an exact * root match ever passed), so `resolveCanonicalPath`/`resolveRealPath_` * treated every file inside an OverlayFs/ReadWriteFs root as "outside the * sandbox" — `exists()`/`readFile()` silently failed for real files, e.g. * sqlite3 loading a fixture .db would see it as missing and open a fresh * empty database instead ("no such table"). Use `nodePath.sep`, which is * "/" on POSIX (no behavior change there) and "\\" on Windows. */ export declare function isPathWithinRoot(resolved: string, canonicalRoot: string): boolean; /** * Convert a real filesystem path already confirmed to be within * `canonicalRoot` (via `isPathWithinRoot`) into the POSIX-style virtual path * callers/tests expect (e.g. "/foo/bar"), regardless of host OS. * * `IFileSystem`'s whole contract is a Unix-like virtual address space, so a * caller-facing "virtual path" must always use "/" - but the naive * `resolved.slice(canonicalRoot.length)` this replaces inherits whatever * separator the REAL path used, which is "\\" on Windows (e.g. * "\\allowed.txt" instead of "/allowed.txt"). The security boundary check * itself (`isPathWithinRoot`) is unaffected by this - only the string * *returned* to the caller was wrong. */ export declare function toVirtualPath(resolved: string, canonicalRoot: string): string; /** * Neutralize a normalized virtual path segment before it is joined onto a * real root via a HOST-NATIVE `nodePath.join`/`dirname` call (as done by * `ReadWriteFs`/`OverlayFs`'s `toRealPath`/`getInternalRealPath`, and the * `resolveCanonicalPath` ENOENT walk-up that recurses through it). * * `normalizePath` (path-utils.ts) is a shared, platform-unconditional * utility used by every VFS backend (including purely virtual ones like * InMemoryFs/CowFs that never touch a real path) - it only ever splits on * "/", by design, so a Windows-native absolute path passed in as a virtual * argument (e.g. "C:\Users\x\pwned.txt") survives it as ONE opaque segment. * On POSIX that's harmless: backslash and colon are legal filename * characters there, so the segment stays intact as a single real file * nested safely inside root. On *Windows*, two separate hazards apply: * * 1. Host-native `path.win32.join`/`dirname` DOES treat "\" as a separator * and re-splits this "unsplittable" segment, producing a real path with * an embedded, non-leading "C:" component - Windows rejects a bare * colon outside the drive-designator position with ERROR_INVALID_NAME * (libuv maps this to ENOENT). * 2. Even after neutralizing "\", the drive-letter colon itself ("C:") * survives as a literal character WITHIN a single filename component. * NTFS treats any ":" in a filename as the start of an Alternate Data * Stream name (`file.txt:streamname`), not a normal character - so a * colon anywhere but the drive-designator position is still rejected. * * This encodes each hazard character to a `%XX` percent-escape (not a fixed * substitute like "_") specifically so the mapping stays INJECTIVE: a fixed * substitute would let two DISTINCT virtual paths collide onto the SAME * real filename (e.g. "/foo:bar" and "/foo_bar" would both become * "foo_bar"), silently aliasing one path's read/write onto the other's * file. A literal "%XY" (X,Y hex digits) already present in the input is * ALSO escaped (its "%" becomes "%25", XY pass through unchanged) so it * can never be misread as one of this function's own encoded sequences - * without that, "/foo\bar" (real backslash, encoded to "/foo%5Cbar") and * "/foo%5Cbar" (a virtual path that already literally contains that text) * would collide on the same output. An ordinary "%" NOT followed by 2 hex * digits (the vast majority of real-world "%"-containing filenames, e.g. * "50% off.txt") is left completely untouched. * * This must NOT live inside `normalizePath` itself (that was tried and * reverted - it unconditionally corrupted legitimate backslash-containing * filenames on every platform and every backend, including two real * bash-parity spec-test failures on POSIX where a literal "\" in a filename * is expected to survive). Call this ONLY at the specific real-path-join * boundary, and ONLY on win32, where the actual hazards exist. */ export declare function sanitizeForHostJoin(normalized: string): string; /** * Validate that a real filesystem path stays within the sandbox root after * resolving all OS-level symlinks (including in parent components). * * Uses `realpathSync`; when the path does not exist (`ENOENT`) it walks up * to the nearest existing parent. Returns `false` for any path that escapes * the sandbox (fail-closed on unexpected errors). */ export declare function validateRealPath(realPath: string, canonicalRoot: string): boolean; /** * Resolve a real filesystem path to its canonical form and verify it stays * within the sandbox root. Returns the canonical path on success, or `null` * if the path escapes the root (fail-closed on unexpected errors). * * Unlike `validateRealPath` (which returns a boolean), this function returns * the canonical path so callers can use it for subsequent I/O, closing the * TOCTOU gap where the original (unresolved) path could be swapped between * validation and use. */ export declare function resolveCanonicalPath(realPath: string, canonicalRoot: string): string | null; /** * Resolve a real filesystem path to its canonical form, verify it stays * within the sandbox root, AND reject the path if any symlinks were * traversed. * * Detection: compare the relative path from `root` (unresolved) with the * relative path from `canonicalRoot` (resolved). A mismatch means a * symlink was followed somewhere in the path. * * This piggybacks on the `realpathSync` call inside `resolveCanonicalPath` * — the only extra cost is one string comparison. */ export declare function resolveCanonicalPathNoSymlinks(realPath: string, root: string, canonicalRoot: string): string | null; /** * Validate that a root directory exists and is actually a directory. * Throws with a descriptive message including `fsName` (e.g. "OverlayFs", * "ReadWriteFs") on failure. Does NOT include the real root path in the * error message to prevent information leakage. */ export declare function validateRootDirectory(root: string, fsName: string): void; export { sanitizeErrorMessage } from "./sanitize-error.js"; /** * Sanitize a raw OS symlink target so it does not leak real filesystem paths * outside the sandbox. * * - Relative targets are returned as-is (they don't leak real paths). * - Absolute targets that resolve within the canonical root are converted to * a path relative to the root (the caller decides how to present it). * - Absolute targets outside the root are reduced to `basename` only. * * Returns `{ withinRoot: true, relativePath }` when the target is inside the * sandbox, or `{ withinRoot: false, safeName }` when it is outside. */ export declare function sanitizeSymlinkTarget(rawTarget: string, canonicalRoot: string): { withinRoot: true; relativePath: string; } | { withinRoot: false; safeName: string; }; /** * Sanitize an error thrown during real-FS operations to prevent leaking * real OS paths. Node.js ErrnoException objects contain a `.path` property * with the real filesystem path — this must never reach the sandbox user. * * Errors whose `.path` is `undefined` are our own errors (constructed with * `new Error(...)`) rather than Node.js ErrnoExceptions. If their message * contains one of `passthroughPatterns`, they are rethrown as-is (they * already contain safe virtual paths). All other errors are replaced with * a generic `"CODE: operation 'virtualPath'"` message. */ export declare function sanitizeFsError(e: unknown, virtualPath: string, operation: string, passthroughPatterns: readonly string[]): never;