Skip to content

Commit a946aa8

Browse files
author
Mike Hearn
committed
Store and use a sanitized subVer
1 parent ba6a4ea commit a946aa8

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

src/main.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3097,8 +3097,10 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
30973097
pfrom->nVersion = 300;
30983098
if (!vRecv.empty())
30993099
vRecv >> addrFrom >> nNonce;
3100-
if (!vRecv.empty())
3100+
if (!vRecv.empty()) {
31013101
vRecv >> pfrom->strSubVer;
3102+
pfrom->cleanSubVer = SanitizeString(pfrom->strSubVer);
3103+
}
31023104
if (!vRecv.empty())
31033105
vRecv >> pfrom->nStartingHeight;
31043106
if (!vRecv.empty())
@@ -3165,7 +3167,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
31653167

31663168
pfrom->fSuccessfullyConnected = true;
31673169

3168-
LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, them=%s, peer=%s\n", pfrom->strSubVer.c_str(), pfrom->nVersion, pfrom->nStartingHeight, addrMe.ToString().c_str(), addrFrom.ToString().c_str(), pfrom->addr.ToString().c_str());
3170+
LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, them=%s, peer=%s\n", pfrom->cleanSubVer.c_str(), pfrom->nVersion, pfrom->nStartingHeight, addrMe.ToString().c_str(), addrFrom.ToString().c_str(), pfrom->addr.ToString().c_str());
31693171

31703172
AddTimeData(pfrom->addr, nTime);
31713173

@@ -3426,7 +3428,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
34263428

34273429

34283430
LogPrint("mempool", "AcceptToMemoryPool: %s %s : accepted %s (poolsz %"PRIszu")\n",
3429-
pfrom->addr.ToString().c_str(), pfrom->strSubVer.c_str(),
3431+
pfrom->addr.ToString().c_str(), pfrom->cleanSubVer.c_str(),
34303432
tx.GetHash().ToString().c_str(),
34313433
mempool.mapTx.size());
34323434

@@ -3480,7 +3482,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
34803482
if (state.IsInvalid(nDoS))
34813483
{
34823484
LogPrint("mempool", "%s from %s %s was not accepted into the memory pool: %s\n", tx.GetHash().ToString().c_str(),
3483-
pfrom->addr.ToString().c_str(), pfrom->strSubVer.c_str(),
3485+
pfrom->addr.ToString().c_str(), pfrom->cleanSubVer.c_str(),
34843486
state.GetRejectReason().c_str());
34853487
pfrom->PushMessage("reject", strCommand, state.GetRejectCode(),
34863488
state.GetRejectReason(), inv.hash);
@@ -3618,7 +3620,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
36183620
if (!(sProblem.empty())) {
36193621
LogPrint("net", "pong %s %s: %s, %"PRIx64" expected, %"PRIx64" received, %"PRIszu" bytes\n",
36203622
pfrom->addr.ToString().c_str(),
3621-
pfrom->strSubVer.c_str(),
3623+
pfrom->cleanSubVer.c_str(),
36223624
sProblem.c_str(),
36233625
pfrom->nPingNonceSent,
36243626
nonce,

src/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ void CNode::copyStats(CNodeStats &stats)
616616
X(nTimeConnected);
617617
X(addrName);
618618
X(nVersion);
619-
X(strSubVer);
619+
X(cleanSubVer);
620620
X(fInbound);
621621
X(nStartingHeight);
622622
X(nMisbehavior);

src/net.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class CNodeStats
121121
int64_t nTimeConnected;
122122
std::string addrName;
123123
int nVersion;
124-
std::string strSubVer;
124+
std::string cleanSubVer;
125125
bool fInbound;
126126
int nStartingHeight;
127127
int nMisbehavior;
@@ -203,7 +203,11 @@ class CNode
203203
std::string addrName;
204204
CService addrLocal;
205205
int nVersion;
206-
std::string strSubVer;
206+
// strSubVer is whatever byte array we read from the wire. However, this field is intended
207+
// to be printed out, displayed to humans in various forms and so on. So we sanitize it and
208+
// store the sanitized version in cleanSubVer. The original should be used when dealing with
209+
// the network or wire types and the cleaned string used when displayed or logged.
210+
std::string strSubVer, cleanSubVer;
207211
bool fOneShot;
208212
bool fClient;
209213
bool fInbound;

src/rpcnet.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ Value getpeerinfo(const Array& params, bool fHelp)
126126
if (stats.dPingWait > 0.0)
127127
obj.push_back(Pair("pingwait", stats.dPingWait));
128128
obj.push_back(Pair("version", stats.nVersion));
129-
obj.push_back(Pair("subver", stats.strSubVer));
129+
// Use the sanitized form of subver here, to avoid tricksy remote peers from
130+
// corrupting or modifiying the JSON output by putting special characters in
131+
// their ver message.
132+
obj.push_back(Pair("subver", stats.cleanSubVer));
130133
obj.push_back(Pair("inbound", stats.fInbound));
131134
obj.push_back(Pair("startingheight", stats.nStartingHeight));
132135
obj.push_back(Pair("banscore", stats.nMisbehavior));

0 commit comments

Comments
 (0)