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

Throw decoding exception if there's an invalid node distance #157

Merged
merged 3 commits into from
Oct 26, 2022
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 @@ -14,6 +14,7 @@
import org.apache.tuweni.rlp.RLPReader;
import org.apache.tuweni.rlp.RLPWriter;
import org.ethereum.beacon.discovery.util.DecodeException;
import org.ethereum.beacon.discovery.util.RlpDecodeException;
import org.ethereum.beacon.discovery.util.RlpUtil;

/**
Expand All @@ -34,12 +35,30 @@ public FindNodeMessage(Bytes requestId, List<Integer> distances) {
this.distances = distances;
}

/**
* According to the <a
* href="https://github.com/ethereum/devp2p/blob/master/discv5/discv5-theory.md#node-table">Node
* Discovery Protocol</a>, there are 256 buckets and the 0th bucket is reserved for our node.
* Therefore, a distance must be greater than or equal to 0 and less than or equal to 256.
*
* @param distance The distance value to check.
* @return True if the distance is valid.
*/
public static boolean isValidDistance(final int distance) {
return 0 <= distance && distance <= 256;
}

public static FindNodeMessage fromBytes(Bytes bytes) throws DecodeException {
return RlpUtil.readRlpList(
bytes,
reader -> {
final Bytes requestId = checkMaxSize(reader.readValue(), MAX_REQUEST_ID_SIZE);
List<Integer> distances = reader.readListContents(RLPReader::readInt);
for (Integer distance : distances) {
if (!isValidDistance(distance)) {
throw new RlpDecodeException("Invalid distance");
}
}
return new FindNodeMessage(requestId, distances);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

package org.ethereum.beacon.discovery.message;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.ethereum.beacon.discovery.TestUtil.assertRejectTrailingBytes;
import static org.ethereum.beacon.discovery.TestUtil.assertRoundTrip;

import java.util.List;
import org.apache.tuweni.bytes.Bytes;
import org.ethereum.beacon.discovery.SimpleIdentitySchemaInterpreter;
import org.ethereum.beacon.discovery.schema.NodeRecordFactory;
import org.ethereum.beacon.discovery.util.RlpDecodeException;
import org.junit.jupiter.api.Test;

class FindNodeMessageTest {
Expand All @@ -28,4 +30,14 @@ void shouldRoundTrip() {
void shouldRejectTrailingBytes() {
assertRejectTrailingBytes(DECODER, MESSAGE);
}

@Test
void shouldRejectInvalidDistance() {
final FindNodeMessage message =
new FindNodeMessage(Bytes.fromHexString("0x134488556699"), List.of(257));
final Bytes rlp = message.getBytes();
assertThatThrownBy(() -> DECODER.decode(rlp))
.isInstanceOf(RlpDecodeException.class)
.hasMessageContaining("Invalid distance");
}
}