PleaseStand has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/56357


Change subject: Fix pretty JSON when strings end with backslashes
......................................................................

Fix pretty JSON when strings end with backslashes

If a string encoded as part of the output ends in a backslash
(e.g. an edit token), FormatJson::prettyPrint() may incorrectly
treat the unescaped double quote marking the end of the string as
a character that is part of the string.

To fix the bug, I exploit strtr's behavior when it is given an
associative array having keys of the same length to skip over
escaped backslashes while replacing escaped double quotes with "\x01".

I also updated the corresponding unit test.

Change-Id: I159105b6493c14b82cd0a41a95e04bfed744931e
---
M includes/json/FormatJson.php
M tests/phpunit/includes/json/FormatJsonTest.php
2 files changed, 3 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/57/56357/1

diff --git a/includes/json/FormatJson.php b/includes/json/FormatJson.php
index 013d589..bdf98d5 100644
--- a/includes/json/FormatJson.php
+++ b/includes/json/FormatJson.php
@@ -168,7 +168,7 @@
        private static function prettyPrint( $json ) {
                $buf = '';
                $indent = 0;
-               $json = str_replace( '\"', "\x01", $json );
+               $json = strtr( $json, array( '\\\\' => '\\\\', '\"' => "\x01" ) 
);
                for ( $i = 0, $n = strlen( $json ); $i < $n; $i += $skip ) {
                        $skip = 1;
                        switch ( $json[$i] ) {
diff --git a/tests/phpunit/includes/json/FormatJsonTest.php 
b/tests/phpunit/includes/json/FormatJsonTest.php
index 9e25e18..89d7ea1 100644
--- a/tests/phpunit/includes/json/FormatJsonTest.php
+++ b/tests/phpunit/includes/json/FormatJsonTest.php
@@ -6,7 +6,7 @@
                $obj = array(
                        'emptyObject' => new stdClass,
                        'emptyArray' => array(),
-                       'string' => 'foobar',
+                       'string' => 'foo"bar\\',
                        'filledArray' => array(
                                array(
                                        123,
@@ -24,7 +24,7 @@
     "emptyArray": [
 
     ],
-    "string": "foobar",
+    "string": "foo\"bar\\\\",
     "filledArray": [
         [
             123,

-- 
To view, visit https://gerrit.wikimedia.org/r/56357
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I159105b6493c14b82cd0a41a95e04bfed744931e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: PleaseStand <pleasest...@live.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to