Download src/security/path-validator.js from chahuadev/Chahuadev-smart-roadmap: direct link, hf CLI and curl.
- Browser
- Download file 3.03 kB
-
https://huggingface.co/datasets/chahuadev/Chahuadev-smart-roadmap/resolve/main/src/security/path-validator.js
- Command line
-
hf download hf://datasets/chahuadev/Chahuadev-smart-roadmap/src/security/path-validator.js
-
curl -L -o path-validator.js https://huggingface.co/datasets/chahuadev/Chahuadev-smart-roadmap/resolve/main/src/security/path-validator.js
3.03 kB
| const fs = require('fs'); | |
| const path = require('path'); | |
| const { SECURITY_CONFIG, PathValidationError, SecurityError } = require('./security-config'); | |
| // Validate Input Path | |
| function validateInput(target) { | |
| if (!target || typeof target !== 'string') { | |
| throw new PathValidationError('Invalid target path', target, 'INVALID_TYPE'); | |
| } | |
| if (target.length > SECURITY_CONFIG.MAX_PATH_LENGTH) { | |
| throw new PathValidationError('Path too long', target, 'PATH_TOO_LONG'); | |
| } | |
| // Check for dangerous characters (excluding : for Windows drives) | |
| const dangerousChars = /[<>"|?*\x00-\x1f]/; | |
| if (dangerousChars.test(target)) { | |
| throw new PathValidationError('Dangerous characters in path', target, 'DANGEROUS_CHARS'); | |
| } | |
| return true; | |
| } | |
| // Check Path Safety | |
| function isPathSafe(targetPath) { | |
| const normalizedPath = path.resolve(targetPath); | |
| // Check forbidden paths | |
| for (const forbiddenPattern of SECURITY_CONFIG.FORBIDDEN_PATHS) { | |
| if (forbiddenPattern.test(normalizedPath)) { | |
| throw new SecurityError( | |
| `Access to system directories is not allowed: ${normalizedPath}`, | |
| targetPath, | |
| 'SYSTEM_DIR_ACCESS' | |
| ); | |
| } | |
| } | |
| // Check path traversal | |
| if (normalizedPath.includes('..')) { | |
| throw new SecurityError('Path traversal is not allowed', targetPath, 'PATH_TRAVERSAL'); | |
| } | |
| return true; | |
| } | |
| // Sanitize Path | |
| function sanitizePath(inputPath) { | |
| const normalizedPath = path.normalize(inputPath); | |
| // Check for null bytes | |
| if (normalizedPath.includes('\x00')) { | |
| throw new PathValidationError('Null bytes in path are not allowed', inputPath, 'NULL_BYTES'); | |
| } | |
| return normalizedPath; | |
| } | |
| // Check File Permissions | |
| function checkFilePermissions(filePath, requireWrite = false) { | |
| try { | |
| // Check read permission | |
| fs.accessSync(filePath, fs.constants.R_OK); | |
| // Check write permission if required | |
| if (requireWrite) { | |
| fs.accessSync(filePath, fs.constants.W_OK); | |
| } | |
| return true; | |
| } catch (error) { | |
| throw new SecurityError(`Permission denied: ${filePath}`, filePath, 'PERMISSION_DENIED'); | |
| } | |
| } | |
| // Validate File Extension | |
| function isAllowedExtension(filePath) { | |
| const ext = path.extname(filePath).toLowerCase(); | |
| return SECURITY_CONFIG.ALLOWED_EXTENSIONS.includes(ext); | |
| } | |
| // Check File Size | |
| function checkFileSize(filePath) { | |
| const stats = fs.statSync(filePath); | |
| if (stats.size > SECURITY_CONFIG.MAX_FILE_SIZE) { | |
| throw new SecurityError( | |
| `File too large (${stats.size} bytes, max ${SECURITY_CONFIG.MAX_FILE_SIZE})`, | |
| filePath, | |
| 'FILE_TOO_LARGE' | |
| ); | |
| } | |
| return stats; | |
| } | |
| module.exports = { | |
| validateInput, | |
| isPathSafe, | |
| sanitizePath, | |
| checkFilePermissions, | |
| isAllowedExtension, | |
| checkFileSize | |
| }; | |