fixing incompatible protocol handling, do not explode immediately on bad clientdata, login encode/decode is now symmetrical

This commit is contained in:
Dylan K. Taylor 2020-05-15 10:55:29 +01:00
parent 86db3af896
commit 31e4fc6fcb
2 changed files with 101 additions and 73 deletions

View File

@ -28,12 +28,17 @@ use pocketmine\event\player\PlayerPreLoginEvent;
use pocketmine\network\BadPacketException; use pocketmine\network\BadPacketException;
use pocketmine\network\mcpe\auth\ProcessLoginTask; use pocketmine\network\mcpe\auth\ProcessLoginTask;
use pocketmine\network\mcpe\convert\SkinAdapterSingleton; use pocketmine\network\mcpe\convert\SkinAdapterSingleton;
use pocketmine\network\mcpe\JwtException;
use pocketmine\network\mcpe\JwtUtils;
use pocketmine\network\mcpe\NetworkSession; use pocketmine\network\mcpe\NetworkSession;
use pocketmine\network\mcpe\protocol\LoginPacket; use pocketmine\network\mcpe\protocol\LoginPacket;
use pocketmine\network\mcpe\protocol\PlayStatusPacket; use pocketmine\network\mcpe\protocol\PlayStatusPacket;
use pocketmine\network\mcpe\protocol\ProtocolInfo; use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\network\mcpe\protocol\types\login\AuthenticationData;
use pocketmine\network\mcpe\protocol\types\login\ClientData;
use pocketmine\network\mcpe\protocol\types\login\ClientDataPersonaPieceTintColor; use pocketmine\network\mcpe\protocol\types\login\ClientDataPersonaPieceTintColor;
use pocketmine\network\mcpe\protocol\types\login\ClientDataPersonaSkinPiece; use pocketmine\network\mcpe\protocol\types\login\ClientDataPersonaSkinPiece;
use pocketmine\network\mcpe\protocol\types\login\JwtChain;
use pocketmine\network\mcpe\protocol\types\PersonaPieceTintColor; use pocketmine\network\mcpe\protocol\types\PersonaPieceTintColor;
use pocketmine\network\mcpe\protocol\types\PersonaSkinPiece; use pocketmine\network\mcpe\protocol\types\PersonaSkinPiece;
use pocketmine\network\mcpe\protocol\types\SkinAnimation; use pocketmine\network\mcpe\protocol\types\SkinAnimation;
@ -45,6 +50,7 @@ use pocketmine\Server;
use pocketmine\uuid\UUID; use pocketmine\uuid\UUID;
use function array_map; use function array_map;
use function base64_decode; use function base64_decode;
use function is_array;
/** /**
* Handles the initial login phase of the session. This handler is used as the initial state. * Handles the initial login phase of the session. This handler is used as the initial state.
@ -94,12 +100,15 @@ class LoginPacketHandler extends PacketHandler{
return true; return true;
} }
if(!Player::isValidUserName($packet->extraData->displayName)){ $extraData = $this->fetchAuthData($packet->chainDataJwt);
if(!Player::isValidUserName($extraData->displayName)){
$this->session->disconnect("disconnectionScreen.invalidName"); $this->session->disconnect("disconnectionScreen.invalidName");
return true; return true;
} }
$clientData = $this->parseClientData($packet->clientDataJwt);
$safeB64Decode = static function(string $base64, string $context) : string{ $safeB64Decode = static function(string $base64, string $context) : string{
$result = base64_decode($base64, true); $result = base64_decode($base64, true);
if($result === false){ if($result === false){
@ -108,7 +117,6 @@ class LoginPacketHandler extends PacketHandler{
return $result; return $result;
}; };
try{ try{
$clientData = $packet->clientData; //this serves no purpose except readability
/** @var SkinAnimation[] $animations */ /** @var SkinAnimation[] $animations */
$animations = []; $animations = [];
foreach($clientData->AnimatedImageData as $k => $animation){ foreach($clientData->AnimatedImageData as $k => $animation){
@ -154,17 +162,17 @@ class LoginPacketHandler extends PacketHandler{
} }
try{ try{
$uuid = UUID::fromString($packet->extraData->identity); $uuid = UUID::fromString($extraData->identity);
}catch(\InvalidArgumentException $e){ }catch(\InvalidArgumentException $e){
throw BadPacketException::wrap($e, "Failed to parse login UUID"); throw BadPacketException::wrap($e, "Failed to parse login UUID");
} }
($this->playerInfoConsumer)(new PlayerInfo( ($this->playerInfoConsumer)(new PlayerInfo(
$packet->extraData->displayName, $extraData->displayName,
$uuid, $uuid,
$skin, $skin,
$packet->clientData->LanguageCode, $clientData->LanguageCode,
$packet->extraData->XUID, $extraData->XUID,
(array) $packet->clientData (array) $clientData
)); ));
$ev = new PlayerPreLoginEvent( $ev = new PlayerPreLoginEvent(
@ -194,6 +202,67 @@ class LoginPacketHandler extends PacketHandler{
return true; return true;
} }
/**
* @throws BadPacketException
*/
protected function fetchAuthData(JwtChain $chain) : AuthenticationData{
/** @var AuthenticationData|null $extraData */
$extraData = null;
foreach($chain->chain as $k => $chain){
//validate every chain element
try{
[, $claims, ] = JwtUtils::parse($chain);
}catch(JwtException $e){
throw BadPacketException::wrap($e);
}
if(isset($claims["extraData"])){
if($extraData !== null){
throw new BadPacketException("Found 'extraData' more than once in chainData");
}
if(!is_array($claims["extraData"])){
throw new BadPacketException("'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
$mapper->bExceptionOnMissingData = true;
$mapper->bExceptionOnUndefinedProperty = true;
try{
/** @var AuthenticationData $extraData */
$extraData = $mapper->map($claims['extraData'], new AuthenticationData);
}catch(\JsonMapper_Exception $e){
throw BadPacketException::wrap($e);
}
}
}
if($extraData === null){
throw new BadPacketException("'extraData' not found in chain data");
}
return $extraData;
}
/**
* @throws BadPacketException
*/
protected function parseClientData(string $clientDataJwt) : ClientData{
try{
[, $clientDataClaims, ] = JwtUtils::parse($clientDataJwt);
}catch(JwtException $e){
throw BadPacketException::wrap($e);
}
$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
$mapper->bExceptionOnMissingData = true;
$mapper->bExceptionOnUndefinedProperty = true;
try{
$clientData = $mapper->map($clientDataClaims, new ClientData);
}catch(\JsonMapper_Exception $e){
throw BadPacketException::wrap($e);
}
return $clientData;
}
/** /**
* TODO: This is separated for the purposes of allowing plugins (like Specter) to hack it and bypass authentication. * TODO: This is separated for the purposes of allowing plugins (like Specter) to hack it and bypass authentication.
* In the future this won't be necessary. * In the future this won't be necessary.

View File

@ -25,18 +25,14 @@ namespace pocketmine\network\mcpe\protocol;
#include <rules/DataPacket.h> #include <rules/DataPacket.h>
use pocketmine\network\mcpe\JwtException;
use pocketmine\network\mcpe\JwtUtils;
use pocketmine\network\mcpe\protocol\serializer\NetworkBinaryStream; use pocketmine\network\mcpe\protocol\serializer\NetworkBinaryStream;
use pocketmine\network\mcpe\protocol\types\login\AuthenticationData;
use pocketmine\network\mcpe\protocol\types\login\ClientData;
use pocketmine\network\mcpe\protocol\types\login\JwtChain; use pocketmine\network\mcpe\protocol\types\login\JwtChain;
use pocketmine\utils\BinaryDataException;
use pocketmine\utils\BinaryStream; use pocketmine\utils\BinaryStream;
use function is_array;
use function is_object; use function is_object;
use function json_decode; use function json_decode;
use function json_encode;
use function json_last_error_msg; use function json_last_error_msg;
use function strlen;
class LoginPacket extends DataPacket implements ServerboundPacket{ class LoginPacket extends DataPacket implements ServerboundPacket{
public const NETWORK_ID = ProtocolInfo::LOGIN_PACKET; public const NETWORK_ID = ProtocolInfo::LOGIN_PACKET;
@ -48,12 +44,8 @@ class LoginPacket extends DataPacket implements ServerboundPacket{
/** @var JwtChain */ /** @var JwtChain */
public $chainDataJwt; public $chainDataJwt;
/** @var AuthenticationData|null extraData index of whichever JWT has it */
public $extraData = null;
/** @var string */ /** @var string */
public $clientDataJwt; public $clientDataJwt;
/** @var ClientData decoded payload of the clientData JWT */
public $clientData;
public function canBeSentBeforeLogin() : bool{ public function canBeSentBeforeLogin() : bool{
return true; return true;
@ -61,17 +53,13 @@ class LoginPacket extends DataPacket implements ServerboundPacket{
protected function decodePayload(NetworkBinaryStream $in) : void{ protected function decodePayload(NetworkBinaryStream $in) : void{
$this->protocol = $in->getInt(); $this->protocol = $in->getInt();
$this->decodeConnectionRequest($in); $this->decodeConnectionRequest($in->getString());
} }
/** protected function decodeConnectionRequest(string $binary) : void{
* @throws PacketDecodeException $connRequestReader = new BinaryStream($binary);
* @throws BinaryDataException
*/
protected function decodeConnectionRequest(NetworkBinaryStream $in) : void{
$buffer = new BinaryStream($in->getString());
$chainDataJson = json_decode($buffer->get($buffer->getLInt())); $chainDataJson = json_decode($connRequestReader->get($connRequestReader->getLInt()));
if(!is_object($chainDataJson)){ if(!is_object($chainDataJson)){
throw new PacketDecodeException("Failed decoding chain data JSON: " . json_last_error_msg()); throw new PacketDecodeException("Failed decoding chain data JSON: " . json_last_error_msg());
} }
@ -85,57 +73,28 @@ class LoginPacket extends DataPacket implements ServerboundPacket{
} }
$this->chainDataJwt = $chainData; $this->chainDataJwt = $chainData;
$this->clientDataJwt = $connRequestReader->get($connRequestReader->getLInt());
foreach($this->chainDataJwt->chain as $k => $chain){
//validate every chain element
try{
[, $claims, ] = JwtUtils::parse($chain);
}catch(JwtException $e){
throw new PacketDecodeException($e->getMessage(), 0, $e);
}
if(isset($claims["extraData"])){
if(!is_array($claims["extraData"])){
throw new PacketDecodeException("'extraData' key should be an array");
}
if($this->extraData !== null){
throw new PacketDecodeException("Found 'extraData' more than once in chainData");
}
$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
$mapper->bExceptionOnMissingData = true;
$mapper->bExceptionOnUndefinedProperty = true;
try{
$this->extraData = $mapper->map($claims['extraData'], new AuthenticationData);
}catch(\JsonMapper_Exception $e){
throw PacketDecodeException::wrap($e);
}
}
}
if($this->extraData === null){
throw new PacketDecodeException("'extraData' not found in chain data");
}
$this->clientDataJwt = $buffer->get($buffer->getLInt());
try{
[, $clientData, ] = JwtUtils::parse($this->clientDataJwt);
}catch(JwtException $e){
throw new PacketDecodeException($e->getMessage(), 0, $e);
}
$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
$mapper->bExceptionOnMissingData = true;
$mapper->bExceptionOnUndefinedProperty = true;
try{
$this->clientData = $mapper->map($clientData, new ClientData);
}catch(\JsonMapper_Exception $e){
throw PacketDecodeException::wrap($e);
}
} }
protected function encodePayload(NetworkBinaryStream $out) : void{ protected function encodePayload(NetworkBinaryStream $out) : void{
//TODO $out->putInt($this->protocol);
$out->putString($this->encodeConnectionRequest());
}
protected function encodeConnectionRequest() : string{
$connRequestWriter = new BinaryStream();
$chainDataJson = json_encode($this->chainDataJwt);
if($chainDataJson === false){
throw new \InvalidStateException("Failed to encode chain data JSON: " . json_last_error_msg());
}
$connRequestWriter->putLInt(strlen($chainDataJson));
$connRequestWriter->put($chainDataJson);
$connRequestWriter->putLInt(strlen($this->clientDataJwt));
$connRequestWriter->put($this->clientDataJwt);
return $connRequestWriter->getBuffer();
} }
public function handle(PacketHandlerInterface $handler) : bool{ public function handle(PacketHandlerInterface $handler) : bool{