fix(net): ParsePeers must tolerate any JSON shape from the server

server.peers.subscribe is attacker-controlled data: ParsePeers assumed
the standard [ip, hostname, [features...]] shape and threw on anything
else (non-array response, wrong element types), and GetString() on a
JSON string containing invalid UTF-8 bytes threw InvalidOperationException
instead of failing to parse — both found by fuzzing. Any unexpected
shape or unparseable string now degrades to "no usable data" instead of
propagating an exception into the sync/discovery path.
This commit is contained in:
2026-07-07 21:51:52 +02:00
parent c868c17505
commit d9c75eaec2
2 changed files with 55 additions and 4 deletions
@@ -27,6 +27,35 @@ public class PeerParsingTests
Assert.Equal(new PeerInfo("nodo.esempio.org", 0, null, "1.4"), peers[1]);
Assert.Equal(new PeerInfo("solo-ssl.esempio.org", null, 50002, "1.4"), peers[2]);
}
[Theory]
[InlineData("""{"not":"an array"}""")]
[InlineData(""" "just a string" """)]
[InlineData("42")]
[InlineData("""[[1,2,3]]""")] // numeric ip/hostname
[InlineData("""[["h","n",42]]""")] // features not an array
[InlineData("""[["h","n",[42,{"x":1},null]]]""")] // non-string features
public void Una_risposta_peers_di_forma_inattesa_produce_una_lista_vuota(string json)
{
// The response is untrusted server data: any shape must degrade to an
// empty list, never throw (found by fuzzing).
Assert.Empty(ElectrumApi.ParsePeers(JsonDocument.Parse(json).RootElement));
}
[Fact]
public void Una_stringa_json_con_utf8_invalido_viene_ignorata_senza_eccezioni()
{
// Raw 0x87 inside a JSON string: JsonDocument.Parse accepts the bytes but
// GetString() cannot transcode them to UTF-16 (found by fuzzing). The
// whole entry degrades to "no usable host/feature", not an exception.
var bytes = "[[\"1.2.3.4\",\"#\",[\"v1\",\"t#\"]]]"u8.ToArray();
var hostIdx = Array.IndexOf(bytes, (byte)'#');
bytes[hostIdx] = 0x87;
bytes[Array.LastIndexOf(bytes, (byte)'#')] = 0x87;
using var doc = JsonDocument.Parse(bytes);
Assert.Empty(ElectrumApi.ParsePeers(doc.RootElement));
}
}
public class ServerRegistryTests