1
0
mirror of https://github.com/square/okhttp.git synced 2025-08-08 23:42:08 +03:00

Avoid InetAddress.getCanonicalHostName(). (#7607)

Just hard-coding 'localhost' is simpler and avoids problems where
the host machine's IP address has additional DNS registrations.

Closes: https://github.com/square/okhttp/issues/7603
This commit is contained in:
Jesse Wilson
2023-01-02 09:04:37 -05:00
committed by GitHub
parent eba89ae4a0
commit 847b5af240
7 changed files with 10 additions and 15 deletions

View File

@@ -154,7 +154,7 @@ class MockWebServer : Closeable {
val hostName: String val hostName: String
get() { get() {
before() before()
return _inetSocketAddress!!.address.canonicalHostName return _inetSocketAddress!!.address.hostName
} }
private var _inetSocketAddress: InetSocketAddress? = null private var _inetSocketAddress: InetSocketAddress? = null
@@ -205,7 +205,7 @@ class MockWebServer : Closeable {
fun toProxyAddress(): Proxy { fun toProxyAddress(): Proxy {
before() before()
val address = InetSocketAddress(_inetSocketAddress!!.address.canonicalHostName, port) val address = InetSocketAddress(_inetSocketAddress!!.address.hostName, port)
return Proxy(Proxy.Type.HTTP, address) return Proxy(Proxy.Type.HTTP, address)
} }

View File

@@ -45,13 +45,11 @@ import org.opentest4j.TestAbortedException
*/ */
@ExtendWith(MockWebServerExtension::class) @ExtendWith(MockWebServerExtension::class)
class AndroidAsyncDnsTest { class AndroidAsyncDnsTest {
private val localhostName: String = InetAddress.getByName("localhost").canonicalHostName
private val localhost: HandshakeCertificates by lazy { private val localhost: HandshakeCertificates by lazy {
// Generate a self-signed cert for the server to serve and the client to trust. // Generate a self-signed cert for the server to serve and the client to trust.
val heldCertificate = HeldCertificate.Builder() val heldCertificate = HeldCertificate.Builder()
.commonName("localhost") .addSubjectAlternativeName("localhost")
.addSubjectAlternativeName(localhostName)
.build() .build()
return@lazy HandshakeCertificates.Builder() return@lazy HandshakeCertificates.Builder()
.addPlatformTrustedCertificates() .addPlatformTrustedCertificates()
@@ -111,7 +109,7 @@ class AndroidAsyncDnsTest {
@Test @Test
@Disabled("No results on CI for localhost") @Disabled("No results on CI for localhost")
fun testDnsRequest() { fun testDnsRequest() {
val (allAddresses, exception) = dnsQuery(localhostName) val (allAddresses, exception) = dnsQuery("localhost")
assertThat(exception).isNull() assertThat(exception).isNull()
assertThat(allAddresses).isNotEmpty assertThat(allAddresses).isNotEmpty

View File

@@ -367,7 +367,7 @@ open class PlatformRule @JvmOverloads constructor(
private val localhostHandshakeCertificatesWithRsa2048: HandshakeCertificates by lazy { private val localhostHandshakeCertificatesWithRsa2048: HandshakeCertificates by lazy {
val heldCertificate = HeldCertificate.Builder() val heldCertificate = HeldCertificate.Builder()
.commonName("localhost") .commonName("localhost")
.addSubjectAlternativeName(InetAddress.getByName("localhost").canonicalHostName) .addSubjectAlternativeName("localhost")
.rsa2048() .rsa2048()
.build() .build()
return@lazy HandshakeCertificates.Builder() return@lazy HandshakeCertificates.Builder()

View File

@@ -8,9 +8,8 @@ A [`HeldCertificate`][held_certificate] is a certificate and its private key. Us
for HTTPS: for HTTPS:
```java ```java
String localhost = InetAddress.getByName("localhost").getCanonicalHostName();
HeldCertificate localhostCertificate = new HeldCertificate.Builder() HeldCertificate localhostCertificate = new HeldCertificate.Builder()
.addSubjectAlternativeName(localhost) .addSubjectAlternativeName("localhost")
.build(); .build();
``` ```
@@ -62,9 +61,8 @@ HeldCertificate intermediateCertificate = new HeldCertificate.Builder()
.signedBy(rootCertificate) .signedBy(rootCertificate)
.build(); .build();
String localhost = InetAddress.getByName("localhost").getCanonicalHostName();
HeldCertificate serverCertificate = new HeldCertificate.Builder() HeldCertificate serverCertificate = new HeldCertificate.Builder()
.addSubjectAlternativeName(localhost) .addSubjectAlternativeName("localhost")
.signedBy(intermediateCertificate) .signedBy(intermediateCertificate)
.build(); .build();
``` ```

View File

@@ -37,7 +37,7 @@ object TlsUtil {
// Generate a self-signed cert for the server to serve and the client to trust. // Generate a self-signed cert for the server to serve and the client to trust.
val heldCertificate = HeldCertificate.Builder() val heldCertificate = HeldCertificate.Builder()
.commonName("localhost") .commonName("localhost")
.addSubjectAlternativeName(InetAddress.getByName("localhost").canonicalHostName) .addSubjectAlternativeName("localhost")
.build() .build()
return@lazy HandshakeCertificates.Builder() return@lazy HandshakeCertificates.Builder()
.heldCertificate(heldCertificate) .heldCertificate(heldCertificate)

View File

@@ -99,7 +99,7 @@ class OpenJSSETest {
// can't use TlsUtil.localhost with a non OpenJSSE trust manager // can't use TlsUtil.localhost with a non OpenJSSE trust manager
val heldCertificate = HeldCertificate.Builder() val heldCertificate = HeldCertificate.Builder()
.commonName("localhost") .commonName("localhost")
.addSubjectAlternativeName(InetAddress.getByName("localhost").canonicalHostName) .addSubjectAlternativeName("localhost")
.build() .build()
val handshakeCertificates = HandshakeCertificates.Builder() val handshakeCertificates = HandshakeCertificates.Builder()
.heldCertificate(heldCertificate) .heldCertificate(heldCertificate)

View File

@@ -30,9 +30,8 @@ import okhttp3.tls.HeldCertificate;
*/ */
public class HttpsServer { public class HttpsServer {
public void run() throws Exception { public void run() throws Exception {
String localhost = InetAddress.getByName("localhost").getCanonicalHostName();
HeldCertificate localhostCertificate = new HeldCertificate.Builder() HeldCertificate localhostCertificate = new HeldCertificate.Builder()
.addSubjectAlternativeName(localhost) .addSubjectAlternativeName("localhost")
.build(); .build();
HandshakeCertificates serverCertificates = new HandshakeCertificates.Builder() HandshakeCertificates serverCertificates = new HandshakeCertificates.Builder()