Renamed BadPacketException -> PacketHandlingException

this better describes the intent, instead of just vaguely describing a packet as 'bad'.
This commit is contained in:
Dylan K. Taylor 2021-03-30 01:15:28 +01:00
parent 25998720ce
commit b7a6c9dc17
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
7 changed files with 35 additions and 31 deletions

View File

@ -197,7 +197,7 @@ class Network{
if(preg_match($handler->getPattern(), $packet) === 1){
try{
$handled = $handler->handle($interface, $address, $port, $packet);
}catch(BadPacketException $e){
}catch(PacketHandlingException $e){
$handled = true;
$this->logger->error("Bad raw packet from /$address:$port: " . $e->getMessage());
$this->blockAddress($address, 600);

View File

@ -23,7 +23,11 @@ declare(strict_types=1);
namespace pocketmine\network;
class BadPacketException extends \RuntimeException{
/**
* Thrown when an error occurs during packet handling - for example, a message contained invalid options, packet shorter
* than expected, unknown packet, etc.
*/
class PacketHandlingException extends \RuntimeException{
public static function wrap(\Throwable $previous, ?string $prefix = null) : self{
return new self(($prefix !== null ? $prefix . ": " : "") . $previous->getMessage(), 0, $previous);

View File

@ -32,7 +32,7 @@ interface RawPacketHandler{
public function getPattern() : string;
/**
* @throws BadPacketException
* @throws PacketHandlingException
*/
public function handle(AdvancedNetworkInterface $interface, string $address, int $port, string $packet) : bool;
}

View File

@ -37,7 +37,6 @@ use pocketmine\form\Form;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\network\BadPacketException;
use pocketmine\network\mcpe\cache\ChunkCache;
use pocketmine\network\mcpe\compression\CompressBatchPromise;
use pocketmine\network\mcpe\compression\Compressor;
@ -96,6 +95,7 @@ use pocketmine\network\mcpe\protocol\types\PlayerListEntry;
use pocketmine\network\mcpe\protocol\types\PlayerPermissions;
use pocketmine\network\mcpe\protocol\UpdateAttributesPacket;
use pocketmine\network\NetworkSessionManager;
use pocketmine\network\PacketHandlingException;
use pocketmine\permission\DefaultPermissions;
use pocketmine\player\GameMode;
use pocketmine\player\Player;
@ -324,7 +324,7 @@ class NetworkSession{
}
/**
* @throws BadPacketException
* @throws PacketHandlingException
*/
public function handleEncoded(string $payload) : void{
if(!$this->connected){
@ -337,7 +337,7 @@ class NetworkSession{
$payload = $this->cipher->decrypt($payload);
}catch(DecryptionException $e){
$this->logger->debug("Encrypted packet: " . base64_encode($payload));
throw BadPacketException::wrap($e, "Packet decryption error");
throw PacketHandlingException::wrap($e, "Packet decryption error");
}finally{
Timings::$playerNetworkReceiveDecrypt->stopTiming();
}
@ -348,7 +348,7 @@ class NetworkSession{
$stream = new PacketBatch($this->compressor->decompress($payload));
}catch(DecompressionException $e){
$this->logger->debug("Failed to decompress packet: " . base64_encode($payload));
throw BadPacketException::wrap($e, "Compressed packet batch decode error");
throw PacketHandlingException::wrap($e, "Compressed packet batch decode error");
}finally{
Timings::$playerNetworkReceiveDecompress->stopTiming();
}
@ -357,19 +357,19 @@ class NetworkSession{
foreach($stream->getPackets($this->packetPool, 500) as $packet){
try{
$this->handleDataPacket($packet);
}catch(BadPacketException $e){
}catch(PacketHandlingException $e){
$this->logger->debug($packet->getName() . ": " . base64_encode($packet->getSerializer()->getBuffer()));
throw BadPacketException::wrap($e, "Error processing " . $packet->getName());
throw PacketHandlingException::wrap($e, "Error processing " . $packet->getName());
}
}
}catch(PacketDecodeException $e){
$this->logger->logException($e);
throw BadPacketException::wrap($e, "Packet batch decode error");
throw PacketHandlingException::wrap($e, "Packet batch decode error");
}
}
/**
* @throws BadPacketException
* @throws PacketHandlingException
*/
public function handleDataPacket(Packet $packet) : void{
if(!($packet instanceof ServerboundPacket)){
@ -377,7 +377,7 @@ class NetworkSession{
$this->logger->debug("Garbage serverbound " . $packet->getName() . ": " . base64_encode($packet->getSerializer()->getBuffer()));
return;
}
throw new BadPacketException("Unexpected non-serverbound packet");
throw new PacketHandlingException("Unexpected non-serverbound packet");
}
$timings = Timings::getReceiveDataPacketTimings($packet);
@ -387,7 +387,7 @@ class NetworkSession{
try{
$packet->decode();
}catch(PacketDecodeException $e){
throw BadPacketException::wrap($e);
throw PacketHandlingException::wrap($e);
}
$stream = $packet->getSerializer();
if(!$stream->feof()){

View File

@ -41,7 +41,6 @@ use pocketmine\item\WrittenBook;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\network\BadPacketException;
use pocketmine\network\mcpe\convert\SkinAdapterSingleton;
use pocketmine\network\mcpe\convert\TypeConverter;
use pocketmine\network\mcpe\InventoryManager;
@ -90,6 +89,7 @@ use pocketmine\network\mcpe\protocol\types\inventory\UIInventorySlotOffset;
use pocketmine\network\mcpe\protocol\types\inventory\UseItemOnEntityTransactionData;
use pocketmine\network\mcpe\protocol\types\inventory\UseItemTransactionData;
use pocketmine\network\mcpe\protocol\types\inventory\WindowTypes;
use pocketmine\network\PacketHandlingException;
use pocketmine\player\Player;
use pocketmine\utils\AssumptionFailedError;
use function array_key_exists;
@ -640,7 +640,7 @@ class InGamePacketHandler extends PacketHandler{
try{
$text = SignText::fromBlob($textBlobTag->getValue());
}catch(\InvalidArgumentException $e){
throw BadPacketException::wrap($e, "Invalid sign text update");
throw PacketHandlingException::wrap($e, "Invalid sign text update");
}
try{
@ -650,7 +650,7 @@ class InGamePacketHandler extends PacketHandler{
}
}
}catch(\UnexpectedValueException $e){
throw BadPacketException::wrap($e);
throw PacketHandlingException::wrap($e);
}
return true;
@ -721,7 +721,7 @@ class InGamePacketHandler extends PacketHandler{
try{
$skin = SkinAdapterSingleton::get()->fromSkinData($packet->skin);
}catch(InvalidSkinException $e){
throw BadPacketException::wrap($e, "Invalid skin in PlayerSkinPacket");
throw PacketHandlingException::wrap($e, "Invalid skin in PlayerSkinPacket");
}
return $this->player->changeSkin($skin, $packet->newSkinName, $packet->oldSkinName);
}
@ -800,7 +800,7 @@ class InGamePacketHandler extends PacketHandler{
* Hack to work around a stupid bug in Minecraft W10 which causes empty strings to be sent unquoted in form responses.
*
* @return mixed
* @throws BadPacketException
* @throws PacketHandlingException
*/
private static function stupid_json_decode(string $json, bool $assoc = false){
if(preg_match('/^\[(.+)\]$/s', $json, $matches) > 0){

View File

@ -26,7 +26,6 @@ namespace pocketmine\network\mcpe\handler;
use Mdanter\Ecc\Crypto\Key\PublicKeyInterface;
use pocketmine\entity\InvalidSkinException;
use pocketmine\event\player\PlayerPreLoginEvent;
use pocketmine\network\BadPacketException;
use pocketmine\network\mcpe\auth\ProcessLoginTask;
use pocketmine\network\mcpe\convert\SkinAdapterSingleton;
use pocketmine\network\mcpe\JwtException;
@ -39,6 +38,7 @@ use pocketmine\network\mcpe\protocol\types\login\AuthenticationData;
use pocketmine\network\mcpe\protocol\types\login\ClientData;
use pocketmine\network\mcpe\protocol\types\login\ClientDataToSkinDataHelper;
use pocketmine\network\mcpe\protocol\types\login\JwtChain;
use pocketmine\network\PacketHandlingException;
use pocketmine\player\Player;
use pocketmine\player\PlayerInfo;
use pocketmine\player\XboxLivePlayerInfo;
@ -113,7 +113,7 @@ class LoginPacketHandler extends PacketHandler{
}
if(!Uuid::isValid($extraData->identity)){
throw new BadPacketException("Invalid login UUID");
throw new PacketHandlingException("Invalid login UUID");
}
$uuid = Uuid::fromString($extraData->identity);
if($extraData->XUID !== ""){
@ -164,7 +164,7 @@ class LoginPacketHandler extends PacketHandler{
}
/**
* @throws BadPacketException
* @throws PacketHandlingException
*/
protected function fetchAuthData(JwtChain $chain) : AuthenticationData{
/** @var AuthenticationData|null $extraData */
@ -174,15 +174,15 @@ class LoginPacketHandler extends PacketHandler{
try{
[, $claims, ] = JwtUtils::parse($jwt);
}catch(JwtException $e){
throw BadPacketException::wrap($e);
throw PacketHandlingException::wrap($e);
}
if(isset($claims["extraData"])){
if($extraData !== null){
throw new BadPacketException("Found 'extraData' more than once in chainData");
throw new PacketHandlingException("Found 'extraData' more than once in chainData");
}
if(!is_array($claims["extraData"])){
throw new BadPacketException("'extraData' key should be an array");
throw new PacketHandlingException("'extraData' key should be an array");
}
$mapper = new \JsonMapper;
$mapper->bEnforceMapType = false; //TODO: we don't really need this as an array, but right now we don't have enough models
@ -192,24 +192,24 @@ class LoginPacketHandler extends PacketHandler{
/** @var AuthenticationData $extraData */
$extraData = $mapper->map($claims["extraData"], new AuthenticationData);
}catch(\JsonMapper_Exception $e){
throw BadPacketException::wrap($e);
throw PacketHandlingException::wrap($e);
}
}
}
if($extraData === null){
throw new BadPacketException("'extraData' not found in chain data");
throw new PacketHandlingException("'extraData' not found in chain data");
}
return $extraData;
}
/**
* @throws BadPacketException
* @throws PacketHandlingException
*/
protected function parseClientData(string $clientDataJwt) : ClientData{
try{
[, $clientDataClaims, ] = JwtUtils::parse($clientDataJwt);
}catch(JwtException $e){
throw BadPacketException::wrap($e);
throw PacketHandlingException::wrap($e);
}
$mapper = new \JsonMapper;
@ -219,7 +219,7 @@ class LoginPacketHandler extends PacketHandler{
try{
$clientData = $mapper->map($clientDataClaims, new ClientData);
}catch(\JsonMapper_Exception $e){
throw BadPacketException::wrap($e);
throw PacketHandlingException::wrap($e);
}
return $clientData;
}

View File

@ -24,7 +24,6 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\raklib;
use pocketmine\network\AdvancedNetworkInterface;
use pocketmine\network\BadPacketException;
use pocketmine\network\mcpe\compression\ZlibCompressor;
use pocketmine\network\mcpe\convert\TypeConverter;
use pocketmine\network\mcpe\NetworkSession;
@ -33,6 +32,7 @@ use pocketmine\network\mcpe\protocol\PacketPool;
use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\network\mcpe\StandardPacketBroadcaster;
use pocketmine\network\Network;
use pocketmine\network\PacketHandlingException;
use pocketmine\Server;
use pocketmine\snooze\SleeperNotifier;
use pocketmine\utils\Filesystem;
@ -184,7 +184,7 @@ class RakLibInterface implements ServerEventListener, AdvancedNetworkInterface{
$buf = substr($packet, 1);
try{
$session->handleEncoded($buf);
}catch(BadPacketException $e){
}catch(PacketHandlingException $e){
$errorId = bin2hex(random_bytes(6));
$logger = $session->getLogger();