From 3bdb0ee5fd16613d778c0f29afa748bed937c7c1 Mon Sep 17 00:00:00 2001 From: Daniel Iancu Date: Wed, 20 Apr 2016 03:13:58 +0300 Subject: [PATCH] Fix addMainKey method of JsonManipulator with content having $n This will store correctly passwords having a dolloar sign followed by a digit in the auth.json file. If the content variable has "$n" (where n consists of digits) it will match as a replacement reference for preg_replace and thus it will get stripped because there's no such parenthesized pattern. --- src/Composer/Json/JsonManipulator.php | 4 +- .../Test/Json/JsonManipulatorTest.php | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Composer/Json/JsonManipulator.php b/src/Composer/Json/JsonManipulator.php index ca0281c29..251b8a00d 100644 --- a/src/Composer/Json/JsonManipulator.php +++ b/src/Composer/Json/JsonManipulator.php @@ -353,7 +353,7 @@ class JsonManipulator if ($this->pregMatch('#[^{\s](\s*)\}$#', $this->contents, $match)) { $this->contents = preg_replace( '#'.$match[1].'\}$#', - addcslashes(',' . $this->newline . $this->indent . JsonFile::encode($key). ': '. $content . $this->newline . '}', '\\'), + addcslashes(',' . $this->newline . $this->indent . JsonFile::encode($key). ': '. $content . $this->newline . '}', '\\$'), $this->contents ); @@ -363,7 +363,7 @@ class JsonManipulator // append at the end of the file $this->contents = preg_replace( '#\}$#', - addcslashes($this->indent . JsonFile::encode($key). ': '.$content . $this->newline . '}', '\\'), + addcslashes($this->indent . JsonFile::encode($key). ': '.$content . $this->newline . '}', '\\$'), $this->contents ); diff --git a/tests/Composer/Test/Json/JsonManipulatorTest.php b/tests/Composer/Test/Json/JsonManipulatorTest.php index 76e39fb95..afb53774e 100644 --- a/tests/Composer/Test/Json/JsonManipulatorTest.php +++ b/tests/Composer/Test/Json/JsonManipulatorTest.php @@ -2045,6 +2045,31 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase ', $manipulator->getContents()); } + public function testAddMainKeyWithContentHavingDollarSignFollowedByDigit() + { + $manipulator = new JsonManipulator('{ + "foo": "bar" +}'); + + $this->assertTrue($manipulator->addMainKey('bar', '$1baz')); + $this->assertEquals('{ + "foo": "bar", + "bar": "$1baz" +} +', $manipulator->getContents()); + } + + public function testAddMainKeyWithContentHavingDollarSignFollowedByDigit2() + { + $manipulator = new JsonManipulator('{}'); + + $this->assertTrue($manipulator->addMainKey('foo', '$1bar')); + $this->assertEquals('{ + "foo": "$1bar" +} +', $manipulator->getContents()); + } + public function testUpdateMainKey() { $manipulator = new JsonManipulator('{ @@ -2105,6 +2130,19 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase ', $manipulator->getContents()); } + public function testUpdateMainKeyWithContentHavingDollarSignFollowedByDigit() + { + $manipulator = new JsonManipulator('{ + "foo": "bar" +}'); + + $this->assertTrue($manipulator->addMainKey('foo', '$1bar')); + $this->assertEquals('{ + "foo": "$1bar" +} +', $manipulator->getContents()); + } + public function testIndentDetection() { $manipulator = new JsonManipulator('{