keep the start of a server response that arrives across socket read timeouts instead of dropping it, and fail the connection on a response over 256m characters
What changed, and why it matters
This commit fixes how Sparrow Wallet reads replies from a Bitcoin/Electrum server over the network. Previously, if a reply arrived in pieces with pauses between them, the wallet could drop the first piece and misread the response. It also adds a 256 million character cap so a malicious or broken server cannot send an endless reply that exhausts the wallet's memory. The change is defensive hardening with a clear reliability and limited security benefit.
Treat as a reliability fix with defensive DoS hardening. Review whether 256M chars is appropriate for all supported networks and whether the response length check should apply to total multi-line responses, not just per-line accumulation. No immediate incident response is indicated.
Security signals we found
Denial-of-service mitigation: 256M character response limit prevents unbounded memory growth from a malicious or misbehaving server
Data integrity fix: partial responses no longer dropped across socket read timeouts, preventing corrupted/truncated JSON-RPC responses
Connection teardown on oversized response via IOException and running=false in signalException
Regression tests added for timeout reassembly and oversized response
Evidence from the diff
TcpTransport.readInputLoop previously wrapped the socket in a BufferedReader and called readLine(). BufferedReader buffers ahead, so when a SocketTimeoutException fired partway through a line, the partial characters already buffered were discarded on the next readLine() call, causing JSON parse failures or truncated responses. The patch replaces BufferedReader with a raw InputStreamReader and a manual 8 KB char buffer, preserving unconsumed characters across timeouts. It also introduces MAX_RESPONSE_CHARS (25610241024) and throws IOException if a single response line exceeds that length, which terminates the connection. A test verifies reassembly across timeouts and another verifies the oversized-response failure path.
Changed components
src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.javasrc/test/java/com/sparrowwallet/sparrow/net/TcpTransportTimeoutTest.javaInspect captured patch +125 / −12
### src/main/java/com/sparrowwallet/sparrow/net/TcpTransport.java
@@ -40,6 +40,7 @@ public class TcpTransport implements CloseableTransport, TimeoutCounter {
private static final int[] SLOW_READ_TIMEOUT_SECS = {34, 68, 124, 208};
public static final long PER_REQUEST_READ_TIMEOUT_MILLIS = 50;
public static final int SOCKET_READ_TIMEOUT_MILLIS = 5000;
+ private static final int MAX_RESPONSE_CHARS = 256 * 1024 * 1024; //Above a default page of transactions at the 1MB stored transaction limit, as hex
private static final Pattern ID_PATTERN = Pattern.compile("\"id\"\\s*:\\s*(\\d+)");
private static final JsonFactory JSON_FACTORY = new JsonFactory();
@@ -67,6 +68,11 @@ public class TcpTransport implements CloseableTransport, TimeoutCounter {
private int readTimeoutIndex;
private int requestIdCount = 1;
+ private final char[] readBuffer = new char[8192];
+ private int readBufferPos;
+ private int readBufferEnd;
+ int maxResponseChars = MAX_RESPONSE_CHARS;
+
private final JsonRpcServer jsonRpcServer = new JsonRpcServer();
private final SubscriptionService subscriptionService = new SubscriptionService();
@@ -213,16 +219,15 @@ private void escalateReadTimeout() {
}
public void readInputLoop() throws ServerException {
- BufferedReader in;
+ Reader in;
try {
- in = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
+ in = new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8);
} catch(IOException e) {
if(!closed) {
log.error("Error opening socket inputstream", e);
}
if(running) {
signalException(e);
- running = false;
}
return;
}
@@ -259,9 +264,8 @@ public void readInputLoop() throws ServerException {
log.trace("Connection error while reading", e);
}
if(running) {
- signalException(e);
//Allow this thread to terminate as we will need to reconnect with a new transport anyway
- running = false;
+ signalException(e);
}
}
}
@@ -286,14 +290,16 @@ private void signalException(Exception e) {
readLock.lock();
try {
lastException = e;
+ //Set before the waiting caller wakes, so it sees the transport disconnected along with the exception
+ running = false;
reading = false;
readingCondition.signal();
} finally {
readLock.unlock();
}
}
- protected String readInputStream(BufferedReader in) throws IOException {
+ protected String readInputStream(Reader in) throws IOException {
String response = readLine(in);
if(response == null) {
@@ -303,12 +309,42 @@ protected String readInputStream(BufferedReader in) throws IOException {
return response;
}
- private String readLine(BufferedReader in) throws IOException {
+ //Unlike BufferedReader.readLine(), keeps the start of a line when a socket read times out partway through it
+ private String readLine(Reader in) throws IOException {
+ StringBuilder line = new StringBuilder();
while(!socket.isClosed()) {
- try {
- return in.readLine();
- } catch(SocketTimeoutException e) {
- //ignore and continue
+ if(readBufferPos == readBufferEnd) {
+ int read;
+ try {
+ read = in.read(readBuffer, 0, readBuffer.length);
+ } catch(SocketTimeoutException e) {
+ continue;
+ }
+
+ if(read < 0) {
+ return line.isEmpty() ? null : line.toString();
+ }
+
+ readBufferPos = 0;
+ readBufferEnd = read;
+ }
+
+ for(int i = readBufferPos; i < readBufferEnd; i++) {
+ if(readBuffer[i] == '\n') {
+ line.append(readBuffer, readBufferPos, i - readBufferPos);
+ readBufferPos = i + 1;
+ if(!line.isEmpty() && line.charAt(line.length() - 1) == '\r') {
+ line.setLength(line.length() - 1);
+ }
+
+ return line.toString();
+ }
+ }
+
+ line.append(readBuffer, readBufferPos, readBufferEnd - readBufferPos);
+ readBufferPos = readBufferEnd;
+ if(line.length() > maxResponseChars) {
+ throw new IOException("Server response exceeded " + maxResponseChars + " characters");
}
}
### src/test/java/com/sparrowwallet/sparrow/net/TcpTransportTimeoutTest.java
@@ -10,6 +10,7 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
@@ -22,6 +23,7 @@
import java.util.concurrent.CountDownLatch;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -30,7 +32,8 @@
* Regression tests for the read timeout in TcpTransport#readResponse. Only acquiring readLock via tryLock() was
* time bounded, but the read thread releases readLock while blocked on the socket, so tryLock returned almost
* immediately regardless of the requested timeout and the actual wait for a response was an unbounded
- * readingCondition.await() that a server which stopped replying would never signal.
+ * readingCondition.await() that a server which stopped replying would never signal. Also covers reading response
+ * lines across socket read timeouts and the response size limit.
*/
public class TcpTransportTimeoutTest {
@TempDir
@@ -97,6 +100,58 @@ public void lateRespondingServerRecoversOnTheNextRequest() throws Exception {
}
}
+ @Test
+ public void responseSplitBySocketReadTimeoutIsReassembled() throws Exception {
+ try(ServerSocket serverSocket = createServerSocket()) {
+ TcpTransport transport = connectTransport(serverSocket);
+ transport.socket.setSoTimeout(100);
+ String response = pingResponse(1);
+ int split = response.length() / 2;
+
+ //Pause between the two halves for several socket read timeouts, but well within the response read timeout
+ startServer(serverSocket, out -> {
+ out.write(response.substring(0, split).getBytes(StandardCharsets.UTF_8));
+ out.flush();
+ Thread.sleep(500);
+ out.write((response.substring(split) + "\n").getBytes(StandardCharsets.UTF_8));
+ out.flush();
+ });
+
+ try {
+ Duration limit = Duration.ofSeconds(transport.readTimeouts[0] + 5);
+ assertEquals(response, assertTimeoutPreemptively(limit, () -> transport.pass(pingRequest(1))));
+ } finally {
+ transport.close();
+ }
+ }
+ }
+
+ @Test
+ public void oversizedResponseFailsTheConnection() throws Exception {
+ try(ServerSocket serverSocket = createServerSocket()) {
+ TcpTransport transport = connectTransport(serverSocket);
+ transport.maxResponseChars = 1024;
+
+ CountDownLatch done = new CountDownLatch(1);
+ startServer(serverSocket, out -> {
+ byte[] chunk = new byte[4096];
+ Arrays.fill(chunk, (byte)'a');
+ out.write(chunk);
+ out.flush();
+ done.await();
+ });
+
+ try {
+ Duration limit = Duration.ofSeconds(transport.readTimeouts[0] + 5);
+ assertTimeoutPreemptively(limit, () -> assertThrows(IOException.class, () -> transport.pass(pingRequest(1))));
+ assertFalse(transport.isConnected(), "Transport must fail the connection");
+ } finally {
+ done.countDown();
+ transport.close();
+ }
+ }
+ }
+
private ServerSocket createServerSocket() throws IOException {
return new ServerSocket(0, 1, InetAddress.getLoopbackAddress());
}
@@ -146,6 +201,28 @@ private void startServer(ServerSocket serverSocket, CountDownLatch firstReplyGat
serverThread.start();
}
+ /**
+ * Reads the first request and writes the reply with the given responder, then holds the connection open until the test tears it down.
+ */
+ private void startServer(ServerSocket serverSocket, Responder responder) {
+ Thread serverThread = new Thread(() -> {
+ try(Socket accepted = serverSocket.accept()) {
+ BufferedReader in = new BufferedReader(new InputStreamReader(accepted.getInputStream(), StandardCharsets.UTF_8));
+ in.readLine();
+ responder.respond(accepted.getOutputStream());
+ in.readLine();
+ } catch(Exception e) {
+ //Expected once the test tears down the server and transport
+ }
+ });
+ serverThread.setDaemon(true);
+ serverThread.start();
+ }
+
+ private interface Responder {
+ void respond(OutputStream out) throws Exception;
+ }
+
private static String pingRequest(int id) {
return "{\"id\":" + id + ",\"method\":\"server.ping\",\"params\":[]}";
}Why this scored 47/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.