Repository navigation
test(vision): add SSRF protection tests for Spring sample #10392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marconi-prog
wants to merge
4
commits into
GoogleCloudPlatform:main
Choose a base branch
from
marconi-prog:review/pr-10389
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f94e8b1
test(vision): add SSRF protection tests for Spring sample
marconi-prog deb4fab
fix(vision): correct test license header
marconi-prog 152a388
test(vision): add regression tests for encoded traversal and Unicode …
marconi-prog 8760bc7
test(vision): align SSRF tests with merged imageUrl validation
marconi-prog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
275 changes: 275 additions & 0 deletions
275
vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,275 @@ | ||
| /* | ||
| * Copyright 2026 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. | ||
| */ | ||
|
|
||
| package com.example.vision; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.fail; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.verifyNoInteractions; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.MockitoAnnotations; | ||
| import org.springframework.cloud.gcp.vision.CloudVisionTemplate; | ||
| import org.springframework.core.io.Resource; | ||
| import org.springframework.core.io.ResourceLoader; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
| import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
| import org.springframework.web.server.ResponseStatusException; | ||
|
|
||
| /** | ||
| * Unit tests for the SSRF / local-file-read guard in {@link VisionController}. These tests do not | ||
| * call Cloud Vision or fetch any URL: the Vision template and resource loader are mocked so the | ||
| * endpoint tests can assert that rejected URLs never reach them. | ||
| */ | ||
| @RunWith(JUnit4.class) | ||
| public class VisionControllerTest { | ||
|
|
||
| private static final String VALID_URL = | ||
| "https://storage.googleapis.com/cloud-samples-data/vision/text/screen.jpg"; | ||
|
|
||
| @Mock private CloudVisionTemplate cloudVisionTemplate; | ||
| @Mock private ResourceLoader resourceLoader; | ||
| @InjectMocks private VisionController controller; | ||
|
|
||
| private AutoCloseable mocks; | ||
| private MockMvc mvc; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| mocks = MockitoAnnotations.openMocks(this); | ||
| mvc = MockMvcBuilders.standaloneSetup(controller).build(); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() throws Exception { | ||
| mocks.close(); | ||
| } | ||
|
|
||
| private static void assertRejected(String imageUrl) { | ||
| try { | ||
| VisionController.requireAllowedImageUrl(imageUrl); | ||
| fail("Expected imageUrl to be rejected: " + imageUrl); | ||
| } catch (ResponseStatusException e) { | ||
| assertEquals(HttpStatus.BAD_REQUEST, e.getStatus()); | ||
| } | ||
| } | ||
|
|
||
| private static void assertAccepted(String imageUrl) { | ||
| try { | ||
| VisionController.requireAllowedImageUrl(imageUrl); | ||
| } catch (ResponseStatusException e) { | ||
| fail("Expected imageUrl to be accepted: " + imageUrl + " (" + e.getReason() + ")"); | ||
| } | ||
| } | ||
|
|
||
| // Accepted URLs. | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_acceptsHttpsStorageObject() { | ||
| assertAccepted(VALID_URL); | ||
| } | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_acceptsUppercaseSchemeAndHost() { | ||
| assertAccepted("HTTPS://STORAGE.GOOGLEAPIS.COM/cloud-samples-data/vision/label/wakeupcat.jpg"); | ||
| } | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_acceptsObjectPathWithQueryString() { | ||
| assertAccepted("https://storage.googleapis.com/my-bucket/dir/image%20name.png?generation=1"); | ||
| } | ||
|
|
||
| // Missing or malformed input. | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_rejectsNullAndBlank() { | ||
| assertRejected(null); | ||
| assertRejected(""); | ||
| assertRejected(" "); | ||
| } | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_rejectsMalformedUri() { | ||
| assertRejected("not a url"); | ||
| assertRejected("https://storage.googleapis.com\\@evil.example/b/o"); | ||
| assertRejected(" https://storage.googleapis.com/b/o"); | ||
| assertRejected("https://storage.googleapis.com/b/o\n"); | ||
| } | ||
|
|
||
| // Schemes and Spring ResourceLoader prefixes. | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_rejectsNonHttpsSchemes() { | ||
| assertRejected("http://storage.googleapis.com/b/o.jpg"); | ||
| assertRejected("file:///etc/passwd"); | ||
| assertRejected("ftp://storage.googleapis.com/b/o.jpg"); | ||
| assertRejected("jar:https://storage.googleapis.com/b/o.jar!/x.jpg"); | ||
| assertRejected("gs://bucket/object.jpg"); | ||
| } | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_rejectsResourceLoaderLocations() { | ||
| assertRejected("classpath:application.properties"); | ||
| assertRejected("/etc/passwd"); | ||
| assertRejected("https:/storage.googleapis.com/b/o"); | ||
| assertRejected("https:storage.googleapis.com/b/o"); | ||
| } | ||
|
|
||
| // Host checks. | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_rejectsOtherHosts() { | ||
| assertRejected("https://www.google.com/images/branding/googlelogo.png"); | ||
| assertRejected("https://169.254.169.254/computeMetadata/v1/"); | ||
| assertRejected("https://metadata.google.internal/computeMetadata/v1/"); | ||
| assertRejected("https://[::1]/x"); | ||
| assertRejected("https://[::ffff:169.254.169.254]/x"); | ||
| assertRejected("https://0xa9fea9fe/x"); | ||
| } | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_rejectsSubdomainAndSuffixSpoofing() { | ||
| assertRejected("https://evil.storage.googleapis.com/b/o"); | ||
| assertRejected("https://evilstorage.googleapis.com/b/o"); | ||
| assertRejected("https://storage.googleapis.com.evil.example/b/o"); | ||
| assertRejected("https://storage.googleapis.com./b/o"); | ||
| assertRejected("https://storage%2egoogleapis.com/b/o"); | ||
| } | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_rejectsUnicodeCaseFoldingLookalikes() { | ||
| // String.equalsIgnoreCase treats U+017F (long s) as 's', U+0131 (dotless i) as 'i' and | ||
| // U+212A (Kelvin sign) as 'k'; these must still be rejected. | ||
| assertRejected("https://ſtorage.googleapis.com/b/o"); | ||
| assertRejected("https://storage.googleapıs.com/b/o"); | ||
| assertRejected("https://storage.googKeapis.com/b/o"); | ||
| } | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_rejectsUserInfo() { | ||
| assertRejected("https://storage.googleapis.com@evil.example/b/o"); | ||
| assertRejected("https://user:password@storage.googleapis.com/b/o"); | ||
| assertRejected("https://@storage.googleapis.com/b/o"); | ||
| assertRejected("https://evil.example#@storage.googleapis.com/b/o"); | ||
| assertRejected("https://evil.example?@storage.googleapis.com/b/o"); | ||
| } | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_acceptsExplicitDefaultHttpsPort() { | ||
| assertAccepted("https://storage.googleapis.com:443/b/o.jpg"); | ||
| } | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_rejectsNonDefaultPorts() { | ||
| assertRejected("https://storage.googleapis.com:80/b/o"); | ||
| assertRejected("https://storage.googleapis.com:8080/b/o"); | ||
| assertRejected("https://storage.googleapis.com:abc/b/o"); | ||
| } | ||
|
|
||
| // Path checks. | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_rejectsEmptyPath() { | ||
| assertRejected("https://storage.googleapis.com"); | ||
| assertRejected("https://storage.googleapis.com/"); | ||
| assertRejected("https://storage.googleapis.com?x=1"); | ||
| } | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_rejectsDotDotSegments() { | ||
| assertRejected("https://storage.googleapis.com/b/../../x"); | ||
| assertRejected("https://storage.googleapis.com/../computeMetadata"); | ||
| assertRejected("https://storage.googleapis.com/b/../x"); | ||
| } | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_rejectsPercentEncodedDotDotSegments() { | ||
| // getRawPath() keeps these encoded, so only the decoded-path check catches them. | ||
| assertRejected("https://storage.googleapis.com/b/%2e%2e/x"); | ||
| assertRejected("https://storage.googleapis.com/b/%2e%2e/%2e%2e/x"); | ||
| assertRejected("https://storage.googleapis.com/b/%2E%2e/x"); | ||
| assertRejected("https://storage.googleapis.com/b/%2e%2E/x"); | ||
| assertRejected("https://storage.googleapis.com/b/%2E%2E/x"); | ||
| assertRejected("https://storage.googleapis.com/b/.%2e/x"); | ||
| assertRejected("https://storage.googleapis.com/b/%2e./x"); | ||
| assertRejected("https://storage.googleapis.com/%2e%2e/computeMetadata"); | ||
| } | ||
|
|
||
| @Test | ||
| public void requireAllowedImageUrl_acceptsSingleEncodedDot() { | ||
| assertAccepted("https://storage.googleapis.com/b/%2e/x.jpg"); | ||
| assertAccepted("https://storage.googleapis.com/b/file%2ejpg"); | ||
| } | ||
|
|
||
| // Endpoint behavior. | ||
|
|
||
| @Test | ||
| public void extractText_invalidUrl_returnsBadRequestWithoutLoadingResource() throws Exception { | ||
| mvc.perform(get("/extractText").param("imageUrl", "file:///etc/passwd")) | ||
| .andExpect(status().isBadRequest()); | ||
| mvc.perform(get("/extractText")).andExpect(status().isBadRequest()); | ||
|
|
||
| verifyNoInteractions(resourceLoader, cloudVisionTemplate); | ||
| } | ||
|
|
||
| @Test | ||
| public void extractLabels_invalidUrl_returnsBadRequestWithoutLoadingResource() throws Exception { | ||
| mvc.perform( | ||
| get("/extractLabels") | ||
| .param("imageUrl", "https://169.254.169.254/computeMetadata/v1/")) | ||
| .andExpect(status().isBadRequest()); | ||
|
|
||
| verifyNoInteractions(resourceLoader, cloudVisionTemplate); | ||
| } | ||
|
|
||
| @Test | ||
| public void extractText_encodedTraversal_returnsBadRequestWithoutLoadingResource() | ||
| throws Exception { | ||
| mvc.perform( | ||
| get("/extractText") | ||
| .param("imageUrl", "https://storage.googleapis.com/b/%2e%2e/%2e%2e/x")) | ||
| .andExpect(status().isBadRequest()); | ||
|
|
||
| verifyNoInteractions(resourceLoader, cloudVisionTemplate); | ||
| } | ||
|
|
||
| @Test | ||
| public void extractText_validUrl_loadsResourceAndCallsVision() throws Exception { | ||
| Resource resource = mock(Resource.class); | ||
| when(resourceLoader.getResource(VALID_URL)).thenReturn(resource); | ||
| when(cloudVisionTemplate.extractTextFromImage(resource)).thenReturn("hello"); | ||
|
|
||
| mvc.perform(get("/extractText").param("imageUrl", VALID_URL)) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(content().string("Text from image: hello")); | ||
|
|
||
| verify(resourceLoader).getResource(VALID_URL); | ||
| verify(cloudVisionTemplate).extractTextFromImage(resource); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add test cases to verify that percent-encoded dot-dot sequences (such as
%2e%2e) are also correctly rejected to prevent path traversal bypasses.