check whether a server host is an ip literal as inetaddress reads one
What changed, and why it matters
This commit tightens how Sparrow Wallet decides whether a server address is a raw IP address (like 192.168.1.1) versus a hostname (like example.com). It replaces a more forgiving Google utility with Java's stricter InetAddress.ofLiteral check. The goal is to prevent hostnames that look like IP addresses from being treated as local network addresses when a proxy is in use, which could otherwise leak those names to local DNS or bypass the proxy. The change also updates warning messages shown to users about whether their Bitcoin Core server is local or remote.
Review whether any other code paths still use InetAddresses.isInetAddress or similar loose parsing for security decisions, and ensure the new isIpLiteral helper is used consistently. Confirm that the stricter parsing does not break legitimate user configurations (e.g., zone IDs are uncommon for wallet server addresses).
Security signals we found
Replaces permissive IP parsing with strict InetAddress.ofLiteral validation
Avoids local DNS resolution of ambiguous host strings when a proxy is configured
Prevents zone-ID IPv6 literals and full-width Unicode digit strings from being accepted as IP literals
Adds regression tests for proxy failure classification and onion URL normalization
Commit message explicitly frames the change as reading IP literals the way InetAddress reads them
Evidence from the diff
The patch removes use of com.google.common.net.InetAddresses.isInetAddress and introduces IpAddressMatcher.isIpLiteral, which delegates to Java’s InetAddress.ofLiteral. That method accepts only canonical IP literal syntax and rejects full-width Unicode digits, scoped IPv6 literals (zone IDs), and other strings the older parser might have accepted. The stricter check is used in two places: (1) ServerSettingsController.isRemoteNode, where a non-literal host is assumed remote without resolving it, and (2) IpAddressMatcher.isLocalNetworkAddress, where when a proxy is active only true IP literals are checked against local ranges to avoid local DNS leaks. A new ProxyFailureTest unit test verifies the stricter behavior and adds several proxy/onion-related regression tests.
Changed components
src/main/java/com/sparrowwallet/sparrow/net/IpAddressMatcher.javasrc/main/java/com/sparrowwallet/sparrow/settings/ServerSettingsController.javasrc/test/java/com/sparrowwallet/sparrow/net/ProxyFailureTest.javaInspect captured patch +155 / −5
### src/main/java/com/sparrowwallet/sparrow/net/IpAddressMatcher.java
@@ -16,7 +16,6 @@
* limitations under the License.
*/
-import com.google.common.net.InetAddresses;
import com.sparrowwallet.sparrow.AppServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -122,6 +121,16 @@ private static InetAddress parseAddress(String address) {
}
}
+ //An IP literal as InetAddress itself reads one, rather than as a looser parse would, so that no string it accepts is then resolved as a name
+ public static boolean isIpLiteral(String host) {
+ try {
+ InetAddress.ofLiteral(host);
+ return true;
+ } catch(IllegalArgumentException e) {
+ return false;
+ }
+ }
+
public static boolean isLocalNetworkName(String host) {
String lowerHost = host.toLowerCase(Locale.ROOT);
return "localhost".equals(lowerHost) || LOCAL_DOMAIN_SUFFIXES.stream().anyMatch(lowerHost::endsWith);
@@ -135,7 +144,7 @@ public static boolean isLocalNetworkAddress(String address) {
//Matching a hostname against the local ranges requires resolving it, which leaks the name to (and trusts the answer of) the local DNS resolver even when a proxy is configured
//Only IP literals are considered potentially local when using a proxy, since local network names have already returned above
- if(AppServices.isUsingProxy() && !InetAddresses.isInetAddress(address)) {
+ if(AppServices.isUsingProxy() && !isIpLiteral(address)) {
log.info("Avoiding local DNS resolution of " + address + ", assuming it is a non-local address to be resolved by the configured proxy");
return false;
}
### src/main/java/com/sparrowwallet/sparrow/settings/ServerSettingsController.java
@@ -4,7 +4,6 @@
import com.google.common.base.Throwables;
import com.google.common.eventbus.Subscribe;
import com.google.common.net.HostAndPort;
-import com.google.common.net.InetAddresses;
import com.sparrowwallet.drongo.Network;
import com.sparrowwallet.drongo.OsType;
import com.sparrowwallet.drongo.policy.PolicyType;
@@ -882,7 +881,7 @@ private void showRemoteCoreServerWarning(Config config) {
coreServerWarningShown = true;
//A host that is not an IP literal is not resolved here, so it can only be reported as unconfirmed
- String location = InetAddresses.isInetAddress(coreServer.getHost()) ? " is not on" : " could not be confirmed to be on";
+ String location = IpAddressMatcher.isIpLiteral(coreServer.getHost()) ? " is not on" : " could not be confirmed to be on";
StringBuilder warning = new StringBuilder("Bitcoin Core at " + coreServer.getHostAndPort() + location + " this computer or your local network.\n");
if(AppServices.isUsingProxy()) {
String proxy = config.isUseProxy() ? "configured proxy" : "internal Tor proxy";
@@ -911,7 +910,7 @@ private boolean isRemoteNode(Server coreServer) {
return false;
}
- if(!InetAddresses.isInetAddress(host)) {
+ if(!IpAddressMatcher.isIpLiteral(host)) {
//Resolving a hostname here would block the user interface thread, and an unresolved host cannot be shown to be local
return true;
}
### src/test/java/com/sparrowwallet/sparrow/net/ProxyFailureTest.java
@@ -0,0 +1,142 @@
+package com.sparrowwallet.sparrow.net;
+
+import com.google.common.net.HostAndPort;
+import com.sparrowwallet.sparrow.SparrowWallet;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.DataInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.nio.file.Path;
+import java.time.Duration;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class ProxyFailureTest {
+ @TempDir
+ private static Path tempHome;
+
+ @BeforeAll
+ public static void setUp() {
+ System.setProperty(SparrowWallet.APP_HOME_PROPERTY, tempHome.toString());
+ }
+
+ @AfterAll
+ public static void tearDown() {
+ System.clearProperty(SparrowWallet.APP_HOME_PROPERTY);
+ }
+
+ @Test
+ public void unreachableServerIsNotAProxyFailure() throws Exception {
+ try(ServerSocket proxy = new ServerSocket(0)) {
+ Thread socks = new Thread(() -> {
+ try(Socket socket = proxy.accept()) {
+ DataInputStream in = new DataInputStream(socket.getInputStream());
+ OutputStream out = socket.getOutputStream();
+ in.readByte();
+ in.readFully(new byte[in.readUnsignedByte()]);
+ out.write(new byte[] {5, 0});
+ in.readFully(new byte[4]);
+ in.readFully(new byte[in.readUnsignedByte() + 2]);
+ //Host unreachable for the server requested, one of the replies an unreachable onion service gives
+ out.write(new byte[] {5, 4, 0, 1, 0, 0, 0, 0, 0, 0});
+ out.flush();
+ InputStream ignored = socket.getInputStream();
+ ignored.read();
+ } catch(Exception e) {
+ //closed at the end of the test
+ }
+ });
+ socks.setDaemon(true);
+ socks.start();
+
+ TcpTransport transport = new TcpTransport(HostAndPort.fromParts("unreachable.onion", 50001), HostAndPort.fromParts("127.0.0.1", proxy.getLocalPort()));
+ ServerException exception = assertTimeoutPreemptively(Duration.ofSeconds(10), () -> assertThrows(ServerException.class, transport::connect));
+ assertFalse(exception instanceof ProxyServerException);
+ }
+ }
+
+ @Test
+ public void proxyThatIsNotSocksIsAProxyFailure() throws Exception {
+ try(ServerSocket proxy = new ServerSocket(0)) {
+ Thread http = new Thread(() -> {
+ try(Socket socket = proxy.accept()) {
+ socket.getInputStream().read(new byte[3]);
+ socket.getOutputStream().write("HTTP/1.1 400 Bad Request\r\n\r\n".getBytes());
+ socket.getOutputStream().flush();
+ socket.getInputStream().read();
+ } catch(Exception e) {
+ //closed at the end of the test
+ }
+ });
+ http.setDaemon(true);
+ http.start();
+
+ TcpTransport transport = new TcpTransport(HostAndPort.fromParts("server.onion", 50001), HostAndPort.fromParts("127.0.0.1", proxy.getLocalPort()));
+ assertTimeoutPreemptively(Duration.ofSeconds(10), () -> assertThrows(ProxyServerException.class, transport::connect));
+ }
+ }
+
+ @Test
+ public void proxyNotListeningIsAProxyFailure() throws Exception {
+ int port;
+ try(ServerSocket closed = new ServerSocket(0)) {
+ port = closed.getLocalPort();
+ }
+
+ TcpTransport transport = new TcpTransport(HostAndPort.fromParts("server.onion", 50001), HostAndPort.fromParts("127.0.0.1", port));
+ assertTimeoutPreemptively(Duration.ofSeconds(10), () -> assertThrows(ProxyServerException.class, transport::connect));
+ }
+
+ @Test
+ public void callsWithoutAConnectionDoNotCreateATransport() {
+ CloseableTransport previous = ElectrumServer.transport;
+ ElectrumServer.transport = null;
+ try {
+ assertThrows(ServerException.class, () -> new ElectrumServer().getServerVersion());
+ assertNull(ElectrumServer.transport);
+ } finally {
+ ElectrumServer.transport = previous;
+ }
+ }
+
+ @Test
+ public void onionHostWithTrailingDotIsOnion() {
+ assertTrue(Protocol.isOnionHost("example.onion."));
+ assertTrue(Protocol.isOnionHost("EXAMPLE.ONION"));
+ assertFalse(Protocol.isOnionHost("example.com."));
+ assertFalse(Protocol.isOnionHost("example.onion.."));
+ }
+
+ @Test
+ public void onionUrlWithTrailingDotIsNormalized() {
+ assertEquals("tcp://example.onion:50001", Protocol.normalizeOnionUrl("tcp://example.onion.:50001"));
+ assertEquals("ssl://example.onion", Protocol.normalizeOnionUrl("ssl://example.onion."));
+ assertEquals("example.onion:50002:s", Protocol.normalizeOnionUrl("example.onion.:50002:s"));
+ assertEquals("http://example.onion:8332/wallet/example.onion.", Protocol.normalizeOnionUrl("http://example.onion.:8332/wallet/example.onion."));
+ assertEquals("tcp://example.com.:50001", Protocol.normalizeOnionUrl("tcp://example.com.:50001"));
+ assertEquals("tcp://example.onion:50001", new com.sparrowwallet.sparrow.io.Server("tcp://example.onion.:50001").getUrl());
+ }
+
+ @Test
+ public void proxyWithoutPortUsesDefaultPort() {
+ TorHttpProxySupplier supplier = new TorHttpProxySupplier(null);
+ supplier._setTorProxy(HostAndPort.fromHost("127.0.0.1"));
+ assertEquals(HostAndPort.fromHost("127.0.0.1"), supplier.getTorProxy());
+ assertEquals(ProxyTcpOverTlsTransport.DEFAULT_PROXY_PORT, supplier.getHttpProxy(null).orElseThrow().getPort());
+ }
+
+ @Test
+ public void ipLiteralsAreReadAsInetAddressReadsThem() {
+ assertTrue(IpAddressMatcher.isIpLiteral("192.168.1.10"));
+ assertTrue(IpAddressMatcher.isIpLiteral("::1"));
+ assertFalse(IpAddressMatcher.isIpLiteral("\uff11\uff19\uff12.\uff11\uff16\uff18.1.1"));
+ assertFalse(IpAddressMatcher.isIpLiteral("192.168.1.1%eth0"));
+ assertFalse(IpAddressMatcher.isIpLiteral("example.com"));
+ }
+}Why this scored 61/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.