What Are Path Traversal and LFI? How to Prevent Them
Path traversal is when an unchecked file name lets the attacker reach unauthorized files, and via LFI can lead to code execution. How it works, a table of LFI and RFI, why filtering is not enough, allow list defense and scanning with KAOS.
Quick answer: Path Traversal is when an application takes a file name from the user and passes it to the file system without enough checking, letting the attacker reach files on the server they should not access. The attacker adds a parent directory sequence to the file name to reach outside the application's folder, for example to system configuration files or password files. This flaw can go as far as remote file reading and sometimes code execution via LFI (Local File Inclusion). The root fix: never add the user's file name directly to the path; fix the allowed directory and file list on the server side.
Features like file download, preview, language file loading and theme selection are very common, and most take a file name from the user. If you do not handle that name safely, the attacker starts navigating your application's directory structure to read any file. This article explains how path traversal and LFI arise, their real impact and proper defense.
How path traversal works
To serve a file, the application usually joins a base directory with the user provided file name. For example "files" plus "report.pdf" equals "files/report.pdf". The problem is when the user provides a parent directory sequence instead of a file name. Using this sequence repeatedly, they climb out of the base directory and reach elsewhere in the file system.
Targets usually are:
- System configuration and password files.
- The application's source code and secret keys. Leaked API keys can be captured this way too.
- Session files and logs.
Path traversal, LFI and RFI
These three terms are often confused:
| Term | What it does | Result |
|---|---|---|
| Path Traversal | Reads an unauthorized file | Information disclosure |
| LFI (local file inclusion) | Executes a file on the server | Code execution risk |
| RFI (remote file inclusion) | Executes a remote file | Full takeover |
Path traversal is usually limited to reading files, but if the application executes the file it reads (LFI), the impact rises from information disclosure to code execution. So a seemingly simple file read flaw must not be underestimated.
Why filtering alone is not enough
Many developers think searching for and removing the dangerous sequence solves it. But this blacklist approach is weak:
- Encoding variations. The same sequence can be written with different character encodings and bypass a simple filter.
- Double encoding. Encoding the sequence once more confuses the filter.
- Nested removal. Filters that remove a single pass from the middle are bypassed when the remaining parts recombine.
So the safe solution is to define the allowed, not to search for the dangerous.
Proper defense
1. Do not leave the file name to the user
The safest method is to never ask the user for a free text file name. Instead keep a file list on the server and let the user pick only an id or option. This also requires you to manage IDOR risk.
2. Resolve the path and verify the root
If user input is unavoidable, the joined path must be fully resolved and the result verified server side to stay within the allowed base directory. Any request that leaves the directory is rejected.
3. Allow list and strict validation
The file name must match only the expected narrow character set and extension. Path separators and parent directory sequences must never be accepted.
4. Least privilege
The application account's file system access should be limited to only the folder it needs. So even if a flaw exists, the reachable file area is narrowed.
Directory traversal scanning with KAOS
We scan for these file access flaws at scale with KAOS, DSET's AI powered autonomous security scanning engine. KAOS discovers all endpoints that take a file, tries different encoding and bypass variations, and when it can access a file without authorization it verifies this in a controlled way and reports only proven findings. So you see whether endpoints you thought were safe behind a simple filter are actually open, without false positive noise. KAOS works alongside the human expert and documents every finding with a working proof.
Frequently asked questions
Does path traversal only read files? Mostly yes, but if the read file is executed (LFI) or writable, the impact can rise to code execution. So it must be taken seriously.
Is removing dangerous characters enough? No. Encoding and double encoding variations bypass simple filters. The safe way is to verify that the resolved path stays within the allowed directory.
Can this flaw exist on a static file site? Any endpoint that selects a file with user input carries risk. Fully static content that takes no user input is low risk.
What is the difference between path traversal and IDOR? Path traversal targets the file system path, IDOR targets an application object id. Both lead to unauthorized access but at different layers.
Sources
- OWASP, Path Traversal: https://owasp.org/www-community/attacks/Path_Traversal
- OWASP, File Inclusion: https://owasp.org
- MITRE, CWE 22: https://cwe.mitre.org/data/definitions/22.html
- PortSwigger Web Security Academy, Directory traversal: https://portswigger.net/web-security/file-path-traversal
To detect path traversal and file inclusion flaws in your application with a working proof from KAOS and our expert team, contact DSET. From our Ankara Hacettepe Teknokent laboratory we provide penetration testing and secure code review.
Kimliğinizi doğrulayın
Yetkilendirilmiş erişim alanı. Tüm giriş denemeleri kayıt altına alınır.