Skip to content

Commit 3df4bd5

Browse files
tijmenbruggemanrkoopmans
authored andcommitted
fix: sanitize meta data before handling image sizes
1 parent 35711c4 commit 3df4bd5

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

src/class-tiny-image.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,8 @@ private function parse_wp_metadata() {
4949
if ( ! is_array( $this->wp_metadata ) ) {
5050
$this->wp_metadata = wp_get_attachment_metadata( $this->id );
5151
}
52-
if ( ! is_array( $this->wp_metadata ) ) {
53-
return;
54-
}
55-
if ( ! isset( $this->wp_metadata['file'] ) ) {
52+
53+
if ( ! is_array( $this->wp_metadata ) || ! isset( $this->wp_metadata['file'] ) ) {
5654
/* No file metadata found, this might be another plugin messing with
5755
metadata. Simply ignore this! */
5856
return;
@@ -72,11 +70,25 @@ private function parse_wp_metadata() {
7270
$filename = $path_prefix . $this->name;
7371
$this->sizes[ self::ORIGINAL ] = new Tiny_Image_Size( $filename );
7472

75-
if ( isset( $this->wp_metadata['sizes'] ) && is_array( $this->wp_metadata['sizes'] ) ) {
76-
foreach ( $this->wp_metadata['sizes'] as $size_name => $info ) {
77-
$this->sizes[ $size_name ] = new Tiny_Image_Size( $path_prefix . $info['file'] );
73+
// Ensure 'sizes' exists and is an array to prevent PHP Warnings
74+
$sizes = isset( $this->wp_metadata['sizes'] ) && is_array( $this->wp_metadata['sizes'] )
75+
? $this->wp_metadata['sizes']
76+
: array();
77+
78+
$sanitized_sizes = array();
79+
foreach ( $sizes as $size_name => $size_info ) {
80+
// size is valid when its an array and has a file
81+
if ( is_array( $size_info ) && isset( $size_info['file'] ) ) {
82+
// Add to sanitized metadata
83+
$sanitized_sizes[ $size_name ] = $size_info;
84+
$this->sizes[ $size_name ] = new Tiny_Image_Size(
85+
$path_prefix . $size_info['file']
86+
);
7887
}
7988
}
89+
90+
// Update the metadata with only the valid sizes found
91+
$this->wp_metadata['sizes'] = $sanitized_sizes;
8092
}
8193

8294
private function detect_duplicates( $active_sizes, $active_tinify_sizes ) {

test/unit/TinyImageTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,52 @@ public function test_update_wp_metadata_should_update_with_resized_original() {
4444
$this->assertEquals( 100, $tiny_image_metadata['height'] );
4545
$this->assertEquals( 100, $tiny_image_metadata['filesize'] );
4646
}
47+
48+
public function test_parse_wp_metadata_should_ignore_invalid_sizes() {
49+
$invalid_metadata = array(
50+
'width' => 1256,
51+
'height' => 1256,
52+
'file' => '2015/09/tinypng_gravatar.png',
53+
'sizes' => array(
54+
'valid' => array(
55+
'file' => 'tinypng_gravatar-200x200.png',
56+
'width' => 200,
57+
'height' => 200,
58+
'mime-type' => 'image/png',
59+
),
60+
'missing-file' => array(
61+
'width' => 50,
62+
'height' => 50,
63+
'mime-type' => 'image/png',
64+
),
65+
'scalar-size' => 'tinypng_gravatar-300x300.png',
66+
'null-size' => null,
67+
'valid-second' => array(
68+
'file' => 'tinypng_gravatar-400x400.png',
69+
'mime-type' => 'image/png',
70+
),
71+
),
72+
'image_meta' => array(),
73+
);
74+
75+
$tiny_image = new Tiny_Image( $this->settings, 999, $invalid_metadata );
76+
77+
$this->assertEquals(
78+
array(
79+
'valid' => array(
80+
'file' => 'tinypng_gravatar-200x200.png',
81+
'width' => 200,
82+
'height' => 200,
83+
'mime-type' => 'image/png',
84+
),
85+
'valid-second' => array(
86+
'file' => 'tinypng_gravatar-400x400.png',
87+
'mime-type' => 'image/png',
88+
),
89+
),
90+
$tiny_image->get_wp_metadata()['sizes']
91+
);
92+
}
4793

4894
public function test_get_images_should_return_all_images() {
4995
$this->assertEquals( array(

0 commit comments

Comments
 (0)