File size: 8,533 Bytes
5fcd275 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | diff --git a/src/Composer/Repository/Vcs/GitDriver.php b/src/Composer/Repository/Vcs/GitDriver.php
index b91508acd17b..039f9052f876 100644
--- a/src/Composer/Repository/Vcs/GitDriver.php
+++ b/src/Composer/Repository/Vcs/GitDriver.php
@@ -138,6 +138,10 @@ public function getDist($identifier)
*/
public function getFileContent($file, $identifier)
{
+ if (isset($identifier[0]) && $identifier[0] === '-') {
+ throw new \RuntimeException('Invalid git identifier detected. Identifier must not start with a -, given: ' . $identifier);
+ }
+
$resource = sprintf('%s:%s', ProcessExecutor::escape($identifier), ProcessExecutor::escape($file));
$this->process->execute(sprintf('git show %s', $resource), $content, $this->repoDir);
@@ -191,7 +195,7 @@ public function getBranches()
$this->process->execute('git branch --no-color --no-abbrev -v', $output, $this->repoDir);
foreach ($this->process->splitLines($output) as $branch) {
if ($branch && !Preg::isMatch('{^ *[^/]+/HEAD }', $branch)) {
- if (Preg::isMatch('{^(?:\* )? *(\S+) *([a-f0-9]+)(?: .*)?$}', $branch, $match)) {
+ if (Preg::isMatch('{^(?:\* )? *(\S+) *([a-f0-9]+)(?: .*)?$}', $branch, $match) && $match[1][0] !== '-') {
$branches[$match[1]] = $match[2];
}
}
diff --git a/src/Composer/Repository/Vcs/HgDriver.php b/src/Composer/Repository/Vcs/HgDriver.php
index 73bc426e5361..f9f8064ba08c 100644
--- a/src/Composer/Repository/Vcs/HgDriver.php
+++ b/src/Composer/Repository/Vcs/HgDriver.php
@@ -126,7 +126,11 @@ public function getDist($identifier)
*/
public function getFileContent($file, $identifier)
{
- $resource = sprintf('hg cat -r %s %s', ProcessExecutor::escape($identifier), ProcessExecutor::escape($file));
+ if (isset($identifier[0]) && $identifier[0] === '-') {
+ throw new \RuntimeException('Invalid hg identifier detected. Identifier must not start with a -, given: ' . $identifier);
+ }
+
+ $resource = sprintf('hg cat -r %s -- %s', ProcessExecutor::escape($identifier), ProcessExecutor::escape($file));
$this->process->execute($resource, $content, $this->repoDir);
if (!trim($content)) {
@@ -186,14 +190,14 @@ public function getBranches()
$this->process->execute('hg branches', $output, $this->repoDir);
foreach ($this->process->splitLines($output) as $branch) {
- if ($branch && Preg::isMatch('(^([^\s]+)\s+\d+:([a-f0-9]+))', $branch, $match)) {
+ if ($branch && Preg::isMatch('(^([^\s]+)\s+\d+:([a-f0-9]+))', $branch, $match) && $match[1][0] !== '-') {
$branches[$match[1]] = $match[2];
}
}
$this->process->execute('hg bookmarks', $output, $this->repoDir);
foreach ($this->process->splitLines($output) as $branch) {
- if ($branch && Preg::isMatch('(^(?:[\s*]*)([^\s]+)\s+\d+:(.*)$)', $branch, $match)) {
+ if ($branch && Preg::isMatch('(^(?:[\s*]*)([^\s]+)\s+\d+:(.*)$)', $branch, $match) && $match[1][0] !== '-') {
$bookmarks[$match[1]] = $match[2];
}
}
diff --git a/tests/Composer/Test/Repository/Vcs/GitDriverTest.php b/tests/Composer/Test/Repository/Vcs/GitDriverTest.php
new file mode 100644
index 000000000000..c8cfdab0a733
--- /dev/null
+++ b/tests/Composer/Test/Repository/Vcs/GitDriverTest.php
@@ -0,0 +1,81 @@
+<?php
+
+namespace Composer\Test\Repository\Vcs;
+
+use Composer\Config;
+use Composer\Repository\Vcs\GitDriver;
+use Composer\Test\Mock\ProcessExecutorMock;
+use Composer\Test\TestCase;
+
+class GitDriverTest extends TestCase
+{
+ /** @var Config */
+ private $config;
+ /** @var string */
+ private $home;
+
+ public function setUp()
+ {
+ $this->home = self::getUniqueTmpDirectory();
+ $this->config = new Config();
+ $this->config->merge(array(
+ 'config' => array(
+ 'home' => $this->home,
+ ),
+ ));
+ }
+
+ public function testGetBranchesFilterInvalidBranchNames()
+ {
+ $process = new ProcessExecutorMock;
+ $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
+
+ $driver = new GitDriver(array('url' => 'https://example.org/acme.git'), $io, $this->config, $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(), $process);
+ $this->setRepoDir($driver, $this->home);
+
+ // Branches starting with a - character are not valid git branches names
+ // Still assert that they get filtered to prevent issues later on
+ $stdout = <<<GIT
+* main 089681446ba44d6d9004350192486f2ceb4eaa06 commit
+ 2.2 12681446ba44d6d9004350192486f2ceb4eaa06 commit
+ -h 089681446ba44d6d9004350192486f2ceb4eaa06 commit
+GIT;
+
+ $process
+ ->expects(array(array(
+ 'cmd' => 'git branch --no-color --no-abbrev -v',
+ 'stdout' => $stdout,
+ )));
+
+ $branches = $driver->getBranches();
+ $this->assertSame(array(
+ 'main' => '089681446ba44d6d9004350192486f2ceb4eaa06',
+ '2.2' => '12681446ba44d6d9004350192486f2ceb4eaa06',
+ ), $branches);
+ }
+
+ public function testFileGetContentInvalidIdentifier()
+ {
+ $this->expectException('\RuntimeException');
+
+ $process = new ProcessExecutorMock;
+ $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
+ $driver = new GitDriver(array('url' => 'https://example.org/acme.git'), $io, $this->config, $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(), $process);
+
+ $this->assertNull($driver->getFileContent('file.txt', 'h'));
+
+ $driver->getFileContent('file.txt', '-h');
+ }
+
+ /**
+ * @param GitDriver $driver
+ * @param string $path
+ */
+ private function setRepoDir($driver, $path)
+ {
+ $reflectionClass = new \ReflectionClass($driver);
+ $reflectionProperty = $reflectionClass->getProperty('repoDir');
+ $reflectionProperty->setAccessible(true);
+ $reflectionProperty->setValue($driver, $path);
+ }
+}
diff --git a/tests/Composer/Test/Repository/Vcs/HgDriverTest.php b/tests/Composer/Test/Repository/Vcs/HgDriverTest.php
index 2fcd27bdf76c..356a28c48ab3 100644
--- a/tests/Composer/Test/Repository/Vcs/HgDriverTest.php
+++ b/tests/Composer/Test/Repository/Vcs/HgDriverTest.php
@@ -13,6 +13,7 @@
namespace Composer\Test\Repository\Vcs;
use Composer\Repository\Vcs\HgDriver;
+use Composer\Test\Mock\ProcessExecutorMock;
use Composer\Test\TestCase;
use Composer\Util\Filesystem;
use Composer\Config;
@@ -66,4 +67,49 @@ public function supportsDataProvider()
array('https://user@bitbucket.org/user/repo'),
);
}
+
+ public function testGetBranchesFilterInvalidBranchNames()
+ {
+ $process = new ProcessExecutorMock;
+
+ $driver = new HgDriver(array('url' => 'https://example.org/acme.git'), $this->io, $this->config, $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(), $process);
+
+ $stdout = <<<HG_BRANCHES
+default 1:dbf6c8acb640
+--help 1:dbf6c8acb640
+HG_BRANCHES;
+
+ $stdout1 = <<<HG_BOOKMARKS
+help 1:dbf6c8acb641
+--help 1:dbf6c8acb641
+
+HG_BOOKMARKS;
+
+ $process
+ ->expects(array(array(
+ 'cmd' => 'hg branches',
+ 'stdout' => $stdout,
+ ), array(
+ 'cmd' => 'hg bookmarks',
+ 'stdout' => $stdout1,
+ )));
+
+ $branches = $driver->getBranches();
+ $this->assertSame(array(
+ 'help' => 'dbf6c8acb641',
+ 'default' => 'dbf6c8acb640',
+ ), $branches);
+ }
+
+ public function testFileGetContentInvalidIdentifier()
+ {
+ $this->expectException('\RuntimeException');
+
+ $process = new ProcessExecutorMock;
+ $driver = new HgDriver(array('url' => 'https://example.org/acme.git'), $this->io, $this->config, $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(), $process);
+
+ $this->assertNull($driver->getFileContent('file.txt', 'h'));
+
+ $driver->getFileContent('file.txt', '-h');
+ }
}
|