Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for empty in disconnect reason bytes #6938

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void onDisconnect(
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 {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ public static DisconnectReason forCode(final Byte code) {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ public void denylistIncompatiblePeerWhoIssuesDisconnect() {
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()) {
Expand Down
Loading