add some UTF-8 validation

This commit is contained in:
Dylan K. Taylor 2019-08-01 19:51:31 +01:00
parent 399ef13069
commit 135a2f520c
5 changed files with 19 additions and 5 deletions

View File

@ -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");
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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");
}
}
}