Path Traversal
Overview
Path traversal (or directory traversal) is a vulnerability that allows an attacker to read or write arbitrary files on the server that is running an application.
For example, consider an application that loads images via some HTML like the following:
The /load
URL takes the filename
parameter and returns the contents of the specified file. The image files themselves are stored on disk in the location /var/www/images/
. To return an image, the application:
appends the requested filename
218.png
to the base directory/var/www/images/
,uses a filesystem API to read the contents of the
/var/www/images/218.png
file.
This behaviour can be abused by an attacker. An attacker can request the following URL to retrieve an arbitrary file from the server:
This causes the application to read from the following file path:
As a result, the application will return to an attacker a content of the /etc/passwd
.
You can find more details at PortSwigger Web Security Academy: Directory traversal.
This page contains recommendations for the implementation of protection against path traversal attacks.
General
Do not allow a user to control data that is passed to the file system API.
Generate random folder/file names using a UUID or random generator instead of relying on user-provided names, see the Cryptography: Universal Unique Identifier (UUID) and Cryptography: Random Generators pages.
If data can be controlled by a user, implement comprehensive input validation for all data that is passed to the file system API, see the Input Validation page.
Use an allow list of paths to validate user-provided data if possible.
Include only alphanumeric characters in an allow list of characters.
If it is necessary to use special characters such as
.
or/
, prevent the use of combinations represented as regular expressions below.Do not rely solely on a block list validation, it can be bypassed in many cases.
Make sure the canonicalized path starts with the expected base directory, see the Implementing the canonical path validation section.
Methods used to construct file paths can have non-intuitive behaviour. Make sure the methods you use work as expected with data like this:
Use a sandbox to obtain or save data.
Implementing the canonical path validation
Use the path.Join function to get a canonical path.
References
Last updated