Skip to content

fix(functions): prevent path traversal in http-form-data sample - #10398

Open
taeold wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
taeold:fix/http-form-data-path-traversal
Open

taeold wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
taeold:fix/http-form-data-path-traversal

Conversation

@taeold

@taeold taeold commented Nov 30, 2026

Copy link
Copy Markdown

Description

Prevent path traversal when saving and cleaning up multipart upload filenames in functions/http/http-form-data.

Changes

  • Sanitize uploaded multipart filenames in functions/http/http-form-data/src/main/java/functions/HttpFormData.java before constructing temporary file paths.
  • Add 3 JUnit tests in functions/http/http-form-data/src/test/java/functions/HttpFormDataTest.java.

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended.
  • Tests pass: mvn clean verify required (awaiting CI; mvn unavailable in local environment)
  • Lint passes: mvn -P lint checkstyle:check required
  • Static Analysis: mvn -P lint clean compile pmd:cpd-check spotbugs:check advisory only

Compliance & Style

  • I have followed Sample Format Guide
  • pom.xml parent set to latest shared-configuration
  • Appropriate changes to README are included in PR
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample

Post-Approval Actions

  • Please merge this PR for me once it is approved

@taeold
taeold requested review from a team and yoshi-approver as code owners November 30, 2026 22:34
@product-auto-label product-auto-label Bot added api: cloudfunctions Issues related to the Cloud Run functions API. samples Issues that are directly related to samples. labels Nov 30, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-medium medium

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.

Suggested change
Path baseName = Paths.get(filename).getFileName();
Path baseName = Paths.get(filename.replace('\\', '/')).getFileName();

Comment on lines +130 to +131
Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir")).toAbsolutePath().normalize();
String traversalFilename = tmpDir.relativize(canary.toAbsolutePath().normalize()).toString();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: cloudfunctions Issues related to the Cloud Run functions API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant