Skip to content
/ besu Public
forked from hyperledger/besu

Commit b449145

Browse files
macfarlaamsmota
authored andcommitted
check for empty in disconnect reason bytes (hyperledger#6938)
Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com> Signed-off-by: amsmota <antonio.mota@citi.com>
1 parent f977740 commit b449145

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/network/PeerDenylistManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void onDisconnect(
5252
final boolean initiatedByPeer) {
5353
// we have a number of reasons that use the same code, but with different message strings
5454
// so here we use the code of the reason param to ensure we get the no-message version
55-
if (shouldBlock(DisconnectReason.forCode(reason.getValue().get(0)), initiatedByPeer)) {
55+
if (shouldBlock(DisconnectReason.forCode(reason.getValue()), initiatedByPeer)) {
5656
if (maintainedPeers.contains(connection.getPeer())) {
5757
LOG.debug(
5858
"Skip adding maintained peer {} to peer denylist for reason {}",

ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/wire/messages/DisconnectMessage.java

+8
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ public static DisconnectReason forCode(final Byte code) {
174174
return BY_ID[code];
175175
}
176176

177+
public static DisconnectReason forCode(final Bytes codeBytes) {
178+
if (codeBytes == null || codeBytes.isEmpty()) {
179+
return UNKNOWN;
180+
} else {
181+
return forCode(codeBytes.get(0));
182+
}
183+
}
184+
177185
DisconnectReason(final Byte code) {
178186
this.code = Optional.ofNullable(code);
179187
this.message = Optional.empty();

ethereum/p2p/src/test/java/org/hyperledger/besu/ethereum/p2p/network/PeerDenylistManagerTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ public void denylistIncompatiblePeerWhoIssuesDisconnect() {
111111
checkPermissions(denylist, peer.getPeer(), false);
112112
}
113113

114+
@Test
115+
public void disconnectReasonWithEmptyValue_doesNotAddToDenylist() {
116+
final PeerConnection peer = generatePeerConnection();
117+
118+
checkPermissions(denylist, peer.getPeer(), true);
119+
peerDenylistManager.onDisconnect(peer, DisconnectReason.UNKNOWN, false);
120+
checkPermissions(denylist, peer.getPeer(), true);
121+
}
122+
114123
private void checkPermissions(
115124
final PeerPermissionsDenylist denylist, final Peer remotePeer, final boolean expectedResult) {
116125
for (PeerPermissions.Action action : PeerPermissions.Action.values()) {

0 commit comments

Comments
 (0)