Browse Source

Fix remove command not working with escaped slashes (e.g. foo\/bar), fixes #8249

Jordi Boggiano 5 years ago
parent
commit
14f2a6dd9a

+ 3 - 2
src/Composer/Json/JsonManipulator.php

@@ -326,9 +326,10 @@ class JsonManipulator
         }
         }
 
 
         // try and find a match for the subkey
         // try and find a match for the subkey
-        if ($this->pregMatch('{"'.preg_quote($name).'"\s*:}i', $children)) {
+        $keyRegex = str_replace('/', '\\\\?/', preg_quote($name));
+        if ($this->pregMatch('{"'.$keyRegex.'"\s*:}i', $children)) {
             // find best match for the value of "name"
             // find best match for the value of "name"
-            if (preg_match_all('{'.self::$DEFINES.'"'.preg_quote($name).'"\s*:\s*(?:(?&json))}x', $children, $matches)) {
+            if (preg_match_all('{'.self::$DEFINES.'"'.$keyRegex.'"\s*:\s*(?:(?&json))}x', $children, $matches)) {
                 $bestMatch = '';
                 $bestMatch = '';
                 foreach ($matches[0] as $match) {
                 foreach ($matches[0] as $match) {
                     if (strlen($bestMatch) < strlen($match)) {
                     if (strlen($bestMatch) < strlen($match)) {

+ 16 - 0
tests/Composer/Test/Json/JsonManipulatorTest.php

@@ -1448,6 +1448,22 @@ class JsonManipulatorTest extends TestCase
     "repositories": {
     "repositories": {
     }
     }
 }
 }
+',
+            ),
+            'works on simple ones escaped slash' => array(
+                '{
+    "repositories": {
+        "foo\/bar": {
+            "bar": "baz"
+        }
+    }
+}',
+                'foo/bar',
+                true,
+                '{
+    "repositories": {
+    }
+}
 ',
 ',
             ),
             ),
             'works on simple ones middle' => array(
             'works on simple ones middle' => array(