Repository navigation
test(vision): add SSRF protection tests for Spring sample - #10392
marconi-prog wants to merge 4 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");064eb13 to
6725481
Compare
|
Thanks for putting together these tests, @marconi-prog! I'm landing the minimal security fix in #10389 first (including the Once #10389 is merged, could you rebase this PR onto
|
|
Thanks for the clarification! I'll wait for #10389 to be merged, then rebase my PR onto I'll make sure the PR contains only the test changes after the rebase. |
…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.
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.
6725481 to
8760bc7
Compare
|
Hi @tpryan, I've rebased PR #10392 on the latest I also updated the regression tests to match the merged implementation, including the explicit HTTPS port Thanks again for your guidance! |
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