Skip to content

Commit 4cb1588

Browse files
committed
Add option to validate decompression
1 parent 9e38c61 commit 4cb1588

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

src/main/java/com/github/steveice10/packetlib/Session.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,15 @@ public interface Session {
159159
*
160160
* @return This session's compression threshold.
161161
*/
162-
public int getCompressionThreshold();
162+
int getCompressionThreshold();
163163

164164
/**
165165
* Sets the compression packet length threshold for this session (-1 = disabled).
166166
*
167167
* @param threshold The new compression threshold.
168+
* @param validateDecompression whether to validate that the decompression fits within size checks.
168169
*/
169-
public void setCompressionThreshold(int threshold);
170+
void setCompressionThreshold(int threshold, boolean validateDecompression);
170171

171172
/**
172173
* Enables encryption for this session.

src/main/java/com/github/steveice10/packetlib/tcp/TcpPacketCompression.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ public class TcpPacketCompression extends ByteToMessageCodec<ByteBuf> {
2020
private final Deflater deflater = new Deflater();
2121
private final Inflater inflater = new Inflater();
2222
private final byte[] buf = new byte[8192];
23+
private final boolean validateDecompression;
2324

24-
public TcpPacketCompression(Session session) {
25+
public TcpPacketCompression(Session session, boolean validateDecompression) {
2526
this.session = session;
27+
this.validateDecompression = validateDecompression;
2628
}
2729

2830
@Override
@@ -63,12 +65,14 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out)
6365
if(size == 0) {
6466
out.add(buf.readBytes(buf.readableBytes()));
6567
} else {
66-
if(size < this.session.getCompressionThreshold()) {
67-
throw new DecoderException("Badly compressed packet: size of " + size + " is below threshold of " + this.session.getCompressionThreshold() + ".");
68-
}
68+
if (validateDecompression) { // This is sectioned off as of at least Java Edition 1.18
69+
if (size < this.session.getCompressionThreshold()) {
70+
throw new DecoderException("Badly compressed packet: size of " + size + " is below threshold of " + this.session.getCompressionThreshold() + ".");
71+
}
6972

70-
if(size > MAX_COMPRESSED_SIZE) {
71-
throw new DecoderException("Badly compressed packet: size of " + size + " is larger than protocol maximum of " + MAX_COMPRESSED_SIZE + ".");
73+
if (size > MAX_COMPRESSED_SIZE) {
74+
throw new DecoderException("Badly compressed packet: size of " + size + " is larger than protocol maximum of " + MAX_COMPRESSED_SIZE + ".");
75+
}
7276
}
7377

7478
byte[] bytes = new byte[buf.readableBytes()];

src/main/java/com/github/steveice10/packetlib/tcp/TcpSession.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,12 @@ public int getCompressionThreshold() {
177177
}
178178

179179
@Override
180-
public void setCompressionThreshold(int threshold) {
180+
public void setCompressionThreshold(int threshold, boolean validateDecompression) {
181181
this.compressionThreshold = threshold;
182182
if (this.channel != null) {
183183
if (this.compressionThreshold >= 0) {
184184
if (this.channel.pipeline().get("compression") == null) {
185-
this.channel.pipeline().addBefore("codec", "compression", new TcpPacketCompression(this));
185+
this.channel.pipeline().addBefore("codec", "compression", new TcpPacketCompression(this, validateDecompression));
186186
}
187187
} else if (this.channel.pipeline().get("compression") != null) {
188188
this.channel.pipeline().remove("compression");

0 commit comments

Comments
 (0)