8000 [Filesystem] Fix mirroring a directory into itself or in his child with realpath checks by XuruDragon · Pull Request #30116 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Filesystem] Fix mirroring a directory into itself or in his child with realpath checks #30116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,10 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
$originDir = rtrim($originDir, '/\\');
$originDirLen = \strlen($originDir);

if (!$this->exists($originDir)) {
throw new IOException(sprintf('The origin directory specified "%s" was not found.', $originDir), 0, null, $originDir);
}

// Iterate in destination folder to remove obsolete entries
if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) {
$deleteIterator = $iterator;
Expand All @@ -564,35 +568,24 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
}

if ($this->exists($originDir)) {
$this->mkdir($targetDir);
}
$this->mkdir($targetDir);
$targetDirInfo = new \SplFileInfo($targetDir);

foreach ($iterator as $file) {
if (false === strpos($file->getPath(), $originDir)) {
throw new IOException(sprintf('Unable to mirror "%s" directory. If the origin directory is relative, try using "realpath" before calling the mirror method.', $originDir), 0, null, $originDir);
if ($file->getPathName() === $targetDir || $file->getRealPath() === $targetDir || 0 === strpos($file->getRealPath(), $targetDirInfo->getRealPath())) {
continue;
}

$target = $targetDir.substr($file->getPathname(), $originDirLen);

if ($copyOnWindows) {
if (is_file($file)) {
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
} elseif (is_dir($file)) {
$this->mkdir($target);
} else {
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
}
if (!$copyOnWindows && is_link($file)) {
$this->symlink($file->getLinkTarget(), $target);
} elseif (is_dir($file)) {
$this->mkdir($target);
} elseif (is_file($file)) {
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
} else {
if (is_link($file)) {
$this->symlink($file->getLinkTarget(), $target);
} elseif (is_dir($file)) {
$this->mkdir($target);
} elseif (is_file($file)) {
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
} else {
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
}
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
}
}
}
Expand Down
48 changes: 19 additions & 29 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1332,44 +1332,34 @@ public function testMirrorContentsWithSameNameAsSourceOrTargetWithDeleteOption()
$this->assertFileNotExists($targetPath.'target');
}

public function testMirrorWithCustomIterator()
public function testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory()
{
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
mkdir($sourcePath);

$file = $sourcePath.\DIRECTORY_SEPARATOR.'file';
file_put_contents($file, 'FILE');

$targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
$directory = $sourcePath.'directory'.\DIRECTORY_SEPARATOR;
$file1 = $directory.'file1';
$file2 = $sourcePath.'file2';

$splFile = new \SplFileInfo($file);
$iterator = new \ArrayObject([$splFile]);
mkdir($sourcePath);
mkdir($directory);
file_put_contents($file1, 'FILE1');
file_put_contents($file2, 'FILE2');

$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
$targetPath = $sourcePath.'target'.\DIRECTORY_SEPARATOR;

$this->assertTrue(is_dir($targetPath));
$this->assertFileEquals($file, $targetPath.\DIRECTORY_SEPARATOR.'file');
}

/**
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
* @expectedExceptionMessageRegExp /Unable to mirror "(.*)" directory/
*/
public function testMirrorWithCustomIteratorWithRelativePath()
{
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
$realSourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
mkdir($realSourcePath);
if ('\\' !== \DIRECTORY_SEPARATOR) {
$this->filesystem->symlink($targetPath, $sourcePath.'target_simlink');
}

$file = $realSourcePath.'file';
file_put_contents($file, 'FILE');
$this->filesystem->mirror($sourcePath, $targetPath, null, ['delete' => true]);

$targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
$this->assertTrue($this->filesystem->exists($targetPath));
$this->assertTrue($this->filesystem->exists($targetPath.'directory'));

$splFile = new \SplFileInfo($file);
$iterator = new \ArrayObject([$splFile]);
$this->assertFileEquals($file1, $targetPath.'directory'.\DIRECTORY_SEPARATOR.'file1');
$this->assertFileEquals($file2, $targetPath.'file2');

$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
$this->assertFalse($this->filesystem->exists($targetPath.'target_simlink'));
$this->assertFalse($this->filesystem->exists($targetPath.'target'));
}

/**
Expand Down
0