You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Below is a summary of compliance checks for this PR:
Security Compliance
⚪
Path traversal
Description: The method uses unvalidated path components (targetLocation and fileName) to create directories and write a file (Files.createDirectories(targetLocation) and targetLocation.resolve(fileName)), which can enable path traversal or arbitrary file write if either value can be influenced to include .. segments or an absolute path (e.g., fileName like ../../.ssh/authorized_keys). RemoteWebDriver.java [730-739]
Objective: To create a detailed and reliable record of critical system actions for security analysis and compliance.
Status: No audit logging: The new file-system action (creating directories before saving a downloaded file) is not accompanied by any audit log context (actor, action, outcome), which may be required depending on whether downloads are considered a critical action in this project.
Instead of creating the targetLocation as a directory, create the parent directory of the final destination path (targetLocation.resolve(fileName)). This makes the method more robust if targetLocation is a file path instead of a directory.
Why: The suggestion correctly identifies that if targetLocation is a path to a file, the PR's change will incorrectly create it as a directory. The proposed change to create the parent directory of the resolved path is more robust and handles this edge case correctly, improving the code's reliability.
Medium
Learned best practice
Validate file paths before copying
Defensively validate fileName and ensure the resolved destination stays within targetLocation to prevent ../ traversal or absolute-path writes; then create directories and copy to the validated path.
public void downloadFile(String fileName, Path targetLocation) throws IOException {
requireDownloadsEnabled(capabilities);
Response response = execute(DriverCommand.GET_DOWNLOADED_FILE, Map.of("name", fileName));
+ Path baseDir = Require.nonNull("Target location", targetLocation).toAbsolutePath().normalize();+ String safeName = Require.nonNull("File name", fileName).trim();+ if (safeName.isEmpty()) {+ throw new IllegalArgumentException("File name must not be blank");+ }++ Path destination = baseDir.resolve(safeName).normalize();+ if (!destination.startsWith(baseDir) || destination.getFileName() == null) {+ throw new IllegalArgumentException("Invalid file name: " + fileName);+ }+
Contents.Supplier content = (Contents.Supplier) response.getValue();
try (InputStream fileContent = content.get()) {
- Files.createDirectories(targetLocation);- Files.copy(new BufferedInputStream(fileContent), targetLocation.resolve(fileName));+ Files.createDirectories(baseDir);+ Files.copy(new BufferedInputStream(fileContent), destination);
}
}
[To ensure code accuracy, apply this suggestion manually]
Suggestion importance[1-10]: 6
__
Why:
Relevant best practice - Validate and sanitize external inputs (e.g., names/paths) before using them in filesystem operations to avoid path traversal and invalid paths.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
Fixes #16738
🔄 Types of changes
PR Type
Bug fix
Description
Ensure parent directories exist before file download
Prevents IOException when target directory missing
Diagram Walkthrough
File Walkthrough
RemoteWebDriver.java
Add directory creation before file downloadjava/src/org/openqa/selenium/remote/RemoteWebDriver.java
Files.createDirectories(targetLocation)call before file copyoperation
downloading files to non-existent paths