Skip to content

Commit

Permalink
assert payload size
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Nov 20, 2023
1 parent 7c3648e commit 3573e5a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
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,13 +33,23 @@

@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) {
int size = buf.readableBytes();

if (size < 0 || size > MAX_PAYLOAD_SIZE) {
throw new IllegalArgumentException("Payload may not be larger than " + MAX_PAYLOAD_SIZE + " bytes");
}

cir.setReturnValue(new RetainedPayload(id, PacketByteBufs.retainedSlice(buf)));
buf.skipBytes(buf.readableBytes());
buf.skipBytes(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,13 +33,23 @@

@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) {
int size = buf.readableBytes();

if (size < 0 || size > MAX_PAYLOAD_SIZE) {
throw new IllegalArgumentException("Payload may not be larger than " + MAX_PAYLOAD_SIZE + " bytes");
}

cir.setReturnValue(new RetainedPayload(id, PacketByteBufs.retainedSlice(buf)));
buf.skipBytes(buf.readableBytes());
buf.skipBytes(size);
}
}

0 comments on commit 3573e5a

Please sign in to comment.