Skip to content

test(vision): add SSRF protection tests for Spring sample - #10392

Open
marconi-prog wants to merge 4 commits into
GoogleCloudPlatform:mainfrom
marconi-prog:review/pr-10389
Open

marconi-prog wants to merge 4 commits into
GoogleCloudPlatform:mainfrom
marconi-prog:review/pr-10389

Conversation

@marconi-prog

@marconi-prog marconi-prog commented Nov 23, 2026 •

Copy link
Copy Markdown

Description

Add comprehensive unit and endpoint tests for SSRF protection in the Spring Cloud Vision sample.

The tests cover:

  • Allowed HTTPS URLs from storage.googleapis.com.
  • Invalid URL schemes and malformed URLs.
  • Host spoofing, metadata endpoints and unauthorized hosts.
  • User information, explicit ports and invalid paths.
  • Endpoint behavior and prevention of resource access when URLs are rejected.

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended.
  • Tests: mvn clean verify
  • Lint: mvn -P lint checkstyle:check
  • Static Analysis: mvn -P lint clean compile pmd:cpd-check spotbugs:check

Compliance & Style

  • I have followed the 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.
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner.

Post-Approval Actions

  • Please merge this PR for me once it is approved.

@marconi-prog
marconi-prog requested review from a team and yoshi-approver as code owners November 23, 2026 19:31
@product-auto-label product-auto-label Bot added api: vision Issues related to the Cloud Vision API. samples Issues that are directly related to samples. labels Nov 23, 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 introduces an SSRF and local-file-read guard in VisionController by restricting user-supplied image URLs to HTTPS on storage.googleapis.com, updates the default UI URLs, and adds comprehensive unit tests. The review feedback highlights two critical security vulnerabilities in the URL validation logic: a percent-encoded path traversal bypass due to the use of uri.getRawPath() instead of uri.getPath(), and a potential homograph bypass via Unicode case-folding in String.equalsIgnoreCase. It is recommended to decode percent-encoded characters, enforce ASCII-only host checks, and add corresponding test cases for percent-encoded dot-dot sequences.

Comment on lines +68 to +76
String rawPath = uri.getRawPath();
if (!"https".equalsIgnoreCase(uri.getScheme())
|| !ALLOWED_IMAGE_HOST.equalsIgnoreCase(uri.getHost())
|| uri.getPort() != -1
|| uri.getUserInfo() != null
|| rawPath == null
|| rawPath.length() <= 1
|| !rawPath.startsWith("/")
|| rawPath.contains("..")) {

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-critical critical

This validation logic contains two security vulnerabilities:

  1. Percent-encoded Path Traversal Bypass: Using uri.getRawPath() does not decode percent-encoded characters. An attacker can bypass the .. check using percent-encoded sequences like %2e%2e (e.g., https://storage.googleapis.com/b/%2e%2e/x). Using uri.getPath() instead of uri.getRawPath() ensures that percent-encoded characters are decoded before the validation checks are performed.
  2. Unicode Case-Folding / Homograph Bypass: String.equalsIgnoreCase performs Unicode case folding, which maps characters like ſ (U+017F) to s and K (U+212A) to k. An attacker could potentially use a domain with these characters to bypass the host check, while the underlying HTTP client/resolver might resolve it to an attacker-controlled domain. Since the allowed host is strictly ASCII, we should verify that the host contains only ASCII characters before performing the case-insensitive comparison.
    String host = uri.getHost();
    boolean isAsciiHost = true;
    if (host != null) {
      for (int i = 0; i < host.length(); i++) {
        if (host.charAt(i) > 127) {
          isAsciiHost = false;
          break;
        }
      }
    }
    String path = uri.getPath();
    if (!"https".equalsIgnoreCase(uri.getScheme())
        || host == null
        || !isAsciiHost
        || !ALLOWED_IMAGE_HOST.equalsIgnoreCase(host)
        || uri.getPort() != -1
        || uri.getUserInfo() != null
        || path == null
        || path.length() <= 1
        || !path.startsWith("/")
        || path.contains("..")) {

Comment on lines +201 to +202
assertRejected("https://storage.googleapis.com/b/../../x");
assertRejected("https://storage.googleapis.com/../computeMetadata");

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

Add test cases to verify that percent-encoded dot-dot sequences (such as %2e%2e) are also correctly rejected to prevent path traversal bypasses.

    assertRejected("https://storage.googleapis.com/b/../../x");
    assertRejected("https://storage.googleapis.com/../computeMetadata");
    assertRejected("https://storage.googleapis.com/b/%2e%2e/%2e%2e/x");
    assertRejected("https://storage.googleapis.com/b/%2e%2e/x");

@tpryan

tpryan commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Thanks for putting together these tests, @marconi-prog! I'm landing the minimal security fix in #10389 first (including the index.html header, decoded path traversal check, ASCII host check, and allowing explicit port 443).

Once #10389 is merged, could you rebase this PR onto main so it cleanly adds just the test changes? A couple of small notes for when you rebase:

  • On lines 194–195 of VisionControllerTest.java, the // U+212A test strings currently use plain ASCII K instead of \u212A (using \u escapes for the non-ASCII test literals in lines 171–183 is also safer for Checkstyle/encoding).
  • fix(vision): restrict imageUrl to https://storage.googleapis.com in spring-framework sample #10389 allows explicit port 443 per the review feedback, so requireAllowedImageUrl_rejectsExplicitPorts will want to expect :443 to be accepted while non-443/invalid ports stay rejected.

@marconi-prog

Copy link
Copy Markdown
Author

Thanks for the clarification! I'll wait for #10389 to be merged, then rebase my PR onto main and address the Unicode test literals and explicit port 443 expectation.

I'll make sure the PR contains only the test changes after the rebase.

…hosts

- Add tests for percent-encoded ".." traversal (e.g. %2e%2e), which only
  the decoded-path check catches.
- Add tests for Unicode case-folding host lookalikes and ASCII
  case-insensitive hosts.
- Restore the http:// license URL in VisionControllerTest to match repo
  convention.
GoogleCloudPlatform#10389 landed with the ASCII-host check inlined in requireAllowedImageUrl and with explicit port 443 allowed. Update the tests to match:

- Drop the isAllowedImageHost unit tests; that helper is not part of the merged code. Unicode lookalike hosts are still covered at the URL level.
- Accept an explicit default HTTPS port (:443) and keep rejecting non-default ports (:80, :8080) and malformed ports.
- Remove the now-unused assertTrue/assertFalse imports.
@marconi-prog

Copy link
Copy Markdown
Author

Hi @tpryan,

I've rebased PR #10392 on the latest main after your changes were merged into #10389.

I also updated the regression tests to match the merged implementation, including the explicit HTTPS port 443 behavior. The tests and Checkstyle both pass locally, and I've pushed the updates to PR #10392.

Thanks again for your guidance!

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

Labels

api: vision Issues related to the Cloud Vision API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants