-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathValidator.js
More file actions
71 lines (62 loc) · 1.92 KB
/
pathValidator.js
File metadata and controls
71 lines (62 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* guIDE — Path Validator
*
* Ensures file operations stay within allowed directories.
* Blocks system directories, credential files, and other sensitive paths.
*/
'use strict';
const { app } = require('electron');
const path = require('path');
function createPathValidator(appBasePath, modelsBasePath, getCurrentProjectPath) {
const ALLOWED_ROOTS = [
appBasePath,
modelsBasePath,
app.getPath('userData'),
app.getPath('home'),
app.getPath('documents'),
app.getPath('desktop'),
app.getPath('downloads'),
];
const BLOCKED_PATTERNS = [
/[\\/]windows[\\/]system32/i,
/[\\/]program files/i,
/[\\/]programdata/i,
/[\\/](etc|boot|sbin|proc|sys)[\\/]/i,
/[\\/]\.ssh[\\/]?/i,
/[\\/]\.gnupg[\\/]?/i,
/[\\/]\.aws[\\/]?/i,
/[\\/]\.azure[\\/]?/i,
/[\\/]\.kube[\\/]?/i,
/[\\/]\.docker[\\/]?/i,
/[\\/]\.npmrc$/i,
/[\\/]\.pypirc$/i,
/[\\/]\.netrc$/i,
/[\\/]\.bash_history$/i,
/[\\/]\.zsh_history$/i,
/[\\/]\.gitconfig$/i,
/[\\/]\.git-credentials$/i,
];
/**
* Check if a file path is within allowed boundaries.
* Sanitizes control characters that can result from malformed JSON
* (e.g., \\b becoming backspace in parsed strings).
*/
function isPathAllowed(targetPath) {
if (!targetPath || typeof targetPath !== 'string') return false;
// Strip control characters — none are valid in file paths
const sanitized = targetPath.replace(/[\x00-\x1F]/g, '');
const resolved = path.resolve(sanitized);
for (const pattern of BLOCKED_PATTERNS) {
if (pattern.test(resolved)) return false;
}
const roots = [...ALLOWED_ROOTS];
const projectPath = getCurrentProjectPath();
if (projectPath) roots.push(projectPath);
for (const root of roots) {
if (root && resolved.startsWith(path.resolve(root))) return true;
}
return false;
}
return isPathAllowed;
}
module.exports = { createPathValidator };