Skip to content

Commit

Permalink
fix security issue with okhttp (#11749)
Browse files Browse the repository at this point in the history
* Validate that hostname is ascii in OkHostnameVerifier.java
  • Loading branch information
ZachChuba authored Dec 16, 2024
1 parent e8ff6da commit a0982ca
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;
import java.nio.charset.StandardCharsets;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.security.auth.x500.X500Principal;
import com.google.common.base.Utf8;
import com.google.common.base.Ascii;

/**
* A HostnameVerifier consistent with <a
Expand Down Expand Up @@ -63,6 +66,9 @@ private OkHostnameVerifier() {

@Override
public boolean verify(String host, SSLSession session) {
if (!isAscii(host)) {
return false;
}
try {
Certificate[] certificates = session.getPeerCertificates();
return verify(host, (X509Certificate) certificates[0]);
Expand All @@ -71,7 +77,7 @@ public boolean verify(String host, SSLSession session) {
}
}

public boolean verify(String host, X509Certificate certificate) {
private boolean verify(String host, X509Certificate certificate) {
return verifyAsIpAddress(host)
? verifyIpAddress(host, certificate)
: verifyHostName(host, certificate);
Expand All @@ -98,7 +104,7 @@ private boolean verifyIpAddress(String ipAddress, X509Certificate certificate) {
* Returns true if {@code certificate} matches {@code hostName}.
*/
private boolean verifyHostName(String hostName, X509Certificate certificate) {
hostName = hostName.toLowerCase(Locale.US);
hostName = Ascii.toLowerCase(hostName);
boolean hasDns = false;
List<String> altNames = getSubjectAltNames(certificate, ALT_DNS_NAME);
for (int i = 0, size = altNames.size(); i < size; i++) {
Expand Down Expand Up @@ -198,7 +204,7 @@ private boolean verifyHostName(String hostName, String pattern) {
}
// hostName and pattern are now absolute domain names.

pattern = pattern.toLowerCase(Locale.US);
pattern = Ascii.toLowerCase(pattern);
// hostName and pattern are now in lower case -- domain names are case-insensitive.

if (!pattern.contains("*")) {
Expand Down Expand Up @@ -254,4 +260,13 @@ private boolean verifyHostName(String hostName, String pattern) {
// hostName matches pattern
return true;
}

/**
* Returns true if {@code input} is an ASCII string.
* @param input the string to check.
*/
private static boolean isAscii(String input) {
// Only ASCII characters are 1 byte in UTF-8.
return Utf8.encodedLength(input) == input.length();
}
}

0 comments on commit a0982ca

Please sign in to comment.