Skip to content

Commit 7b79cbd

Browse files
prusnaklaanwj
authored andcommitted
limit total length of user agent comments
Reworked-By: Wladimir J. van der Laan <[email protected]>
1 parent 557f8ea commit 7b79cbd

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

src/init.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,13 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
10181018

10191019
RegisterNodeSignals(GetNodeSignals());
10201020

1021+
// format user agent, check total size
1022+
strSubVersion = FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, mapMultiArgs.count("-uacomment") ? mapMultiArgs["-uacomment"] : std::vector<string>());
1023+
if (strSubVersion.size() > MAX_SUBVERSION_LENGTH) {
1024+
return InitError(strprintf("Total length of network version string %i exceeds maximum of %i characters. Reduce the number and/or size of uacomments.",
1025+
strSubVersion.size(), MAX_SUBVERSION_LENGTH));
1026+
}
1027+
10211028
if (mapArgs.count("-onlynet")) {
10221029
std::set<enum Network> nets;
10231030
BOOST_FOREACH(const std::string& snet, mapMultiArgs["-onlynet"]) {

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3859,7 +3859,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
38593859
if (!vRecv.empty())
38603860
vRecv >> addrFrom >> nNonce;
38613861
if (!vRecv.empty()) {
3862-
vRecv >> LIMITED_STRING(pfrom->strSubVer, 256);
3862+
vRecv >> LIMITED_STRING(pfrom->strSubVer, MAX_SUBVERSION_LENGTH);
38633863
pfrom->cleanSubVer = SanitizeString(pfrom->strSubVer);
38643864
}
38653865
if (!vRecv.empty())

src/net.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ CAddrMan addrman;
8383
int nMaxConnections = 125;
8484
int nWhiteConnections = 0;
8585
bool fAddressesInitialized = false;
86+
std::string strSubVersion;
8687

8788
vector<CNode*> vNodes;
8889
CCriticalSection cs_vNodes;
@@ -445,7 +446,7 @@ void CNode::PushVersion()
445446
else
446447
LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id);
447448
PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe,
448-
nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, mapMultiArgs.count("-uacomment") ? mapMultiArgs["-uacomment"] : std::vector<string>()), nBestHeight, true);
449+
nLocalHostNonce, strSubVersion, nBestHeight, true);
449450
}
450451

451452

src/net.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ static const unsigned int MAX_INV_SZ = 50000;
4646
static const unsigned int MAX_ADDR_TO_SEND = 1000;
4747
/** Maximum length of incoming protocol messages (no message over 2 MiB is currently acceptable). */
4848
static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH = 2 * 1024 * 1024;
49+
/** Maximum length of strSubVer in `version` message */
50+
static const unsigned int MAX_SUBVERSION_LENGTH = 256;
4951
/** -listen default */
5052
static const bool DEFAULT_LISTEN = true;
5153
/** -upnp default */
@@ -166,6 +168,9 @@ extern CCriticalSection cs_vAddedNodes;
166168
extern NodeId nLastNodeId;
167169
extern CCriticalSection cs_nLastNodeId;
168170

171+
/** Subversion as sent to the P2P network in `version` messages */
172+
extern std::string strSubVersion;
173+
169174
struct LocalServiceInfo {
170175
int nScore;
171176
int nPort;

src/rpcnet.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,7 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp)
443443

444444
UniValue obj(UniValue::VOBJ);
445445
obj.push_back(Pair("version", CLIENT_VERSION));
446-
obj.push_back(Pair("subversion",
447-
FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, mapMultiArgs.count("-uacomment") ? mapMultiArgs["-uacomment"] : std::vector<string>())));
446+
obj.push_back(Pair("subversion", strSubVersion));
448447
obj.push_back(Pair("protocolversion",PROTOCOL_VERSION));
449448
obj.push_back(Pair("localservices", strprintf("%016x", nLocalServices)));
450449
obj.push_back(Pair("timeoffset", GetTimeOffset()));

0 commit comments

Comments
 (0)