Repository navigation
fix(vision): restrict imageUrl to https://storage.googleapis.com in spring-framework sample - #10389
Conversation
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
|
Could you please add the Apache 2.0 license header ( <!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! |
|
Hi! I'd like to contribute to this PR by adding automated tests for Would you be open to a contribution focused on security test coverage? |
|
/gemini review |
There was a problem hiding this comment.
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().
| 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("..")) { |
There was a problem hiding this comment.
There are two issues with the current URL validation logic:
- Explicit Port Restriction: The check
uri.getPort() != -1will reject valid HTTPS URLs that explicitly specify the default port 443 (e.g.,https://storage.googleapis.com:443/...). It is better to allow both-1and443. - 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 tofalse, potentially bypassing the path traversal check. Usinguri.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("..")) {|
Hi! I've opened a separate PR (#10392) adding automated security tests for the 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.
|
@angelcaamal Thanks for catching that — I've added the Apache 2.0 header to @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 |
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.
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.