Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add strict image validation #17458

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 1 commit into from
Mar 31, 2016
Merged

Conversation

Koc
Copy link
Contributor

@Koc Koc commented Jan 20, 2016

Q A
Bug fix? yes (current validator does not validates corrupted images)
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? not, but fail looks like not relates to this PR
Fixed tickets -
License MIT
Doc PR -

getimagesize returns correct size for corrupted images, so I've added another check

@@ -43,6 +44,22 @@ public function validate($value, Constraint $constraint)
return;
}

if ($constraint->strict) {
if (!function_exists('imagecreatefromstring')) {
throw new RuntimeException('Strict image validation requires installed and enabled GD extension');
Copy link
Member

Choose a reason for hiding this comment

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

I think the error should be triggered when the constraint is configured not when the validation happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Imo we should then change that in the EmailValidator too (if there isn't a reason for doing it this way that I am missing).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't know, but is constraints can be cached and serialized? If yes and constructor wouldn't called then we will miss this check sometimes ang get fatal error in constraint validator.

@dunglas
Copy link
Member

dunglas commented Jan 21, 2016

👍 great addition

@@ -61,6 +61,7 @@ class Image extends File
public $allowSquare = true;
public $allowLandscape = true;
public $allowPortrait = true;
public $strict = false;
Copy link
Member

Choose a reason for hiding this comment

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

I don't like strict here as it does not convey what it really does.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you suggest other name?

Copy link
Contributor

Choose a reason for hiding this comment

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

checkIfCorrupted
allowCorrupted
detectCorrupted
allowUnsupported
detectUnsupported
checkUnsupported or checkIfUnsupported

An image resource will be returned on success. FALSE is returned if the image type is unsupported, the data is not in a recognised format, or the image is corrupt and cannot be loaded.
http://php.net/manual/en/function.imagecreatefromstring.php

@Koc Koc force-pushed the strict-image-validator branch 2 times, most recently from d16b576 to c46441f Compare March 2, 2016 14:49
@Koc
Copy link
Contributor Author

Koc commented Mar 2, 2016

@fabpot strict renamed to detectCorrupted. Added new error message and code. Travis is green except php7 branch - twig bridge failures

@jjsaunier
Copy link

May be we should add a note on this feature in the docs about memory issue? Because default configuration of php is not fit to open HD image in memory via imagecreatefromstringand that will trigger a Fatal error: Allowed memory size

@nicolas-grekas
Copy link
Member

I think we could accept this PR if the new constraint is disabled by default, and also if the check is done after getimagesize validation

@xabbuh
Copy link
Member

xabbuh commented Mar 6, 2016

I agree with @nicolas-grekas.

@Koc
Copy link
Contributor Author

Koc commented Mar 15, 2016

Are there any things that should to be done which blocks PR merging? @symfony/mergers

@Koc Koc force-pushed the strict-image-validator branch from c46441f to 0405ac6 Compare March 29, 2016 23:12
@Koc
Copy link
Contributor Author

Koc commented Mar 29, 2016

@fabpot I've added imagedestroy call and test for valid image. Travis is green.

@HeahDude
Copy link
Contributor

Thanks for that PR!

@@ -74,4 +77,5 @@ class Image extends File
public $allowSquareMessage = 'The image is square ({{ width }}x{{ height }}px). Square images are not allowed.';
public $allowLandscapeMessage = 'The image is landscape oriented ({{ width }}x{{ height }}px). Landscape oriented images are not allowed.';
public $allowPortraitMessage = 'The image is portrait oriented ({{ width }}x{{ height }}px). Portrait oriented images are not allowed.';
public $corruptedMessage = 'This file contains corrupted image.';
Copy link
Member

Choose a reason for hiding this comment

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

I suggest The image file is corrupted.

@nicolas-grekas
Copy link
Member

@Koc please move corruption check after image size validation.

@Koc Koc force-pushed the strict-image-validator branch 2 times, most recently from 62b1a3e to a298f22 Compare March 30, 2016 20:35
@Koc
Copy link
Contributor Author

Koc commented Mar 30, 2016

@nicolas-grekas done. Travis failure looks unrelated


if ($constraint->detectCorrupted) {
if (!function_exists('imagecreatefromstring')) {
throw new RuntimeException('Strict image validation requires installed and enabled GD extension');
Copy link
Member

Choose a reason for hiding this comment

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

Corrupted images detection requires... ?

@nicolas-grekas
Copy link
Member

👍 with one minor comment

@Koc Koc force-pushed the strict-image-validator branch from a298f22 to 7b6a96e Compare March 30, 2016 21:27
@fabpot
Copy link
Member

fabpot commented Mar 31, 2016

Thank you @Koc.

@fabpot fabpot merged commit 7b6a96e into symfony:master Mar 31, 2016
fabpot added a commit that referenced this pull request Mar 31, 2016
This PR was merged into the 3.1-dev branch.

Discussion
----------

Add strict image validation

| Q             | A
| ------------- | ---
| Bug fix?      | yes (current validator does not validates corrupted images)
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | not, but fail looks like not relates to this PR
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

getimagesize returns correct size for corrupted images, so I've added another check

Commits
-------

7b6a96e Add corrupted images validation
wouterj added a commit to symfony/symfony-docs that referenced this pull request May 5, 2016
…ions (javiereguiluz)

This PR was squashed before being merged into the master branch (closes #6433).

Discussion
----------

Documented the detectCorrupted and corruptedMessage options

| Q             | A
| ------------- | ---
| Doc fix?      | no
| New docs?     | yes
| Applies to    | master (3.1+)
| Fixed tickets | symfony/symfony#17458

Commits
-------

2140822 Documented the detectCorrupted and corruptedMessage options
@fabpot fabpot mentioned this pull request May 13, 2016
@Koc Koc deleted the strict-image-validator branch September 21, 2016 16:01
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.

10 participants