Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

PSR-7 Uploaded File compatibility #237

Merged
merged 6 commits into from
Dec 13, 2018

Conversation

alextech
Copy link
Contributor

@alextech alextech commented Aug 16, 2018

This patch provides changes to both the File\Upload and File\UploadFile validators to provide support for validating PSR-7 UploadedFileInterface instances.

@alextech alextech changed the title Feature/psr uploadfile PSR-7 Uploaded File compatibility Aug 16, 2018
@samsonasik
Copy link
Contributor

samsonasik commented Aug 18, 2018

@alextech please rebase against develop branch to only show your commit:

git checkout develop
git pull https://github.com/zendframework/zend-validator.git develop
git checkout feature/psr-uploadfile
git rebase develop

If you found conflict, you can fix it in the file conflicted, and run:

git add . && git rebase --continue

Lastly, force push:

git push --force origin feature/psr-uploadfile

@alextech alextech force-pushed the feature/psr-uploadfile branch from b86ac61 to b74bd08 Compare August 18, 2018 20:43
@alextech
Copy link
Contributor Author

@samsonasik Thank you for instructions! I saved them to my notes.

$content->getStream()->getMetadata('uri') :
$content['tmp_name'];

if (! is_uploaded_file($tmpFile)) {
Copy link
Member

Choose a reason for hiding this comment

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

The above logic is incorrect.

For PSR-7, we can assume that if we have UPLOAD_ERROR_OK, then the upload performed correctly. Why? because tmp_name has been taken out of the equation, and we're working with streams instead.

I'll update this during merge.

@@ -10,6 +10,7 @@
namespace ZendTest\Validator\File;

use PHPUnit\Framework\TestCase;
use Zend\Diactoros\UploadedFile;
Copy link
Member

Choose a reason for hiding this comment

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

I will update the patch to use PSR-7 mocks instead.

*
* @return void
*/
public function testPsrBasic()
Copy link
Member

Choose a reason for hiding this comment

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

I will update this method to instead use a data provider, with mock uploads.

alextech and others added 5 commits December 13, 2018 14:53
- Removes zend-diactoros from requirements.

- Updates tests to use mock `UploadedFileInterface` instances, instead
  of using zend-diactoros; this keeps the support agnostic.

- Modifies `UploadFile` to store the `UploadedFileInterface` instance
  instead of the stream URI; this is more consistent with the
  implementation of `Upload`, which stores an array of
  `UploadedFileInterface` instances.

- The detection of an attack is moot with PSR-7, as it stores the
  uploaded file as an in-memory stream. As such, I modified both classes
  to omit that check; if we get an `UPLOAD_ERR_OK`, we have a valid
  upload at that point.

- I extracted three methods from `UploadFile::isValid()`:
  - `validateFileFromErrorCode()` does a switch around the error code,
    setting errors and returning a boolean.
  - `validateUploadedFile()` is used in both the string and array value
    cases of `isValid()` to validate the incoming file. If
    `UPLOAD_ERR_OK` is detected, it performs additional logic to detect
    an upload attack, but otherwise delegates to
    `validateFileFromErrorCode()`.
  - `validatePsr7UploadedFile()` is used when the value is an
    `UploadedFileInterface`, and delegates to
    `validateFileFromErrorCode()`, using the result of `getError()`.

- I refactored the new unit tests to use data providers whenever
  possible.
@weierophinney weierophinney merged commit a10270a into zendframework:develop Dec 13, 2018
@weierophinney
Copy link
Member

Thanks, @alextech!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants