Repository navigation
test(vision): add SSRF protection tests for Spring sample - #10392
marconi-prog wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces an SSRF and local-file-read guard in VisionController by restricting user-supplied image URLs to HTTPS on storage.googleapis.com, updates the default UI URLs, and adds comprehensive unit tests. The review feedback highlights two critical security vulnerabilities in the URL validation logic: a percent-encoded path traversal bypass due to the use of uri.getRawPath() instead of uri.getPath(), and a potential homograph bypass via Unicode case-folding in String.equalsIgnoreCase. It is recommended to decode percent-encoded characters, enforce ASCII-only host checks, and add corresponding test cases for percent-encoded dot-dot sequences.
| 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.
This validation logic contains two security vulnerabilities:
- Percent-encoded Path Traversal Bypass: Using
uri.getRawPath()does not decode percent-encoded characters. An attacker can bypass the..check using percent-encoded sequences like%2e%2e(e.g.,https://storage.googleapis.com/b/%2e%2e/x). Usinguri.getPath()instead ofuri.getRawPath()ensures that percent-encoded characters are decoded before the validation checks are performed. - Unicode Case-Folding / Homograph Bypass:
String.equalsIgnoreCaseperforms Unicode case folding, which maps characters likeſ(U+017F) tosandK(U+212A) tok. An attacker could potentially use a domain with these characters to bypass the host check, while the underlying HTTP client/resolver might resolve it to an attacker-controlled domain. Since the allowed host is strictly ASCII, we should verify that the host contains only ASCII characters before performing the case-insensitive comparison.
String host = uri.getHost();
boolean isAsciiHost = true;
if (host != null) {
for (int i = 0; i < host.length(); i++) {
if (host.charAt(i) > 127) {
isAsciiHost = false;
break;
}
}
}
String path = uri.getPath();
if (!"https".equalsIgnoreCase(uri.getScheme())
|| host == null
|| !isAsciiHost
|| !ALLOWED_IMAGE_HOST.equalsIgnoreCase(host)
|| uri.getPort() != -1
|| uri.getUserInfo() != null
|| path == null
|| path.length() <= 1
|| !path.startsWith("/")
|| path.contains("..")) {| assertRejected("https://storage.googleapis.com/b/../../x"); | ||
| assertRejected("https://storage.googleapis.com/../computeMetadata"); |
There was a problem hiding this comment.
Add test cases to verify that percent-encoded dot-dot sequences (such as %2e%2e) are also correctly rejected to prevent path traversal bypasses.
assertRejected("https://storage.googleapis.com/b/../../x");
assertRejected("https://storage.googleapis.com/../computeMetadata");
assertRejected("https://storage.googleapis.com/b/%2e%2e/%2e%2e/x");
assertRejected("https://storage.googleapis.com/b/%2e%2e/x");- Reject percent-encoded ".." traversal (e.g. %2e%2e) by also checking the decoded URI path; existing raw-path checks are kept. - Require an ASCII-only host before the case-insensitive comparison with storage.googleapis.com, so Unicode case-folding lookalikes cannot match. - Add the missing license header to static/index.html (the file the header-check bot actually flagged). - Restore the http:// license URL in VisionControllerTest to match repo convention. - Add regression tests for encoded traversal, Unicode host lookalikes and ASCII case-insensitive hosts.
064eb13 to
6725481
Compare
Description
Add comprehensive unit and endpoint tests for SSRF protection in the Spring Cloud Vision sample.
The tests cover:
storage.googleapis.com.Checklist
Testing
mvn clean verifymvn -P lint checkstyle:checkmvn -P lint clean compile pmd:cpd-check spotbugs:checkCompliance & Style
pom.xmlparent set to latestshared-configurationPost-Approval Actions