Make FS paths configurable

This commit is contained in:
Alex Gleason committed 2025-10-23 14:50:05 -05:00
1 parent f89a481c9c
commit d75bd386aa
43 files changed
+393 -124

No files matched your search

+52 -16
View File
@@ -15,12 +15,23 @@ export function isAbsolutePath(path: string): boolean {
* Check if write operations are allowed for the given path
*
* Write operations are only allowed in:
* - /tmp/ directory and its subdirectories
* - /projects/ directory and its subdirectories (project directories)
* - Configured tmp directory and its subdirectories
* - Configured projects directory and its subdirectories (project directories)
* - Relative paths (always allowed, regardless of where they resolve to)
* - Absolute paths that resolve to within the current working directory
*
* @param path - The path to check
* @param cwd - Current working directory (optional)
* @param options - Optional configuration for allowed paths
*/
export function isWriteAllowed(path: string, cwd?: string): boolean {
export function isWriteAllowed(
path: string,
cwd?: string,
options?: {
tmpPath?: string;
projectsPath?: string;
}
): boolean {
// Always allow relative paths (this follows traditional Unix security model)
if (!isAbsolutePath(path)) {
return true;
@@ -29,13 +40,17 @@ export function isWriteAllowed(path: string, cwd?: string): boolean {
// For absolute paths, apply restrictions
const absolutePath = normalize(path);
// Allow writes to /tmp/ and its subdirectories
if (absolutePath.startsWith('/tmp/') || absolutePath === '/tmp') {
// Get configured paths with defaults
const tmpPath = options?.tmpPath || '/tmp';
const projectsPath = options?.projectsPath || '/projects';
// Allow writes to tmp directory and its subdirectories
if (absolutePath.startsWith(tmpPath + '/') || absolutePath === tmpPath) {
return true;
}
// Allow writes to /projects/ and its subdirectories (project directories)
if (absolutePath.startsWith('/projects/') || absolutePath === '/projects') {
// Allow writes to projects directory and its subdirectories (project directories)
if (absolutePath.startsWith(projectsPath + '/') || absolutePath === projectsPath) {
return true;
}
@@ -57,14 +72,24 @@ export function isWriteAllowed(path: string, cwd?: string): boolean {
* @param path - The path to validate (can be absolute or relative)
* @param operationName - Name of the operation for error messages (e.g., 'touch', 'mkdir')
* @param cwd - Current working directory for context in error messages
* @param options - Optional configuration for allowed paths
* @throws Error if the path is not allowed for write operations
*/
export function validateWritePath(path: string, operationName: string, cwd?: string): void {
if (!isWriteAllowed(path, cwd)) {
export function validateWritePath(
path: string,
operationName: string,
cwd?: string,
options?: {
tmpPath?: string;
projectsPath?: string;
}
): void {
if (!isWriteAllowed(path, cwd, options)) {
const tmpPath = options?.tmpPath || '/tmp';
const cwdInfo = cwd ? `\n\nCurrent working directory: ${cwd}` : '';
throw new Error(
`${operationName}: write access denied to ${path}. ` +
`Write operations are only allowed in project directories and /tmp/${cwdInfo}`
`Write operations are only allowed in project directories and ${tmpPath}${cwdInfo}`
);
}
}
@@ -75,23 +100,34 @@ export function validateWritePath(path: string, operationName: string, cwd?: str
* @param path - The path that was denied
* @param toolName - Name of the tool/command for context
* @param cwd - Current working directory for context
* @param options - Optional configuration for allowed paths
* @returns Formatted error message
*/
export function createWriteAccessDeniedError(path: string, toolName?: string, cwd?: string): string {
export function createWriteAccessDeniedError(
path: string,
toolName?: string,
cwd?: string,
options?: {
tmpPath?: string;
projectsPath?: string;
}
): string {
const prefix = toolName ? `❌ Write access denied to "${path}".` : `Write access denied to ${path}.`;
const cwdInfo = cwd ? `\n\nCurrent working directory: ${cwd}` : '';
const tmpPath = options?.tmpPath || '/tmp';
const projectsPath = options?.projectsPath || '/projects';
return (
`${prefix}\n\n` +
`Write operations are only allowed in:\n` +
`- Current working directory and subdirectories\n` +
`- Project directories (/projects/ and subdirectories)\n` +
`- Temporary directory (/tmp/ and subdirectories)\n` +
`- Project directories (${projectsPath}/ and subdirectories)\n` +
`- Temporary directory (${tmpPath}/ and subdirectories)\n` +
`- Relative paths (resolved relative to current directory)\n\n` +
`💡 Examples of allowed paths:\n` +
`- "src/index.ts" (relative to current directory)\n` +
`- "/projects/my-project/src/file.js" (absolute project path)\n` +
`- "/tmp/temp-file.txt" (temporary files)\n` +
`- "/tmp/workspace/file.js" (temporary workspace)${cwdInfo}`
`- "${projectsPath}/my-project/src/file.js" (absolute project path)\n` +
`- "${tmpPath}/temp-file.txt" (temporary files)\n` +
`- "${tmpPath}/workspace/file.js" (temporary workspace)${cwdInfo}`
);
}