Skip to content

Commit

Permalink
[HttpFoundation] Fix name sanitization after perfoming move
Browse files Browse the repository at this point in the history
  • Loading branch information
helios-ag authored and fabpot committed Oct 27, 2012
1 parent 462f93a commit 9872d26
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
18 changes: 17 additions & 1 deletion src/Symfony/Component/HttpFoundation/File/File.php
Expand Up @@ -532,7 +532,7 @@ public function move($directory, $name = null)
throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
}

$target = $directory.DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : basename($name));
$target = $directory.DIRECTORY_SEPARATOR.(null === $name ? $this->getBasename() : $this->getName($name));

if (!@rename($this->getPathname(), $target)) {
$error = error_get_last();
Expand All @@ -543,4 +543,20 @@ public function move($directory, $name = null)

return new File($target);
}

/**
* Returns locale independent base name of the given path.
*
* @param string $name The new file name
*
* @return string containing
*/
protected function getName($name)
{
$originalName = str_replace('\\', '/', $name);
$pos = strrpos($originalName, '/');
$originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);

return $originalName;
}
}
6 changes: 2 additions & 4 deletions src/Symfony/Component/HttpFoundation/File/UploadedFile.php
Expand Up @@ -94,9 +94,7 @@ public function __construct($path, $originalName, $mimeType = null, $size = null
throw new FileException(sprintf('Unable to create UploadedFile because "file_uploads" is disabled in your php.ini file (%s)', get_cfg_var('cfg_file_path')));
}

$originalName = str_replace('\\', '/', $originalName);
$pos = strrpos($originalName, '/');
$this->originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
$this->originalName = $this->getName($originalName);
$this->mimeType = $mimeType ?: 'application/octet-stream';
$this->size = $size;
$this->error = $error ?: UPLOAD_ERR_OK;
Expand Down Expand Up @@ -168,7 +166,7 @@ public function getError()
/**
* Returns whether the file was uploaded successfully.
*
* @return Boolean True if no error occurred during uploading
* @return Boolean True if no error occurred during uploading
*
* @api
*/
Expand Down
35 changes: 35 additions & 0 deletions tests/Symfony/Tests/Component/HttpFoundation/File/FileTest.php
Expand Up @@ -91,6 +91,41 @@ public function testMoveWithNewName()
@unlink($targetPath);
}

public function getFilenameFixtures()
{
return array(
array('original.gif', 'original.gif'),
array('..\\..\\original.gif', 'original.gif'),
array('../../original.gif', 'original.gif'),
array('файлfile.gif', 'файлfile.gif'),
array('..\\..\\файлfile.gif', 'файлfile.gif'),
array('../../файлfile.gif', 'файлfile.gif'),
);
}

/**
* @dataProvider getFilenameFixtures
*/
public function testMoveWithNonLatinName($filename, $sanitizedFilename)
{
$path = __DIR__.'/Fixtures/'.$sanitizedFilename;
$targetDir = __DIR__.'/Fixtures/directory/';
$targetPath = $targetDir.$sanitizedFilename;
@unlink($path);
@unlink($targetPath);
copy(__DIR__.'/Fixtures/test.gif', $path);

$file = new File($path);
$movedFile = $file->move($targetDir,$filename);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $movedFile);

$this->assertTrue(file_exists($targetPath));
$this->assertFalse(file_exists($path));
$this->assertEquals(realpath($targetPath), $movedFile->getRealPath());

@unlink($targetPath);
}

public function testMoveToAnUnexistentDirectory()
{
$sourcePath = __DIR__.'/Fixtures/test.copy.gif';
Expand Down

0 comments on commit 9872d26

Please sign in to comment.