import { promises as fs } from 'fs'; import path from 'path'; export const SYSTEM_READ_ALLOWED_PREFIXES = ['/root', '/home', '/tmp', '/app', '/data'] as const; const BLOCKED_EXTENSIONS = ['.key', '.pem', '.cert', '.p12', '.pfx', '.kdbx']; // Exact-name blocklist for secret files without credential extensions. const BLOCKED_EXACT_NAMES = new Set([ 'authorized_keys', 'id_rsa', 'id_dsa', 'id_ecdsa', 'id_ed25519', '.netrc', '.pgpass', '.npmrc', ]); function statusError(message: string, statusCode: number): Error & { statusCode: number } { return Object.assign(new Error(message), { statusCode }); } function isWithin(root: string, target: string): boolean { return target === root || target.startsWith(root + path.sep); } async function getCanonicalAllowedRoots(): Promise { const roots = await Promise.all( SYSTEM_READ_ALLOWED_PREFIXES.map(async (prefix) => { try { return await fs.realpath(prefix); } catch { return prefix; } }), ); return Array.from(new Set([...SYSTEM_READ_ALLOWED_PREFIXES, ...roots])); } function isBlockedBasename(p: string): boolean { const base = path.basename(p).toLowerCase(); if (/^\.env(\.|$)/.test(base)) return true; if (BLOCKED_EXACT_NAMES.has(base)) return true; // id_rsa, id_rsa.pub, etc. if (/^id_(rsa|dsa|ecdsa|ed25519)(\.|$)/.test(base)) return true; return BLOCKED_EXTENSIONS.some((ext) => base.endsWith(ext)); } function requireNoNullBytes(input: string): void { if (input.includes('\0')) { throw statusError('Invalid path', 400); } } // Gatekeeper for /system/fs/read: allowed roots, symlinks, and secret filenames. export async function resolveReadableSystemPath(requestedPath: string): Promise { requireNoNullBytes(requestedPath); const resolvedPath = path.resolve(requestedPath); const allowedRoots = await getCanonicalAllowedRoots(); const allowed = allowedRoots.some((prefix) => isWithin(prefix, resolvedPath)); if (!allowed) { throw statusError('Access to this path not allowed', 403); } if (isBlockedBasename(resolvedPath)) { throw statusError('Access to this file type not allowed', 403); } let realPath: string; try { realPath = await fs.realpath(resolvedPath); } catch { throw statusError('File not found', 404); } const realPathAllowed = allowedRoots.some((prefix) => isWithin(prefix, realPath)); if (!realPathAllowed) { throw statusError('Access to this path not allowed', 403); } if (isBlockedBasename(realPath)) { throw statusError('Access to this file type not allowed', 403); } return realPath; } // Resolve a path inside a root and reject traversal or symlink escapes. export async function resolveExistingPathWithinRoot(root: string, requestedPath: string): Promise { requireNoNullBytes(requestedPath); const realRoot = await fs.realpath(root).catch(() => path.resolve(root)); const relativePath = requestedPath.replace(/\\/g, '/').replace(/^\/+/, ''); const candidatePath = path.resolve(realRoot, relativePath); if (!isWithin(realRoot, candidatePath)) { throw statusError('Invalid path', 400); } let realTarget: string; try { realTarget = await fs.realpath(candidatePath); } catch { throw statusError('Not a file', 404); } if (!isWithin(realRoot, realTarget)) { throw statusError('Invalid path', 400); } return realTarget; }