Skip to content

Commit 0182a11

Browse files
committed
torcontrol: Log invalid parameters in Tor reply strings where meaningful
1 parent 0b6f40d commit 0182a11

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/torcontrol.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ static std::pair<std::string,std::string> SplitTorReplyLine(const std::string &s
267267
}
268268

269269
/** Parse reply arguments in the form 'METHODS=COOKIE,SAFECOOKIE COOKIEFILE=".../control_auth_cookie"'.
270+
* Returns a map of keys to values, or an empty map if there was an error.
270271
* Grammar is implicitly defined in https://spec.torproject.org/control-spec by
271272
* the server reply formats for PROTOCOLINFO (S3.21), AUTHCHALLENGE (S3.24),
272273
* and ADD_ONION (S3.27). See also sections 2.1 and 2.3.
@@ -450,6 +451,13 @@ void TorController::add_onion_cb(TorControlConnection& _conn, const TorControlRe
450451
if ((i = m.find("PrivateKey")) != m.end())
451452
private_key = i->second;
452453
}
454+
if (service_id.empty()) {
455+
LogPrintf("tor: Error parsing ADD_ONION parameters:\n");
456+
for (const std::string &s : reply.lines) {
457+
LogPrintf(" %s\n", SanitizeString(s));
458+
}
459+
return;
460+
}
453461
service = LookupNumeric(std::string(service_id+".onion").c_str(), GetListenPort());
454462
LogPrintf("tor: Got service ID %s, advertising service %s\n", service_id, service.ToString());
455463
if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) {
@@ -527,6 +535,10 @@ void TorController::authchallenge_cb(TorControlConnection& _conn, const TorContr
527535
std::pair<std::string,std::string> l = SplitTorReplyLine(reply.lines[0]);
528536
if (l.first == "AUTHCHALLENGE") {
529537
std::map<std::string,std::string> m = ParseTorReplyMapping(l.second);
538+
if (m.empty()) {
539+
LogPrintf("tor: Error parsing AUTHCHALLENGE parameters: %s\n", SanitizeString(l.second));
540+
return;
541+
}
530542
std::vector<uint8_t> serverHash = ParseHex(m["SERVERHASH"]);
531543
std::vector<uint8_t> serverNonce = ParseHex(m["SERVERNONCE"]);
532544
LogPrint(BCLog::TOR, "tor: AUTHCHALLENGE ServerHash %s ServerNonce %s\n", HexStr(serverHash), HexStr(serverNonce));

0 commit comments

Comments
 (0)