Skip to content

fix: prevent path traversal via directory prefix collision in _joinDirectoryName#1062

Open
vnykmshr wants to merge 1 commit intonode-formidable:masterfrom
vnykmshr:fix/join-directory-name-traversal
Open

fix: prevent path traversal via directory prefix collision in _joinDirectoryName#1062
vnykmshr wants to merge 1 commit intonode-formidable:masterfrom
vnykmshr:fix/join-directory-name-traversal

Conversation

@vnykmshr
Copy link

Fixes #1061

The startsWith check in _joinDirectoryName() compared string prefixes, not filesystem paths. When uploadDir lacks a trailing separator (the default — os.tmpdir() never includes one), sibling directories sharing the same prefix bypass the traversal check.

Changes:

  • Use path.resolve() to normalize both paths before comparison
  • Append path.sep to the resolved directory, ensuring the path is inside the directory
  • Handle the edge case where the resolved path equals the directory itself

Tests added:

  • Standard traversal blocked (regression)
  • Sibling directory prefix collision blocked (the fix)
  • Valid subdirectory within uploadDir allowed (happy path)
  • Name resolving to uploadDir itself blocked (edge case)

…rectoryName

The startsWith check compared string prefixes, not filesystem paths.
When uploadDir lacks a trailing separator (the default), sibling
directories sharing the same prefix bypass the check.

Use path.resolve + path.sep to ensure the resolved path is inside the
upload directory, not just sharing a string prefix.

Fixes node-formidable#1061
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.

_joinDirectoryName traversal check bypassable via directory prefix collision

1 participant