nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Security] File tools should use existing path boundary validation

Open JimStenstrom opened this issue 11 months ago • 1 comments

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.txt or ~/secret.txt

Proposed Fix

  1. Move isValidFilePath and resolveFilePath to a shared utility (e.g., source/utils/path-validation.ts)
  2. 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.tsx
  • source/tools/string-replace.tsx
  • source/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

JimStenstrom avatar Dec 26 '25 05:12 JimStenstrom

On this one

will-lamerton avatar Dec 26 '25 13:12 will-lamerton