Description
When reading or writing files, confine paths from requests to the intended directory. A fixed parent directory still leaves the caller in control of the supplied child path.
Potential impact
The application account’s permissions can expose system files or let callers change another user’s data. If the application later runs a modified file, its contents can execute.
Remediation
- Use a server-generated name, such as a UUID, when users do not need to choose a path. If accepting relative paths, reject absolute ones.
- Combine the path with the base using
Path.resolve(), normalize it and compare components withPath.startsWith(Path). For existing files, resolve links withtoRealPath()and check again. - Restrict write access to the base and its descendants so paths cannot change after the check. If untrusted users can modify them, use directory-relative operations such as
SecureDirectoryStreamwhere supported, opening each path component safely.
Examples
Before
private static final String BASE_PATH ="/restricted/directory";
public String readFile(String filePath) throws IOException {
Path file = Paths.get("/restricted/directory/" + filePath);
return new String(Files.readAllBytes(file));
}
In File(parent, child), child contributes to the path. Supplying a parent alone does not establish the final file’s location.
import java.io.File;
import javax.servlet.http.HttpServletRequest;
public final class UnsafeUploadPath {
private static final File UPLOAD_DIR = new File("/var/app/uploads");
public File destination(HttpServletRequest request) {
String child = request.getParameter("name");
// Unsafe: the second constructor argument is also an attacker-controlled path.
return new File(UPLOAD_DIR, child);
}
}
After
This Java 11 or later example assumes a server-controlled directory tree. Handle file-size limits and error responses for invalid input separately.
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
public final class SafeFileReader {
private static final Path BASE_PATH = Path.of("/restricted/directory");
public String readFile(String filePath) throws IOException {
Path base = BASE_PATH.toRealPath();
Path supplied = Path.of(filePath);
if (supplied.isAbsolute()) {
throw new SecurityException("Absolute paths are not allowed.");
}
Path candidate = base.resolve(supplied).normalize();
if (!candidate.startsWith(base)) {
throw new SecurityException("Unauthorized access attempt detected.");
}
// Resolve existing symbolic links and require the actual target to remain inside the base directory.
Path requestedPath = candidate.toRealPath();
if (!requestedPath.startsWith(base)
|| !Files.isRegularFile(requestedPath, LinkOption.NOFOLLOW_LINKS)) {
throw new SecurityException("Unauthorized access attempt detected.");
}
return Files.readString(requestedPath, StandardCharsets.UTF_8);
}
}
The reader verifies that requestedPath is a regular file inside the real base directory, then reads that same path.
Related CVEs
- CVE-2024-37032: Unvalidated registry digest permits path traversal (CWE-1287/CWE-23, Probllama)
- CVE-2024-4315: Windows path-separator omission permits traversal and file deletion (CWE-184/CWE-22)
- CVE-2022-45918: Unvalidated debugger log path permits traversal (CWE-73/CWE-20/CWE-24)