-
Fil d’actualités
- EXPLORER
-
Pages
-
Groupes
-
Evènements
-
Reels
-
Blogs
-
Offres
-
Emplois
Path Traversal Vulnerability A BeginnerGuide
A path traversal vulnerability (also called a directory traversal attack) happens when a web application builds a file path from user input without restricting where that path can lead. An attacker adds sequences like ../ to climb out of the intended folder and read files the application never meant to share, such as configuration files, source code, or credentials. The fix is to avoid passing raw input to the file system, validate against an allowlist, canonicalize the final path and confirm it still sits inside an approved base directory.
What Is Path Traversal?
Path traversal is a web security flaw that lets an attacker access files and directories stored outside the folder an application intends to expose. The weakness is catalogued as CWE22 and it sits within the broader Broken Access Control category of modern web risk lists. If you want the wider picture of where this flaw fits among other common risks, the guide to OWASP Top 10 web application security is a useful companion read.
Think of a hotel. A keycard should open your room and the shared gym. A path traversal flaw is like a keycard that also opens the staff office because nobody checked which doors the card was allowed to open. The application is the hotel, the file system is the building and the usersupplied filename is the keycard.They all describe the same core mistake. The application lets a user influence a file location and nothing stops that location from escaping the safe zone.
Why Web Applications Are Exposed to File System Access Problems
Almost every real application touches the file system. It serves downloads, loads images, reads templates, writes logs, stores uploads and imports configuration. Each of those operations needs a path. Whenever any part of that path comes from a request, whether a query parameter, a form field, a header, a cookie, or a URL segment, the application has handed a small piece of control to a stranger.
Here is a typical vulnerable pattern in plain terms. A site offers a document viewer at a URL like /download?file=report.pdf. Behind the scenes the server joins a base folder, say /var/www/documents/, with the value of the file parameter and opens the result. When the input is report.pdf, everything works. When the input is ../../../../etc/passwd, the joined path points far outside the document folder and the server dutifully opens it.The developer never wrote a line of code that says show users the password file. The flaw is an omission; the code never asked whether the final path is still inside the document folder. That is why this class of bug survives so long in mature codebases. Nothing looks broken during normal use.
Absolute File Path vs Relative File Path
To understand the attack, you need two basic ideas.
An absolute file path describes a location starting from the root of the file system. On Unixlike systems it begins with a forward slash, such as /etc/hosts. On Windows it begins with a drive letter, such as C\Windows\win.ini. An absolute path means the same thing no matter where the program is running.
A relative file path describes a location starting from the current working directory. The special entry .. means go up one directory and . means stay here. So ../logs/app.log means leave this folder, enter the sibling folder named logs and open app.log.
|
Concept |
Example |
Meaning |
|
Absolute path (Unix) |
/etc/hosts |
Starts at the file system root |
|
Absolute path (Windows) |
C\Windows\win.ini |
Starts at a drive root |
|
Relative path |
images/logo.png |
Starts at the current directory |
|
Parent reference |
../ |
Moves one directory up |
|
Relative path traversal |
../../secret.txt |
Climbs several levels, then reads |
Both forms matter to attackers. A relative path traversal attack uses ../ sequences to climb from the intended directory. An absolute path traversal attack supplies a full path such as /etc/passwd directly, which works when the application uses the input basis instead of appending it to a base folder.Automated tooling helps too and the overview of application security scanning explains where scanners are strong and where they miss context.
How Path Traversal Attacks Work, Step by Step
Most successful attacks follow the same rhythm. Understanding it helps you both exploit and defend.
Step 1 Find a filehandling parameter. The attacker looks for requests where a value looks like a filename or path. Parameters named file, path, page, template, doc, image, or lang are classic candidates, but so are URL segments such as /static/css/site.css.
Step 2 Establish a baseline. The attacker requests a legitimate file and notes the response, including length, headers and content type. This gives a reference for what working looks like.
Step 3 Inject traversal sequences. The attacker replaces the filename with ../ sequences leading to a known file, usually one that exists on nearly every system of that type.
Step 4 Observe the difference. If the response now contains the contents of the target file, or an error message that mentions it, the application is vulnerable. Even a different error (file not found versus access denied) can reveal that the input reached the file system.
Step 5 Escalate. With read access confirmed, the attacker hunts for valuable files application configuration with database passwords, private keys, environment files, source code and logs. Those finds often unlock deeper compromise.If the server builds the path with simple string concatenation, the operating system resolves the climbs and returns the file. The number of ../ segments needed depends on how deep the base directory sits, so attackers often add extra ones. Extra climbs at the root are harmless because the rootparent is the root itself.
The Dot Dot Slash Attack and Its Variants
The dot dot slash attack is the signature move of directory traversal. Developers who learn about it often respond by blocking the string ../. That reaction is understandable and it is the reason variants exist.
Attackers adapt payloads to defeat naive filters
-
Backslash variants ..\..\windows\win.ini works against Windows based servers and some frameworks treat backslashes as separators even on other systems.
-
URL encoding %2e%2e%2f represents ../. Double encoding, such as %252e%252e%252f, survives one round of decoding.
-
Nested sequences ....// becomes ../ after a filter strips the inner ../ once.
-
Overlong or alternative encodings Some older components accept unusual Unicode representations of dots and slashes.
-
Absolute paths Supplying /etc/passwd directly bypasses any filter that only looks for ...
-
Null byte tricks In older runtimes, a trailing %00 could truncate a forced file extension, letting ../../secret%00.png open secret.
The lesson is simple. Any defense that tries to recognize bad strings is playing a losing game. A defense that checks the resolved location, the final path after the operating system and runtime finishes interpreting it, does not care how the attacker wrote the payload. That idea, called path canonicalization, is the heart of every reliable fix and is covered below.
Sensitive File Exposure What Attackers Go After
Reading arbitrary files is only dangerous because of what those files contain. Common targets include
-
System files such as /etc/passwd and /etc/hosts on Unixlike systems, or win.ini on Windows. These are mostly used to prove the bug exists.
-
Application configuration such as .env, config.php, application.properties, web.config, or settings.py, which frequently hold database credentials and API keys.
-
Source code, which reveals business logic, hidden endpoints and other vulnerabilities. Understanding how reviewers hunt for such issues is the focus of this secure code review guide.
-
Private keys and tokens such as SSH keys, TLS private keys and cloud credentials stored in home directories.
-
Logs and backups, which may contain session identifiers, personal data, or older copies of sensitive files.
A traversal flaw that begins as read one text file often ends as full account takeover, because one leaked credential unlocks the next system.
Path Traversal vs LFI, RFI and Related Flaws
Beginners often mix up traversal with similar sounding bugs. The differences are worth getting straight.
|
Flaw |
What the attacker controls |
Typical outcome |
|
Path traversal |
Which file is read or written |
Disclosure of files, sometimes overwrite |
|
Local File Inclusion (LFI) |
Which local file the app executes or includes |
Disclosure and code execution if the file contains code |
|
Remote File Inclusion (RFI) |
A URL the app fetches and includes |
Remote code execution |
|
IDOR |
An object identifier |
Access to another user record |
|
Command injection |
Part of an operating system command |
Arbitrary command execution |
Local File Inclusion is path traversal closest relative. In LFI the vulnerable function does not merely read the file, it includes it in the page, so any code inside it can run. Traversal is usually the technique used to reach the file and inclusion is what makes the impact worse. Remote File Inclusion goes further by letting an attacker point the include at a file hosted on their own server, which is why modern runtimes disable that behavior by default.
If the application hands user input to a shell command instead of a file API, you are dealing with a different problem. The article on command injection vulnerability explains how that risk differs and why the two are sometimes chained together. Likewise, an object reference flaw lets a user fetch someone else record rather than a system file; see this guide to preventing insecure direct object references for that parallel access control failure.
Path Traversal Security Risks and Business Impact
The risk of a traversal bug depends on what the web server process can read and sometimes what it can write.Confidentiality loss is the baseline. Customer data, internal documents and secrets can all leave the building.Credential theft turns a single read into a chain. A leaked database password, signing key, or cloud token can give the attacker the same power as the application itself.Integrity loss appears when the vulnerable code writes files. Overwriting a configuration file, a startup script, or a webaccessible template can lead to code execution.
Reputation and compliance damage follow when exposed data falls under privacy or payment rules. Regulators and customers care little about how simple the underlying bug was.Because exploitation needs only an HTTP request and often no authentication, traversal flaws are attractive to automated scanners. A vulnerable endpoint is usually found and probed within hours of exposure, not months.A realworld illustration in 2021 of a path traversal flaw in a widely deployed web server (CVE202141773) allowed attackers to map URLs to files outside the document root when certain configuration was present. It was exploited quickly after disclosure, showing that even mature, heavily reviewed software can contain this class of bug.
Path Traversal Prevention Techniques The Short Version
Prevention works best in layers. The full implementation guidance deserves its own article, but a beginner should remember these five ideas.
-
Do not pass user input to file APIs at all when you can avoid it. Store files under generated identifiers and look them up in a database instead of accepting filenames.
-
Validate input against an allowlist. Accept only known filenames, or restrict characters to a safe set such as letters, digits, hyphens and one dot.
-
Canonicalize, then check. Resolve the final absolute path, then confirm it begins with the approved base directory.
-
Apply least privilege. The account running the web application should read only what it needs. A traversal bug cannot expose files the process cannot open.
-
Add defense in depth. Containers, readonly file systems and web application firewall rules reduce blast radius, though none replaces the code fix.
Catching these mistakes before release is far cheaper than responding to an incident. Teams that review code systematically, including code produced by assistants, tend to spot unsafe path handling early; this walkthrough on how to review AIgenerated code shows why generated filehandling snippets deserve extra scrutiny.
Conclusion
A path traversal vulnerability is simple to understand and surprisingly easy to introduce one parameter, one string join and no check on where the final path lands. The attack relies on the file system rules for .., absolute paths and separators, which is why filtering for bad strings fails and checking the resolved location works.
Remember the core chain. User input shapes a path, the path reaches the file system and the application never confirms the result stays inside a safe folder. Break any link by avoiding raw input, validating against an allowlist, canonicalizing and restricting permissions and the attack collapses. Keep practicing in realistic labs, review every filehandling function you write or generate and treat each file path as untrusted until proven otherwise.You can explore guided practice at AppSecMaster and when you want a deeper reference on the topic itself, the dedicated path traversal vulnerability resource goes further into the details.
Frequently Asked Questions (FAQs)
What is a path traversal vulnerability in simple terms?
It is a flaw where an application lets a user choose a file location without limiting the choice to a safe folder. By adding ../ sequences or absolute paths, an attacker can reach files outside the intended directory, such as configuration files, source code, or credentials.
Is directory traversal the same as path traversal?
Yes. Directory traversal, path traversal, dotdot slash attack and file path traversal are different names for the same weakness. Some writers use directory traversal for reading files and path traversal for the broader flaw, but security tools and standards treat them as one issue.
What is the difference between path traversal and local file inclusion?
Path traversal reads or writes files outside an allowed folder. Local file inclusion happens when the application includes and processes a local file, so code inside it can execute. Traversal is often the technique used to reach the file in an LFI attack.
Can a web application firewall stop path traversal attacks?
A firewall can block many common payloads and reduce noise, but it should not be the only defense. Attackers use encodings and unusual separators to evade rules. The dependable fix is in the code to canonicalize the final path and verify it stays inside an approved base directory.
Which files do attackers target first?
They usually start with harmless proof files like /etc/passwd or win.ini, then move to environment files, application configuration, private keys, source code, logs and backups, since those contain secrets that enable further compromise.
- Art
- Causes
- Crafts
- Dance
- Drinks
- Film
- Fitness
- Food
- Jeux
- Gardening
- Health
- Domicile
- Literature
- Music
- Networking
- Autre
- Party
- Religion
- Shopping
- Sports
- Theater
- Wellness