mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-09 06:31:41 +02:00
net: Clarify message header validation errors
Make the errors less shouty and more descriptive.
This commit is contained in:
parent
955eee7680
commit
09205b33aa
10
src/net.cpp
10
src/net.cpp
@ -681,19 +681,19 @@ int V1TransportDeserializer::readHeader(Span<const uint8_t> msg_bytes)
|
|||||||
hdrbuf >> hdr;
|
hdrbuf >> hdr;
|
||||||
}
|
}
|
||||||
catch (const std::exception&) {
|
catch (const std::exception&) {
|
||||||
LogPrint(BCLog::NET, "HEADER ERROR - UNABLE TO DESERIALIZE, peer=%d\n", m_node_id);
|
LogPrint(BCLog::NET, "Header error: Unable to deserialize, peer=%d\n", m_node_id);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check start string, network magic
|
// Check start string, network magic
|
||||||
if (memcmp(hdr.pchMessageStart, m_chain_params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
|
if (memcmp(hdr.pchMessageStart, m_chain_params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
|
||||||
LogPrint(BCLog::NET, "HEADER ERROR - MESSAGESTART received %s, peer=%d\n", HexStr(hdr.pchMessageStart), m_node_id);
|
LogPrint(BCLog::NET, "Header error: Wrong MessageStart %s received, peer=%d\n", HexStr(hdr.pchMessageStart), m_node_id);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// reject messages larger than MAX_SIZE or MAX_PROTOCOL_MESSAGE_LENGTH
|
// reject messages larger than MAX_SIZE or MAX_PROTOCOL_MESSAGE_LENGTH
|
||||||
if (hdr.nMessageSize > MAX_SIZE || hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
|
if (hdr.nMessageSize > MAX_SIZE || hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
|
||||||
LogPrint(BCLog::NET, "HEADER ERROR - SIZE (%s, %u bytes), peer=%d\n", SanitizeString(hdr.GetCommand()), hdr.nMessageSize, m_node_id);
|
LogPrint(BCLog::NET, "Header error: Size too large (%s, %u bytes), peer=%d\n", SanitizeString(hdr.GetCommand()), hdr.nMessageSize, m_node_id);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -746,7 +746,7 @@ std::optional<CNetMessage> V1TransportDeserializer::GetMessage(const std::chrono
|
|||||||
|
|
||||||
// Check checksum and header command string
|
// Check checksum and header command string
|
||||||
if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0) {
|
if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0) {
|
||||||
LogPrint(BCLog::NET, "CHECKSUM ERROR (%s, %u bytes), expected %s was %s, peer=%d\n",
|
LogPrint(BCLog::NET, "Header error: Wrong checksum (%s, %u bytes), expected %s was %s, peer=%d\n",
|
||||||
SanitizeString(msg->m_command), msg->m_message_size,
|
SanitizeString(msg->m_command), msg->m_message_size,
|
||||||
HexStr(Span<uint8_t>(hash.begin(), hash.begin() + CMessageHeader::CHECKSUM_SIZE)),
|
HexStr(Span<uint8_t>(hash.begin(), hash.begin() + CMessageHeader::CHECKSUM_SIZE)),
|
||||||
HexStr(hdr.pchChecksum),
|
HexStr(hdr.pchChecksum),
|
||||||
@ -754,7 +754,7 @@ std::optional<CNetMessage> V1TransportDeserializer::GetMessage(const std::chrono
|
|||||||
out_err_raw_size = msg->m_raw_message_size;
|
out_err_raw_size = msg->m_raw_message_size;
|
||||||
msg = std::nullopt;
|
msg = std::nullopt;
|
||||||
} else if (!hdr.IsCommandValid()) {
|
} else if (!hdr.IsCommandValid()) {
|
||||||
LogPrint(BCLog::NET, "HEADER ERROR - COMMAND (%s, %u bytes), peer=%d\n",
|
LogPrint(BCLog::NET, "Header error: Invalid message type (%s, %u bytes), peer=%d\n",
|
||||||
SanitizeString(hdr.GetCommand()), msg->m_message_size, m_node_id);
|
SanitizeString(hdr.GetCommand()), msg->m_message_size, m_node_id);
|
||||||
out_err_raw_size = msg->m_raw_message_size;
|
out_err_raw_size = msg->m_raw_message_size;
|
||||||
msg.reset();
|
msg.reset();
|
||||||
|
@ -104,7 +104,7 @@ class InvalidMessagesTest(BitcoinTestFramework):
|
|||||||
def test_magic_bytes(self):
|
def test_magic_bytes(self):
|
||||||
self.log.info("Test message with invalid magic bytes disconnects peer")
|
self.log.info("Test message with invalid magic bytes disconnects peer")
|
||||||
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
|
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
|
||||||
with self.nodes[0].assert_debug_log(['HEADER ERROR - MESSAGESTART received ffffffff']):
|
with self.nodes[0].assert_debug_log(['Header error: Wrong MessageStart ffffffff received']):
|
||||||
msg = conn.build_message(msg_unrecognized(str_data="d"))
|
msg = conn.build_message(msg_unrecognized(str_data="d"))
|
||||||
# modify magic bytes
|
# modify magic bytes
|
||||||
msg = b'\xff' * 4 + msg[4:]
|
msg = b'\xff' * 4 + msg[4:]
|
||||||
@ -115,7 +115,7 @@ class InvalidMessagesTest(BitcoinTestFramework):
|
|||||||
def test_checksum(self):
|
def test_checksum(self):
|
||||||
self.log.info("Test message with invalid checksum logs an error")
|
self.log.info("Test message with invalid checksum logs an error")
|
||||||
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
|
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
|
||||||
with self.nodes[0].assert_debug_log(['CHECKSUM ERROR (badmsg, 2 bytes), expected 78df0a04 was ffffffff']):
|
with self.nodes[0].assert_debug_log(['Header error: Wrong checksum (badmsg, 2 bytes), expected 78df0a04 was ffffffff']):
|
||||||
msg = conn.build_message(msg_unrecognized(str_data="d"))
|
msg = conn.build_message(msg_unrecognized(str_data="d"))
|
||||||
# Checksum is after start bytes (4B), message type (12B), len (4B)
|
# Checksum is after start bytes (4B), message type (12B), len (4B)
|
||||||
cut_len = 4 + 12 + 4
|
cut_len = 4 + 12 + 4
|
||||||
@ -130,7 +130,7 @@ class InvalidMessagesTest(BitcoinTestFramework):
|
|||||||
def test_size(self):
|
def test_size(self):
|
||||||
self.log.info("Test message with oversized payload disconnects peer")
|
self.log.info("Test message with oversized payload disconnects peer")
|
||||||
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
|
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
|
||||||
with self.nodes[0].assert_debug_log(['HEADER ERROR - SIZE (badmsg, 4000001 bytes)']):
|
with self.nodes[0].assert_debug_log(['Header error: Size too large (badmsg, 4000001 bytes)']):
|
||||||
msg = msg_unrecognized(str_data="d" * (VALID_DATA_LIMIT + 1))
|
msg = msg_unrecognized(str_data="d" * (VALID_DATA_LIMIT + 1))
|
||||||
msg = conn.build_message(msg)
|
msg = conn.build_message(msg)
|
||||||
conn.send_raw_message(msg)
|
conn.send_raw_message(msg)
|
||||||
@ -140,7 +140,7 @@ class InvalidMessagesTest(BitcoinTestFramework):
|
|||||||
def test_msgtype(self):
|
def test_msgtype(self):
|
||||||
self.log.info("Test message with invalid message type logs an error")
|
self.log.info("Test message with invalid message type logs an error")
|
||||||
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
|
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
|
||||||
with self.nodes[0].assert_debug_log(['HEADER ERROR - COMMAND']):
|
with self.nodes[0].assert_debug_log(['Header error: Invalid message type']):
|
||||||
msg = msg_unrecognized(str_data="d")
|
msg = msg_unrecognized(str_data="d")
|
||||||
msg = conn.build_message(msg)
|
msg = conn.build_message(msg)
|
||||||
# Modify msgtype
|
# Modify msgtype
|
||||||
|
Loading…
x
Reference in New Issue
Block a user