check for empty in disconnect reason bytes (#6938)

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
pull/6948/head
Sally MacFarlane 7 months ago committed by GitHub
parent ec06ab5567
commit 6699907204
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/PeerDenylistManager.java
  2. 8
      ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/wire/messages/DisconnectMessage.java
  3. 9
      ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/network/PeerDenylistManagerTest.java

@ -52,7 +52,7 @@ public class PeerDenylistManager implements DisconnectCallback {
final boolean initiatedByPeer) {
// we have a number of reasons that use the same code, but with different message strings
// so here we use the code of the reason param to ensure we get the no-message version
if (shouldBlock(DisconnectReason.forCode(reason.getValue().get(0)), initiatedByPeer)) {
if (shouldBlock(DisconnectReason.forCode(reason.getValue()), initiatedByPeer)) {
if (maintainedPeers.contains(connection.getPeer())) {
LOG.debug(
"Skip adding maintained peer {} to peer denylist for reason {}",

@ -174,6 +174,14 @@ public final class DisconnectMessage extends AbstractMessageData {
return BY_ID[code];
}
public static DisconnectReason forCode(final Bytes codeBytes) {
if (codeBytes == null || codeBytes.isEmpty()) {
return UNKNOWN;
} else {
return forCode(codeBytes.get(0));
}
}
DisconnectReason(final Byte code) {
this.code = Optional.ofNullable(code);
this.message = Optional.empty();

@ -111,6 +111,15 @@ public class PeerDenylistManagerTest {
checkPermissions(denylist, peer.getPeer(), false);
}
@Test
public void disconnectReasonWithEmptyValue_doesNotAddToDenylist() {
final PeerConnection peer = generatePeerConnection();
checkPermissions(denylist, peer.getPeer(), true);
peerDenylistManager.onDisconnect(peer, DisconnectReason.UNKNOWN, false);
checkPermissions(denylist, peer.getPeer(), true);
}
private void checkPermissions(
final PeerPermissionsDenylist denylist, final Peer remotePeer, final boolean expectedResult) {
for (PeerPermissions.Action action : PeerPermissions.Action.values()) {

Loading…
Cancel
Save