From 82748eeeb707064062614e326bf25e6253324049 Mon Sep 17 00:00:00 2001 From: Jin Seop Kim Date: Wed, 2 Dec 2026 18:51:17 -0500 Subject: [PATCH] fix(datastore): avoid DNS lookup for default host in isLocalHost --- .../cloud/datastore/DatastoreUtils.java | 34 ++++++++++++++++--- .../datastore/spi/v1/HttpDatastoreRpc.java | 29 ++-------------- .../cloud/datastore/DatastoreUtilsTest.java | 9 +++++ 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreUtils.java b/java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreUtils.java index e991fd51d02b..104758bc10f1 100644 --- a/java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreUtils.java +++ b/java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreUtils.java @@ -19,12 +19,17 @@ import com.google.api.core.InternalApi; import com.google.cloud.NoCredentials; import com.google.common.base.Strings; +import com.google.common.net.InetAddresses; import java.net.InetAddress; +import java.net.MalformedURLException; import java.net.URL; +import java.net.UnknownHostException; @InternalApi public class DatastoreUtils { + private static final String DEFAULT_DATASTORE_HOSTNAME = "datastore.googleapis.com"; + public static boolean isEmulator(DatastoreOptions datastoreOptions) { return isLocalHost(datastoreOptions.getHost()) || NoCredentials.getInstance().equals(datastoreOptions.getCredentials()); @@ -34,12 +39,33 @@ public static boolean isLocalHost(String host) { if (Strings.isNullOrEmpty(host)) { return false; } + String hostWithoutScheme = removeScheme(host); + if (Strings.isNullOrEmpty(hostWithoutScheme)) { + return false; + } try { - String normalizedHost = "http://" + removeScheme(host); - InetAddress hostAddr = InetAddress.getByName(new URL(normalizedHost).getHost()); + String hostname = new URL("http://" + hostWithoutScheme).getHost(); + // Short-circuit the default Datastore host before DNS resolution so default clients in + // air-gapped or non-default-universe networks never look up datastore.googleapis.com. + if (Strings.isNullOrEmpty(hostname) || DEFAULT_DATASTORE_HOSTNAME.equals(hostname)) { + return false; + } + // Avoid DNS lookups for literal "localhost" and numeric IPv4/IPv6 addresses (e.g., 127.0.0.1, + // 0.0.0.0, [::1]). + if ("localhost".equals(hostname)) { + return true; + } + if (InetAddresses.isUriInetAddress(hostname)) { + InetAddress hostAddr = InetAddresses.forUriString(hostname); + return hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress(); + } + // Fall back to DNS for custom local aliases (e.g., /etc/hosts entries mapped to loopback). + InetAddress hostAddr = InetAddress.getByName(hostname); return hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress(); - } catch (Exception e) { - throw new RuntimeException(e); + } catch (UnknownHostException | MalformedURLException e) { + // Unresolvable or malformed hosts are not local emulators; fail open to non-emulator mode + // instead of crashing client initialization in air-gapped networks. + return false; } } diff --git a/java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/spi/v1/HttpDatastoreRpc.java b/java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/spi/v1/HttpDatastoreRpc.java index e54c1a512271..6272bd6946b4 100644 --- a/java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/spi/v1/HttpDatastoreRpc.java +++ b/java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/spi/v1/HttpDatastoreRpc.java @@ -16,6 +16,9 @@ package com.google.cloud.datastore.spi.v1; +import static com.google.cloud.datastore.DatastoreUtils.isLocalHost; +import static com.google.cloud.datastore.DatastoreUtils.removeScheme; + import com.google.api.client.http.HttpRequest; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.HttpTransport; @@ -47,8 +50,6 @@ import com.google.rpc.Code; import io.opencensus.trace.Tracing; import java.io.IOException; -import java.net.InetAddress; -import java.net.URL; import javax.annotation.Nullable; @InternalApi @@ -184,30 +185,6 @@ public void initialize(HttpRequest httpRequest) throws IOException { }; } - private static boolean isLocalHost(String host) { - if (!host.isEmpty()) { - try { - String normalizedHost = "http://" + removeScheme(host); - InetAddress hostAddr = InetAddress.getByName(new URL(normalizedHost).getHost()); - return hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress(); - } catch (Exception e) { - // ignore - } - } - return false; - } - - private static String removeScheme(String url) { - if (url != null) { - if (url.startsWith("https://")) { - return url.substring("https://".length()); - } else if (url.startsWith("http://")) { - return url.substring("http://".length()); - } - } - return url; - } - private static DatastoreException translate( com.google.datastore.v1.client.DatastoreException exception) { return translate(exception, true); diff --git a/java-datastore/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreUtilsTest.java b/java-datastore/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreUtilsTest.java index 74d7e9d288f4..23804a1f26f9 100644 --- a/java-datastore/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreUtilsTest.java +++ b/java-datastore/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreUtilsTest.java @@ -20,6 +20,8 @@ import static com.google.cloud.datastore.DatastoreUtils.removeScheme; import static com.google.common.truth.Truth.assertThat; +import com.google.cloud.datastore.v1.DatastoreSettings; +import com.google.datastore.v1.client.DatastoreFactory; import org.junit.Test; public class DatastoreUtilsTest { @@ -30,7 +32,14 @@ public void testIsLocalHost() { assertThat(isLocalHost("")).isFalse(); assertThat(isLocalHost("http://localhost:9090")).isTrue(); assertThat(isLocalHost("https://localhost:9090")).isTrue(); + assertThat(isLocalHost("127.0.0.1:8081")).isTrue(); + assertThat(isLocalHost("0.0.0.0:8081")).isTrue(); + assertThat(isLocalHost("http://[::1]:8081")).isTrue(); assertThat(isLocalHost("10.10.10.10:9090")).isFalse(); + assertThat(isLocalHost(DatastoreSettings.getDefaultEndpoint())).isFalse(); + assertThat(isLocalHost(DatastoreFactory.DEFAULT_HOST)).isFalse(); + assertThat(isLocalHost("unresolvable.invalid:443")).isFalse(); + assertThat(isLocalHost(":::invalid")).isFalse(); } @Test