Add limits to byte arrays and string lists

This commit is contained in:
Thinkofname 2016-03-28 21:06:53 +01:00
parent f265f7c594
commit 540e924bfb
2 changed files with 13 additions and 3 deletions

View File

@ -35,19 +35,28 @@ public abstract class DefinedPacket
public static void writeArray(byte[] b, ByteBuf buf) public static void writeArray(byte[] b, ByteBuf buf)
{ {
Preconditions.checkArgument( b.length <= Short.MAX_VALUE, "Cannot send byte array longer than Short.MAX_VALUE (got %s bytes)", b.length );
writeVarInt( b.length, buf ); writeVarInt( b.length, buf );
buf.writeBytes( b ); buf.writeBytes( b );
} }
public static byte[] readArray(ByteBuf buf) public static byte[] readArray(ByteBuf buf)
{ {
byte[] ret = new byte[ readVarInt( buf ) ]; return readArray( buf, Short.MAX_VALUE );
}
public static byte[] readArray(ByteBuf buf, int limit)
{
int len = readVarInt( buf );
Preconditions.checkArgument( len <= limit, "Cannot receive byte array longer than %d (got %s bytes)", limit, len );
byte[] ret = new byte[ len ];
buf.readBytes( ret ); buf.readBytes( ret );
return ret; return ret;
} }
public static void writeStringArray(List<String> s, ByteBuf buf) public static void writeStringArray(List<String> s, ByteBuf buf)
{ {
Preconditions.checkArgument( s.size() <= 64, "Cannot send string array longer than 64 (got %s strings)", s.size() );
writeVarInt( s.size(), buf ); writeVarInt( s.size(), buf );
for ( String str : s ) for ( String str : s )
{ {
@ -58,6 +67,7 @@ public abstract class DefinedPacket
public static List<String> readStringArray(ByteBuf buf) public static List<String> readStringArray(ByteBuf buf)
{ {
int len = readVarInt( buf ); int len = readVarInt( buf );
Preconditions.checkArgument( len <= 64, "Cannot receive string array longer than 64 (got %s strings)", len );
List<String> ret = new ArrayList<>( len ); List<String> ret = new ArrayList<>( len );
for ( int i = 0; i < len; i++ ) for ( int i = 0; i < len; i++ )
{ {

View File

@ -22,8 +22,8 @@ public class EncryptionResponse extends DefinedPacket
@Override @Override
public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
{ {
sharedSecret = readArray( buf ); sharedSecret = readArray( buf, 256 );
verifyToken = readArray( buf ); verifyToken = readArray( buf, 256 );
} }
@Override @Override