diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index ae13af6a6e..e699873ddc 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -2391,12 +2391,8 @@ public class BesuCommand implements DefaultCommandValues, Runnable { // Loopback IP is used by default as this is how smokeTests require it to be // and it's probably a good security behaviour to default only on the localhost. private InetAddress autoDiscoverDefaultIP() { - - if (autoDiscoveredDefaultIP != null) { - return autoDiscoveredDefaultIP; - } - - autoDiscoveredDefaultIP = InetAddress.getLoopbackAddress(); + autoDiscoveredDefaultIP = + Optional.ofNullable(autoDiscoveredDefaultIP).orElseGet(InetAddress::getLoopbackAddress); return autoDiscoveredDefaultIP; } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java index 80b5521da8..4aa14fb0d0 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java @@ -235,6 +235,11 @@ public class EthProtocolManager implements ProtocolManager, MinedBlockObserver { return; } + if (message.getData().getSize() > 10 * 1_000_000 /*10MB*/) { + LOG.debug("Received message over 10MB. Disconnecting from {}", peer); + peer.disconnect(DisconnectReason.BREACH_OF_PROTOCOL); + } + // Handle STATUS processing if (message.getData().getCode() == EthPV62.STATUS) { handleStatusMessage(peer, message.getData()); diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTest.java index 1658d62bc8..874bbb5b8b 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManagerTest.java @@ -22,6 +22,7 @@ import static org.mockito.Mockito.reset; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.chain.Blockchain; @@ -180,6 +181,25 @@ public final class EthProtocolManagerTest { } } + @Test + public void disconnectOnVeryLargeMessage() { + try (final EthProtocolManager ethManager = + EthProtocolManagerTestUtil.create( + blockchain, + () -> false, + protocolContext.getWorldStateArchive(), + transactionPool, + EthProtocolConfiguration.defaultConfig())) { + final MessageData messageData = mock(MessageData.class); + when(messageData.getSize()).thenReturn(10 * 1_000_000 + 1 /* just over 10MB*/); + when(messageData.getCode()).thenReturn(EthPV62.TRANSACTIONS); + final MockPeerConnection peer = setupPeer(ethManager, (cap, msg, conn) -> {}); + + ethManager.processMessage(EthProtocol.ETH63, new DefaultMessage(peer, messageData)); + assertThat(peer.isDisconnected()).isTrue(); + } + } + @Test public void disconnectOnWrongGenesisHash() { try (final EthProtocolManager ethManager =