nanocoder
nanocoder copied to clipboard
[Security] File tools should use existing path boundary validation
Description
File manipulation tools (write_file, string_replace) do not validate that file paths stay within the project directory, but the codebase already has validation functions that can be reused.
Existing Validation (file-mention-parser.ts)
// Validates path format - rejects .., absolute paths, null bytes
export function isValidFilePath(filePath: string): boolean
// Resolves path and ensures it stays within project directory
export function resolveFilePath(filePath: string, cwd: string): string
Current Tool Behavior
write_file: Only blocks system dirs (/etc,/sys, etc.)string_replace: No path validation at all- Both accept paths like
../other-project/file.txtor~/secret.txt
Proposed Fix
- Move
isValidFilePathandresolveFilePathto a shared utility (e.g.,source/utils/path-validation.ts) - Update tool validators to use these functions:
// In write-file validator:
import { resolveFilePath } from '@/utils/path-validation';
const writeFileValidator = async (args) => {
try {
resolveFilePath(args.path, process.cwd());
} catch (error) {
return { valid: false, error: error.message };
}
// ... existing system dir checks
};
Affected Tools
source/tools/write-file.tsxsource/tools/string-replace.tsxsource/tools/read-file.tsx(verify)
Additional Context
- [x] I have searched existing issues to ensure this is not a duplicate
- [x] Existing validation code can be reused
On this one