|
| 1 | +package org.tron.core.net; |
| 2 | + |
| 3 | +import com.google.protobuf.ByteString; |
| 4 | +import com.google.protobuf.Message; |
| 5 | +import com.google.protobuf.UnknownFieldSet; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Test; |
| 8 | +import org.springframework.test.util.ReflectionTestUtils; |
| 9 | +import org.tron.core.net.message.MessageTypes; |
| 10 | +import org.tron.core.net.message.adv.FetchInvDataMessage; |
| 11 | +import org.tron.core.net.message.adv.InventoryMessage; |
| 12 | +import org.tron.core.net.message.base.DisconnectMessage; |
| 13 | +import org.tron.core.net.message.handshake.HelloMessage; |
| 14 | +import org.tron.core.net.message.sync.BlockInventoryMessage; |
| 15 | +import org.tron.core.net.message.sync.ChainInventoryMessage; |
| 16 | +import org.tron.core.net.message.sync.SyncBlockChainMessage; |
| 17 | +import org.tron.protos.Protocol; |
| 18 | +import org.tron.protos.Protocol.Inventory.InventoryType; |
| 19 | +import org.tron.protos.Protocol.ReasonCode; |
| 20 | + |
| 21 | +/** |
| 22 | + * Verifies that transient control messages (not persisted to the database) drop attacker-appended |
| 23 | + * unknown fields at parse time via {@code DiscardUnknownFieldsParser}, so padding cannot be |
| 24 | + * retained in memory. trx/trxs/block/pbft messages are intentionally excluded because they are |
| 25 | + * stored and rely on their exact serialized bytes for hashing/signature verification. |
| 26 | + */ |
| 27 | +public class MessageUnknownFieldTest { |
| 28 | + |
| 29 | + // A field number not used by any message under test, so it always parses as an unknown field. |
| 30 | + private static final int UNKNOWN_FIELD_NUMBER = 1000; |
| 31 | + private static final int PADDING_SIZE = 8192; |
| 32 | + |
| 33 | + private static byte[] withUnknown(Message msg) { |
| 34 | + UnknownFieldSet unknown = UnknownFieldSet.newBuilder() |
| 35 | + .addField(UNKNOWN_FIELD_NUMBER, UnknownFieldSet.Field.newBuilder() |
| 36 | + .addLengthDelimited(ByteString.copyFrom(new byte[PADDING_SIZE])) |
| 37 | + .build()) |
| 38 | + .build(); |
| 39 | + byte[] padded = msg.toBuilder().setUnknownFields(unknown).build().toByteArray(); |
| 40 | + // Sanity: the padded bytes really are larger than the clean message. |
| 41 | + Assert.assertTrue(padded.length > msg.toByteArray().length + PADDING_SIZE - 16); |
| 42 | + return padded; |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void inventoryDiscardsUnknownFields() throws Exception { |
| 47 | + Protocol.Inventory inv = Protocol.Inventory.newBuilder() |
| 48 | + .setType(InventoryType.TRX) |
| 49 | + .addIds(ByteString.copyFrom(new byte[32])) |
| 50 | + .build(); |
| 51 | + InventoryMessage msg = new InventoryMessage(withUnknown(inv)); |
| 52 | + Assert.assertTrue(msg.getInventory().getUnknownFields().asMap().isEmpty()); |
| 53 | + Assert.assertEquals(inv, msg.getInventory()); |
| 54 | + Assert.assertEquals(1, msg.getHashList().size()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void fetchInvDataDiscardsUnknownFields() throws Exception { |
| 59 | + Protocol.Inventory inv = Protocol.Inventory.newBuilder() |
| 60 | + .setType(InventoryType.BLOCK) |
| 61 | + .addIds(ByteString.copyFrom(new byte[32])) |
| 62 | + .build(); |
| 63 | + FetchInvDataMessage msg = new FetchInvDataMessage(withUnknown(inv)); |
| 64 | + Assert.assertTrue(msg.getInventory().getUnknownFields().asMap().isEmpty()); |
| 65 | + Assert.assertEquals(inv, msg.getInventory()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void helloMessageDiscardsUnknownFields() throws Exception { |
| 70 | + Protocol.HelloMessage.BlockId blockId = Protocol.HelloMessage.BlockId.newBuilder() |
| 71 | + .setHash(ByteString.copyFrom(new byte[32])) |
| 72 | + .build(); |
| 73 | + Protocol.HelloMessage hello = Protocol.HelloMessage.newBuilder() |
| 74 | + .setVersion(1) |
| 75 | + .setTimestamp(123456789L) |
| 76 | + .setGenesisBlockId(blockId) |
| 77 | + .setSolidBlockId(blockId) |
| 78 | + .setHeadBlockId(blockId) |
| 79 | + .build(); |
| 80 | + HelloMessage msg = new HelloMessage(withUnknown(hello)); |
| 81 | + Assert.assertTrue(msg.getHelloMessage().getUnknownFields().asMap().isEmpty()); |
| 82 | + Assert.assertEquals(1, msg.getVersion()); |
| 83 | + Assert.assertEquals(123456789L, msg.getTimestamp()); |
| 84 | + assertCleanHelloBytes(hello, msg); |
| 85 | + } |
| 86 | + |
| 87 | + private static void assertCleanHelloBytes(Protocol.HelloMessage hello, HelloMessage msg) { |
| 88 | + Assert.assertEquals(hello, msg.getHelloMessage()); |
| 89 | + byte[] clean = hello.toByteArray(); |
| 90 | + Assert.assertArrayEquals(clean, msg.getData()); |
| 91 | + byte[] expectedSendBytes = new byte[clean.length + 1]; |
| 92 | + expectedSendBytes[0] = MessageTypes.P2P_HELLO.asByte(); |
| 93 | + System.arraycopy(clean, 0, expectedSendBytes, 1, clean.length); |
| 94 | + Assert.assertArrayEquals(expectedSendBytes, msg.getSendBytes()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void chainInventoryDiscardsUnknownFields() throws Exception { |
| 99 | + Protocol.ChainInventory chainInv = Protocol.ChainInventory.newBuilder() |
| 100 | + .addIds(Protocol.ChainInventory.BlockId.newBuilder() |
| 101 | + .setHash(ByteString.copyFrom(new byte[32])).setNumber(1).build()) |
| 102 | + .setRemainNum(0) |
| 103 | + .build(); |
| 104 | + ChainInventoryMessage msg = new ChainInventoryMessage(withUnknown(chainInv)); |
| 105 | + Protocol.ChainInventory parsed = |
| 106 | + (Protocol.ChainInventory) ReflectionTestUtils.getField(msg, "chainInventory"); |
| 107 | + Assert.assertNotNull(parsed); |
| 108 | + Assert.assertTrue(parsed.getUnknownFields().asMap().isEmpty()); |
| 109 | + Assert.assertEquals(chainInv, parsed); |
| 110 | + Assert.assertEquals(1, msg.getBlockIds().size()); |
| 111 | + Assert.assertEquals(Long.valueOf(0L), msg.getRemainNum()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void disconnectMessageDiscardsUnknownFields() throws Exception { |
| 116 | + Protocol.DisconnectMessage disconnect = Protocol.DisconnectMessage.newBuilder() |
| 117 | + .setReason(ReasonCode.TOO_MANY_PEERS) |
| 118 | + .build(); |
| 119 | + DisconnectMessage msg = new DisconnectMessage(withUnknown(disconnect)); |
| 120 | + Protocol.DisconnectMessage parsed = |
| 121 | + (Protocol.DisconnectMessage) ReflectionTestUtils.getField(msg, "disconnectMessage"); |
| 122 | + Assert.assertNotNull(parsed); |
| 123 | + Assert.assertTrue(parsed.getUnknownFields().asMap().isEmpty()); |
| 124 | + Assert.assertEquals(disconnect, parsed); |
| 125 | + Assert.assertEquals(ReasonCode.TOO_MANY_PEERS, msg.getReason()); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void blockInventoryDiscardsUnknownFields() throws Exception { |
| 130 | + Protocol.BlockInventory inventory = Protocol.BlockInventory.newBuilder() |
| 131 | + .addIds(Protocol.BlockInventory.BlockId.newBuilder() |
| 132 | + .setHash(ByteString.copyFrom(new byte[32])).setNumber(1).build()) |
| 133 | + .setType(Protocol.BlockInventory.Type.ADVTISE) |
| 134 | + .build(); |
| 135 | + BlockInventoryMessage msg = new BlockInventoryMessage(withUnknown(inventory)); |
| 136 | + Protocol.BlockInventory parsed = |
| 137 | + (Protocol.BlockInventory) ReflectionTestUtils.getField(msg, "blockInventory"); |
| 138 | + Assert.assertNotNull(parsed); |
| 139 | + Assert.assertTrue(parsed.getUnknownFields().asMap().isEmpty()); |
| 140 | + Assert.assertEquals(inventory, parsed); |
| 141 | + Assert.assertEquals(1, msg.getBlockIds().size()); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void syncBlockChainDiscardsUnknownFields() throws Exception { |
| 146 | + Protocol.BlockInventory inventory = Protocol.BlockInventory.newBuilder() |
| 147 | + .addIds(Protocol.BlockInventory.BlockId.newBuilder() |
| 148 | + .setHash(ByteString.copyFrom(new byte[32])).setNumber(1).build()) |
| 149 | + .setType(Protocol.BlockInventory.Type.SYNC) |
| 150 | + .build(); |
| 151 | + SyncBlockChainMessage msg = new SyncBlockChainMessage(withUnknown(inventory)); |
| 152 | + Protocol.BlockInventory parsed = |
| 153 | + (Protocol.BlockInventory) ReflectionTestUtils.getField(msg, "blockInventory"); |
| 154 | + Assert.assertNotNull(parsed); |
| 155 | + Assert.assertTrue(parsed.getUnknownFields().asMap().isEmpty()); |
| 156 | + Assert.assertEquals(inventory, parsed); |
| 157 | + Assert.assertEquals(1, msg.getBlockIds().size()); |
| 158 | + } |
| 159 | +} |
0 commit comments