Merge 10731 via log_more_uacomment

This commit is contained in:
Luke Dashjr 2023-11-15 23:49:11 +00:00
commit 0b1f4bfaaa
3 changed files with 10 additions and 5 deletions

View File

@ -3263,7 +3263,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
if (!vRecv.empty()) {
std::string strSubVer;
vRecv >> LIMITED_STRING(strSubVer, MAX_SUBVERSION_LENGTH);
cleanSubVer = SanitizeString(strSubVer);
cleanSubVer = SanitizeString(strSubVer, SAFE_CHARS_PRINTABLE);
}
if (!vRecv.empty()) {
vRecv >> starting_height;
@ -3405,7 +3405,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
remoteAddr = ", peeraddr=" + pfrom.addr.ToStringAddrPort();
LogPrint(BCLog::NET, "receive version message: %s: version %d, blocks=%d, us=%s, txrelay=%d, peer=%d%s\n",
cleanSubVer, pfrom.nVersion,
SanitizeString(cleanSubVer, SAFE_CHARS_DEFAULT, true), pfrom.nVersion,
peer->m_starting_height, addrMe.ToStringAddrPort(), fRelay, pfrom.GetId(),
remoteAddr);

View File

@ -4,6 +4,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <span.h>
#include <tinyformat.h>
#include <util/strencodings.h>
#include <array>
@ -23,14 +24,17 @@ static const std::string SAFE_CHARS[] =
CHARS_ALPHA_NUM + " .,;-_?@", // SAFE_CHARS_UA_COMMENT
CHARS_ALPHA_NUM + ".-_", // SAFE_CHARS_FILENAME
CHARS_ALPHA_NUM + "!*'();:@&=+$,/?#[]-_.~%", // SAFE_CHARS_URI
CHARS_ALPHA_NUM + " .,;-_/:?@()!\"#$%&'*+<=>[\\]^`{|}~" // SAFE_CHARS_PRINTABLE
};
std::string SanitizeString(std::string_view str, int rule)
std::string SanitizeString(std::string_view str, int rule, bool escape)
{
std::string result;
for (char c : str) {
if (SAFE_CHARS[rule].find(c) != std::string::npos) {
if (SAFE_CHARS[rule].find(c) != std::string::npos || (c == '%' && escape)) {
result.push_back(c);
} else if (escape) {
result += strprintf("%%%02X", c);
}
}
return result;

View File

@ -30,6 +30,7 @@ enum SafeChars
SAFE_CHARS_UA_COMMENT, //!< BIP-0014 subset
SAFE_CHARS_FILENAME, //!< Chars allowed in filenames
SAFE_CHARS_URI, //!< Chars allowed in URIs (RFC 3986)
SAFE_CHARS_PRINTABLE, //!< The full set of printable chars
};
/**
@ -56,7 +57,7 @@ enum class ByteUnit : uint64_t {
* @param[in] rule The set of safe chars to choose (default: least restrictive)
* @return A new string without unsafe chars
*/
std::string SanitizeString(std::string_view str, int rule = SAFE_CHARS_DEFAULT);
std::string SanitizeString(std::string_view str, int rule = SAFE_CHARS_DEFAULT, bool escape = false);
/** Parse the hex string into bytes (uint8_t or std::byte). Ignores whitespace. Returns nullopt on invalid input. */
template <typename Byte = std::byte>
std::optional<std::vector<Byte>> TryParseHex(std::string_view str);