Improper directory path restriction (path traversal)

Restrict file access to an approved directory

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

  1. 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.
  2. Combine the path with the base using Path.resolve(), normalize it and compare components with Path.startsWith(Path). For existing files, resolve links with toRealPath() and check again.
  3. 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 SecureDirectoryStream where supported, opening each path component safely.

Examples

Before

java
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.

java
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.

java
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

References