check packet length against max packet length

This commit is contained in:
Bixilon 2020-09-06 13:41:11 +02:00
parent 32fb5092c3
commit f7dd739aad
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4

View File

@ -45,7 +45,6 @@ public class Network {
int compressionThreshold = -1; int compressionThreshold = -1;
Socket socket; Socket socket;
OutputStream outputStream; OutputStream outputStream;
InputStream cipherInputStream;
InputStream inputStream; InputStream inputStream;
boolean encryptionEnabled = false; boolean encryptionEnabled = false;
SecretKey secretKey; SecretKey secretKey;
@ -78,7 +77,6 @@ public class Network {
socket.setKeepAlive(true); socket.setKeepAlive(true);
outputStream = socket.getOutputStream(); outputStream = socket.getOutputStream();
inputStream = socket.getInputStream(); inputStream = socket.getInputStream();
cipherInputStream = inputStream;
socketRThread.setName(String.format("%d/SocketR", connection.getConnectionId())); socketRThread.setName(String.format("%d/SocketR", connection.getConnectionId()));
@ -153,7 +151,7 @@ public class Network {
int length = 0; int length = 0;
int read; int read;
do { do {
read = cipherInputStream.read(); read = inputStream.read();
if (read == -1) { if (read == -1) {
disconnect(); disconnect();
return; return;
@ -166,8 +164,12 @@ public class Network {
throw new RuntimeException("VarInt is too big"); throw new RuntimeException("VarInt is too big");
} }
} while ((read & 0b10000000) != 0); } while ((read & 0b10000000) != 0);
if (length > ProtocolDefinition.PROTOCOL_PACKET_MAX_SIZE) {
byte[] data = cipherInputStream.readNBytes(length); Log.protocol(String.format("Server sent us a to big packet (%d bytes > %d bytes)", length, ProtocolDefinition.PROTOCOL_PACKET_MAX_SIZE));
inputStream.skip(length);
continue;
}
byte[] data = inputStream.readNBytes(length);
if (compressionThreshold >= 0) { if (compressionThreshold >= 0) {
// compression is enabled // compression is enabled
@ -257,7 +259,7 @@ public class Network {
public void enableEncryption(SecretKey secretKey) { public void enableEncryption(SecretKey secretKey) {
Cipher cipherEncrypt = CryptManager.createNetCipherInstance(Cipher.ENCRYPT_MODE, secretKey); Cipher cipherEncrypt = CryptManager.createNetCipherInstance(Cipher.ENCRYPT_MODE, secretKey);
Cipher cipherDecrypt = CryptManager.createNetCipherInstance(Cipher.DECRYPT_MODE, secretKey); Cipher cipherDecrypt = CryptManager.createNetCipherInstance(Cipher.DECRYPT_MODE, secretKey);
cipherInputStream = new CipherInputStream(inputStream, cipherDecrypt); inputStream = new CipherInputStream(inputStream, cipherDecrypt);
outputStream = new CipherOutputStream(outputStream, cipherEncrypt); outputStream = new CipherOutputStream(outputStream, cipherEncrypt);
encryptionEnabled = true; encryptionEnabled = true;
Log.debug("Encryption enabled!"); Log.debug("Encryption enabled!");