Skip to content

Commit 5bb44f5

Browse files
committed
[HttpFoundation] UploadedFile - moved a security check
Squashed commit of the following: commit b03b32ecc985c4a4f9dc7df2d3336a4cd75aae30 Merge: fb7004b fc70e13 Author: Bilal Amarni <bilal.amarni@gmail.com> Date: Wed Feb 27 11:33:37 2013 +0100 [HttpFoundation] UploadedFile - moved a security check commit fc70e13 Author: Bilal Amarni <bilal.amarni@gmail.com> Date: Thu Jan 24 11:07:29 2013 +0100 explicitly passed UPLOAD_ERR_OK constant in a test commit dda03a2 Author: Bilal Amarni <bilal.amarni@gmail.com> Date: Fri Jan 18 17:24:06 2013 +0100 [HttpFoundation] UploadedFile - moved a security check from move() to isValid()
1 parent 69dbbdd commit 5bb44f5

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

src/Symfony/Component/HttpFoundation/File/UploadedFile.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,15 @@ public function getError()
179179
/**
180180
* Returns whether the file was uploaded successfully.
181181
*
182-
* @return Boolean True if no error occurred during uploading
182+
* @return Boolean True if the file has been uploaded with HTTP and no error occurred.
183183
*
184184
* @api
185185
*/
186186
public function isValid()
187187
{
188-
return $this->error === UPLOAD_ERR_OK;
188+
$isOk = $this->error === UPLOAD_ERR_OK;
189+
190+
return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
189191
}
190192

191193
/**
@@ -196,7 +198,7 @@ public function isValid()
196198
*
197199
* @return File A File object representing the new file
198200
*
199-
* @throws FileException if the file has not been uploaded via Http
201+
* @throws FileException if, for any reason, the file could not have been moved
200202
*
201203
* @api
202204
*/
@@ -205,21 +207,21 @@ public function move($directory, $name = null)
205207
if ($this->isValid()) {
206208
if ($this->test) {
207209
return parent::move($directory, $name);
208-
} elseif (is_uploaded_file($this->getPathname())) {
209-
$target = $this->getTargetFile($directory, $name);
210-
211-
if (!@move_uploaded_file($this->getPathname(), $target)) {
212-
$error = error_get_last();
213-
throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
214-
}
210+
}
215211

216-
@chmod($target, 0666 & ~umask());
212+
$target = $this->getTargetFile($directory, $name);
217213

218-
return $target;
214+
if (!@move_uploaded_file($this->getPathname(), $target)) {
215+
$error = error_get_last();
216+
throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
219217
}
218+
219+
@chmod($target, 0666 & ~umask());
220+
221+
return $target;
220222
}
221223

222-
throw new FileException(sprintf('The file "%s" has not been uploaded via Http', $this->getPathname()));
224+
throw new FileException(sprintf('The file "%s" is not valid', $this->getPathname()));
223225
}
224226

225227
/**

src/Symfony/Component/HttpFoundation/Tests/File/UploadedFileTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ public function testIsValid()
197197
'original.gif',
198198
null,
199199
filesize(__DIR__.'/Fixtures/test.gif'),
200-
UPLOAD_ERR_OK
200+
UPLOAD_ERR_OK,
201+
true
201202
);
202203

203204
$this->assertTrue($file->isValid());
@@ -229,4 +230,17 @@ public function uploadedFileErrorProvider()
229230
array(UPLOAD_ERR_EXTENSION),
230231
);
231232
}
233+
234+
public function testIsInvalidIfNotHttpUpload()
235+
{
236+
$file = new UploadedFile(
237+
__DIR__.'/Fixtures/test.gif',
238+
'original.gif',
239+
null,
240+
filesize(__DIR__.'/Fixtures/test.gif'),
241+
UPLOAD_ERR_OK
242+
);
243+
244+
$this->assertFalse($file->isValid());
245+
}
232246
}

src/Symfony/Component/HttpKernel/Tests/ClientTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function testUploadedFile()
114114

115115
$files = array(
116116
array('tmp_name' => $source, 'name' => 'original', 'type' => 'mime/original', 'size' => 123, 'error' => UPLOAD_ERR_OK),
117-
new UploadedFile($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK),
117+
new UploadedFile($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK, true),
118118
);
119119

120120
foreach ($files as $file) {
@@ -147,7 +147,7 @@ public function testUploadedFileWhenSizeExceedsUploadMaxFileSize()
147147

148148
$file = $this
149149
->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
150-
->setConstructorArgs(array($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK))
150+
->setConstructorArgs(array($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK, true))
151151
->setMethods(array('getSize'))
152152
->getMock()
153153
;

src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function testValidUploadedfile()
8282
$this->context->expects($this->never())
8383
->method('addViolation');
8484

85-
$file = new UploadedFile($this->path, 'originalName');
85+
$file = new UploadedFile($this->path, 'originalName', null, null, null, true);
8686
$this->validator->validate($file, new File());
8787
}
8888

0 commit comments

Comments
 (0)