Issue
Path traversal, also called directory traversal, occurs when an application uses untrusted input to build a file path but fails to keep the resolved path within the directory the application intended to use. A path such as ../ refers to a parent directory on many systems. Absolute paths and platform-specific separators can also matter. CWE-22
For example, a download endpoint might accept a filename and combine it with a directory containing downloadable files. If the application does not safely resolve and restrict the resulting path, a crafted filename may point somewhere else on the server.
Common causes include:
- Accepting a filesystem path when the feature only needs a file identifier.
- Removing suspicious strings instead of validating the final resolved path.
- Decoding or normalizing input inconsistently, or decoding it more than once.
- Checking that a path begins with the allowed directory as plain text, rather than checking its path components.
- Following a symbolic link that points outside the allowed directory.
The same class of weakness can affect read, write, rename, or delete operations. Unsafe archive extraction is a related file-handling risk: an archive entry’s name can escape the extraction directory if the extractor does not enforce a safe destination.
Examples
A normal request might look like this:
GET /download?name=report.txt HTTP/1.1Host: app.example.test
In a controlled test, compare it with a request for a marker outside the download directory:
GET /download?name=..%2Ftest-fixtures%2Fprivate-marker.txt HTTP/1.1Host: app.example.test
If the application’s download directory is /srv/app/public/, the test fixture might be /srv/app/test-fixtures/private-marker.txt. A vulnerable implementation could resolve the second request to the fixture and return its known content:
HTTP/1.1 200 OKContent-Type: text/plain PATH-TEST-OK
That response would confirm that this request reached the controlled file, assuming the marker content is unique and known. It would not show that other files are accessible or that the application supports arbitrary writes.
A secure implementation should reject the request or return a generic not-found response without the marker content. A 404 or 400 response by itself is not proof that the server safely contained the path; verify the result against the controlled fixture.
Impact
The impact depends on what the affected operation can do and what files the application process can access.
A read flaw may expose files if the application can read them and returns their contents to the requester. A write or delete flaw may change or remove files if the application has those permissions. Code execution is possible only in additional circumstances—for example, if an attacker can write to a location where the application or another process will execute or load the file.
A traversal-looking input, a scanner alert, or an error message is only an indicator. It does not prove that a file was accessed or changed. Confirm impact with a harmless, known test file or another controlled effect.
Testing
1. Check whether file reads escape the intended directory
Identify a file parameter and compare a normal file with a path that moves outside the allowed folder. Verify the returned content against a known test file.
Example: A file viewer should serve files only from its configured directory. A different status or error alone does not prove a file was read.
GET /view?file=..%2F..%2Ftest-marker.txt HTTP/1.1
Host: app.example.test
2. Check alternate path locations and encodings
Repeat with relevant query fields, form fields, cookies, and path encodings the application accepts. Decode and normalize the request as the server would before comparing behavior.
Example: The same file name may be accepted through a cookie or a request body. Test each actual input separately.
GET /view HTTP/1.1
Host: app.example.test
Cookie: file=..%2F..%2Ftest-marker.txt
3. Check write and extraction paths separately
Review upload names, archive extraction, and file operations for paths that may escape the intended directory. Confirm any change using a dedicated test file and actual storage state.
Example: A read issue does not automatically prove file write access. Report each demonstrated operation separately.
4. Check file authorization separately
Request a file owned by a second test account using the application’s normal file identifier. Compare the response with the documented sharing rules.
Example: A file can remain within the allowed directory but belong to another user. Access to that file is an authorization issue rather than proof of traversal.
Browser and platform checks
A browser may encode characters or normalize a URL before sending it. For tests involving encoded paths, inspect the actual HTTP request with a proxy or another authorized client, and compare it with the value the application receives after framework parsing. Test slash and backslash behavior only when relevant to the server’s operating system and filesystem API. A browser-side result alone cannot establish how the server resolved a path.
Common false positives
- A scanner flags a parameter because it resembles a filename, but the application never uses it in a filesystem operation.
- The response reflects the supplied path or displays it in an error, without reading the referenced file.
- A generic error or response-size change is mistaken for file access.
- A test fails because the marker file does not exist or the application process cannot read it; that result does not prove the path handling is safe.
- One endpoint rejects a payload, but another endpoint, method, or file operation has not been assessed.
Mitigation
Primary fixes
- Use an application-controlled identifier. Map a validated file ID to a stored file on the server. This avoids letting the requester choose a filesystem path. Check that the current user is authorized to access the mapped file. OWASP recommends application-generated storage names and mapping public retrieval through an application handler. OWASP File Upload Cheat Sheet
- If paths are necessary, validate and contain the resolved path. Decode input once into the form the application will use, validate it against the feature’s allowed values, resolve the candidate path using the platform’s filesystem APIs, and confirm that it remains beneath the intended base directory. Compare path components or use a safe relative-path operation; a plain string-prefix check can accept a sibling such as
/srv/app/public-old/. - Account for symbolic links and race conditions. Resolving a path and checking it, then opening it later, can be unsafe if another process can change a link or directory in between. Where the platform supports it, use directory-relative file APIs and options that prevent following links outside the approved directory.
- Apply the same controls to every operation. Reads, uploads, exports, renames, deletes, and archive extraction each need a safe destination check. For archives, validate each entry’s resolved destination before writing it.
Additional defenses
- Run the service with only the filesystem permissions it needs. This limits what a successful traversal could reach; it does not fix unsafe path construction.
- Store uploaded files outside the web root when practical, and generate storage names on the server.
- Keep error responses generic so they do not disclose internal paths or file contents.
- A web application firewall can provide an additional detection or blocking layer, but it may miss variants and should not replace a code fix. CWE-22 mitigation guidance
Retest
Repeat the normal request and the controlled traversal tests for each affected operation. Confirm that authorized files still work, the marker cannot be read or changed through the feature, and access checks still prevent one test account from retrieving another account’s files. Review the path-resolution code and filesystem permissions as well as the HTTP response.