Repository navigation
fix(functions): prevent path traversal in http-form-data sample #10398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -32,6 +32,9 @@ | |||||||||
| import java.io.StringWriter; | ||||||||||
| import java.net.HttpURLConnection; | ||||||||||
| import java.nio.charset.StandardCharsets; | ||||||||||
| import java.nio.file.Files; | ||||||||||
| import java.nio.file.Path; | ||||||||||
| import java.nio.file.Paths; | ||||||||||
| import java.util.List; | ||||||||||
| import java.util.Map; | ||||||||||
| import java.util.Optional; | ||||||||||
|
|
@@ -116,4 +119,66 @@ public void functionsHttpFormData_shouldProcessFields() throws IOException { | |||||||||
| assertThat(logHandler.getStoredLogRecords().get(0).getMessage()).isEqualTo( | ||||||||||
| "Processed field: foo (value: bar)"); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Test | ||||||||||
| public void functionsHttpFormData_shouldPreventPathTraversal() throws IOException { | ||||||||||
| when(request.getMethod()).thenReturn("POST"); | ||||||||||
|
|
||||||||||
| Path canaryDir = Files.createTempDirectory("http-form-data-canary"); | ||||||||||
| Path canary = Files.writeString(canaryDir.resolve("canary.txt"), "original"); | ||||||||||
| try { | ||||||||||
| Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")).toAbsolutePath().normalize(); | ||||||||||
| String traversalFilename = tmpDir.relativize(canary.toAbsolutePath().normalize()).toString(); | ||||||||||
|
Comment on lines
+130
to
+131
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Using
Suggested change
|
||||||||||
|
|
||||||||||
| MockHttpPart mockHttpPart = new MockHttpPart(); | ||||||||||
| mockHttpPart.setFileName(traversalFilename); | ||||||||||
| mockHttpPart.setInputStream( | ||||||||||
| new ByteArrayInputStream("attacker".getBytes(StandardCharsets.UTF_8))); | ||||||||||
|
|
||||||||||
| when(request.getParts()).thenReturn(Map.of("mock", mockHttpPart)); | ||||||||||
|
|
||||||||||
| new HttpFormData().service(request, response); | ||||||||||
|
|
||||||||||
| assertThat(Files.exists(canary)).isTrue(); | ||||||||||
| assertThat(Files.readString(canary)).isEqualTo("original"); | ||||||||||
| assertThat(logHandler.getStoredLogRecords().get(0).getMessage()) | ||||||||||
| .isEqualTo("Processed file: canary.txt"); | ||||||||||
| } finally { | ||||||||||
| Files.deleteIfExists(canary); | ||||||||||
| Files.deleteIfExists(canaryDir); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Test | ||||||||||
| public void functionsHttpFormData_shouldRejectDotDotFilename() throws IOException { | ||||||||||
| when(request.getMethod()).thenReturn("POST"); | ||||||||||
|
|
||||||||||
| MockHttpPart mockHttpPart = new MockHttpPart(); | ||||||||||
| mockHttpPart.setFileName(".."); | ||||||||||
| mockHttpPart.setInputStream( | ||||||||||
| new ByteArrayInputStream("attacker".getBytes(StandardCharsets.UTF_8))); | ||||||||||
|
|
||||||||||
| when(request.getParts()).thenReturn(Map.of("mock", mockHttpPart)); | ||||||||||
|
|
||||||||||
| new HttpFormData().service(request, response); | ||||||||||
|
|
||||||||||
| verify(response, times(1)).setStatusCode(HttpURLConnection.HTTP_BAD_REQUEST); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Test | ||||||||||
| public void functionsHttpFormData_shouldStripParentTraversalPrefix() throws IOException { | ||||||||||
| when(request.getMethod()).thenReturn("POST"); | ||||||||||
|
|
||||||||||
| MockHttpPart mockHttpPart = new MockHttpPart(); | ||||||||||
| mockHttpPart.setFileName("../../escape-test.txt"); | ||||||||||
| mockHttpPart.setInputStream( | ||||||||||
| new ByteArrayInputStream("attacker".getBytes(StandardCharsets.UTF_8))); | ||||||||||
|
|
||||||||||
| when(request.getParts()).thenReturn(Map.of("mock", mockHttpPart)); | ||||||||||
|
|
||||||||||
| new HttpFormData().service(request, response); | ||||||||||
|
|
||||||||||
| assertThat(logHandler.getStoredLogRecords().get(0).getMessage()) | ||||||||||
| .isEqualTo("Processed file: escape-test.txt"); | ||||||||||
| } | ||||||||||
| } | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.txtas 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.