Skip to content

fix: path traversal vulnerability in listDirectory (#463)#469

Open
Jah-yee wants to merge 1 commit intoCodebuffAI:mainfrom
Jah-yee:fix/list-directory-path-traversal
Open

fix: path traversal vulnerability in listDirectory (#463)#469
Jah-yee wants to merge 1 commit intoCodebuffAI:mainfrom
Jah-yee:fix/list-directory-path-traversal

Conversation

@Jah-yee
Copy link

@Jah-yee Jah-yee commented Mar 10, 2026

Summary

Root Cause

The original check !resolvedPath.startsWith(projectPath) fails when:

  • projectPath = /home/user/project
  • directoryPath = ../project-evil
  • resolvedPath = /home/user/project-evil
  • /home/user/project-evil.startsWith(/home/user/project) = true (incorrectly allows)

Fix

if (
  !resolvedPath.startsWith(projectPath + path.sep) &&
  resolvedPath !== projectPath
) {

This matches the pattern already used in code-search.ts (lines 52-53).

Testing

The fix ensures:

  1. Direct child directories are accessible (e.g., src, ../sibling)
  2. Parent directories are blocked (e.g., .., ../../etc)
  3. Sibling directories with shared prefix are blocked (e.g., ../project-evil)
  4. Project root itself can be listed

Fixes security vulnerability CodebuffAI#463

The original check using startsWith(projectPath) could be bypassed
with sibling directories that share a prefix with the project path.

Example: projectPath=/home/user/project, directoryPath=../project-evil
resolves to /home/user/project-evil which passes startsWith('/home/user/project')
because 'project-evil' starts with 'project'.

This fix adds path.sep to ensure we're checking for proper directory
boundary, and also checks for exact match to allow listing the project
root itself. This matches the pattern already used in code-search.ts.
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.

1 participant