From 55efde1f5c31ef1fbe1c7e2e1a72ce728881bf4b Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 27 Oct 2026 14:02:12 +0300 Subject: [PATCH] fixed server crash using invalid nbt, fixed playerinputpacket and setentitylinkpacket being handled if the player did not spawn --- src/Player.php | 12 ++++--- src/utils/NBT.php | 67 ++++++++++++++++++++++++++++++++++++-- src/utils/NBTException.php | 8 +++++ 3 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 src/utils/NBTException.php diff --git a/src/Player.php b/src/Player.php index 29e398a4..a5932981 100755 --- a/src/Player.php +++ b/src/Player.php @@ -3275,23 +3275,27 @@ class Player{ $t->spawn($this); }else{ $nbt = new NBT(); - $nbt->load($packet->namedtag); - $d = array_shift($nbt->tree); - if($d["id"] !== TILE_SIGN){ + $d = $nbt->loadSignNBT($packet->namedtag); + if($d === false){ $t->spawn($this); }else{ - $t->setText($d["Text1"], $d["Text2"], $d["Text3"], $d["Text4"]); } } } break; case ProtocolInfo::SET_ENTITY_LINK_PACKET: + if($this->spawned === false){ + break; + } if($this->entity->riding != 0){ $this->entity->setRiding(null); } break; case ProtocolInfo::PLAYER_INPUT_PACKET: + if($this->spawned === false){ + break; + } $this->isJumping = $packet->isJumping; $this->isSneaking = $packet->isSneaking; $this->entity->moveForward = $packet->moveForward; diff --git a/src/utils/NBT.php b/src/utils/NBT.php index 6d1084ac..bbf92e5e 100644 --- a/src/utils/NBT.php +++ b/src/utils/NBT.php @@ -22,6 +22,62 @@ class NBT{ $this->binary .= $bin; } + /** + * A function to safely load sign nbt + * @param string $str raw nbt data + * @return false|array array with sign data, false if failed + */ + public function loadSignNBT($str){ + $this->offset = 0; + $this->binary = (string) $str; + //sign nbt in vanilla is + /* + * [ + * "id": "Sign", + * "x": , + * "y": , + * "z": , + * "Text1": , //max 15 chars + * "Text2": , //^ + * "Text3": , //^ + * "Text4": //^ + * ] + */ + $tag = ord($this->read(1)); + if($tag == self::TAG_COMPOUND){ + $name = $this->readTAG_STRING_maxlen(2); + if($name != "") return false; + $value = []; + for($n = 0; $n < 8; ++$n){ + $tag = ord($this->read(1)); + if($tag == self::TAG_END){ + break; + }else if($tag == self::TAG_INT){ + $name = $this->readTAG_STRING_maxlen(1); + if($name != "x" && $name != "y" && $name != "z") return false; + $value[$name] = $this->readTAG_INT(); + }else if($tag == self::TAG_STRING){ + $name = $this->readTAG_STRING_maxlen(5); + if($name == "id"){ + $s = $this->readTAG_STRING_maxlen(4); + if($s != "Sign") return false; + $value[$name] = $s; + }else if($name == "Text1" || $name == "Text2" || $name == "Text3" || $name == "Text4"){ + $value[$name] = $this->readTAG_STRING_maxlen(15*4); //mcpe 0.8.1 supports unicode, so the max char size 4 bytes + }else{ + return false; + } + }else{ + return false; + } + } + if(count($value) != 8) return false; + return $value; + }else{ + return false; + } + } + public function load($str){ $this->offset = 0; $this->binary = (string) $str; @@ -66,7 +122,7 @@ class NBT{ break; default: echo bin2hex(substr($this->binary, $this->offset - 1)) . PHP_EOL . PHP_EOL; - die("Invalid NBT Tag $tag"); + throw new NBTException("Invalid NBT Tag $tag"); } $node[$name] = $value; } @@ -83,7 +139,14 @@ class NBT{ private function feof(){ return !isset($this->binary[$this->offset]); } - + + //i guess i could have modified old method? + //*but it will make it slower read(2), false); + return $this->read($len <= $ml ? $len : $ml); + } public function readTAG_STRING(){ return $this->read(Utils::readLShort($this->read(2), false)); } diff --git a/src/utils/NBTException.php b/src/utils/NBTException.php new file mode 100644 index 00000000..9af46728 --- /dev/null +++ b/src/utils/NBTException.php @@ -0,0 +1,8 @@ +