Skip to content

Commit b38a0e0

Browse files
authored
Merge pull request #48 from mpretty-cyro/fix/handle-invalid-snodes
Updated the code to ignore nodes which fail to parse
2 parents 6df5dcb + 53d020b commit b38a0e0

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if(CCACHE_PROGRAM)
1717
endif()
1818

1919
project(libsession-util
20-
VERSION 1.4.0
20+
VERSION 1.4.1
2121
DESCRIPTION "Session client utility library"
2222
LANGUAGES ${LANGS})
2323

src/session_network.cpp

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,18 @@ namespace {
184184
else
185185
ip = json["ip"].get<std::string>();
186186

187+
if (ip == "0.0.0.0")
188+
throw std::runtime_error{"Invalid IP address"};
189+
187190
uint16_t port;
188191
if (json.contains("storage_lmq_port"))
189192
port = json["storage_lmq_port"].get<uint16_t>();
190193
else
191194
port = json["port_omq"].get<uint16_t>();
192195

196+
if (port == 0)
197+
throw std::runtime_error{"Invalid lmq port"};
198+
193199
swarm_id_t swarm_id = INVALID_SWARM_ID;
194200
if (json.contains("swarm_id"))
195201
swarm_id = json["swarm_id"].get<swarm_id_t>();
@@ -349,22 +355,38 @@ namespace detail {
349355
auto node = result_dict.consume_list_consumer();
350356

351357
while (!node.is_finished()) {
352-
auto node_consumer = node.consume_dict_consumer();
353-
auto pubkey_ed25519 = oxenc::from_hex(consume_string(node_consumer, "pubkey_ed25519"));
354-
auto public_ip = consume_string(node_consumer, "public_ip");
355-
auto storage_lmq_port = consume_integer<uint16_t>(node_consumer, "storage_lmq_port");
356-
357-
std::vector<int> storage_server_version;
358-
node_consumer.skip_until("storage_server_version");
359-
auto version_consumer = node_consumer.consume_list_consumer();
360-
auto swarm_id = consume_integer<uint64_t>(node_consumer, "swarm_id");
361-
362-
while (!version_consumer.is_finished()) {
363-
storage_server_version.emplace_back(version_consumer.consume_integer<int>());
364-
}
358+
try {
359+
auto node_consumer = node.consume_dict_consumer();
360+
auto pubkey_ed25519 =
361+
oxenc::from_hex(consume_string(node_consumer, "pubkey_ed25519"));
362+
auto public_ip = consume_string(node_consumer, "public_ip");
363+
auto storage_lmq_port =
364+
consume_integer<uint16_t>(node_consumer, "storage_lmq_port");
365+
366+
if (public_ip == "0.0.0.0")
367+
throw std::runtime_error{"Invalid IP address"};
368+
369+
if (storage_lmq_port == 0)
370+
throw std::runtime_error{"Invalid lmq port"};
371+
372+
std::vector<int> storage_server_version;
373+
node_consumer.skip_until("storage_server_version");
374+
auto version_consumer = node_consumer.consume_list_consumer();
375+
auto swarm_id = consume_integer<uint64_t>(node_consumer, "swarm_id");
376+
377+
while (!version_consumer.is_finished()) {
378+
storage_server_version.emplace_back(version_consumer.consume_integer<int>());
379+
}
365380

366-
result.emplace_back(
367-
pubkey_ed25519, storage_server_version, swarm_id, public_ip, storage_lmq_port);
381+
result.emplace_back(
382+
pubkey_ed25519,
383+
storage_server_version,
384+
swarm_id,
385+
public_ip,
386+
storage_lmq_port);
387+
} catch (const std::exception& e) {
388+
log::warning(cat, "Ignoring invalid snode: {}.", e.what());
389+
}
368390
}
369391

370392
return result;
@@ -381,7 +403,11 @@ namespace detail {
381403

382404
std::vector<service_node> result;
383405
for (auto& snode : result_json["service_node_states"])
384-
result.emplace_back(node_from_json(snode));
406+
try {
407+
result.emplace_back(node_from_json(snode));
408+
} catch (const std::exception& e) {
409+
log::warning(cat, "Ignoring invalid snode: {}.", e.what());
410+
}
385411

386412
return result;
387413
}

0 commit comments

Comments
 (0)