keep the saved server or proxy when an invalid port is entered in server settings, instead of connecting to the default port
What changed, and why it matters
This commit fixes a behavior in Sparrow Wallet's server and proxy settings where typing an invalid port number would silently fall back to the default port. After the change, an invalid port keeps the previously saved server or proxy instead of unexpectedly switching to a default port. This is a defensive fix that prevents accidental misrouting of wallet traffic.
Review and merge if not already included in a release; consider adding user-visible validation feedback when an invalid port is entered rather than silently retaining the old value.
Security signals we found
prevents silent fallback to default port on invalid user input
reduces risk of unintended server/proxy connection
defensive input validation change
Evidence from the diff
The patch adds an intermediate branch in three settings dialogs: when a host is provided and the port field is non-blank but invalid, the code now returns/retains the existing server or proxy configuration rather than falling through to the default-port branch. For proxies, if no prior proxy exists, it still falls back to the default port because a failing proxy is considered preferable to no proxy. The change affects Core server, Electrum server, and proxy configuration in both GUI and terminal settings.
Changed components
ServerSettingsController.javaServerProxyDialog.javaServerUrlDialog.javaInspect captured patch +17 / −0
### src/main/java/com/sparrowwallet/sparrow/settings/ServerSettingsController.java
@@ -829,6 +829,9 @@ private void setCoreServerInConfig(Config config) {
if(hostAsString != null && !hostAsString.isEmpty() && portAsInteger != null && isValidPort(portAsInteger)) {
Protocol protocol = portAsInteger == Protocol.HTTPS.getDefaultPort() || hostAsString.equalsIgnoreCase(coreHttpsHost) ? Protocol.HTTPS : Protocol.HTTP;
config.setCoreServer(new Server(protocol.toUrlString(hostAsString, portAsInteger)));
+ } else if(hostAsString != null && !hostAsString.isEmpty() && !corePort.getText().isBlank()) {
+ //An invalid port leaves the server as it was, rather than connecting to the default port as an empty one does
+ return;
} else if(hostAsString != null && !hostAsString.isEmpty()) {
Protocol protocol = hostAsString.equalsIgnoreCase(coreHttpsHost) ? Protocol.HTTPS : Protocol.HTTP;
config.setCoreServer(new Server(protocol.toUrlString(hostAsString)));
@@ -947,6 +950,9 @@ private void setElectrumServerInConfig(Config config) {
Integer portAsInteger = getPort(electrumPort.getText());
if(hostAsString != null && !hostAsString.isEmpty() && portAsInteger != null && isValidPort(portAsInteger)) {
config.setElectrumServer(new Server(getProtocol().toUrlString(hostAsString, portAsInteger)));
+ } else if(hostAsString != null && !hostAsString.isEmpty() && !electrumPort.getText().isBlank()) {
+ //An invalid port leaves the server as it was, rather than connecting to the default port as an empty one does
+ return;
} else if(hostAsString != null && !hostAsString.isEmpty()) {
config.setElectrumServer(new Server(getProtocol().toUrlString(hostAsString)));
} else {
@@ -971,6 +977,10 @@ private void setProxyConfig(Config config) {
String proxyServer = null;
if(hostAsString != null && !hostAsString.isBlank() && portAsInteger != null && isValidPort(portAsInteger)) {
proxyServer = HostAndPort.fromParts(hostAsString, portAsInteger).toString();
+ } else if(hostAsString != null && !hostAsString.isBlank() && !proxyPort.getText().isBlank()) {
+ //An invalid port leaves the proxy as it was, rather than connecting to the default port as an empty one does. With no proxy to leave, the
+ //default port is used all the same, since a proxy that fails to connect is better than connecting without one
+ proxyServer = config.getProxyServer() != null ? config.getProxyServer() : HostAndPort.fromHost(hostAsString).toString();
} else if(hostAsString != null && !hostAsString.isBlank()) {
proxyServer = HostAndPort.fromHost(hostAsString).toString();
}
### src/main/java/com/sparrowwallet/sparrow/terminal/settings/ServerProxyDialog.java
@@ -83,6 +83,10 @@ private void setProxyConfig(boolean useProxySelected) {
String proxyServer = null;
if(hostAsString != null && !hostAsString.isBlank() && portAsInteger != null && isValidPort(portAsInteger)) {
proxyServer = HostAndPort.fromParts(hostAsString, portAsInteger).toString();
+ } else if(hostAsString != null && !hostAsString.isBlank() && !proxyPort.getText().isBlank()) {
+ //An invalid port leaves the proxy as it was, rather than connecting to the default port as an empty one does. With no proxy to leave, the
+ //default port is used all the same, since a proxy that fails to connect is better than connecting without one
+ proxyServer = Config.get().getProxyServer() != null ? Config.get().getProxyServer() : HostAndPort.fromHost(hostAsString).toString();
} else if(hostAsString != null && !hostAsString.isBlank()) {
proxyServer = HostAndPort.fromHost(hostAsString).toString();
}
### src/main/java/com/sparrowwallet/sparrow/terminal/settings/ServerUrlDialog.java
@@ -96,6 +96,9 @@ protected Server getCurrentServer() {
Integer portAsInteger = getPort(port.getText());
if(hostAsString != null && portAsInteger != null && isValidPort(portAsInteger)) {
return new Server(getProtocol().toUrlString(hostAsString, portAsInteger), getAlias());
+ } else if(hostAsString != null && !port.getText().isBlank()) {
+ //An invalid port leaves the server as it was, rather than connecting to the default port as an empty one does
+ return null;
} else if(hostAsString != null) {
return new Server(getProtocol().toUrlString(hostAsString), getAlias());
}Why this scored 37/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.