Skip to content

Commit 6621d45

Browse files
glaubinixSeldaek
authored andcommitted
Fix git/hg driver identifier validation for getChangeDate when using method programmatically
This is no issue for Composer users but when using Composer as a library a call to this method with a leading dash could still cause issues
1 parent ef3fc08 commit 6621d45

4 files changed

Lines changed: 29 additions & 0 deletions

File tree

src/Composer/Repository/Vcs/GitDriver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ public function getFileContent(string $file, string $identifier): ?string
164164
*/
165165
public function getChangeDate(string $identifier): ?\DateTimeImmutable
166166
{
167+
if (isset($identifier[0]) && $identifier[0] === '-') {
168+
throw new \RuntimeException('Invalid git identifier detected. Identifier must not start with a -, given: ' . $identifier);
169+
}
170+
167171
$command = GitUtil::buildRevListCommand($this->process, ['-n1', '--format=%at', $identifier]);
168172
$this->process->execute($command, $output, $this->repoDir);
169173

src/Composer/Repository/Vcs/HgDriver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ public function getFileContent(string $file, string $identifier): ?string
146146
*/
147147
public function getChangeDate(string $identifier): ?\DateTimeImmutable
148148
{
149+
if (isset($identifier[0]) && $identifier[0] === '-') {
150+
throw new \RuntimeException('Invalid hg identifier detected. Identifier must not start with a -, given: ' . $identifier);
151+
}
152+
149153
$this->process->execute(
150154
['hg', 'log', '--template', '{date|rfc3339date}', '-r', $identifier],
151155
$output,

tests/Composer/Test/Repository/Vcs/GitDriverTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@ public function testFileGetContentInvalidIdentifier(): void
184184
$driver->getFileContent('file.txt', '-h');
185185
}
186186

187+
public function testGetChangeDateInvalidIdentifier(): void
188+
{
189+
self::expectException('\RuntimeException');
190+
191+
$process = $this->getProcessExecutorMock();
192+
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
193+
$driver = new GitDriver(['url' => 'https://example.org/acme.git'], $io, $this->config, $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(), $process);
194+
195+
$driver->getChangeDate('-n1 --format=%at HEAD');
196+
}
197+
187198
private function setRepoDir(GitDriver $driver, string $path): void
188199
{
189200
$reflectionClass = new \ReflectionClass($driver);

tests/Composer/Test/Repository/Vcs/HgDriverTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,14 @@ public function testFileGetContentInvalidIdentifier(): void
110110

111111
$driver->getFileContent('file.txt', '-h');
112112
}
113+
114+
public function testGetChangeDateInvalidIdentifier(): void
115+
{
116+
self::expectException('\RuntimeException');
117+
118+
$process = $this->getProcessExecutorMock();
119+
$driver = new HgDriver(['url' => 'https://example.org/acme.git'], $this->io, $this->config, $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(), $process);
120+
121+
$driver->getChangeDate('-r foo');
122+
}
113123
}

0 commit comments

Comments
 (0)