From b66095cb36b24ccd0ce8ace1b2448cc3f9de7c9d Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 19 Aug 2018 10:10:40 +0100 Subject: [PATCH 1/2] Added a hack for MC W10 JSON empty strings bug (#2383) --- .../mcpe/PlayerNetworkSessionAdapter.php | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter.php b/src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter.php index b8801815fe..d405905cc2 100644 --- a/src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter.php +++ b/src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter.php @@ -239,7 +239,37 @@ class PlayerNetworkSessionAdapter extends NetworkSession{ } public function handleModalFormResponse(ModalFormResponsePacket $packet) : bool{ - return $this->player->onFormSubmit($packet->formId, json_decode($packet->formData, true)); + return $this->player->onFormSubmit($packet->formId, self::stupid_json_decode($packet->formData, true)); + } + + /** + * Hack to work around a stupid bug in Minecraft W10 which causes empty strings to be sent unquoted in form responses. + * + * @param string $json + * @param bool $assoc + * + * @return mixed + */ + private static function stupid_json_decode(string $json, bool $assoc = false){ + if(preg_match('/^\[(.+)\]$/s', $json, $matches) > 0){ + $parts = preg_split('/(?:"(?:\\"|[^"])*"|)\K(,)/', $matches[1]); //Splits on commas not inside quotes, ignoring escaped quotes + foreach($parts as $k => $part){ + $part = trim($part); + if($part === ""){ + $part = "\"\""; + } + $parts[$k] = $part; + } + + $fixed = "[" . implode(",", $parts) . "]"; + if(($ret = json_decode($fixed, $assoc)) === null){ + throw new \InvalidArgumentException("Failed to fix JSON: " . json_last_error_msg() . "(original: $json, modified: $fixed)"); + } + + return $ret; + } + + return json_decode($json, $assoc); } public function handleServerSettingsRequest(ServerSettingsRequestPacket $packet) : bool{ From bfa415e1089b30b6f7686ea325108a6b7cf08958 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 19 Aug 2018 10:30:52 +0100 Subject: [PATCH 2/2] Add unit tests for json decoding hack --- .../network/mcpe/StupidJsonDecodeTest.php | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tests/phpunit/network/mcpe/StupidJsonDecodeTest.php diff --git a/tests/phpunit/network/mcpe/StupidJsonDecodeTest.php b/tests/phpunit/network/mcpe/StupidJsonDecodeTest.php new file mode 100644 index 0000000000..b096c6c9d7 --- /dev/null +++ b/tests/phpunit/network/mcpe/StupidJsonDecodeTest.php @@ -0,0 +1,56 @@ +setAccessible(true); + + $decoded = $func->invoke(null, $brokenJson, true); + self::assertEquals($expect, $decoded); + } +}