bugfix: handle malformed/old-format negentropy messages gracefully

This commit is contained in:
Doug Hoyte
2023-09-22 14:38:31 -04:00
parent a7aefde56c
commit c9254adaf8
2 changed files with 43 additions and 5 deletions

View File

@ -126,7 +126,14 @@ void cmd_sync(const std::vector<std::string> &subArgs) {
if (msg.at(0) == "NEG-MSG") {
uint64_t origHaves = have.size(), origNeeds = need.size();
auto neMsg = ne.reconcile(from_hex(msg.at(2).get_string()), have, need);
std::optional<std::string> neMsg;
try {
neMsg = ne.reconcile(from_hex(msg.at(2).get_string()), have, need);
} catch (std::exception &e) {
LE << "Unable to parse negentropy message from relay: " << e.what();
doExit(1);
}
totalHaves += have.size() - origHaves;
totalNeeds += need.size() - origNeeds;

View File

@ -111,7 +111,23 @@ void RelayServer::runNegentropy(ThreadPool<MsgNegentropy>::Thread &thr) {
view->ne.seal();
auto resp = view->ne.reconcile(view->initialMsg);
std::string resp;
try {
resp = view->ne.reconcile(view->initialMsg);
} catch (std::exception &e) {
LI << "[" << sub.connId << "] Error parsing negentropy initial message: " << e.what();
sendToConn(sub.connId, tao::json::to_string(tao::json::value::array({
"NEG-ERR",
sub.subId.str(),
"PROTOCOL-ERROR"
})));
views.removeView(sub.connId, sub.subId);
return;
}
view->initialMsg = "";
sendToConn(sub.connId, tao::json::to_string(tao::json::value::array({
@ -150,15 +166,30 @@ void RelayServer::runNegentropy(ThreadPool<MsgNegentropy>::Thread &thr) {
"CLOSED"
})));
return;
continue;
}
if (!view->ne.sealed) {
sendNoticeError(msg->connId, "negentropy error: got NEG-MSG before NEG-OPEN complete");
return;
continue;
}
auto resp = view->ne.reconcile(msg->negPayload);
std::string resp;
try {
resp = view->ne.reconcile(msg->negPayload);
} catch (std::exception &e) {
LI << "[" << msg->connId << "] Error parsing negentropy continuation message: " << e.what();
sendToConn(msg->connId, tao::json::to_string(tao::json::value::array({
"NEG-ERR",
msg->subId.str(),
"PROTOCOL-ERROR"
})));
views.removeView(msg->connId, msg->subId);
continue;
}
sendToConn(msg->connId, tao::json::to_string(tao::json::value::array({
"NEG-MSG",