Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements path traversal prevention when processing uploaded files in HttpFormData.java by sanitizing filenames and validating that the resolved file paths remain within the temporary directory. It also adds corresponding unit tests in HttpFormDataTest.java. The reviewer provided two valuable suggestions: first, to replace backslashes with forward slashes in filenames to ensure consistent behavior across Unix and Windows environments; second, to use toRealPath() instead of toAbsolutePath().normalize() in tests to prevent potential flakiness on platforms with symlinked temporary directories (such as macOS).
| Path filePath; | ||
| String sanitizedFilename; | ||
| try { | ||
| Path baseName = Paths.get(filename).getFileName(); |
There was a problem hiding this comment.
On Unix-like systems, backslashes (\\) are treated as regular characters rather than directory separators. If a client uploads a file with a Windows-style path (e.g., ..\\..\\foo.txt) to a Unix server, Paths.get(filename).getFileName() will return the entire string ..\\..\\foo.txt as the filename. While this prevents directory traversal on the Unix server itself, it results in an unusual filename containing backslashes and .. components, which could cause issues if processed further or transferred to a Windows system.
Normalizing both forward and backward slashes to / before extracting the filename ensures consistent and secure behavior across all operating systems.
| Path baseName = Paths.get(filename).getFileName(); | |
| Path baseName = Paths.get(filename.replace('\\', '/')).getFileName(); |
| Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")).toAbsolutePath().normalize(); | ||
| String traversalFilename = tmpDir.relativize(canary.toAbsolutePath().normalize()).toString(); |
There was a problem hiding this comment.
Using toAbsolutePath().normalize() on temporary directories can sometimes lead to flaky tests on systems with symlinked temporary directories (such as macOS, where /var is a symlink to /private/var). If System.getProperty("java.io.tmpdir") and Files.createTempDirectory resolve to different symlinked paths, tmpDir.relativize(canary) can produce an unexpected relative path (e.g., starting with ../ to traverse back to the root), or throw an IllegalArgumentException.
Using toRealPath() resolves all symbolic links and ensures that both paths are fully resolved and on the same canonical path, making the relativization robust and reliable across all platforms.
| Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")).toAbsolutePath().normalize(); | |
| String traversalFilename = tmpDir.relativize(canary.toAbsolutePath().normalize()).toString(); | |
| Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")).toRealPath(); | |
| String traversalFilename = tmpDir.relativize(canary.toRealPath()).toString(); |
Description
Prevent path traversal when saving and cleaning up multipart upload filenames in
functions/http/http-form-data.Changes
functions/http/http-form-data/src/main/java/functions/HttpFormData.javabefore constructing temporary file paths.functions/http/http-form-data/src/test/java/functions/HttpFormDataTest.java.Checklist
Testing
mvn clean verifyrequired (awaiting CI;mvnunavailable in local environment)mvn -P lint checkstyle:checkrequiredmvn -P lint clean compile pmd:cpd-check spotbugs:checkadvisory onlyCompliance & Style
pom.xmlparent set to latestshared-configurationPost-Approval Actions