Skip to content

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

Merged
angelcaamal merged 3 commits into
GoogleCloudPlatform:mainfrom
tpryan:fix/vision-spring-ssrf
Nov 26, 2026
Merged

angelcaamal merged 3 commits into
GoogleCloudPlatform:mainfrom
tpryan:fix/vision-spring-ssrf

Conversation

@tpryan

@tpryan tpryan commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

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
Contributor 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!

…tion

- Add Apache 2.0 license header to vision/spring-framework/src/main/resources/static/index.html to satisfy header-check.
- Check decoded URI path in addition to raw path to reject percent-encoded path traversal (e.g. %2e%2e).
- Allow explicit port 443 for HTTPS URLs and require an ASCII host before case-insensitive comparison.
@tpryan

tpryan commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@angelcaamal Thanks for catching that — I've added the Apache 2.0 header to index.html and updated requireAllowedImageUrl to address the Gemini review comments (checking decoded uri.getPath() for percent-encoded .., allowing explicit port 443, and requiring an ASCII host). All checks (including header-check and Kokoro CI) are now green — ready for another look!

@marconi-prog Thanks a lot for putting together the security test coverage! I'd like to keep this PR focused on the minimal security fix and get it merged first — once #10389 lands, feel free to rebase #10392 onto main so it cleanly adds the test suite as a follow-up PR.

@angelcaamal angelcaamal 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.

LGTM!

@angelcaamal
angelcaamal merged commit 366527e into GoogleCloudPlatform:main Nov 26, 2026
12 checks passed
marconi-prog added a commit to marconi-prog/java-docs-samples that referenced this pull request Nov 27, 2026
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.
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