Skip to content

Commit

Permalink
Check payload size
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Oct 29, 2023
1 parent 0596ed7 commit ee43962
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ public static void write(PacketByteBuf byteBuf, PacketByteBuf data) {
byteBuf.writeBytes(data.copy());
}

public static PacketByteBuf read(PacketByteBuf byteBuf) {
public static PacketByteBuf read(PacketByteBuf byteBuf, int maxSize) {
int size = byteBuf.readableBytes();

if (size < 0 || size > maxSize) {
throw new IllegalArgumentException("Payload may not be larger than %d bytes".formatted(maxSize));
}

PacketByteBuf newBuf = PacketByteBufs.create();
newBuf.writeBytes(byteBuf.copy());
byteBuf.skipBytes(byteBuf.readableBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package net.fabricmc.fabric.mixin.networking;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
Expand All @@ -31,12 +33,16 @@

@Mixin(CustomPayloadC2SPacket.class)
public class CustomPayloadC2SPacketMixin {
@Shadow
@Final
private static int MAX_PAYLOAD_SIZE;

@Inject(
method = "readPayload",
at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/c2s/common/CustomPayloadC2SPacket;readUnknownPayload(Lnet/minecraft/util/Identifier;Lnet/minecraft/network/PacketByteBuf;)Lnet/minecraft/network/packet/UnknownCustomPayload;"),
cancellable = true
)
private static void readPayload(Identifier id, PacketByteBuf buf, CallbackInfoReturnable<CustomPayload> cir) {
cir.setReturnValue(new PacketByteBufPayload(id, PayloadHelper.read(buf)));
cir.setReturnValue(new PacketByteBufPayload(id, PayloadHelper.read(buf, MAX_PAYLOAD_SIZE)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package net.fabricmc.fabric.mixin.networking;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
Expand All @@ -31,12 +33,16 @@

@Mixin(CustomPayloadS2CPacket.class)
public class CustomPayloadS2CPacketMixin {
@Shadow
@Final
private static int MAX_PAYLOAD_SIZE;

@Inject(
method = "readPayload",
at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/common/CustomPayloadS2CPacket;readUnknownPayload(Lnet/minecraft/util/Identifier;Lnet/minecraft/network/PacketByteBuf;)Lnet/minecraft/network/packet/UnknownCustomPayload;"),
cancellable = true
)
private static void readPayload(Identifier id, PacketByteBuf buf, CallbackInfoReturnable<CustomPayload> cir) {
cir.setReturnValue(new PacketByteBufPayload(id, PayloadHelper.read(buf)));
cir.setReturnValue(new PacketByteBufPayload(id, PayloadHelper.read(buf, MAX_PAYLOAD_SIZE)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package net.fabricmc.fabric.mixin.networking;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
Expand All @@ -31,8 +33,12 @@

@Mixin(LoginQueryRequestS2CPacket.class)
public class LoginQueryRequestS2CPacketMixin {
@Shadow
@Final
private static int MAX_PAYLOAD_SIZE;

@Inject(method = "readPayload", at = @At("HEAD"), cancellable = true)
private static void readPayload(Identifier id, PacketByteBuf buf, CallbackInfoReturnable<LoginQueryRequestPayload> cir) {
cir.setReturnValue(new PacketByteBufLoginQueryRequestPayload(id, PayloadHelper.read(buf)));
cir.setReturnValue(new PacketByteBufLoginQueryRequestPayload(id, PayloadHelper.read(buf, MAX_PAYLOAD_SIZE)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@

package net.fabricmc.fabric.mixin.networking;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.c2s.login.LoginQueryResponsePayload;
import net.minecraft.network.packet.c2s.login.LoginQueryResponseC2SPacket;
import net.minecraft.network.packet.c2s.login.LoginQueryResponsePayload;

import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryResponse;
import net.fabricmc.fabric.impl.networking.payload.PayloadHelper;

@Mixin(LoginQueryResponseC2SPacket.class)
public class LoginQueryResponseC2SPacketMixin {
@Shadow
@Final
private static int MAX_PAYLOAD_SIZE;

@Inject(method = "readPayload", at = @At("HEAD"), cancellable = true)
private static void readResponse(int queryId, PacketByteBuf buf, CallbackInfoReturnable<LoginQueryResponsePayload> cir) {
boolean hasPayload = buf.readBoolean();
Expand All @@ -39,6 +45,6 @@ private static void readResponse(int queryId, PacketByteBuf buf, CallbackInfoRet
return;
}

cir.setReturnValue(new PacketByteBufLoginQueryResponse(PayloadHelper.read(buf)));
cir.setReturnValue(new PacketByteBufLoginQueryResponse(PayloadHelper.read(buf, MAX_PAYLOAD_SIZE)));
}
}

0 comments on commit ee43962

Please sign in to comment.