More conservative filtering

this now only drops consecutive packets with identical buffers, and only when instructed to.
This is good enough to fix both right- and left-click spam bugs as of 1.21.100.
This commit is contained in:
Dylan K. Taylor
2025-09-15 19:07:54 +01:00
parent f80bef27e5
commit 593d6cc6a1
3 changed files with 72 additions and 12 deletions

View File

@@ -0,0 +1,31 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\network;
/**
* Thrown by packet handlers to instruct network sessions to drop repeated packets.
*/
final class FilterNoisyPacketException extends \RuntimeException{
}

View File

@@ -36,6 +36,7 @@ use pocketmine\lang\Translatable;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\network\FilterNoisyPacketException;
use pocketmine\network\mcpe\cache\ChunkCache;
use pocketmine\network\mcpe\compression\CompressBatchPromise;
use pocketmine\network\mcpe\compression\Compressor;
@@ -146,6 +147,8 @@ class NetworkSession{
private const INCOMING_GAME_PACKETS_PER_TICK = 2;
private const INCOMING_GAME_PACKETS_BUFFER_TICKS = 100;
private const INCOMING_PACKET_BATCH_HARD_LIMIT = 300;
private PacketRateLimiter $packetBatchLimiter;
private PacketRateLimiter $gamePacketLimiter;
@@ -387,18 +390,15 @@ class NetworkSession{
}
private function checkRepeatedPacketFilter(string $buffer) : bool{
//TODO: would be great if we didn't repeat reading the ID inside PacketPool
$dummy = 0;
$packetId = Binary::readUnsignedVarInt($buffer, $dummy);
if(isset($this->repeatedPacketFilters[$packetId])){
if($buffer === $this->repeatedPacketFilters[$packetId]){
$this->repeatedPacketFilterStats[$packetId]++;
if($buffer === $this->noisyPacketBuffer){
$this->noisyPacketsDropped++;
return true;
}
$this->repeatedPacketFilters[$packetId] = $buffer;
}
//stop filtering once we see a packet with a different buffer
//this won't be any good for interleaved spammy packets, but we haven't seen any of those so far, and this
//is the simplest and most conservative filter we can do
$this->noisyPacketBuffer = "";
$this->noisyPacketsDropped = 0;
return false;
}
@@ -453,9 +453,17 @@ class NetworkSession{
$decompressed = $payload;
}
$count = 0;
try{
$stream = new BinaryStream($decompressed);
foreach(PacketBatch::decodeRaw($stream) as $buffer){
if(++$count >= self::INCOMING_PACKET_BATCH_HARD_LIMIT){
//this should be well more than enough; under normal conditions the game packet rate limiter
//will kick in well before this. This is only here to make sure we can't get huge batches of
//noisy packets to bog down the server, since those aren't counted by the regular limiter.
throw new PacketHandlingException("Reached hard limit of " . self::INCOMING_PACKET_BATCH_HARD_LIMIT . " per batch packet");
}
if($this->checkRepeatedPacketFilter($buffer)){
continue;
}
@@ -471,6 +479,8 @@ class NetworkSession{
}catch(PacketHandlingException $e){
$this->logger->debug($packet->getName() . ": " . base64_encode($buffer));
throw PacketHandlingException::wrap($e, "Error processing " . $packet->getName());
}catch(FilterNoisyPacketException){
$this->noisyPacketBuffer = $buffer;
}
if(!$this->isConnected()){
//handling this packet may have caused a disconnection

View File

@@ -44,6 +44,7 @@ use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\network\FilterNoisyPacketException;
use pocketmine\network\mcpe\InventoryManager;
use pocketmine\network\mcpe\NetworkSession;
use pocketmine\network\mcpe\protocol\ActorEventPacket;
@@ -473,6 +474,22 @@ class InGamePacketHandler extends PacketHandler{
switch($data->getActionType()){
case UseItemTransactionData::ACTION_CLICK_BLOCK:
//TODO: start hack for client spam bug
$clickPos = $data->getClickPosition();
$spamBug = ($this->lastRightClickData !== null &&
microtime(true) - $this->lastRightClickTime < 0.1 && //100ms
$this->lastRightClickData->getPlayerPosition()->distanceSquared($data->getPlayerPosition()) < 0.00001 &&
$this->lastRightClickData->getBlockPosition()->equals($data->getBlockPosition()) &&
$this->lastRightClickData->getClickPosition()->distanceSquared($clickPos) < 0.00001 //signature spam bug has 0 distance, but allow some error
);
//get rid of continued spam if the player clicks and holds right-click
$this->lastRightClickData = $data;
$this->lastRightClickTime = microtime(true);
if($spamBug){
throw new FilterNoisyPacketException();
}
//TODO: end hack for client spam bug
self::validateFacing($data->getFace());
$blockPos = $data->getBlockPosition();
@@ -724,7 +741,9 @@ class InGamePacketHandler extends PacketHandler{
}
public function handleAnimate(AnimatePacket $packet) : bool{
return true; //Not used
//this spams harder than a firehose on left click if "Improved Input Response" is enabled, and we don't even
//use it anyway :<
throw new FilterNoisyPacketException();
}
public function handleContainerClose(ContainerClosePacket $packet) : bool{