packfs-core
Version:
Semantic filesystem operations for LLM agent frameworks with natural language understanding. See LLM_AGENT_GUIDE.md for copy-paste examples.
58 lines • 1.79 kB
JavaScript
/**
* Path validation utilities
*/
import { resolve, normalize, relative } from 'path';
export class PathValidator {
constructor(sandboxPath) {
this.sandboxPath = sandboxPath;
}
/**
* Validate and normalize a file path
*/
validate(inputPath) {
try {
// Normalize the path
const normalizedPath = 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 = resolve(normalizedPath);
const sandboxResolved = resolve(this.sandboxPath);
const relativePath = 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;
}
}
//# sourceMappingURL=path-validator.js.map