Summary
The basic-ftp Node.js client library contains a path traversal flaw in the downloadToDir() method that allows a malicious FTP server to influence where files are written on the client's filesystem.
By returning directory listings with ../ segments in filenames, a compromised or hostile FTP server can cause files to be stored outside the intended download directory, potentially overwriting or planting files in sensitive paths.
Affected context
This vulnerability affects applications using basic-ftp versions prior to 5.2.0 that call downloadToDir() to recursively download files from an FTP server.
The issue appears when client code assumes the remote server is trustworthy, and relies on downloadToDir() to enforce a local download boundary.
Any scenario where the FTP server is not fully trusted, can be controlled by an attacker, or can be man-in-the-middled increases the practical risk of this bug.
Exploit preconditions
- Attack surface: Client systems running Node.js applications that use
basic-ftpto download from FTP servers. - Required access: Ability to control or impersonate the FTP server, or to tamper with FTP directory listings in transit.
- Trigger point: A call to
downloadToDir()that processes directory listings containing filenames or paths with traversal patterns such as../. - Why exploitation is possible: The client library fails to canonicalize and validate server-supplied paths against the intended download root, allowing traversal segments to influence the final local file path.
Why this happens
The core issue is a missing normalization and validation step when translating server-provided paths into local filesystem paths.
The downloadToDir() method trusts filenames and directory entries supplied by the FTP server, concatenating them with a local base directory without removing or rejecting .. components or absolute path constructs.
This aligns with CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal'), where user- or externally-controlled path input is not constrained to remain within an expected directory boundary.
Potential impact
- Attacker-controlled files could be written outside the designated download directory.
- Existing files in writable paths (e.g., application data, configuration files, logs) could be overwritten.
- Malicious scripts or executables could be planted in locations that may later be executed by users, system services, or deployment tooling.
- Sensitive data integrity could be compromised by tampered assets, leading to downstream security or reliability issues.
General mitigation guidance
Effective mitigation centers on strict path normalization and enforcement of directory boundaries on the client side.
General defensive patterns include:
- Canonicalizing server-supplied paths (e.g., using
path.resolve) and verifying that the result remains under the intended download root before any file operations. - Rejecting or sanitizing path components containing
.., absolute paths, or unusual separators when mapping remote paths to local ones. - Applying least-privilege file-system permissions so the process downloading files can write only to dedicated, non-sensitive directories.
- Isolating downloaded content (e.g., in a sandboxed or temporary directory) and validating files before promoting them into more privileged locations or using them in production flows.
- Treating all data from FTP servers as untrusted, especially when servers are external, shared, or unauthenticated.
How teams detect issues like this
Organizations can investigate and monitor for this class of issue using:
- Static analysis and code scanning: Look for uses of
downloadToDir()or similar file download routines where remote paths are concatenated with local directories without normalization and boundary checks. - Code review practices: Flag any file-write logic that directly uses server-supplied or user-supplied path components, and verify that canonical path comparisons against an allowed root are in place.
- Runtime monitoring: Enable file integrity monitoring or logging on client systems to detect unexpected writes outside designated download directories when downloads occur.
- Policy enforcement: Incorporate security checks into CI pipelines to require path traversal defenses in file handling utilities and to discourage trusting path information from remote services by default.

