Skip to content

Commit

Permalink
Trim trailing slash in path downloader to avoid symlink issues, and i…
Browse files Browse the repository at this point in the history
…n FileDownloader::getLocalChanges as we append a string without slash, refs #9422
  • Loading branch information
Seldaek committed Nov 5, 2020
1 parent c04c42b commit bc93369
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Composer/Downloader/FileDownloader.php
Expand Up @@ -429,6 +429,7 @@ public function getLocalChanges(PackageInterface $package, $targetDir)
$e = null;
$output = '';

$targetDir = Filesystem::trimTrailingSlash($targetDir);
try {
if (is_dir($targetDir.'_compare')) {
$this->filesystem->removeDirectory($targetDir.'_compare');
Expand Down
4 changes: 4 additions & 0 deletions src/Composer/Downloader/PathDownloader.php
Expand Up @@ -47,6 +47,7 @@ class PathDownloader extends FileDownloader implements VcsCapableDownloaderInter
*/
public function download(PackageInterface $package, $path, PackageInterface $prevPackage = null, $output = true)
{
$path = Filesystem::trimTrailingSlash($path);
$url = $package->getDistUrl();
$realUrl = realpath($url);
if (false === $realUrl || !file_exists($realUrl) || !is_dir($realUrl)) {
Expand Down Expand Up @@ -80,6 +81,7 @@ public function download(PackageInterface $package, $path, PackageInterface $pre
*/
public function install(PackageInterface $package, $path, $output = true)
{
$path = Filesystem::trimTrailingSlash($path);
$url = $package->getDistUrl();
$realUrl = realpath($url);

Expand Down Expand Up @@ -178,6 +180,7 @@ public function install(PackageInterface $package, $path, $output = true)
*/
public function remove(PackageInterface $package, $path, $output = true)
{
$path = Filesystem::trimTrailingSlash($path);
/**
* realpath() may resolve Windows junctions to the source path, so we'll check for a junction first
* to prevent a false positive when checking if the dist and install paths are the same.
Expand Down Expand Up @@ -209,6 +212,7 @@ public function remove(PackageInterface $package, $path, $output = true)
*/
public function getVcsReference(PackageInterface $package, $path)
{
$path = Filesystem::trimTrailingSlash($path);
$parser = new VersionParser;
$guesser = new VersionGuesser($this->config, $this->process, $parser);
$dumper = new ArrayDumper;
Expand Down
17 changes: 17 additions & 0 deletions src/Composer/Util/Filesystem.php
Expand Up @@ -506,6 +506,23 @@ public function normalizePath($path)
return $prefix.($absolute ? '/' : '').implode('/', $parts);
}

/**
* Remove trailing slashes if present to avoid issues with symlinks
*
* And other possible unforeseen disasters, see https://github.com/composer/composer/pull/9422
*
* @param string $path
* @return bool
*/
public static function trimTrailingSlash($path)
{
if (!preg_match('{^[/\\\\]+$}', $path)) {
$path = rtrim($path, '/\\');
}

return $path;
}

/**
* Return if the given path is local
*
Expand Down

1 comment on commit bc93369

@kdambekalns
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes what I described in neos/composer-plugin#16, thanks!

Please sign in to comment.