Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Validator] Validate SVG ratio in Image validator #59265

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
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 5 src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add support for ratio checks for SVG files to the `Image` constraint

7.2
---

Expand Down
70 changes: 63 additions & 7 deletions 70 src/Symfony/Component/Validator/Constraints/ImageValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Mime\MimeTypes;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\LogicException;
Expand Down Expand Up @@ -50,7 +52,13 @@ public function validate(mixed $value, Constraint $constraint): void
return;
}

$size = @getimagesize($value);
$isSvg = $this->isSvg($value);

if ($isSvg) {
$size = $this->getSvgSize($value);
} else {
$size = @getimagesize($value);
}

if (!$size || (0 === $size[0]) || (0 === $size[1])) {
$this->context->buildViolation($constraint->sizeNotDetectedMessage)
Expand All @@ -63,7 +71,7 @@ public function validate(mixed $value, Constraint $constraint): void
$width = $size[0];
$height = $size[1];

if ($constraint->minWidth) {
if (!$isSvg && $constraint->minWidth) {
if (!ctype_digit((string) $constraint->minWidth)) {
throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid minimum width.', $constraint->minWidth));
}
Expand All @@ -79,7 +87,7 @@ public function validate(mixed $value, Constraint $constraint): void
}
}

if ($constraint->maxWidth) {
if (!$isSvg && $constraint->maxWidth) {
if (!ctype_digit((string) $constraint->maxWidth)) {
throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid maximum width.', $constraint->maxWidth));
}
Expand All @@ -95,7 +103,7 @@ public function validate(mixed $value, Constraint $constraint): void
}
}

if ($constraint->minHeight) {
if (!$isSvg && $constraint->minHeight) {
if (!ctype_digit((string) $constraint->minHeight)) {
throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid minimum height.', $constraint->minHeight));
}
Expand All @@ -111,7 +119,7 @@ public function validate(mixed $value, Constraint $constraint): void
}
}

if ($constraint->maxHeight) {
if (!$isSvg && $constraint->maxHeight) {
if (!ctype_digit((string) $constraint->maxHeight)) {
throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid maximum height.', $constraint->maxHeight));
}
Expand All @@ -127,7 +135,7 @@ public function validate(mixed $value, Constraint $constraint): void

$pixels = $width * $height;

if (null !== $constraint->minPixels) {
if (!$isSvg && null !== $constraint->minPixels) {
if (!ctype_digit((string) $constraint->minPixels)) {
throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid minimum amount of pixels.', $constraint->minPixels));
}
Expand All @@ -143,7 +151,7 @@ public function validate(mixed $value, Constraint $constraint): void
}
}

if (null !== $constraint->maxPixels) {
if (!$isSvg && null !== $constraint->maxPixels) {
if (!ctype_digit((string) $constraint->maxPixels)) {
throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid maximum amount of pixels.', $constraint->maxPixels));
}
Expand Down Expand Up @@ -231,4 +239,52 @@ public function validate(mixed $value, Constraint $constraint): void
imagedestroy($resource);
}
}

private function isSvg(mixed $value): bool
{
if ($value instanceof File) {
$mime = $value->getMimeType();
} elseif (class_exists(MimeTypes::class)) {
$mime = MimeTypes::getDefault()->guessMimeType($value);
} elseif (!class_exists(File::class)) {
return false;
} else {
$mime = (new File($value))->getMimeType();
}

return 'image/svg+xml' === $mime;
}

/**
* @return array{int, int}|null index 0 and 1 contains respectively the width and the height of the image, null if size can't be found
*/
private function getSvgSize(mixed $value): ?array
{
if ($value instanceof File) {
$content = $value->getContent();
} elseif (!class_exists(File::class)) {
return null;
} else {
$content = (new File($value))->getContent();
}

if (1 === preg_match('/<svg[^<>]+width="(?<width>[0-9]+)"[^<>]*>/', $content, $widthMatches)) {
$width = (int) $widthMatches['width'];
}

if (1 === preg_match('/<svg[^<>]+height="(?<height>[0-9]+)"[^<>]*>/', $content, $heightMatches)) {
$height = (int) $heightMatches['height'];
}

if (1 === preg_match('/<svg[^<>]+viewBox="-?[0-9]+ -?[0-9]+ (?<width>-?[0-9]+) (?<height>-?[0-9]+)"[^<>]*>/', $content, $viewBoxMatches)) {
$width ??= (int) $viewBoxMatches['width'];
$height ??= (int) $viewBoxMatches['height'];
}

if (isset($width) && isset($height)) {
return [$width, $height];
}

return null;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,140 @@ public static function provideInvalidMimeTypeWithNarrowedSet()
]),
];
}

/** @dataProvider provideSvgWithViolation */
public function testSvgWithViolation(string $image, Image $constraint, string $violation, array $parameters = [])
{
$this->validator->validate($image, $constraint);

$this->buildViolation('myMessage')
->setCode($violation)
->setParameters($parameters)
->assertRaised();
}

public static function provideSvgWithViolation(): iterable
{
yield 'No size svg' => [
__DIR__.'/Fixtures/test_no_size.svg',
new Image(allowLandscape: false, sizeNotDetectedMessage: 'myMessage'),
Image::SIZE_NOT_DETECTED_ERROR,
];

yield 'Landscape SVG not allowed' => [
__DIR__.'/Fixtures/test_landscape.svg',
new Image(allowLandscape: false, allowLandscapeMessage: 'myMessage'),
Image::LANDSCAPE_NOT_ALLOWED_ERROR,
[
'{{ width }}' => 500,
'{{ height }}' => 200,
],
];

yield 'Portrait SVG not allowed' => [
__DIR__.'/Fixtures/test_portrait.svg',
new Image(allowPortrait: false, allowPortraitMessage: 'myMessage'),
Image::PORTRAIT_NOT_ALLOWED_ERROR,
[
'{{ width }}' => 200,
'{{ height }}' => 500,
],
];

yield 'Square SVG not allowed' => [
__DIR__.'/Fixtures/test_square.svg',
new Image(allowSquare: false, allowSquareMessage: 'myMessage'),
Image::SQUARE_NOT_ALLOWED_ERROR,
[
'{{ width }}' => 500,
'{{ height }}' => 500,
],
];

yield 'Landscape with width attribute SVG allowed' => [
__DIR__.'/Fixtures/test_landscape_width.svg',
new Image(allowLandscape: false, allowLandscapeMessage: 'myMessage'),
Image::LANDSCAPE_NOT_ALLOWED_ERROR,
[
'{{ width }}' => 600,
'{{ height }}' => 200,
],
];

yield 'Landscape with height attribute SVG not allowed' => [
__DIR__.'/Fixtures/test_landscape_height.svg',
new Image(allowLandscape: false, allowLandscapeMessage: 'myMessage'),
Image::LANDSCAPE_NOT_ALLOWED_ERROR,
[
'{{ width }}' => 500,
'{{ height }}' => 300,
],
];

yield 'Landscape with width and height attribute SVG not allowed' => [
__DIR__.'/Fixtures/test_landscape_width_height.svg',
new Image(allowLandscape: false, allowLandscapeMessage: 'myMessage'),
Image::LANDSCAPE_NOT_ALLOWED_ERROR,
[
'{{ width }}' => 600,
'{{ height }}' => 300,
],
];

yield 'SVG Min ratio 2' => [
__DIR__.'/Fixtures/test_square.svg',
new Image(minRatio: 2, minRatioMessage: 'myMessage'),
Image::RATIO_TOO_SMALL_ERROR,
[
'{{ ratio }}' => '1',
'{{ min_ratio }}' => '2',
],
];

yield 'SVG Min ratio 0.5' => [
__DIR__.'/Fixtures/test_square.svg',
new Image(maxRatio: 0.5, maxRatioMessage: 'myMessage'),
Image::RATIO_TOO_BIG_ERROR,
[
'{{ ratio }}' => '1',
'{{ max_ratio }}' => '0.5',
],
];
}

/** @dataProvider provideSvgWithoutViolation */
public function testSvgWithoutViolation(string $image, Image $constraint)
{
$this->validator->validate($image, $constraint);

$this->assertNoViolation();
}

public static function provideSvgWithoutViolation(): iterable
{
yield 'Landscape SVG allowed' => [
__DIR__.'/Fixtures/test_landscape.svg',
new Image(allowLandscape: true, allowLandscapeMessage: 'myMessage'),
];

yield 'Portrait SVG allowed' => [
__DIR__.'/Fixtures/test_portrait.svg',
new Image(allowPortrait: true, allowPortraitMessage: 'myMessage'),
];

yield 'Square SVG allowed' => [
__DIR__.'/Fixtures/test_square.svg',
new Image(allowSquare: true, allowSquareMessage: 'myMessage'),
];

yield 'SVG Min ratio 1' => [
__DIR__.'/Fixtures/test_square.svg',
new Image(minRatio: 1, minRatioMessage: 'myMessage'),
];

yield 'SVG Max ratio 1' => [
__DIR__.'/Fixtures/test_square.svg',
new Image(maxRatio: 1, maxRatioMessage: 'myMessage'),
];
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.