From 0d073128dec5c789b96fccf13aba8512b13107ab Mon Sep 17 00:00:00 2001 From: maryo Date: Thu, 27 Apr 2017 15:12:46 +0200 Subject: [PATCH] [Process] Ecaping of CLI arguments containing slashes on Windows --- src/Symfony/Component/Process/Process.php | 2 +- .../Component/Process/Tests/ProcessBuilderTest.php | 12 ++++++------ src/Symfony/Component/Process/Tests/ProcessTest.php | 12 ++++++++++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Process/Process.php b/src/Symfony/Component/Process/Process.php index 06ec2d767b81..a709f4a61d26 100644 --- a/src/Symfony/Component/Process/Process.php +++ b/src/Symfony/Component/Process/Process.php @@ -1717,7 +1717,7 @@ private function escapeArgument($argument) if (false !== strpos($argument, "\0")) { $argument = str_replace("\0", '?', $argument); } - if (!preg_match('/[()%!^"<>&|\s]/', $argument)) { + if (!preg_match('/[\/()%!^"<>&|\s]/', $argument)) { return $argument; } $argument = preg_replace('/(\\\\+)$/', '$1$1', $argument); diff --git a/src/Symfony/Component/Process/Tests/ProcessBuilderTest.php b/src/Symfony/Component/Process/Tests/ProcessBuilderTest.php index 58a5ac995e99..34bb3732722e 100644 --- a/src/Symfony/Component/Process/Tests/ProcessBuilderTest.php +++ b/src/Symfony/Component/Process/Tests/ProcessBuilderTest.php @@ -91,14 +91,14 @@ public function testPrefixIsPrependedToAllGeneratedProcess() $proc = $pb->setArguments(array('-v'))->getProcess(); if ('\\' === DIRECTORY_SEPARATOR) { - $this->assertEquals('/usr/bin/php -v', $proc->getCommandLine()); + $this->assertEquals('"/usr/bin/php" -v', $proc->getCommandLine()); } else { $this->assertEquals("'/usr/bin/php' '-v'", $proc->getCommandLine()); } $proc = $pb->setArguments(array('-i'))->getProcess(); if ('\\' === DIRECTORY_SEPARATOR) { - $this->assertEquals('/usr/bin/php -i', $proc->getCommandLine()); + $this->assertEquals('"/usr/bin/php" -i', $proc->getCommandLine()); } else { $this->assertEquals("'/usr/bin/php' '-i'", $proc->getCommandLine()); } @@ -111,14 +111,14 @@ public function testArrayPrefixesArePrependedToAllGeneratedProcess() $proc = $pb->setArguments(array('-v'))->getProcess(); if ('\\' === DIRECTORY_SEPARATOR) { - $this->assertEquals('/usr/bin/php composer.phar -v', $proc->getCommandLine()); + $this->assertEquals('"/usr/bin/php" composer.phar -v', $proc->getCommandLine()); } else { $this->assertEquals("'/usr/bin/php' 'composer.phar' '-v'", $proc->getCommandLine()); } $proc = $pb->setArguments(array('-i'))->getProcess(); if ('\\' === DIRECTORY_SEPARATOR) { - $this->assertEquals('/usr/bin/php composer.phar -i', $proc->getCommandLine()); + $this->assertEquals('"/usr/bin/php" composer.phar -i', $proc->getCommandLine()); } else { $this->assertEquals("'/usr/bin/php' 'composer.phar' '-i'", $proc->getCommandLine()); } @@ -164,7 +164,7 @@ public function testShouldNotThrowALogicExceptionIfNoArgument() ->getProcess(); if ('\\' === DIRECTORY_SEPARATOR) { - $this->assertEquals('/usr/bin/php', $process->getCommandLine()); + $this->assertEquals('"/usr/bin/php"', $process->getCommandLine()); } else { $this->assertEquals("'/usr/bin/php'", $process->getCommandLine()); } @@ -176,7 +176,7 @@ public function testShouldNotThrowALogicExceptionIfNoPrefix() ->getProcess(); if ('\\' === DIRECTORY_SEPARATOR) { - $this->assertEquals('/usr/bin/php', $process->getCommandLine()); + $this->assertEquals('"/usr/bin/php"', $process->getCommandLine()); } else { $this->assertEquals("'/usr/bin/php'", $process->getCommandLine()); } diff --git a/src/Symfony/Component/Process/Tests/ProcessTest.php b/src/Symfony/Component/Process/Tests/ProcessTest.php index f95f95a25d05..642df9c4a3c0 100644 --- a/src/Symfony/Component/Process/Tests/ProcessTest.php +++ b/src/Symfony/Component/Process/Tests/ProcessTest.php @@ -715,8 +715,8 @@ public function testRestart() // Ensure that both processed finished and the output is numeric $this->assertFalse($process1->isRunning()); $this->assertFalse($process2->isRunning()); - $this->assertTrue(is_numeric($process1->getOutput())); - $this->assertTrue(is_numeric($process2->getOutput())); + $this->assertInternalType('numeric', $process1->getOutput()); + $this->assertInternalType('numeric', $process2->getOutput()); // Ensure that restart returned a new process by check that the output is different $this->assertNotEquals($process1->getOutput(), $process2->getOutput()); @@ -1446,6 +1446,14 @@ public function testInheritEnvDisabled() $this->assertSame($expected, $env); } + public function testGetCommandLine() + { + $p = new Process(array('/usr/bin/php')); + + $expected = '\\' === DIRECTORY_SEPARATOR ? '"/usr/bin/php"' : "'/usr/bin/php'"; + $this->assertSame($expected, $p->getCommandLine()); + } + /** * @dataProvider provideEscapeArgument */