Skip to content

Commit

Permalink
Fix only/exclude to avoid matching names as sub-strings of full packa…
Browse files Browse the repository at this point in the history
…ge names, fixes #10001
  • Loading branch information
Seldaek committed Jul 22, 2021
1 parent 29a52ff commit 24f5e54
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Composer/Repository/FilterRepository.php
Expand Up @@ -33,17 +33,17 @@ public function __construct(RepositoryInterface $repo, array $options)
if (!is_array($options['only'])) {
throw new \InvalidArgumentException('"only" key for repository '.$repo->getRepoName().' should be an array');
}
$this->only = '{^'.implode('|', array_map(function ($val) {
$this->only = '{^(?:'.implode('|', array_map(function ($val) {
return BasePackage::packageNameToRegexp($val, '%s');
}, $options['only'])) .'$}iD';
}, $options['only'])) .')$}iD';
}
if (isset($options['exclude'])) {
if (!is_array($options['exclude'])) {
throw new \InvalidArgumentException('"exclude" key for repository '.$repo->getRepoName().' should be an array');
}
$this->exclude = '{^'.implode('|', array_map(function ($val) {
$this->exclude = '{^(?:'.implode('|', array_map(function ($val) {
return BasePackage::packageNameToRegexp($val, '%s');
}, $options['exclude'])) .'$}iD';
}, $options['exclude'])) .')$}iD';
}
if ($this->exclude && $this->only) {
throw new \InvalidArgumentException('Only one of "only" and "exclude" can be specified for repository '.$repo->getRepoName());
Expand Down
3 changes: 3 additions & 0 deletions tests/Composer/Test/Repository/FilterRepositoryTest.php
Expand Up @@ -50,6 +50,9 @@ public static function repoMatchingTests()
array(array('foo/aaa', 'foo/bbb'), array('only' => array('foo/*'))),
array(array('foo/aaa', 'baz/yyy'), array('only' => array('foo/aaa', 'baz/yyy'))),
array(array('bar/xxx'), array('exclude' => array('foo/*', 'baz/yyy'))),
// make sure sub-patterns are not matched without wildcard
array(array('foo/aaa', 'foo/bbb', 'bar/xxx', 'baz/yyy'), array('exclude' => array('foo/aa', 'az/yyy'))),
array(array(), array('only' => array('foo/aa', 'az/yyy'))),
);
}

Expand Down

0 comments on commit 24f5e54

Please sign in to comment.