Skip to content

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

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

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

Conversation

@marconi-prog

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

- Reject percent-encoded ".." traversal (e.g. %2e%2e) by also checking the
  decoded URI path; existing raw-path checks are kept.
- Require an ASCII-only host before the case-insensitive comparison with
  storage.googleapis.com, so Unicode case-folding lookalikes cannot match.
- Add the missing license header to static/index.html (the file the
  header-check bot actually flagged).
- Restore the http:// license URL in VisionControllerTest to match repo
  convention.
- Add regression tests for encoded traversal, Unicode host lookalikes and
  ASCII case-insensitive hosts.
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