Skip to content

Commit

Permalink
VcsRepositories: handle initialize with invalid repository URL (#10525)
Browse files Browse the repository at this point in the history
  • Loading branch information
glaubinix committed Feb 16, 2022
1 parent 6698317 commit 3eb12ef
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/Composer/Repository/Vcs/GitBitbucketDriver.php
Expand Up @@ -61,7 +61,10 @@ class GitBitbucketDriver extends VcsDriver
*/
public function initialize()
{
Preg::match('#^https?://bitbucket\.org/([^/]+)/([^/]+?)(\.git|/?)?$#i', $this->url, $match);
if (!Preg::isMatch('#^https?://bitbucket\.org/([^/]+)/([^/]+?)(\.git|/?)?$#i', $this->url, $match)) {
throw new \InvalidArgumentException(sprintf('The Bitbucket repository URL %s is invalid. It must be the HTTPS URL of a Bitbucket repository.', $this->url));
}

$this->owner = $match[1];
$this->repository = $match[2];
$this->originUrl = 'bitbucket.org';
Expand Down
5 changes: 4 additions & 1 deletion src/Composer/Repository/Vcs/GitHubDriver.php
Expand Up @@ -59,7 +59,10 @@ class GitHubDriver extends VcsDriver
*/
public function initialize()
{
Preg::match('#^(?:(?:https?|git)://([^/]+)/|git@([^:]+):/?)([^/]+)/(.+?)(?:\.git|/)?$#', $this->url, $match);
if (!Preg::isMatch('#^(?:(?:https?|git)://([^/]+)/|git@([^:]+):/?)([^/]+)/(.+?)(?:\.git|/)?$#', $this->url, $match)) {
throw new \InvalidArgumentException(sprintf('The GitHub repository URL %s is invalid.', $this->url));
}

$this->owner = $match[3];
$this->repository = $match[4];
$this->originUrl = strtolower(!empty($match[1]) ? $match[1] : $match[2]);
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Repository/Vcs/GitLabDriver.php
Expand Up @@ -94,7 +94,7 @@ class GitLabDriver extends VcsDriver
public function initialize()
{
if (!Preg::isMatch(self::URL_REGEX, $this->url, $match)) {
throw new \InvalidArgumentException('The URL provided is invalid. It must be the HTTP URL of a GitLab project.');
throw new \InvalidArgumentException(sprintf('The GitLab repository URL %s is invalid. It must be the HTTP URL of a GitLab project.', $this->url));
}

$guessedDomain = !empty($match['domain']) ? $match['domain'] : $match['domain2'];
Expand Down
8 changes: 8 additions & 0 deletions tests/Composer/Test/Repository/Vcs/GitBitbucketDriverTest.php
Expand Up @@ -218,6 +218,14 @@ public function testGetParams($driver)
);
}

public function testInitializeInvalidRepositoryUrl()
{
$this->setExpectedException('\InvalidArgumentException');

$driver = $this->getDriver(array('url' => 'https://bitbucket.org/acme'));
$driver->initialize();
}

public function testSupports()
{
$this->assertTrue(
Expand Down
48 changes: 46 additions & 2 deletions tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php
Expand Up @@ -14,11 +14,11 @@

use Composer\Downloader\TransportException;
use Composer\Repository\Vcs\GitHubDriver;
use Composer\Test\Mock\ProcessExecutorMock;
use Composer\Test\TestCase;
use Composer\Util\Filesystem;
use Composer\Util\Http\Response;
use Composer\Test\Mock\ProcessExecutorMock;
use Composer\Config;
use Composer\Util\Http\Response;
use Composer\Util\ProcessExecutor;

class GitHubDriverTest extends TestCase
Expand Down Expand Up @@ -341,6 +341,50 @@ public function testPrivateRepositoryNoInteraction()
$process->assertComplete($this);
}

/**
* @return void
*/
public function initializeInvalidReoUrl()
{
$this->setExpectedException('\InvalidArgumentException');

$repoConfig = array(
'url' => 'https://github.com/acme',
);

$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')
->setConstructorArgs(array($io, $this->config))
->getMock();

$gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $httpDownloader, new ProcessExecutorMock);
$gitHubDriver->initialize();
}

/**
* @dataProvider supportsProvider
* @param bool $expected
* @param string $repoUrl
*/
public function testSupports($expected, $repoUrl)
{
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();

$this->assertSame($expected, GitHubDriver::supports($io, $this->config, $repoUrl));
}

/**
* @return list<array{bool, string}>
*/
public function supportsProvider()
{
return array(
array(false, 'https://github.com/acme'),
array(true, 'https://github.com/acme/repository'),
array(true, 'git@github.com:acme/repository.git'),
);
}

/**
* @param string|object $object
* @param string $attribute
Expand Down

0 comments on commit 3eb12ef

Please sign in to comment.