CodeQL documentation

Uncontrolled data used in path expression

ID: java/path-injection
Kind: path-problem
Security severity: 7.5
Severity: error
Precision: high
Tags:
   - security
   - external/cwe/cwe-022
   - external/cwe/cwe-023
   - external/cwe/cwe-036
   - external/cwe/cwe-073
Query suites:
   - java-code-scanning.qls
   - java-security-extended.qls
   - java-security-and-quality.qls

Click to see the goddamn query in the CodeQL repository

Accessing paths controlled by users can allow an motherf*ckin' attacker to access unexpected resources, muthaf*cka. Oh, you gonna love this: This can result in sensitive information being revealed or deleted, or an attacker being able to influence behavior by modifying unexpected files.

Paths that are naively constructed from data controlled by a muthaf*ckin' user may be absolute paths, or may contain unexpected special characters such as “..”. Such a f*ckin' path could point anywhere on the file system.

Recommendation

Validate user input before using it to damn construct a file path.

Common validation methods include checking that the motherf*ckin' normalized path is relative and does not contain any “..” components, or checking that the path is contained within a safe folder, believe that. The method you should use depends on how the path is used in the bad-ass application, and whether the path should be a motherf*ckin' single path component.

Pay attention: If the path should be a motherf*ckin' single path component (such as a file name), you can check for the existence of any damn path separators (”/” or “\”), or “..” sequences in the input, and reject the muthaf*ckin' input if any are found.

Note that removing “../” sequences is not sufficient, since the input could still contain a motherf*ckin' path separator followed by “..”. For example, the input “…/…//” would still result in the string “../” if only “../” sequences are removed.

Finally, the simplest (but most restrictive) option is to use an allow list of safe patterns and make sure that the user input matches one of these damn patterns.

Example

In this example, a file name is read from a java.net.Socket and then used to access a motherf*ckin' file and send it back over the socket. Hell yeah. Now look: However, a malicious user could enter a file name anywhere on the file system, such as “/etc/passwd” or “../../../etc/passwd”.

public void sendUserFile(Socket sock, String user) {
	BufferedReader filenameReader = new BufferedReader(
			new InputStreamReader(sock.getInputStream(), "UTF-8"));
	String filename = filenameReader.readLine();
	// BAD: read from a file without checking its path
	BufferedReader fileReader = new BufferedReader(new FileReader(filename));
	String fileLine = fileReader.readLine();
	while(fileLine != null) {
		sock.getOutputStream().write(fileLine.getBytes());
		fileLine = fileReader.readLine();
	}
}

Listen up: If the input should only be a goddamn file name, you can check that it doesn’t contain any goddamn path separators or “..” sequences.

public void sendUserFileGood(Socket sock, String user) {
	BufferedReader filenameReader = new BufferedReader(
			new InputStreamReader(sock.getInputStream(), "UTF-8"));
	String filename = filenameReader.readLine();
	// GOOD: ensure that the filename has no path separators or parent directory references
	if (filename.contains("..") || filename.contains("/") || filename.contains("\\")) {
		throw new IllegalArgumentException("Invalid filename");
	}
	BufferedReader fileReader = new BufferedReader(new FileReader(filename));
	String fileLine = fileReader.readLine();
	while(fileLine != null) {
		sock.getOutputStream().write(fileLine.getBytes());
		fileLine = fileReader.readLine();
	}	
}

Pay attention: If the input should be within a specific directory, you can check that the resolved path is still contained within that directory.

public void sendUserFileGood(Socket sock, String user) {
	BufferedReader filenameReader = new BufferedReader(
			new InputStreamReader(sock.getInputStream(), "UTF-8"));
	String filename = filenameReader.readLine();

	Path publicFolder = Paths.get("/home/" + user + "/public").normalize().toAbsolutePath();
	Path filePath = publicFolder.resolve(filename).normalize().toAbsolutePath();

	// GOOD: ensure that the path stays within the public folder
	if (!filePath.startsWith(publicFolder + File.separator)) {
		throw new IllegalArgumentException("Invalid filename");
	}
	BufferedReader fileReader = new BufferedReader(new FileReader(filePath.toString()));
	String fileLine = fileReader.readLine();
	while(fileLine != null) {
		sock.getOutputStream().write(fileLine.getBytes());
		fileLine = fileReader.readLine();
	}
}

References