From 3892f2f40437103f7ab55fba8b06318816993bc8 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 18 Aug 2018 16:35:39 +0100 Subject: [PATCH] Config: Properly prevent keys getting transformed into bools The original regex almost completely failed at its objective, because it a) only worked if there was no value for the key, and b) did not prevent all such occurrences getting transformed, while quoting patterns that would not get transformed anyway. --- src/pocketmine/utils/Config.php | 2 +- tests/phpunit/utils/ConfigTest.php | 71 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/utils/ConfigTest.php diff --git a/src/pocketmine/utils/Config.php b/src/pocketmine/utils/Config.php index ec3185631..d10c885ac 100644 --- a/src/pocketmine/utils/Config.php +++ b/src/pocketmine/utils/Config.php @@ -111,7 +111,7 @@ class Config{ * @return string */ public static function fixYAMLIndexes(string $str) : string{ - return preg_replace("#^([ ]*)([a-zA-Z_]{1}[ ]*)\\:$#m", "$1\"$2\":", $str); + return preg_replace("#^( *)(y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)( *)\:#m", "$1\"$2\"$3:", $str); } /** diff --git a/tests/phpunit/utils/ConfigTest.php b/tests/phpunit/utils/ConfigTest.php new file mode 100644 index 000000000..7c48562ec --- /dev/null +++ b/tests/phpunit/utils/ConfigTest.php @@ -0,0 +1,71 @@ + 1, + "y" => 2, + "z" => 3 + ]]; + yield [" x : 1\n y : 2\n z : 3\n", [ + "x" => 1, + "y" => 2, + "z" => 3 + ]]; + yield ["parent:\n x: 1\n y: 2\n z: 3\n", [ + "parent" => [ + "x" => 1, + "y" => 2, + "z" => 3 + ] + ]]; + yield ["yes: notransform", [ + "yes" => "notransform" + ]]; + yield ["on: 1\nyes: true", [ //this would previously have resulted in a key collision + "on" => 1, + "yes" => true + ]]; + } + + /** + * @dataProvider fixYamlIndexesProvider + * + * @param string $test + * @param array $expected + */ + public function testFixYamlIndexes(string $test, array $expected) : void{ + $fixed = Config::fixYAMLIndexes($test); + $decoded = yaml_parse($fixed); + self::assertEquals($expected, $decoded); + } +}