From 135a2f520ca28830415f120dafcf444d47db4ca1 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 1 Aug 2019 19:51:31 +0100 Subject: [PATCH] add some UTF-8 validation --- src/block/utils/SignText.php | 5 ++--- src/item/Item.php | 4 +++- src/item/WritableBookPage.php | 5 ++++- src/item/WrittenBook.php | 3 +++ src/utils/Utils.php | 7 +++++++ 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/block/utils/SignText.php b/src/block/utils/SignText.php index 74aea044b..adefba307 100644 --- a/src/block/utils/SignText.php +++ b/src/block/utils/SignText.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\block\utils; +use pocketmine\utils\Utils; use function array_fill; use function array_pad; use function array_slice; @@ -118,9 +119,7 @@ class SignText{ */ public function setLine(int $index, string $line) : void{ $this->checkLineIndex($index); - if(!mb_check_encoding($line, 'UTF-8')){ - throw new \InvalidArgumentException("Line must be valid UTF-8 text"); - } + Utils::checkUTF8($line); if(strpos($line, "\n") !== false){ throw new \InvalidArgumentException("Line must not contain newlines"); } diff --git a/src/item/Item.php b/src/item/Item.php index f5896fa3f..3b564bb97 100644 --- a/src/item/Item.php +++ b/src/item/Item.php @@ -45,6 +45,7 @@ use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\TreeRoot; use pocketmine\player\Player; use pocketmine\utils\Binary; +use pocketmine\utils\Utils; use function base64_decode; use function base64_encode; use function get_class; @@ -163,7 +164,7 @@ class Item implements \JsonSerializable{ * @return $this */ public function setCustomName(string $name) : Item{ - //TODO: encoding might need to be checked here + Utils::checkUTF8($name); $this->customName = $name; return $this; } @@ -193,6 +194,7 @@ class Item implements \JsonSerializable{ if(!is_string($line)){ throw new \TypeError("Expected string[], but found " . gettype($line) . " in given array"); } + Utils::checkUTF8($line); } $this->lore = $lines; return $this; diff --git a/src/item/WritableBookPage.php b/src/item/WritableBookPage.php index 1723db418..20ce682cb 100644 --- a/src/item/WritableBookPage.php +++ b/src/item/WritableBookPage.php @@ -23,6 +23,8 @@ declare(strict_types=1); namespace pocketmine\item; +use pocketmine\utils\Utils; + class WritableBookPage{ /** @var string */ @@ -31,7 +33,8 @@ class WritableBookPage{ private $photoName; public function __construct(string $text, string $photoName = ""){ - //TODO: data validation, encoding checks + //TODO: data validation + Utils::checkUTF8($text); $this->text = $text; $this->photoName = $photoName; } diff --git a/src/item/WrittenBook.php b/src/item/WrittenBook.php index 683d90456..e0d5c0fad 100644 --- a/src/item/WrittenBook.php +++ b/src/item/WrittenBook.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\item; use pocketmine\nbt\tag\CompoundTag; +use pocketmine\utils\Utils; class WrittenBook extends WritableBookBase{ @@ -92,6 +93,7 @@ class WrittenBook extends WritableBookBase{ * @return $this */ public function setAuthor(string $authorName) : self{ + Utils::checkUTF8($authorName); $this->author = $authorName; return $this; } @@ -113,6 +115,7 @@ class WrittenBook extends WritableBookBase{ * @return $this */ public function setTitle(string $title) : self{ + Utils::checkUTF8($title); $this->title = $title; return $this; } diff --git a/src/utils/Utils.php b/src/utils/Utils.php index c9eef9d8c..d6cabe118 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -57,6 +57,7 @@ use function is_readable; use function is_string; use function json_decode; use function json_last_error_msg; +use function mb_check_encoding; use function ob_end_clean; use function ob_get_contents; use function ob_start; @@ -559,4 +560,10 @@ class Utils{ unlink($dir); } } + + public static function checkUTF8(string $string) : void{ + if(!mb_check_encoding($string, 'UTF-8')){ + throw new \InvalidArgumentException("Text must be valid UTF-8"); + } + } }