Skip to content

fix(vision): restrict imageUrl to https://storage.googleapis.com in spring-framework sample - #10389

Open
tpryan wants to merge 2 commits into
GoogleCloudPlatform:mainfrom
tpryan:fix/vision-spring-ssrf
Open

tpryan wants to merge 2 commits into
GoogleCloudPlatform:mainfrom
tpryan:fix/vision-spring-ssrf

Conversation

@tpryan

@tpryan tpryan commented Nov 20, 2026

Copy link
Copy Markdown

Prevents SSRF and local file reads via Spring ResourceLoader.getResource(imageUrl) in VisionController by restricting imageUrl to HTTPS URLs on storage.googleapis.com and updating index.html defaults to official cloud-samples-data Vision images.

@tpryan
tpryan requested review from a team and yoshi-approver as code owners November 20, 2026 21:33
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@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 20, 2026

@tpryan tpryan left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

LGTM

@angelcaamal

Copy link
Copy Markdown
Contributor

Could you please add the Apache 2.0 license header (Google LLC) to vision/spring-framework/src/main/resources/static/index.html to fix the failing header-check CI check?

<!DOCTYPE html>
<!--
Copyright 2019 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html lang="en">

Thanks!

@marconi-prog

Copy link
Copy Markdown

Hi! I'd like to contribute to this PR by adding automated tests for requireAllowedImageUrl, covering valid URLs, malformed URIs, unsupported schemes, host validation, and potential URL parsing bypasses.

Would you be open to a contribution focused on security test coverage?

@angelcaamal

Copy link
Copy Markdown
Contributor

/gemini review

@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 URL validation to restrict user-supplied image URLs to HTTPS on storage.googleapis.com, mitigating SSRF and path traversal vulnerabilities. However, the validation logic contains two issues: it rejects URLs that explicitly specify port 443, and it is vulnerable to path traversal bypasses via percent-encoding (e.g., %2e%2e) because it uses getRawPath() instead of getPath().

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

There are two issues with the current URL validation logic:

  1. Explicit Port Restriction: The check uri.getPort() != -1 will reject valid HTTPS URLs that explicitly specify the default port 443 (e.g., https://storage.googleapis.com:443/...). It is better to allow both -1 and 443.
  2. Path Traversal Bypass via Percent-Encoding: Using uri.getRawPath() returns the raw, percent-encoded path. If a user passes a URL containing encoded dots (e.g., %2e%2e), rawPath.contains("..") will evaluate to false, potentially bypassing the path traversal check. Using uri.getPath() instead will decode percent-encoded characters before performing the check.
    String path = uri.getPath();
    int port = uri.getPort();
    if (!"https".equalsIgnoreCase(uri.getScheme())
        || !ALLOWED_IMAGE_HOST.equalsIgnoreCase(uri.getHost())
        || (port != -1 && port != 443)
        || uri.getUserInfo() != null
        || path == null
        || path.length() <= 1
        || !path.startsWith("/")
        || path.contains("..")) {

@marconi-prog

Copy link
Copy Markdown

Hi! I've opened a separate PR (#10392) adding automated security tests for the VisionController URL validation.

The tests cover allowed and rejected URLs, malformed URIs, unsupported schemes, host validation, and URL parsing bypass cases, including encoded path traversal attempts.

Could you please take a look when you have a chance? I'd appreciate any feedback on the test coverage and whether the changes align with the original fix.

Thanks!

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.

3 participants