From f94e8b1ace0581af9240beed237ccd21fd923525 Mon Sep 17 00:00:00 2001 From: Marconi-prog Date: Mon, 23 Nov 2026 16:25:10 -0300 Subject: [PATCH 1/4] test(vision): add SSRF protection tests for Spring sample --- vision/spring-framework/pom.xml | 11 + .../example/vision/VisionControllerTest.java | 239 ++++++++++++++++++ 2 files changed, 250 insertions(+) create mode 100644 vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java diff --git a/vision/spring-framework/pom.xml b/vision/spring-framework/pom.xml index dde513c712a..6cb8c893f40 100644 --- a/vision/spring-framework/pom.xml +++ b/vision/spring-framework/pom.xml @@ -76,6 +76,17 @@ limitations under the License. org.springframework.boot spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + test + diff --git a/vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java b/vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java new file mode 100644 index 00000000000..bfaa35f0d7e --- /dev/null +++ b/vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java @@ -0,0 +1,239 @@ +/* + * 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_rejectsExplicitPorts() { + assertRejected("https://storage.googleapis.com:443/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"); + } + + // 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_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); + } +} From deb4fab5626249a0b24858e315f3a84846969148 Mon Sep 17 00:00:00 2001 From: Marconi-prog Date: Mon, 23 Nov 2026 16:37:11 -0300 Subject: [PATCH 2/4] fix(vision): correct test license header --- .../src/test/java/com/example/vision/VisionControllerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java b/vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java index bfaa35f0d7e..756202675ab 100644 --- a/vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java +++ b/vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java @@ -5,7 +5,7 @@ * 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 + * https://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, From 152a38815c0153b698a637a8390928a8bbebdfef Mon Sep 17 00:00:00 2001 From: Marconi-prog Date: Mon, 23 Nov 2026 16:44:09 -0300 Subject: [PATCH 3/4] test(vision): add regression tests for encoded traversal and Unicode hosts - Add tests for percent-encoded ".." traversal (e.g. %2e%2e), which only the decoded-path check catches. - Add tests for Unicode case-folding host lookalikes and ASCII case-insensitive hosts. - Restore the http:// license URL in VisionControllerTest to match repo convention. --- .../example/vision/VisionControllerTest.java | 66 ++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java b/vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java index 756202675ab..fd23a95d7fc 100644 --- a/vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java +++ b/vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * 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, @@ -17,6 +17,8 @@ package com.example.vision; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -171,6 +173,37 @@ public void requireAllowedImageUrl_rejectsUnicodeCaseFoldingLookalikes() { assertRejected("https://storage.googKeapis.com/b/o"); } + @Test + public void isAllowedImageHost_rejectsNonAsciiLookalikesThatEqualsIgnoreCaseWouldAccept() { + // Exercises the ASCII guard directly: java.net.URI currently returns a null host for these + // authorities, so the URL-level test above would pass even without the guard. + String[] lookalikes = { + "ſtorage.googleapis.com", // U+017F long s folds to 's' + "storage.googleapıs.com", // U+0131 dotless i folds to 'i' + "ſtorage.googleapıs.com", // both + }; + for (String host : lookalikes) { + assertTrue( + "precondition: equalsIgnoreCase matches " + host, + "storage.googleapis.com".equalsIgnoreCase(host)); + assertFalse( + "Expected host to be rejected: " + host, VisionController.isAllowedImageHost(host)); + } + // U+212A Kelvin sign folds to 'k'. The allowed host has no 'k', so it cannot produce a + // case-insensitive match here, but any non-ASCII host must still be rejected. + assertFalse(VisionController.isAllowedImageHost("storage.googKeapis.com")); // U+212A + assertFalse(VisionController.isAllowedImageHost("Kstorage.googleapis.com")); // U+212A + } + + @Test + public void isAllowedImageHost_acceptsAsciiHostInAnyCase() { + assertTrue(VisionController.isAllowedImageHost("storage.googleapis.com")); + assertTrue(VisionController.isAllowedImageHost("STORAGE.GOOGLEAPIS.COM")); + assertTrue(VisionController.isAllowedImageHost("Storage.GoogleApis.Com")); + assertFalse(VisionController.isAllowedImageHost(null)); + assertFalse(VisionController.isAllowedImageHost("")); + } + @Test public void requireAllowedImageUrl_rejectsUserInfo() { assertRejected("https://storage.googleapis.com@evil.example/b/o"); @@ -200,6 +233,26 @@ public void requireAllowedImageUrl_rejectsEmptyPath() { 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. @@ -223,6 +276,17 @@ public void extractLabels_invalidUrl_returnsBadRequestWithoutLoadingResource() t 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); From 8760bc77359a7afec240fa4416a26afa4cd4e4cd Mon Sep 17 00:00:00 2001 From: Marconi-prog Date: Fri, 27 Nov 2026 02:57:42 +0000 Subject: [PATCH 4/4] test(vision): align SSRF tests with merged imageUrl validation #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. --- .../example/vision/VisionControllerTest.java | 42 ++++--------------- 1 file changed, 7 insertions(+), 35 deletions(-) diff --git a/vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java b/vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java index fd23a95d7fc..7757ae12f30 100644 --- a/vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java +++ b/vision/spring-framework/src/test/java/com/example/vision/VisionControllerTest.java @@ -17,8 +17,6 @@ package com.example.vision; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -173,37 +171,6 @@ public void requireAllowedImageUrl_rejectsUnicodeCaseFoldingLookalikes() { assertRejected("https://storage.googKeapis.com/b/o"); } - @Test - public void isAllowedImageHost_rejectsNonAsciiLookalikesThatEqualsIgnoreCaseWouldAccept() { - // Exercises the ASCII guard directly: java.net.URI currently returns a null host for these - // authorities, so the URL-level test above would pass even without the guard. - String[] lookalikes = { - "ſtorage.googleapis.com", // U+017F long s folds to 's' - "storage.googleapıs.com", // U+0131 dotless i folds to 'i' - "ſtorage.googleapıs.com", // both - }; - for (String host : lookalikes) { - assertTrue( - "precondition: equalsIgnoreCase matches " + host, - "storage.googleapis.com".equalsIgnoreCase(host)); - assertFalse( - "Expected host to be rejected: " + host, VisionController.isAllowedImageHost(host)); - } - // U+212A Kelvin sign folds to 'k'. The allowed host has no 'k', so it cannot produce a - // case-insensitive match here, but any non-ASCII host must still be rejected. - assertFalse(VisionController.isAllowedImageHost("storage.googKeapis.com")); // U+212A - assertFalse(VisionController.isAllowedImageHost("Kstorage.googleapis.com")); // U+212A - } - - @Test - public void isAllowedImageHost_acceptsAsciiHostInAnyCase() { - assertTrue(VisionController.isAllowedImageHost("storage.googleapis.com")); - assertTrue(VisionController.isAllowedImageHost("STORAGE.GOOGLEAPIS.COM")); - assertTrue(VisionController.isAllowedImageHost("Storage.GoogleApis.Com")); - assertFalse(VisionController.isAllowedImageHost(null)); - assertFalse(VisionController.isAllowedImageHost("")); - } - @Test public void requireAllowedImageUrl_rejectsUserInfo() { assertRejected("https://storage.googleapis.com@evil.example/b/o"); @@ -214,8 +181,13 @@ public void requireAllowedImageUrl_rejectsUserInfo() { } @Test - public void requireAllowedImageUrl_rejectsExplicitPorts() { - assertRejected("https://storage.googleapis.com:443/b/o"); + 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"); }