Skip to content

[Validator] Improvement: provide file basename for constr. violation messages in FileValidator. #26261

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

Conversation

TheCelavi
Copy link
Contributor

Q A
Branch? 3.4
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets no
License MIT
Doc PR N/A

\Symfony\Component\Validator\Constraints\FileValidator provides absolute path to file on server when user, per example, uploads empty file, too large file, of wrong mime type, etc...

Absolute path to file on server does not have value to the end user, on top of that, exposing it can be a security issue - end user should not be aware of server filesystem.

Basename of file, however, has value (per example: MyAwesomeSheet.xlsx, MyCV.doc, etc..) - if something is wrong with file upload (size, mime, etc...).

If basename is exposed, we can construct messages like: "Your file 'MyCV.doc' is not allowed for upload due to....whatever"...

This PR provides basename of file so end user of \Symfony\Component\Validator\Constraints\FileValidator can construct error messages of higher value for end user.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

Thanks for this proposal
That's a new feature so it should target master.
Can you please add some tests also?

@@ -138,10 +138,12 @@ public function validate($value, Constraint $constraint)
}

$sizeInBytes = filesize($path);
$basename = ($value instanceof UploadedFile) ? $value->getClientOriginalName() : basename($path);
Copy link
Member

Choose a reason for hiding this comment

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

brackets should be remove

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed as requested - I use brackets for readability purposes, no side effects

@TheCelavi
Copy link
Contributor Author

That's a new feature so it should target master.

Do you want me to create PR on master branch?

Can you please add some tests also?

I have updated tests to reflect this change, so, it is kinda covered already. Only thin that can be tested is getting the basename of file - do you want me to write that test?

@TheCelavi TheCelavi changed the base branch from 3.4 to master March 16, 2018 12:59
@nicolas-grekas nicolas-grekas modified the milestones: 4.1, next Apr 20, 2018
@fabpot fabpot force-pushed the feature/expose-filename-in-file-validator branch from 5892e0e to a77abad Compare October 10, 2018 12:29
@fabpot
Copy link
Member

fabpot commented Oct 10, 2018

Thank you @TheCelavi.

@fabpot fabpot merged commit a77abad into symfony:master Oct 10, 2018
fabpot added a commit that referenced this pull request Oct 10, 2018
…str. violation messages in FileValidator. (TheCelavi)

This PR was squashed before being merged into the 4.2-dev branch (closes #26261).

Discussion
----------

[Validator] Improvement: provide file basename for constr. violation messages in FileValidator.

| Q             | A
| ------------- | ---
| Branch?       | 3.4 <!-- see below -->
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | no
| License       | MIT
| Doc PR        | N/A

`\Symfony\Component\Validator\Constraints\FileValidator` provides absolute path to file on server when user, per example, uploads empty file, too large file, of wrong mime type, etc...

Absolute path to file on server does not have value to the end user, on top of that, exposing it can be a security issue - end user should not be aware of server filesystem.

Basename of file, however, has value (per example: MyAwesomeSheet.xlsx, MyCV.doc, etc..) - if something is wrong with file upload (size, mime, etc...).

If basename is exposed, we can construct messages like: "Your file 'MyCV.doc' is not allowed for upload due to....whatever"...

This PR provides basename of file so end user of `\Symfony\Component\Validator\Constraints\FileValidator` can construct error messages of higher value for end user.

Commits
-------

a77abad [Validator] Improvement: provide file basename for constr. violation messages in FileValidator.
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
This was referenced Nov 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants