Skip to content

fix(vscode): add path validation to ACP writeTextFile handler#2305

Closed
ossaidqadri wants to merge 1 commit intoQwenLM:mainfrom
ossaidqadri:fix/acp-writeTextFile-path-validation
Closed

fix(vscode): add path validation to ACP writeTextFile handler#2305
ossaidqadri wants to merge 1 commit intoQwenLM:mainfrom
ossaidqadri:fix/acp-writeTextFile-path-validation

Conversation

@ossaidqadri
Copy link
Copy Markdown
Contributor

@ossaidqadri ossaidqadri commented Mar 12, 2026

TLDR

Fixes #2294 - Adds path validation to the ACP writeTextFile handler in the VSCode extension to prevent invalid path errors when creating new files.

Changes:

  • Validate path parameter is a non-empty string before processing
  • Trim whitespace from paths to handle common input errors
  • Detect and reject paths containing null bytes (security improvement)
  • Provide specific, actionable error messages for different error codes (EINVAL, EACCES, ENOSPC, EISDIR)
  • Add comprehensive test coverage (10 new test cases)

Dive Deeper

Problem

When using ACP's writeTextFile to create a new file that doesn't exist, the operation was failing with an invalid path error. The root cause was missing input validation in the handleWriteTextFile method.

Solution

Added comprehensive path validation in packages/vscode-ide-companion/src/services/acpFileHandler.ts:

  1. Type and emptiness check: Ensures path is a non-empty string
  2. Whitespace trimming: Handles paths with leading/trailing whitespace
  3. Null byte detection: Prevents path injection attacks via null bytes
  4. Specific error handling: Different error codes get actionable messages:
    • EINVAL: Invalid path format
    • EACCES: Permission denied
    • ENOSPC: No space left on device
    • EISDIR: Target is a directory

Testing

All 16 tests pass including 10 new test cases covering:

  • Empty path rejection
  • Whitespace-only path rejection
  • Null byte path rejection
  • Path trimming behavior
  • Specific error code handling

Reviewer Test Plan

  1. Pull the branch and run the VSCode extension in development mode
  2. Test file creation via ACP:
    • Create a new file in an existing directory
    • Create a new file in a non-existent directory (should create parent dirs)
    • Try to create a file with an empty path (should fail with clear error)
    • Try to create a file with whitespace-only path (should fail)
  3. Run tests: bunx vitest run packages/vscode-ide-companion/src/services/acpFileHandler.test.ts
  4. Verify build: bun run build in vscode-ide-companion package

Testing Matrix

macOS Windows Linux
npm run
npx
Docker

Linked issues

Fixes #2294

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

// Validate path parameter
if (!params.path || typeof params.path !== 'string') {
const error = new Error(
`Invalid path: path must be a non-empty string, received ${JSON.stringify(params.path)}`,
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON.stringify(params.path) can itself throw (e.g., when params.path is a bigint), which would replace the intended validation error with a different exception. Use a non-throwing representation for the received value (e.g., String(params.path) or a safe-stringify helper that handles BigInt/circular structures) so the validation reliably throws the intended "Invalid path" error.

Suggested change
`Invalid path: path must be a non-empty string, received ${JSON.stringify(params.path)}`,
`Invalid path: path must be a non-empty string, received ${String(params.path)}`,

Copilot uses AI. Check for mistakes.
ossaidqadri added a commit to ossaidqadri/qwen-code that referenced this pull request Mar 12, 2026
- Use String() instead of JSON.stringify() to avoid throwing on bigint
- Preserve original error details using { cause: error } option
- Add tests for non-string path values (number, null, undefined)

Addresses Copilot review comments on PR QwenLM#2305
@ossaidqadri
Copy link
Copy Markdown
Contributor Author

Review Comments Resolved ✅

All 3 Copilot review comments have been addressed:

  1. JSON.stringify pitfall - Changed to String(params.path) which never throws
  2. Preserve original error - Added { cause: error } to all Error constructions
  3. Missing test cases - Added tests for non-string path values (number, null, undefined)

All changes have been pushed and tests pass (19/19).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Collaborator

@Mingholy Mingholy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yiliang114
Copy link
Copy Markdown
Collaborator

@ossaidqadri Thank you for your contribution. It seems that there is still a small conflict. Can you help me solve it again?

@ossaidqadri ossaidqadri force-pushed the fix/acp-writeTextFile-path-validation branch from e14d3b5 to 69043d1 Compare March 12, 2026 14:57
@ossaidqadri ossaidqadri requested a review from yiliang114 as a code owner March 12, 2026 14:57
@ossaidqadri
Copy link
Copy Markdown
Contributor Author

@yiliang114 I've resolved the merge conflict by rebasing the branch on the latest upstream/main. The PR now includes:

  • Path type and emptiness validation
  • Whitespace trimming
  • Null byte detection (security)
  • Specific error handling (EINVAL, EACCES, ENOSPC, EISDIR)
  • Error preservation with { cause: error }
  • 16 comprehensive tests covering all edge cases

The branch is now up to date with main and should merge cleanly. Could you please try merging again?

Thanks for reviewing!

@ossaidqadri
Copy link
Copy Markdown
Contributor Author

@yiliang114 reminder to look on this

@tanzhenxin
Copy link
Copy Markdown
Collaborator

We reproduced the issue and found that the "invalid path" on the Zed editor side was caused by permission check of the editor itself. It is by design, no need to fix. The other issue has been resolved in another PR already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ACP writeTextFile fails with "invalid path" when target file does not exist

5 participants