Skip to content

Commit

Permalink
feat(log):verify columns of hellomessage to avoid too long log (tronp…
Browse files Browse the repository at this point in the history
  • Loading branch information
317787106 authored Jan 15, 2024
1 parent 32e1e15 commit 46454a2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.DecodeUtil;
import org.tron.common.utils.StringUtil;
import org.tron.core.ChainBaseManager;
import org.tron.core.capsule.BlockCapsule;
Expand Down Expand Up @@ -169,6 +170,22 @@ public boolean valid() {
return false;
}

int maxByteSize = 200;
ByteString address = this.helloMessage.getAddress();
if (!address.isEmpty() && address.toByteArray().length > maxByteSize) {
return false;
}

ByteString sig = this.helloMessage.getSignature();
if (!sig.isEmpty() && sig.toByteArray().length > maxByteSize) {
return false;
}

ByteString codeVersion = this.helloMessage.getCodeVersion();
if (!codeVersion.isEmpty() && codeVersion.toByteArray().length > maxByteSize) {
return false;
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ public void processHelloMessage(PeerConnection peer, HelloMessage msg) {
}

if (!msg.valid()) {
logger.warn("Peer {} invalid hello message parameters, "
+ "GenesisBlockId: {}, SolidBlockId: {}, HeadBlockId: {}",
peer.getInetSocketAddress(),
ByteArray.toHexString(msg.getInstance().getGenesisBlockId().getHash().toByteArray()),
ByteArray.toHexString(msg.getInstance().getSolidBlockId().getHash().toByteArray()),
ByteArray.toHexString(msg.getInstance().getHeadBlockId().getHash().toByteArray()));
logger.warn("Peer {} invalid hello message parameters, GenesisBlockId: {}, SolidBlockId: {}, "
+ "HeadBlockId: {}, address: {}, sig: {}, codeVersion: {}",
peer.getInetSocketAddress(),
ByteArray.toHexString(msg.getInstance().getGenesisBlockId().getHash().toByteArray()),
ByteArray.toHexString(msg.getInstance().getSolidBlockId().getHash().toByteArray()),
ByteArray.toHexString(msg.getInstance().getHeadBlockId().getHash().toByteArray()),
msg.getInstance().getAddress().toByteArray().length,
msg.getInstance().getSignature().toByteArray().length,
msg.getInstance().getCodeVersion().toByteArray().length);
peer.disconnect(ReasonCode.UNEXPECTED_IDENTITY);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.mockito.Mockito;
import org.springframework.context.ApplicationContext;
import org.tron.common.application.TronApplicationContext;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.ReflectUtils;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.ChainBaseManager;
Expand Down Expand Up @@ -137,6 +138,37 @@ public void testInvalidHelloMessage() {
}
}

@Test
public void testInvalidHelloMessage2() throws Exception {
Protocol.HelloMessage.Builder builder = getTestHelloMessageBuilder();
Assert.assertTrue(new HelloMessage(builder.build().toByteArray()).valid());

builder.setAddress(ByteString.copyFrom(new byte[201]));
HelloMessage helloMessage = new HelloMessage(builder.build().toByteArray());
Assert.assertFalse(helloMessage.valid());

builder.setAddress(ByteString.copyFrom(new byte[200]));
helloMessage = new HelloMessage(builder.build().toByteArray());
Assert.assertTrue(helloMessage.valid());

builder.setSignature(ByteString.copyFrom(new byte[201]));
helloMessage = new HelloMessage(builder.build().toByteArray());
Assert.assertFalse(helloMessage.valid());

builder.setSignature(ByteString.copyFrom(new byte[200]));
helloMessage = new HelloMessage(builder.build().toByteArray());
Assert.assertTrue(helloMessage.valid());

builder.setCodeVersion(ByteString.copyFrom(new byte[201]));
helloMessage = new HelloMessage(builder.build().toByteArray());
Assert.assertFalse(helloMessage.valid());

builder.setCodeVersion(ByteString.copyFrom(new byte[200]));
helloMessage = new HelloMessage(builder.build().toByteArray());
Assert.assertTrue(helloMessage.valid());
}


@Test
public void testRelayHelloMessage() throws NoSuchMethodException {
InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001);
Expand Down Expand Up @@ -266,4 +298,13 @@ private Protocol.HelloMessage.Builder getHelloMessageBuilder(Node from, long tim

return builder;
}

private Protocol.HelloMessage.Builder getTestHelloMessageBuilder() {
InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001);
Node node = new Node(NetUtil.getNodeId(), a1.getAddress().getHostAddress(), null, a1.getPort());
Protocol.HelloMessage.Builder builder =
getHelloMessageBuilder(node, System.currentTimeMillis(),
ChainBaseManager.getChainBaseManager());
return builder;
}
}

0 comments on commit 46454a2

Please sign in to comment.