Skip to content

Commit eb8e314

Browse files
authored
okhttp: Skip enabling SNI and session ticket for fake/test host names (#6949)
Work around for cases (usually for tests) where hostname is overridden for test certs and it is in invalid syntax.
1 parent 6bcc182 commit eb8e314

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

okhttp/src/main/java/io/grpc/okhttp/OkHttpProtocolNegotiator.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

2121
import com.google.common.annotations.VisibleForTesting;
22+
import io.grpc.internal.GrpcUtil;
2223
import io.grpc.okhttp.internal.OptionalMethod;
2324
import io.grpc.okhttp.internal.Platform;
2425
import io.grpc.okhttp.internal.Platform.TlsExtensionType;
@@ -235,7 +236,11 @@ protected void configureTlsExtensions(
235236
SSLParameters sslParams = sslSocket.getSSLParameters();
236237
try {
237238
// Enable SNI and session tickets.
238-
if (hostname != null) {
239+
// Hostname is normally validated in the builder (see checkAuthority) and it should
240+
// virtually always succeed. Check again here to avoid troubles (e.g., hostname with
241+
// underscore) enabling SNI, which works around cases where checkAuthority is disabled.
242+
// See b/154375837.
243+
if (hostname != null && isValidHostName(hostname)) {
239244
if (SSL_SOCKETS_IS_SUPPORTED_SOCKET != null
240245
&& (boolean) SSL_SOCKETS_IS_SUPPORTED_SOCKET.invoke(null, sslSocket)) {
241246
SSL_SOCKETS_SET_USE_SESSION_TICKET.invoke(null, sslSocket, true);
@@ -356,4 +361,13 @@ private static String[] protocolIds(List<Protocol> protocols) {
356361
}
357362
return result.toArray(new String[0]);
358363
}
364+
365+
private static boolean isValidHostName(String name) {
366+
try {
367+
GrpcUtil.checkAuthority(name);
368+
return true;
369+
} catch (IllegalArgumentException e) {
370+
return false;
371+
}
372+
}
359373
}

0 commit comments

Comments
 (0)