CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
Also known as: Path traversal, Directory traversal, Path transversal
The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
Last updated
Overview
Many file operations are intended to take place within a restricted directory. By using special elements such as ".." and "/" separators, attackers can escape outside of the restricted location to access files or directories that are elsewhere on the system. One of the most common special elements is the "../" sequence, which in most modern operating systems is interpreted as the parent directory of the current location. This is referred to as relative path traversal. Path traversal also covers the use of absolute pathnames such as "/usr/local/bin" to access unexpected files. This is referred to as absolute path traversal.
Real-world CVEs
4,697 recorded CVEs are caused by CWE-22 (Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')), including 71 in CISA's KEV (Known Exploited Vulnerabilities) catalog. KEVs are shown first. 1,339 new CWE-22 CVEs have been recorded so far in 2026 (1,027 in 2025).
- CVE-2026-48282CISA KEV
ColdFusion | Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') (CWE-22)
Critical · CVSS 10.0 · EPSS 98th2026-06-30 - CVE-2026-34909CISA KEVCritical · CVSS 10.0 · EPSS 81th2026-05-22
- CVE-2023-47246CISA KEVCritical · CVSS 10.0 · EPSS 100th2023-11-10
- CVE-2021-41773CISA KEV
Path traversal and file disclosure vulnerability in Apache HTTP Server 2.4.49
Critical · CVSS 10.0 · EPSS 100th2021-10-05 - CVE-2021-27065CISA KEV
Microsoft Exchange Server Remote Code Execution Vulnerability
Critical · CVSS 10.0 · EPSS 100th2021-03-02 - CVE-2021-21972CISA KEVCritical · CVSS 10.0 · EPSS 100th2021-02-24
- CVE-2020-5902CISA KEVCritical · CVSS 10.0 · EPSS 100th2020-07-01
- CVE-2019-16278CISA KEVCritical · CVSS 10.0 · EPSS 100th2019-10-14
- CVE-2022-26500CISA KEVCritical · CVSS 9.4 · EPSS 92th2022-03-17
- CVE-2021-38163CISA KEVCritical · CVSS 9.4 · EPSS 98th2021-09-14
- CVE-2019-3398CISA KEVCritical · CVSS 9.4 · EPSS 100th2019-04-18
- CVE-2025-4632CISA KEVCritical · CVSS 9.3 · EPSS 98th2025-05-13
Showing 12 of 4,697 recorded CWE-22 CVEs. Track new ones as they are published and get AI-written analysis and fixes.
Monitor CWE-22 vulnerabilitiesCommon consequences
What can happen when CWE-22 is exploited.
Execute Unauthorized Code or Commands
Affects: Integrity, Confidentiality, Availability
The attacker may be able to create or overwrite critical files that are used to execute code, such as programs or libraries.
Modify Files or Directories
Affects: Integrity
The attacker may be able to overwrite or create critical files, such as programs, libraries, or important data. If the targeted file is used for a security mechanism, then the attacker may be able to bypass that mechanism. For example, appending a new account at the end of a password file may allow an attacker to bypass authentication.
Read Files or Directories
Affects: Confidentiality
The attacker may be able read the contents of unexpected files and expose sensitive data. If the targeted file is used for a security mechanism, then the attacker may be able to bypass that mechanism. For example, by reading a password file, the attacker could conduct brute force password guessing attacks in order to break into an account on the system.
DoS: Crash, Exit, or Restart
Affects: Availability
The attacker may be able to overwrite, delete, or corrupt unexpected critical files such as programs, libraries, or important data. This may prevent the product from working at all and in the case of protection mechanisms such as authentication, it has the potential to lock out product users.
How it happens
When it is introduced
Typically introduced during these phases of the software lifecycle.
Applies to
Technologies
How to prevent it
Practical mitigations for CWE-22, grouped by where in the lifecycle they apply.
Assume all input is malicious. Use an "accept known good" input validation strategy, i.e., use a list of acceptable inputs that strictly conform to specifications. Reject any input that does not strictly conform to specifications, or transform it into something that does.
When performing input validation, consider all potentially relevant properties, including length, type of input, the full range of acceptable values, missing or extra inputs, syntax, consistency across related fields, and conformance to business rules. As an example of business rule logic, "boat" may be syntactically valid because it only contains alphanumeric characters, but it is not valid if the input is only expected to contain colors such as "red" or "blue."
Do not rely exclusively on looking for malicious or malformed inputs. This is likely to miss at least one undesirable input, especially if the code's environment changes. This can give attackers enough room to bypass the intended validation. However, denylists can be useful for detecting potential attacks or determining which inputs are so malformed that they should be rejected outright.
When validating filenames, use stringent allowlists that limit the character set to be used. If feasible, only allow a single "." character in the filename to avoid weaknesses such as CWE-23, and exclude directory separators such as "/" to avoid CWE-36. Use a list of allowable file extensions, which will help to avoid CWE-434.
Do not rely exclusively on a filtering mechanism that removes potentially dangerous characters. This is equivalent to a denylist, which may be incomplete (CWE-184). For example, filtering "/" is insufficient protection if the filesystem also supports the use of "\" as a directory separator. Another possible error could occur when the filtering is applied in a way that still produces dangerous data (CWE-182). For example, if "../" sequences are removed from the ".../...//" string in a sequential fashion, two instances of "../" would be removed from the original string, but the remaining characters would still form the "../" string.
For any security checks that are performed on the client side, ensure that these checks are duplicated on the server side, in order to avoid CWE-602. Attackers can bypass the client-side checks by modifying values after the checks have been performed, or by changing the client to remove the client-side checks entirely. Then, these modified values would be submitted to the server.
Inputs should be decoded and canonicalized to the application's current internal representation before being validated (CWE-180). Make sure that the application does not decode the same input twice (CWE-174). Such errors could be used to bypass allowlist validation schemes by introducing dangerous inputs after they have been checked.
Use a built-in path canonicalization function (such as realpath() in C) that produces the canonical version of the pathname, which effectively removes ".." sequences and symbolic links (CWE-23, CWE-59). This includes:
- realpath() in C
- getCanonicalPath() in Java
- GetFullPath() in ASP.NET
- realpath() or abs_path() in Perl
- realpath() in PHP
Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid [REF-1482].
Use an application firewall that can detect attacks against this weakness. It can be beneficial in cases in which the code cannot be fixed (because it is controlled by a third party), as an emergency prevention measure while more comprehensive software assurance measures are applied, or to provide defense in depth [REF-1481].
Effectiveness: Moderate — An application firewall might not cover all possible input vectors. In addition, attack techniques might be available to bypass the protection mechanism, such as using malformed inputs that can still be processed by the component that receives those inputs. Depending on functionality, an application firewall might inadvertently reject or modify legitimate requests. Finally, some manual effort may be required for customization.
Run your code using the lowest privileges that are required to accomplish the necessary tasks [REF-76]. If possible, create isolated accounts with limited privileges that are only used for a single task. That way, a successful attack will not immediately give the attacker access to the rest of the software or its environment. For example, database applications rarely need to run as the database administrator, especially in day-to-day operations.
When the set of acceptable objects, such as filenames or URLs, is limited or known, create a mapping from a set of fixed input values (such as numeric IDs) to the actual filenames or URLs, and reject all other inputs.
For example, ID 1 could map to "inbox.txt" and ID 2 could map to "profile.txt". Features such as the ESAPI AccessReferenceMap [REF-185] provide this capability.
Run the code in a "jail" or similar sandbox environment that enforces strict boundaries between the process and the operating system. This may effectively restrict which files can be accessed in a particular directory or which commands can be executed by the software.
OS-level examples include the Unix chroot jail, AppArmor, and SELinux. In general, managed code may provide some protection. For example, java.io.FilePermission in the Java SecurityManager allows the software to specify restrictions on file operations.
This may not be a feasible solution, and it only limits the impact to the operating system; the rest of the application may still be subject to compromise.
Be careful to avoid CWE-243 and other weaknesses related to jails.
Effectiveness: Limited — The effectiveness of this mitigation depends on the prevention capabilities of the specific sandbox or jail being used and might only help to reduce the scope of an attack, such as restricting the attacker to certain system calls or limiting the portion of the file system that can be accessed.
Store library, include, and utility files outside of the web document root, if possible. Otherwise, store them in a separate directory and use the web server's access control capabilities to prevent attackers from directly requesting them. One common practice is to define a fixed constant in each calling program, then check for the existence of the constant in the library/include file; if the constant does not exist, then the file was directly requested, and it can exit immediately.
This significantly reduces the chance of an attacker being able to bypass any protection mechanisms that are in the base program but not in the include files. It will also reduce the attack surface.
Ensure that error messages only contain minimal details that are useful to the intended audience and no one else. The messages need to strike the balance between being too cryptic (which can confuse users) or being too detailed (which may reveal more than intended). The messages should not reveal the methods that were used to determine the error. Attackers can use detailed information to refine or optimize their original attack, thereby increasing their chances of success.
If errors must be captured in some detail, record them in log messages, but consider what could occur if the log messages can be viewed by attackers. Highly sensitive information such as passwords should never be saved to log files.
Avoid inconsistent messaging that might accidentally tip off an attacker about internal state, such as whether a user account exists or not.
In the context of path traversal, error messages which disclose path information can help attackers craft the appropriate attack strings to move through the file system hierarchy.
When using PHP, configure the application so that it does not use register_globals. During implementation, develop the application so that it does not rely on this feature, but be wary of implementing a register_globals emulation that is subject to weaknesses such as CWE-95, CWE-621, and similar issues.
How to detect it
Automated Static Analysis
Automated techniques can find areas where path traversal weaknesses exist. However, tuning or customization may be required to remove or de-prioritize path-traversal problems that are only exploitable by the product's administrator - or other privileged users - and thus potentially valid behavior or, at worst, a bug instead of a vulnerability.
Effectiveness: High
Manual Static Analysis
Manual white box techniques may be able to provide sufficient code coverage and reduction of false positives if all file access operations can be assessed within limited time constraints.
Effectiveness: High
Automated Static Analysis - Binary or Bytecode
According to SOAR [REF-1479], the following detection techniques may be useful:
Effectiveness: High
Manual Static Analysis - Binary or Bytecode
According to SOAR [REF-1479], the following detection techniques may be useful:
Effectiveness: SOAR Partial
Dynamic Analysis with Automated Results Interpretation
According to SOAR [REF-1479], the following detection techniques may be useful:
Effectiveness: High
Dynamic Analysis with Manual Results Interpretation
According to SOAR [REF-1479], the following detection techniques may be useful:
Effectiveness: High
Manual Static Analysis - Source Code
According to SOAR [REF-1479], the following detection techniques may be useful:
Effectiveness: High
Automated Static Analysis - Source Code
According to SOAR [REF-1479], the following detection techniques may be useful:
Effectiveness: High
Architecture or Design Review
According to SOAR [REF-1479], the following detection techniques may be useful:
Effectiveness: High
Code examples
Illustrative examples from MITRE showing how the weakness appears in code.
The following code could be for a social networking application in which each user's profile information is stored in a separate file. All files are stored in a single directory.
Vulnerable example
my $dataPath = "/users/cwe/profiles";Attack input
../../../etc/passwdResulting query
/users/cwe/profiles/../../../etc/passwdResulting query
/etc/passwdIn the example below, the path to a dictionary file is read from a system property and used to initialize a File object.
Vulnerable example
String filename = System.getProperty("com.domain.application.dictionaryFile");However, the path is not validated or modified to prevent it from containing relative or absolute path sequences before creating the File object. This allows anyone who can control the system property to determine what file is used. Ideally, the path should be resolved relative to some kind of application or user home directory.
The following code takes untrusted input and uses a regular expression to filter "../" from the input. It then appends this result to the /home/user/ directory and attempts to read the file in the final resulting path.
Vulnerable example
my $Username = GetUntrustedInput();Attack input
../../../etc/passwdResulting query
../../etc/passwdResulting query
/home/user/../../etc/passwdThe following code attempts to validate a given input path by checking it against an allowlist and once validated delete the given file. In this specific case, the path is considered valid if it starts with the string "/safe_dir/".
Vulnerable example
String path = getInputPath();Attack input
/safe_dir/../important.datThe following code demonstrates the unrestricted upload of a file with a Java servlet and a path traversal vulnerability. The action attribute of an HTML form is sending the upload file request to the Java servlet.
Safe example
<form action="FileUploadServlet" method="post" enctype="multipart/form-data">Vulnerable example
public class FileUploadServlet extends HttpServlet {This script intends to read a user-supplied file from the current directory. The user inputs the relative path to the file and the script uses Python's os.path.join() function to combine the path to the current working directory with the provided path to the specified file. This results in an absolute path to the desired file. If the file does not exist when the script attempts to read it, an error is printed to the user.
Vulnerable example
import osSafe example
import osThis PHP code takes user input in a file argument, obtains its contents, and presents the contents back to the caller.
Vulnerable example
$filename = $_GET['file'];Safe example
$allowed_files = [An attacker could manipulate the file path using relative (../) or absolute paths, potentially accessing sensitive system files. For example, if an attacker provides ../../../../etc/passwd as input, the script will fetch and display the contents of the system's password file. Ideally, the file path should be validated and restricted to a specific directory to prevent unauthorized file access.
Illustrative examples
Real CVEs that MITRE cites as examples of this weakness.
- CVE-2021-21972CISA KEV— Chain: Cloud computing virtualization platform does not require authentication for upload of a tar format file (CWE-306), then uses .. path traversal sequences (CWE-23) in the file to access unexpected files, as exploited in the wild per CISA KEV.
- CVE-2020-3452CISA KEV— Chain: security product has improper input validation (CWE-20) leading to directory traversal (CWE-22), as exploited in the wild per CISA KEV.
- CVE-2024-37032 — Large language model (LLM) management tool does not validate the format of a digest value (CWE-1287) from a private, untrusted model registry, enabling relative path traversal (CWE-23), a.k.a. Probllama
- CVE-2024-4315 — Chain: API for text generation using Large Language Models (LLMs) does not include the "\" Windows folder separator in its denylist (CWE-184) when attempting to prevent Local File Inclusion via path traversal (CWE-22), allowing deletion of arbitrary files on Windows systems.
- CVE-2024-0520 — Product for managing datasets for AI model training and evaluation allows both relative (CWE-23) and absolute (CWE-36) path traversal to overwrite files via the Content-Disposition header
- CVE-2022-45918 — Chain: a learning management tool debugger uses external input to locate previous session logs (CWE-73) and does not properly validate the given path (CWE-20), allowing for filesystem path traversal using "../" sequences (CWE-24)
- CVE-2019-20916 — Python package manager does not correctly restrict the filename specified in a Content-Disposition header, allowing arbitrary file read using path traversal sequences such as "../"
- CVE-2022-31503 — Python package constructs filenames using an unsafe os.path.join call on untrusted input, allowing absolute path traversal because os.path.join resets the pathname to an absolute path that is specified as part of the input.
- CVE-2022-24877 — directory traversal in Go-based Kubernetes operator app allows accessing data from the controller's pod file system via ../ sequences in a yaml file
- CVE-2020-4053 — a Kubernetes package manager written in Go allows malicious plugins to inject path traversal sequences into a plugin archive ("Zip slip") to copy a file outside the intended directory
- CVE-2019-10743 — Go-based archive library allows extraction of files to locations outside of the target folder with "../" path traversal sequences in filenames in a zip file, aka "Zip Slip"
- CVE-2010-0467 — Newsletter module allows reading arbitrary files using "../" sequences.
- CVE-2006-7079 — Chain: PHP app uses extract for register_globals compatibility layer (CWE-621), enabling path traversal (CWE-22)
- CVE-2009-4194 — FTP server allows deletion of arbitrary files using ".." in the DELE command.
- CVE-2009-4053 — FTP server allows creation of arbitrary directories using ".." in the MKD command.
- CVE-2009-0244 — FTP service for a Bluetooth device allows listing of directories, and creation or reading of files using ".." sequences.
- CVE-2009-4013 — Software package maintenance program allows overwriting arbitrary files using "../" sequences.
- CVE-2009-4449 — Bulletin board allows attackers to determine the existence of files using the avatar.
- CVE-2009-4581 — PHP program allows arbitrary code execution using ".." in filenames that are fed to the include() function.
- CVE-2010-0012 — Overwrite of files using a .. in a Torrent file.
- CVE-2010-0013 — Chat program allows overwriting files using a custom smiley request.
- CVE-2008-5748 — Chain: external control of values for user's desired language and theme enables path traversal.
- CVE-2009-1936 — Chain: library file sends a redirect if it is directly requested but continues to execute, allowing remote file inclusion and path traversal.
Terminology & mappings
Alternate terms
- Path traversal
- "Path traversal" is preferred over "directory traversal," but both terms are attack-focused.
- Directory traversal
- Path transversal
- an alternate phrasing of "path traversal"
Mapped taxonomies
- PLOVER: Path Traversal
- OWASP Top Ten 2007: Insecure Direct Object Reference (A4) — CWE More Specific fit
- OWASP Top Ten 2004: Broken Access Control (A2) — CWE More Specific fit
- CERT C Secure Coding: Canonicalize path names originating from untrusted sources (FIO02-C)
- SEI CERT Perl Coding Standard: Canonicalize path names before validating them (IDS00-PL) — Exact fit
- WASC: Path Traversal (33)
- Software Fault Patterns: Path Traversal (SFP16)
- OMG ASCSM (ASCSM-CWE-22)
Attack patterns
CAPEC attack patterns that exploit this weakness.
Frequently asked questions
Common questions about CWE-22.
- What is CWE-22?
- The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
- What CVEs are caused by CWE-22?
- 4,697 recorded CVEs are attributed to CWE-22, including CVE-2026-48282, CVE-2026-34909, CVE-2023-47246. 71 are listed in CISA's Known Exploited Vulnerabilities (KEV) catalog.
- Is CWE-22 part of the OWASP Top 10?
- CWE-22 maps to OWASP Top Ten 2007: Insecure Direct Object Reference (A4) in the OWASP security taxonomy.
- How do you prevent CWE-22?
- Assume all input is malicious. Use an "accept known good" input validation strategy, i.e., use a list of acceptable inputs that strictly conform to specifications. Reject any input that does not strictly conform to specifications, or transform it into something that does.
- How is CWE-22 detected?
- Automated Static Analysis: Automated techniques can find areas where path traversal weaknesses exist. However, tuning or customization may be required to remove or de-prioritize path-traversal problems that are only exploitable by the product's administrator - or other privileged users - and thus potentially valid behavior or, at worst, a bug instead of a vulnerability.
- What are the consequences of CWE-22?
- Exploiting CWE-22 can lead to: Execute Unauthorized Code or Commands, Modify Files or Directories, Read Files or Directories, DoS: Crash, Exit, or Restart.
- Is CWE-22 actively exploited?
- Yes. 71 CWE-22 vulnerabilities are in CISA's KEV catalog of actively exploited flaws, out of 4,697 recorded CVEs.
References
- MITRE CWE definition (CWE-22) (opens in a new tab)
- CWE-22 vulnerabilities on NVD (opens in a new tab)
- Learn: What is a CWE?
Weakness data is sourced from the MITRE CWE catalog (v4.20). CVE associations are aggregated and kept current by RadicalNotion.AI.
Stay ahead of CWE-22
Get alerted the moment a new CWE-22 vulnerability affects your stack, with AI-written analysis, severity context, and remediation guidance.