UNPKG

packfs-core

Version:

Semantic filesystem operations for LLM agent frameworks with natural language understanding. See LLM_AGENT_GUIDE.md for copy-paste examples.

62 lines 1.96 kB
"use strict"; /** * Path validation utilities */ Object.defineProperty(exports, "__esModule", { value: true }); exports.PathValidator = void 0; const path_1 = require("path"); class PathValidator { constructor(sandboxPath) { this.sandboxPath = sandboxPath; } /** * Validate and normalize a file path */ validate(inputPath) { try { // Normalize the path const normalizedPath = (0, path_1.normalize)(inputPath); // Check for path traversal attempts if (normalizedPath.includes('..')) { return { isValid: false, error: 'Path traversal not allowed' }; } // Check sandbox restrictions if (this.sandboxPath) { const resolvedPath = (0, path_1.resolve)(normalizedPath); const sandboxResolved = (0, path_1.resolve)(this.sandboxPath); const relativePath = (0, path_1.relative)(sandboxResolved, resolvedPath); if (relativePath.startsWith('..')) { return { isValid: false, error: 'Path outside sandbox not allowed' }; } } return { isValid: true, normalizedPath }; } catch (error) { return { isValid: false, error: `Invalid path: ${error instanceof Error ? error.message : 'Unknown error'}` }; } } /** * Check if path is within sandbox */ isInSandbox(inputPath) { if (!this.sandboxPath) { return true; // No sandbox configured } const validation = this.validate(inputPath); return validation.isValid; } } exports.PathValidator = PathValidator; //# sourceMappingURL=path-validator.js.map