From 3d82719b70e214ed6205d74c0ce2a4fd4e18367b Mon Sep 17 00:00:00 2001 From: Florian Engelhardt Date: Fri, 4 Feb 2022 10:23:10 +0100 Subject: [PATCH] Fix package search not urlencoding the input (#10500) --- .../Repository/ComposerRepository.php | 2 +- .../Repository/ComposerRepositoryTest.php | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Composer/Repository/ComposerRepository.php b/src/Composer/Repository/ComposerRepository.php index 7d0bcab80..0154a49a5 100644 --- a/src/Composer/Repository/ComposerRepository.php +++ b/src/Composer/Repository/ComposerRepository.php @@ -540,7 +540,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito $this->loadRootServerFile(600); if ($this->searchUrl && $mode === self::SEARCH_FULLTEXT) { - $url = str_replace(array('%query%', '%type%'), array($query, $type), $this->searchUrl); + $url = str_replace(array('%query%', '%type%'), array(urlencode($query), $type), $this->searchUrl); $search = $this->httpDownloader->get($url, $this->options)->decodeJson(); diff --git a/tests/Composer/Test/Repository/ComposerRepositoryTest.php b/tests/Composer/Test/Repository/ComposerRepositoryTest.php index d2f1ca8ed..ed5dbf93d 100644 --- a/tests/Composer/Test/Repository/ComposerRepositoryTest.php +++ b/tests/Composer/Test/Repository/ComposerRepositoryTest.php @@ -202,6 +202,36 @@ class ComposerRepositoryTest extends TestCase ); } + public function testSearchWithSpecialChars() + { + $repoConfig = array( + 'url' => 'http://example.org', + ); + + $result = array( + 'results' => array( + array( + 'name' => 'foo', + 'description' => null, + ), + ), + ); + + $httpDownloader = new HttpDownloaderMock(array( + 'http://example.org/packages.json' => JsonFile::encode(array('search' => '/search.json?q=%query%&type=%type%')), + 'http://example.org/search.json?q=foo+bar&type=' => JsonFile::encode(array()), + )); + $eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher') + ->disableOriginalConstructor() + ->getMock(); + + $repository = new ComposerRepository($repoConfig, new NullIO, FactoryMock::createConfig(), $httpDownloader, $eventDispatcher); + + $this->assertEmpty( + $repository->search('foo bar', RepositoryInterface::SEARCH_FULLTEXT) + ); + } + public function testSearchWithAbandonedPackages() { $repoConfig = array(