|
19 | 19 | import static com.google.common.base.Preconditions.checkNotNull;
|
20 | 20 |
|
21 | 21 | import com.google.common.annotations.VisibleForTesting;
|
| 22 | +import io.grpc.internal.GrpcUtil; |
22 | 23 | import io.grpc.okhttp.internal.OptionalMethod;
|
23 | 24 | import io.grpc.okhttp.internal.Platform;
|
24 | 25 | import io.grpc.okhttp.internal.Platform.TlsExtensionType;
|
@@ -235,7 +236,11 @@ protected void configureTlsExtensions(
|
235 | 236 | SSLParameters sslParams = sslSocket.getSSLParameters();
|
236 | 237 | try {
|
237 | 238 | // 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)) { |
239 | 244 | if (SSL_SOCKETS_IS_SUPPORTED_SOCKET != null
|
240 | 245 | && (boolean) SSL_SOCKETS_IS_SUPPORTED_SOCKET.invoke(null, sslSocket)) {
|
241 | 246 | SSL_SOCKETS_SET_USE_SESSION_TICKET.invoke(null, sslSocket, true);
|
@@ -356,4 +361,13 @@ private static String[] protocolIds(List<Protocol> protocols) {
|
356 | 361 | }
|
357 | 362 | return result.toArray(new String[0]);
|
358 | 363 | }
|
| 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 | + } |
359 | 373 | }
|
0 commit comments