Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.EntityAnnotation;
import com.google.cloud.vision.v1.Feature.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gcp.vision.CloudVisionTemplate;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.HttpStatus;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.ModelAndView;

/**
Expand All @@ -44,6 +48,46 @@ public class VisionController {
@Autowired private CloudVisionTemplate cloudVisionTemplate;
// [END vision_spring_autowire]

private static final String ALLOWED_IMAGE_HOST = "storage.googleapis.com";

/**
* Restrict user-supplied image URLs to HTTPS URLs on {@link #ALLOWED_IMAGE_HOST}. Passing an
* arbitrary URI to {@link ResourceLoader#getResource(String)} allows callers to make the server
* open local files ({@code file:}) or issue SSRF requests to internal endpoints.
*/
static void requireAllowedImageUrl(String imageUrl) {
if (imageUrl == null || imageUrl.trim().isEmpty()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "imageUrl is required");
}
URI uri;
try {
uri = new URI(imageUrl);
} catch (URISyntaxException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid imageUrl", e);
}
String host = uri.getHost();
String rawPath = uri.getRawPath();
String path = uri.getPath();
int port = uri.getPort();
if (!"https".equalsIgnoreCase(uri.getScheme())
|| host == null
|| !host.chars().allMatch(c -> c < 0x80)
|| !ALLOWED_IMAGE_HOST.equalsIgnoreCase(host)
|| (port != -1 && port != 443)
|| uri.getUserInfo() != null
|| rawPath == null
|| rawPath.length() <= 1
|| !rawPath.startsWith("/")
|| rawPath.contains("..")
|| path == null
|| path.length() <= 1
|| !path.startsWith("/")
|| path.contains("..")) {
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST, "imageUrl must be an https://" + ALLOWED_IMAGE_HOST + "/... URL");
}
}

/**
* This method downloads an image from a URL and sends its contents to the Vision API for label
* detection.
Expand All @@ -54,6 +98,7 @@ public class VisionController {
*/
@GetMapping("/extractLabels")
public ModelAndView extractLabels(String imageUrl, ModelMap map) {
requireAllowedImageUrl(imageUrl);
// [START vision_spring_image_labelling]
AnnotateImageResponse response =
this.cloudVisionTemplate.analyzeImage(
Expand All @@ -79,6 +124,7 @@ public ModelAndView extractLabels(String imageUrl, ModelMap map) {

@GetMapping("/extractText")
public String extractText(String imageUrl) {
requireAllowedImageUrl(imageUrl);
// [START vision_spring_text_extraction]
String textFromImage =
this.cloudVisionTemplate.extractTextFromImage(this.resourceLoader.getResource(imageUrl));
Expand Down
19 changes: 17 additions & 2 deletions vision/spring-framework/src/main/resources/static/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
<!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">
<head>
<meta charset="UTF-8">
Expand All @@ -13,7 +28,7 @@ <h1>Image Label Annotations</h1>
Web URL of image to analyze:
<input type="text"
name="imageUrl"
value="https://www.google.com/logos/doodles/2018/childrens-day-2018-argentina-peru-5906663952351232-2x.png"/>
value="https://storage.googleapis.com/cloud-samples-data/vision/label/wakeupcat.jpg"/>
<input type="submit"/>
</form>
</div>
Expand All @@ -25,7 +40,7 @@ <h1>Text Extraction</h1>
Web URL of image to analyze:
<input type="text"
name="imageUrl"
value="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"/>
value="https://storage.googleapis.com/cloud-samples-data/vision/text/screen.jpg"/>
<input type="submit" />
</form>
</div>
Expand Down