Skip to content

Commit

Permalink
use bytebufhelper instead
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Dec 20, 2024
1 parent d021f2c commit e8399a9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;

import java.nio.charset.StandardCharsets;

Expand Down Expand Up @@ -36,23 +35,23 @@ public void onPacketReceive(PacketReceiveEvent event) {
|| event.getPacketType() == PacketType.Play.Client.CHAT_COMMAND
|| event.getPacketType() == PacketType.Play.Client.CHAT_COMMAND_UNSIGNED
) {
if (isStringTooBig(new PacketWrapper<>(event))) {
if (isStringTooBig(event)) {
event.setCancelled(true);
onCancel(log, kick, event.getUser());
}
}
}

private boolean isStringTooBig(PacketWrapper<?> packetWrapper) {
int strBufLen = packetWrapper.readVarInt();
private boolean isStringTooBig(PacketReceiveEvent event) {
int strBufLen = ByteBufHelper.readVarInt(event.getByteBuf());

// Check if the received encoded string buffer length is zero or longer than maximum allowed
if (strBufLen < 0 || strBufLen > charLimit * 4) {
return true;
}

// The received string length is longer than maximum allowed
return ByteBufHelper.toString(packetWrapper.buffer, ByteBufHelper.readerIndex(packetWrapper.buffer), strBufLen, StandardCharsets.UTF_8)
return ByteBufHelper.toString(event.getByteBuf(), ByteBufHelper.readerIndex(event.getByteBuf()), strBufLen, StandardCharsets.UTF_8)
.length() > charLimit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;

import java.nio.charset.StandardCharsets;

Expand Down Expand Up @@ -36,23 +35,23 @@ public void onPacketReceive(PacketReceiveEvent event) {
|| event.getPacketType() == PacketType.Play.Client.CHAT_COMMAND
|| event.getPacketType() == PacketType.Play.Client.CHAT_COMMAND_UNSIGNED
) {
if (isStringTooBig(new PacketWrapper<>(event))) {
if (isStringTooBig(event)) {
event.setCancelled(true);
onCancel(log, kick, event.getUser());
}
}
}

private boolean isStringTooBig(PacketWrapper<?> packetWrapper) {
int strBufLen = packetWrapper.readVarInt();
private boolean isStringTooBig(PacketReceiveEvent event) {
int strBufLen = ByteBufHelper.readVarInt(event.getByteBuf());

// Check if the received encoded string buffer length is zero or longer than maximum allowed
if (strBufLen < 0 || strBufLen > charLimit * 4) {
return true;
}

// The received string length is longer than maximum allowed
return ByteBufHelper.toString(packetWrapper.buffer, ByteBufHelper.readerIndex(packetWrapper.buffer), strBufLen, StandardCharsets.UTF_8)
return ByteBufHelper.toString(event.getByteBuf(), ByteBufHelper.readerIndex(event.getByteBuf()), strBufLen, StandardCharsets.UTF_8)
.length() > charLimit;
}
}

0 comments on commit e8399a9

Please sign in to comment.