@howlil/ez-agents 3.1.0 → 3.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +288 -718
- package/bin/install.js +438 -71
- package/commands/ez/auth.md +87 -0
- package/commands/ez/join-discord.md +18 -18
- package/ez-agents/bin/ez-tools.cjs +120 -2
- package/ez-agents/bin/lib/assistant-adapter.cjs +264 -205
- package/ez-agents/bin/lib/audit-exec.cjs +26 -9
- package/ez-agents/bin/lib/auth.cjs +2 -1
- package/ez-agents/bin/lib/circuit-breaker.cjs +118 -118
- package/ez-agents/bin/lib/commands.cjs +42 -23
- package/ez-agents/bin/lib/config.cjs +190 -183
- package/ez-agents/bin/lib/core.cjs +42 -25
- package/ez-agents/bin/lib/file-lock.cjs +236 -236
- package/ez-agents/bin/lib/frontmatter.cjs +299 -299
- package/ez-agents/bin/lib/fs-utils.cjs +153 -153
- package/ez-agents/bin/lib/git-utils.cjs +203 -203
- package/ez-agents/bin/lib/health-check.cjs +2 -3
- package/ez-agents/bin/lib/index.cjs +113 -113
- package/ez-agents/bin/lib/init.cjs +757 -710
- package/ez-agents/bin/lib/logger.cjs +52 -15
- package/ez-agents/bin/lib/milestone.cjs +241 -241
- package/ez-agents/bin/lib/model-provider.cjs +241 -146
- package/ez-agents/bin/lib/phase.cjs +925 -908
- package/ez-agents/bin/lib/planning-write.cjs +107 -0
- package/ez-agents/bin/lib/retry.cjs +119 -119
- package/ez-agents/bin/lib/roadmap.cjs +306 -305
- package/ez-agents/bin/lib/safe-exec.cjs +91 -5
- package/ez-agents/bin/lib/safe-path.cjs +130 -130
- package/ez-agents/bin/lib/state.cjs +736 -721
- package/ez-agents/bin/lib/temp-file.cjs +239 -239
- package/ez-agents/bin/lib/template.cjs +223 -222
- package/ez-agents/bin/lib/test-file-lock.cjs +112 -112
- package/ez-agents/bin/lib/test-graceful.cjs +93 -93
- package/ez-agents/bin/lib/test-logger.cjs +60 -60
- package/ez-agents/bin/lib/test-safe-exec.cjs +38 -38
- package/ez-agents/bin/lib/test-safe-path.cjs +33 -33
- package/ez-agents/bin/lib/test-temp-file.cjs +125 -125
- package/ez-agents/bin/lib/timeout-exec.cjs +63 -62
- package/ez-agents/bin/lib/verify.cjs +69 -26
- package/ez-agents/references/checkpoints.md +776 -776
- package/ez-agents/references/continuation-format.md +249 -249
- package/ez-agents/references/questioning.md +162 -162
- package/ez-agents/references/tdd.md +263 -263
- package/ez-agents/templates/codebase/concerns.md +310 -310
- package/ez-agents/templates/codebase/conventions.md +307 -307
- package/ez-agents/templates/codebase/integrations.md +280 -280
- package/ez-agents/templates/codebase/stack.md +186 -186
- package/ez-agents/templates/codebase/testing.md +480 -480
- package/ez-agents/templates/config.json +37 -37
- package/ez-agents/templates/continue-here.md +78 -78
- package/ez-agents/templates/milestone-archive.md +123 -123
- package/ez-agents/templates/milestone.md +115 -115
- package/ez-agents/templates/requirements.md +231 -231
- package/ez-agents/templates/research-project/ARCHITECTURE.md +204 -204
- package/ez-agents/templates/research-project/FEATURES.md +147 -147
- package/ez-agents/templates/research-project/PITFALLS.md +200 -200
- package/ez-agents/templates/research-project/STACK.md +120 -120
- package/ez-agents/templates/research-project/SUMMARY.md +170 -170
- package/ez-agents/templates/retrospective.md +54 -54
- package/ez-agents/templates/roadmap.md +202 -202
- package/ez-agents/templates/summary-minimal.md +41 -41
- package/ez-agents/templates/summary-standard.md +48 -48
- package/ez-agents/templates/summary.md +248 -248
- package/ez-agents/templates/user-setup.md +311 -311
- package/ez-agents/templates/verification-report.md +322 -322
- package/ez-agents/workflows/add-phase.md +112 -112
- package/ez-agents/workflows/add-tests.md +351 -351
- package/ez-agents/workflows/add-todo.md +158 -158
- package/ez-agents/workflows/audit-milestone.md +332 -332
- package/ez-agents/workflows/autonomous.md +743 -743
- package/ez-agents/workflows/check-todos.md +177 -177
- package/ez-agents/workflows/cleanup.md +152 -152
- package/ez-agents/workflows/complete-milestone.md +766 -766
- package/ez-agents/workflows/diagnose-issues.md +219 -219
- package/ez-agents/workflows/discovery-phase.md +289 -289
- package/ez-agents/workflows/discuss-phase.md +762 -762
- package/ez-agents/workflows/execute-phase.md +468 -468
- package/ez-agents/workflows/execute-plan.md +483 -483
- package/ez-agents/workflows/health.md +159 -159
- package/ez-agents/workflows/help.md +492 -492
- package/ez-agents/workflows/insert-phase.md +130 -130
- package/ez-agents/workflows/list-phase-assumptions.md +178 -178
- package/ez-agents/workflows/map-codebase.md +316 -316
- package/ez-agents/workflows/new-milestone.md +384 -384
- package/ez-agents/workflows/new-project.md +1113 -1111
- package/ez-agents/workflows/node-repair.md +92 -92
- package/ez-agents/workflows/pause-work.md +122 -122
- package/ez-agents/workflows/plan-milestone-gaps.md +274 -274
- package/ez-agents/workflows/plan-phase.md +651 -651
- package/ez-agents/workflows/progress.md +382 -382
- package/ez-agents/workflows/quick.md +610 -610
- package/ez-agents/workflows/remove-phase.md +155 -155
- package/ez-agents/workflows/research-phase.md +74 -74
- package/ez-agents/workflows/resume-project.md +307 -307
- package/ez-agents/workflows/set-profile.md +81 -81
- package/ez-agents/workflows/settings.md +242 -242
- package/ez-agents/workflows/stats.md +57 -57
- package/ez-agents/workflows/transition.md +544 -544
- package/ez-agents/workflows/ui-phase.md +290 -290
- package/ez-agents/workflows/ui-review.md +157 -157
- package/ez-agents/workflows/update.md +320 -320
- package/ez-agents/workflows/validate-phase.md +167 -167
- package/ez-agents/workflows/verify-phase.md +243 -243
- package/ez-agents/workflows/verify-work.md +584 -584
- package/package.json +2 -3
- package/scripts/build-hooks.js +43 -43
- package/scripts/fix-qwen-installation.js +144 -0
- package/scripts/run-tests.cjs +29 -29
- package/README.zh-CN.md +0 -702
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* EZ Safe Exec — Secure command execution with allowlist and validation
|
|
5
|
+
*
|
|
6
6
|
* Prevents command injection by:
|
|
7
7
|
* - Using execFile instead of execSync with string concatenation
|
|
8
8
|
* - Validating commands against allowlist
|
|
9
9
|
* - Blocking dangerous shell metacharacters in arguments
|
|
10
10
|
* - Logging all commands for audit
|
|
11
|
-
*
|
|
11
|
+
* - Cross-platform shell detection (Windows: powershell/cmd, Unix: bash)
|
|
12
|
+
*
|
|
12
13
|
* Usage:
|
|
13
|
-
* const { safeExec, safeExecJSON } = require('./safe-exec.cjs');
|
|
14
|
+
* const { safeExec, safeExecJSON, getShellConfig } = require('./safe-exec.cjs');
|
|
14
15
|
* const result = await safeExec('git', ['status']);
|
|
15
16
|
*/
|
|
16
17
|
|
|
17
|
-
const { execFile } = require('child_process');
|
|
18
|
+
const { execFile, spawn } = require('child_process');
|
|
18
19
|
const { promisify } = require('util');
|
|
20
|
+
const os = require('os');
|
|
19
21
|
const execFileAsync = promisify(execFile);
|
|
20
22
|
const Logger = require('./logger.cjs');
|
|
21
23
|
const logger = new Logger();
|
|
@@ -30,6 +32,90 @@ const ALLOWED_COMMANDS = new Set([
|
|
|
30
32
|
// Dangerous shell metacharacters that could enable injection
|
|
31
33
|
const DANGEROUS_PATTERN = /[;&|`$(){}\\<>]/;
|
|
32
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Get shell configuration for current platform
|
|
37
|
+
* Returns shell executable and any platform-specific flags
|
|
38
|
+
* @returns {{shell: string, shellFlag: string}} - Shell config object
|
|
39
|
+
*/
|
|
40
|
+
function getShellConfig() {
|
|
41
|
+
const platform = process.platform;
|
|
42
|
+
|
|
43
|
+
if (platform === 'win32') {
|
|
44
|
+
// Windows: prefer PowerShell if available, fallback to cmd.exe
|
|
45
|
+
// Check if PowerShell is available
|
|
46
|
+
try {
|
|
47
|
+
// PowerShell Core (pwsh) or Windows PowerShell (powershell)
|
|
48
|
+
const pwshPath = require('path').join(
|
|
49
|
+
process.env.ProgramFiles || 'C:\\Program Files',
|
|
50
|
+
'PowerShell', '7', 'pwsh.exe'
|
|
51
|
+
);
|
|
52
|
+
if (require('fs').existsSync(pwshPath)) {
|
|
53
|
+
return { shell: 'pwsh', shellFlag: '-Command' };
|
|
54
|
+
}
|
|
55
|
+
return { shell: 'powershell', shellFlag: '-Command' };
|
|
56
|
+
} catch {
|
|
57
|
+
return { shell: 'cmd', shellFlag: '/C' };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Unix-like platforms (macOS, Linux): use bash or sh
|
|
62
|
+
return { shell: 'bash', shellFlag: '-c' };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Execute a shell command with platform-appropriate shell
|
|
67
|
+
* @param {string} command - Full command string to execute
|
|
68
|
+
* @param {Object} options - Execution options
|
|
69
|
+
* @returns {Promise<string>} - Command stdout
|
|
70
|
+
*/
|
|
71
|
+
async function safeShellExec(command, options = {}) {
|
|
72
|
+
const { timeout = 30000, log = true } = options;
|
|
73
|
+
const { shell, shellFlag } = getShellConfig();
|
|
74
|
+
|
|
75
|
+
const startTime = Date.now();
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
if (log) {
|
|
79
|
+
logger.info('Executing shell command', {
|
|
80
|
+
command,
|
|
81
|
+
shell,
|
|
82
|
+
platform: process.platform,
|
|
83
|
+
timestamp: new Date().toISOString()
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const result = await execFileAsync(shell, [shellFlag, command], {
|
|
88
|
+
timeout,
|
|
89
|
+
maxBuffer: 10 * 1024 * 1024, // 10MB buffer
|
|
90
|
+
windowsHide: true // Prevent console flash on Windows
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const duration = Date.now() - startTime;
|
|
94
|
+
if (log) {
|
|
95
|
+
logger.debug('Shell command completed', {
|
|
96
|
+
command,
|
|
97
|
+
shell,
|
|
98
|
+
duration,
|
|
99
|
+
stdout_length: result.stdout?.length || 0
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return result.stdout.trim();
|
|
104
|
+
} catch (err) {
|
|
105
|
+
const duration = Date.now() - startTime;
|
|
106
|
+
logger.error('Shell command failed', {
|
|
107
|
+
command,
|
|
108
|
+
shell,
|
|
109
|
+
error: err.message,
|
|
110
|
+
duration,
|
|
111
|
+
code: err.code,
|
|
112
|
+
signal: err.signal,
|
|
113
|
+
platform: process.platform
|
|
114
|
+
});
|
|
115
|
+
throw err;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
33
119
|
/**
|
|
34
120
|
* Validate command is in allowlist
|
|
35
121
|
* @param {string} cmd - Command to validate
|
|
@@ -1,130 +1,130 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* Prevents path traversal attacks by:
|
|
7
|
-
* - Resolving and validating paths against base directory
|
|
8
|
-
* - Blocking paths that escape base directory
|
|
9
|
-
* - Handling Windows and Unix path formats
|
|
10
|
-
* - Logging blocked attempts for security audit
|
|
11
|
-
*
|
|
12
|
-
* Usage:
|
|
13
|
-
* const { normalizePath, isPathSafe, safeReadFile } = require('./safe-path.cjs');
|
|
14
|
-
* const safePath = normalizePath(process.cwd(), userPath);
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
const path = require('path');
|
|
18
|
-
const fs = require('fs');
|
|
19
|
-
const Logger = require('./logger.cjs');
|
|
20
|
-
const logger = new Logger();
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Normalize and validate a user-provided path against a base directory
|
|
24
|
-
* @param {string} baseDir - Base directory (trusted)
|
|
25
|
-
* @param {string} userPath - User-provided path (untrusted)
|
|
26
|
-
* @returns {string} - Resolved absolute path if safe
|
|
27
|
-
* @throws {Error} If path traversal detected
|
|
28
|
-
*/
|
|
29
|
-
function normalizePath(baseDir, userPath) {
|
|
30
|
-
// Resolve both paths to absolute
|
|
31
|
-
const resolvedBase = path.resolve(baseDir);
|
|
32
|
-
const resolvedUser = path.resolve(baseDir, userPath);
|
|
33
|
-
|
|
34
|
-
// Normalize for comparison (handle Windows backslashes)
|
|
35
|
-
const normalizedBase = resolvedBase + path.sep;
|
|
36
|
-
|
|
37
|
-
// Check if user path is within base directory
|
|
38
|
-
const isWithin =
|
|
39
|
-
resolvedUser === resolvedBase ||
|
|
40
|
-
resolvedUser.startsWith(normalizedBase);
|
|
41
|
-
|
|
42
|
-
if (!isWithin) {
|
|
43
|
-
logger.error('Path traversal detected', {
|
|
44
|
-
baseDir: resolvedBase,
|
|
45
|
-
userPath,
|
|
46
|
-
resolvedUser,
|
|
47
|
-
timestamp: new Date().toISOString()
|
|
48
|
-
});
|
|
49
|
-
throw new Error(`Path traversal detected: ${userPath}`);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return resolvedUser;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Check if a path is safe (within base directory) without throwing
|
|
57
|
-
* @param {string} baseDir - Base directory (trusted)
|
|
58
|
-
* @param {string} userPath - User-provided path (untrusted)
|
|
59
|
-
* @returns {boolean} - True if path is safe
|
|
60
|
-
*/
|
|
61
|
-
function isPathSafe(baseDir, userPath) {
|
|
62
|
-
try {
|
|
63
|
-
normalizePath(baseDir, userPath);
|
|
64
|
-
return true;
|
|
65
|
-
} catch (err) {
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Validate path exists and is safe
|
|
72
|
-
* @param {string} baseDir - Base directory
|
|
73
|
-
* @param {string} userPath - User-provided path
|
|
74
|
-
* @returns {string} - Resolved path if exists and safe
|
|
75
|
-
* @throws {Error} If not found or traversal detected
|
|
76
|
-
*/
|
|
77
|
-
function validatePathExists(baseDir, userPath) {
|
|
78
|
-
const resolvedPath = normalizePath(baseDir, userPath);
|
|
79
|
-
|
|
80
|
-
if (!fs.existsSync(resolvedPath)) {
|
|
81
|
-
logger.warn('Path does not exist', {
|
|
82
|
-
resolvedPath,
|
|
83
|
-
userPath
|
|
84
|
-
});
|
|
85
|
-
throw new Error(`Path not found: ${userPath}`);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return resolvedPath;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Safely read a file (validates path before reading)
|
|
93
|
-
* @param {string} baseDir - Base directory
|
|
94
|
-
* @param {string} userPath - User-provided path
|
|
95
|
-
* @param {string} encoding - File encoding (default: utf-8)
|
|
96
|
-
* @returns {string} - File content
|
|
97
|
-
* @throws {Error} If path unsafe or file not found
|
|
98
|
-
*/
|
|
99
|
-
function safeReadFile(baseDir, userPath, encoding = 'utf-8') {
|
|
100
|
-
const resolvedPath = validatePathExists(baseDir, userPath);
|
|
101
|
-
|
|
102
|
-
logger.debug('Reading file', { resolvedPath, userPath });
|
|
103
|
-
|
|
104
|
-
return fs.readFileSync(resolvedPath, encoding);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Get relative path from base, with validation
|
|
109
|
-
* @param {string} baseDir - Base directory
|
|
110
|
-
* @param {string} fullPath - Full path to convert
|
|
111
|
-
* @returns {string} - Relative path or throws if outside base
|
|
112
|
-
*/
|
|
113
|
-
function toRelativePath(baseDir, fullPath) {
|
|
114
|
-
const resolvedFull = path.resolve(fullPath);
|
|
115
|
-
const resolvedBase = path.resolve(baseDir);
|
|
116
|
-
|
|
117
|
-
if (!isPathSafe(baseDir, resolvedFull)) {
|
|
118
|
-
throw new Error(`Path outside base: ${fullPath}`);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return path.relative(resolvedBase, resolvedFull);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
module.exports = {
|
|
125
|
-
normalizePath,
|
|
126
|
-
isPathSafe,
|
|
127
|
-
validatePathExists,
|
|
128
|
-
safeReadFile,
|
|
129
|
-
toRelativePath
|
|
130
|
-
};
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* EZ Safe Path — Path traversal prevention utility
|
|
5
|
+
*
|
|
6
|
+
* Prevents path traversal attacks by:
|
|
7
|
+
* - Resolving and validating paths against base directory
|
|
8
|
+
* - Blocking paths that escape base directory
|
|
9
|
+
* - Handling Windows and Unix path formats
|
|
10
|
+
* - Logging blocked attempts for security audit
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* const { normalizePath, isPathSafe, safeReadFile } = require('./safe-path.cjs');
|
|
14
|
+
* const safePath = normalizePath(process.cwd(), userPath);
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const path = require('path');
|
|
18
|
+
const fs = require('fs');
|
|
19
|
+
const Logger = require('./logger.cjs');
|
|
20
|
+
const logger = new Logger();
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Normalize and validate a user-provided path against a base directory
|
|
24
|
+
* @param {string} baseDir - Base directory (trusted)
|
|
25
|
+
* @param {string} userPath - User-provided path (untrusted)
|
|
26
|
+
* @returns {string} - Resolved absolute path if safe
|
|
27
|
+
* @throws {Error} If path traversal detected
|
|
28
|
+
*/
|
|
29
|
+
function normalizePath(baseDir, userPath) {
|
|
30
|
+
// Resolve both paths to absolute
|
|
31
|
+
const resolvedBase = path.resolve(baseDir);
|
|
32
|
+
const resolvedUser = path.resolve(baseDir, userPath);
|
|
33
|
+
|
|
34
|
+
// Normalize for comparison (handle Windows backslashes)
|
|
35
|
+
const normalizedBase = resolvedBase + path.sep;
|
|
36
|
+
|
|
37
|
+
// Check if user path is within base directory
|
|
38
|
+
const isWithin =
|
|
39
|
+
resolvedUser === resolvedBase ||
|
|
40
|
+
resolvedUser.startsWith(normalizedBase);
|
|
41
|
+
|
|
42
|
+
if (!isWithin) {
|
|
43
|
+
logger.error('Path traversal detected', {
|
|
44
|
+
baseDir: resolvedBase,
|
|
45
|
+
userPath,
|
|
46
|
+
resolvedUser,
|
|
47
|
+
timestamp: new Date().toISOString()
|
|
48
|
+
});
|
|
49
|
+
throw new Error(`Path traversal detected: ${userPath}`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return resolvedUser;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Check if a path is safe (within base directory) without throwing
|
|
57
|
+
* @param {string} baseDir - Base directory (trusted)
|
|
58
|
+
* @param {string} userPath - User-provided path (untrusted)
|
|
59
|
+
* @returns {boolean} - True if path is safe
|
|
60
|
+
*/
|
|
61
|
+
function isPathSafe(baseDir, userPath) {
|
|
62
|
+
try {
|
|
63
|
+
normalizePath(baseDir, userPath);
|
|
64
|
+
return true;
|
|
65
|
+
} catch (err) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Validate path exists and is safe
|
|
72
|
+
* @param {string} baseDir - Base directory
|
|
73
|
+
* @param {string} userPath - User-provided path
|
|
74
|
+
* @returns {string} - Resolved path if exists and safe
|
|
75
|
+
* @throws {Error} If not found or traversal detected
|
|
76
|
+
*/
|
|
77
|
+
function validatePathExists(baseDir, userPath) {
|
|
78
|
+
const resolvedPath = normalizePath(baseDir, userPath);
|
|
79
|
+
|
|
80
|
+
if (!fs.existsSync(resolvedPath)) {
|
|
81
|
+
logger.warn('Path does not exist', {
|
|
82
|
+
resolvedPath,
|
|
83
|
+
userPath
|
|
84
|
+
});
|
|
85
|
+
throw new Error(`Path not found: ${userPath}`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return resolvedPath;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Safely read a file (validates path before reading)
|
|
93
|
+
* @param {string} baseDir - Base directory
|
|
94
|
+
* @param {string} userPath - User-provided path
|
|
95
|
+
* @param {string} encoding - File encoding (default: utf-8)
|
|
96
|
+
* @returns {string} - File content
|
|
97
|
+
* @throws {Error} If path unsafe or file not found
|
|
98
|
+
*/
|
|
99
|
+
function safeReadFile(baseDir, userPath, encoding = 'utf-8') {
|
|
100
|
+
const resolvedPath = validatePathExists(baseDir, userPath);
|
|
101
|
+
|
|
102
|
+
logger.debug('Reading file', { resolvedPath, userPath });
|
|
103
|
+
|
|
104
|
+
return fs.readFileSync(resolvedPath, encoding);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Get relative path from base, with validation
|
|
109
|
+
* @param {string} baseDir - Base directory
|
|
110
|
+
* @param {string} fullPath - Full path to convert
|
|
111
|
+
* @returns {string} - Relative path or throws if outside base
|
|
112
|
+
*/
|
|
113
|
+
function toRelativePath(baseDir, fullPath) {
|
|
114
|
+
const resolvedFull = path.resolve(fullPath);
|
|
115
|
+
const resolvedBase = path.resolve(baseDir);
|
|
116
|
+
|
|
117
|
+
if (!isPathSafe(baseDir, resolvedFull)) {
|
|
118
|
+
throw new Error(`Path outside base: ${fullPath}`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return path.relative(resolvedBase, resolvedFull);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
module.exports = {
|
|
125
|
+
normalizePath,
|
|
126
|
+
isPathSafe,
|
|
127
|
+
validatePathExists,
|
|
128
|
+
safeReadFile,
|
|
129
|
+
toRelativePath
|
|
130
|
+
};
|