Skip to content

Commit 714119f

Browse files
committed
Validate that hostname is ascii in OkHostnameVerifier.java
Sec vuln fix
1 parent 486b8ba commit 714119f

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

okhttp/third_party/okhttp/main/java/io/grpc/okhttp/internal/OkHostnameVerifier.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.List;
3030
import java.util.Locale;
3131
import java.util.regex.Pattern;
32+
import java.nio.charset.StandardCharsets;
3233
import javax.net.ssl.HostnameVerifier;
3334
import javax.net.ssl.SSLException;
3435
import javax.net.ssl.SSLSession;
@@ -63,6 +64,9 @@ private OkHostnameVerifier() {
6364

6465
@Override
6566
public boolean verify(String host, SSLSession session) {
67+
if (!isAscii(host)) {
68+
return false;
69+
}
6670
try {
6771
Certificate[] certificates = session.getPeerCertificates();
6872
return verify(host, (X509Certificate) certificates[0]);
@@ -98,7 +102,7 @@ private boolean verifyIpAddress(String ipAddress, X509Certificate certificate) {
98102
* Returns true if {@code certificate} matches {@code hostName}.
99103
*/
100104
private boolean verifyHostName(String hostName, X509Certificate certificate) {
101-
hostName = hostName.toLowerCase(Locale.US);
105+
hostName = asciiToLowercase(hostName);
102106
boolean hasDns = false;
103107
List<String> altNames = getSubjectAltNames(certificate, ALT_DNS_NAME);
104108
for (int i = 0, size = altNames.size(); i < size; i++) {
@@ -254,4 +258,22 @@ private boolean verifyHostName(String hostName, String pattern) {
254258
// hostName matches pattern
255259
return true;
256260
}
261+
262+
/**
263+
* Normalize the input to lowercase, if it is an ASCII string.
264+
* Avoid unicode characters like \u1E0E from returning lowercased ascii
265+
* that could match real hostnames.
266+
*/
267+
private static String asciiToLowercase(String input) {
268+
if (isAscii(input)) {
269+
return input.toLowerCase(Locale.US);
270+
} else {
271+
return input;
272+
}
273+
}
274+
275+
private static boolean isAscii(String input) {
276+
// Only ASCII characters are 1 byte in UTF-8.
277+
return input.getBytes(StandardCharsets.UTF_8).length == input.length();
278+
}
257279
}

0 commit comments

Comments
 (0)