@probelabs/probe 0.6.0-rc174 → 0.6.0-rc176

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.
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Path validation utilities for the probe package
3
+ * @module utils/path-validation
4
+ */
5
+
6
+ import path from 'path';
7
+ import { promises as fs } from 'fs';
8
+
9
+ /**
10
+ * Validates and normalizes a path to be used as working directory (cwd).
11
+ *
12
+ * Security considerations:
13
+ * - Normalizes path to resolve '..' and '.' components
14
+ * - Returns absolute path to prevent ambiguity
15
+ * - Does NOT restrict access to specific directories (that's the responsibility
16
+ * of higher-level components like ProbeAgent with allowedFolders)
17
+ *
18
+ * @param {string} inputPath - The path to validate
19
+ * @param {string} [defaultPath] - Default path to use if inputPath is not provided
20
+ * @returns {Promise<string>} Normalized absolute path
21
+ * @throws {Error} If the path is invalid or doesn't exist
22
+ */
23
+ export async function validateCwdPath(inputPath, defaultPath = process.cwd()) {
24
+ // Use default if not provided
25
+ const targetPath = inputPath || defaultPath;
26
+
27
+ // Normalize and resolve to absolute path
28
+ // This handles '..' traversal and makes the path unambiguous
29
+ const normalizedPath = path.normalize(path.resolve(targetPath));
30
+
31
+ // Verify the path exists and is a directory
32
+ try {
33
+ const stats = await fs.stat(normalizedPath);
34
+ if (!stats.isDirectory()) {
35
+ throw new Error(`Path is not a directory: ${normalizedPath}`);
36
+ }
37
+ } catch (error) {
38
+ if (error.code === 'ENOENT') {
39
+ throw new Error(`Path does not exist: ${normalizedPath}`);
40
+ }
41
+ throw error;
42
+ }
43
+
44
+ return normalizedPath;
45
+ }
46
+
47
+ /**
48
+ * Validates a path option without requiring it to exist.
49
+ * Use this for paths that might be created or are optional.
50
+ *
51
+ * @param {string} inputPath - The path to validate
52
+ * @param {string} [defaultPath] - Default path to use if inputPath is not provided
53
+ * @returns {string} Normalized absolute path
54
+ */
55
+ export function normalizePath(inputPath, defaultPath = process.cwd()) {
56
+ const targetPath = inputPath || defaultPath;
57
+ return path.normalize(path.resolve(targetPath));
58
+ }