PaperMC / Velocity

The modern, next-generation Minecraft server proxy.
https://papermc.io/software/velocity
GNU General Public License v3.0
1.68k stars 583 forks source link

CorruptedFrameException: Bad VarInt decoded #1370

Open Sander0542 opened 3 days ago

Sander0542 commented 3 days ago

Expected Behavior

When I connect to my backend server which is running Valhelsia 6 (Forge 1.20.1) I can connect and start playing.

Actual Behavior

When I am trying to connect to the backend server I get the following message: Unable to connect gou to walhelsia6. Please try again later.

image

Steps to Reproduce

The ProtocolUtils.readVarInt method calls the readVarIntSafely method, but that returns the Integer.MIN_VALUE which is invalid. When I call the same method again it returns a valid Integer. In my case 17.

https://github.com/PaperMC/Velocity/blob/dev/3.0.0/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java#L129-L136

Current implementation:

  public static int readVarInt(ByteBuf buf) {
    int read = readVarIntSafely(buf);
    if (read == Integer.MIN_VALUE) {
      throw MinecraftDecoder.DEBUG ? new CorruptedFrameException("Bad VarInt decoded")
          : BAD_VARINT_CACHED;
    }
    return read;
  }

Working implementation:

    public static int readVarInt(ByteBuf buf) {
        int read = readVarIntSafely(buf);
        if (read == Integer.MIN_VALUE) {
            read = readVarIntSafely(buf);
            if (read == Integer.MIN_VALUE) {
                throw MinecraftDecoder.DEBUG ? new CorruptedFrameException("Bad VarInt decoded")
                        : BAD_VARINT_CACHED;
            }
        }
        return read;
    }

Plugin List

https://modrinth.com/plugin/ambassador

Velocity Version

Velocity 3.3.0-SNAPSHOT (git-aa4e8780-b401) velocity.command.version-copyright velocitypowered.com - GitHub

Additional Information

[20:18:40] [Netty epoll Worker #1/ERROR] [com.velocitypowered.proxy.connection.MinecraftConnection]: [server connection] Sander0542 -> valhelsia6: exception encountered in org.adde0109.ambassador.velocity.backend.ForgeLoginSessionHandler@e1cc3f2
io.netty.handler.codec.CorruptedFrameException: Error decoding class com.velocitypowered.proxy.protocol.packet.LoginPluginMessagePacket Direction CLIENTBOUND Protocol 1.20 State LOGIN ID 4
    at com.velocitypowered.proxy.protocol.netty.MinecraftDecoder.handleDecodeFailure(MinecraftDecoder.java:130) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at com.velocitypowered.proxy.protocol.netty.MinecraftDecoder.tryDecode(MinecraftDecoder.java:85) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at com.velocitypowered.proxy.protocol.netty.MinecraftDecoder.channelRead(MinecraftDecoder.java:60) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:346) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:318) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.epoll.AbstractEpollStreamChannel$EpollStreamUnsafe.epollInReady(AbstractEpollStreamChannel.java:800) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:509) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:407) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at java.base/java.lang.Thread.run(Unknown Source) [?:?]
Caused by: io.netty.handler.codec.CorruptedFrameException: Bad VarInt decoded
    at com.velocitypowered.proxy.protocol.ProtocolUtils.readVarInt(ProtocolUtils.java:132) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at com.velocitypowered.proxy.protocol.packet.LoginPluginMessagePacket.decode(LoginPluginMessagePacket.java:66) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    at com.velocitypowered.proxy.protocol.netty.MinecraftDecoder.tryDecode(MinecraftDecoder.java:83) ~[velocity-3.3.0-SNAPSHOT-400.jar:3.3.0-SNAPSHOT (git-9d25d309-b400)]
    ... 24 more
electronicboy commented 3 days ago

that working implementation just screams that the proxy is being sent bad (vanilla) data, i can't look right now, but, I'd guess that an earlier packet was malformed or modified in an unsupported manner

Sander0542 commented 3 days ago

I am running the proxy-compatible-forge mod in the backend server to enable modern forwarding.

Also, I really don't have any experience using buffers, so the only reason I found this solution was that I was trying to debug it by creating a temp variable. Then I saw the next time I called the function I got 17 as a result. This made my try it and it worked, but I have no clue why.