From 44697e784a262b739eb79329cd49c0489a9ce108 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 14 Feb 2019 15:16:51 +0000 Subject: [PATCH] Sign: add validity checks on text encoding, scrub invalid UTF-8 on load this works around a bug where corrupted text on preexisting signs can mess up the client. This also prevents corrupted text getting onto signs in the future by having them scrubbed and validated before applying them. --- src/pocketmine/tile/Sign.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/pocketmine/tile/Sign.php b/src/pocketmine/tile/Sign.php index ca577ad93..39b3b550b 100644 --- a/src/pocketmine/tile/Sign.php +++ b/src/pocketmine/tile/Sign.php @@ -33,6 +33,8 @@ use function array_pad; use function array_slice; use function explode; use function implode; +use function mb_check_encoding; +use function mb_scrub; use function sprintf; class Sign extends Spawnable{ @@ -57,6 +59,9 @@ class Sign extends Spawnable{ } } } + $this->text = array_map(function(string $line) : string{ + return mb_scrub($line, 'UTF-8'); + }, $this->text); } protected function writeSaveData(CompoundTag $nbt) : void{ @@ -79,16 +84,16 @@ class Sign extends Spawnable{ */ public function setText(?string $line1 = "", ?string $line2 = "", ?string $line3 = "", ?string $line4 = "") : void{ if($line1 !== null){ - $this->text[0] = $line1; + $this->setLine(0, $line1, false); } if($line2 !== null){ - $this->text[1] = $line2; + $this->setLine(1, $line2, false); } if($line3 !== null){ - $this->text[2] = $line3; + $this->setLine(2, $line3, false); } if($line4 !== null){ - $this->text[3] = $line4; + $this->setLine(3, $line4, false); } $this->onChanged(); @@ -103,6 +108,9 @@ class Sign extends Spawnable{ if($index < 0 or $index > 3){ throw new \InvalidArgumentException("Index must be in the range 0-3!"); } + if(!mb_check_encoding($line, 'UTF-8')){ + throw new \InvalidArgumentException("Text must be valid UTF-8"); + } $this->text[$index] = $line; if($update){