Make WordPress Core

Changeset 49753 for branches/5.6


Ignore:
Timestamp:
12/04/2020 09:44:27 PM (6 years ago)
Author:
iandunn
Message:

Media: Return WP_Error when cropping with bad input to avoid fatal.

This avoids an error on PHP 8 caused by calling wp_imagecreatetruecolor() with inputs that aren't numeric, or are less than 0.

Props hellofromtonya, Boniu91, metalandcoffee, SergeyBiryukov.
Reviewed by SergeyBiryukov, iandunn.
Merges [49751] to the 5.6 branch.
Fixes #51937.

Location:
branches/5.6
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/5.6

  • branches/5.6/src/wp-includes/class-wp-image-editor-gd.php

    r49019 r49753  
    324324                }
    325325
    326                 $dst = wp_imagecreatetruecolor( $dst_w, $dst_h );
     326                foreach ( array( $src_w, $src_h, $dst_w, $dst_h ) as $value ) {
     327                        if ( ! is_numeric( $value ) || (int) $value <= 0 ) {
     328                                return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file );
     329                        }
     330                }
     331
     332                $dst = wp_imagecreatetruecolor( (int) $dst_w, (int) $dst_h );
    327333
    328334                if ( $src_abs ) {
     
    335341                }
    336342
    337                 imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
     343                imagecopyresampled( $dst, $this->image, 0, 0, (int) $src_x, (int) $src_y, (int) $dst_w, (int) $dst_h, (int) $src_w, (int) $src_h );
    338344
    339345                if ( is_gd_image( $dst ) ) {
  • branches/5.6/src/wp-includes/media.php

    r49613 r49753  
    35063506 * @param int $width  Image width in pixels.
    35073507 * @param int $height Image height in pixels.
    3508  * @return resource|GdImage The GD image resource or GdImage instance.
     3508 * @return resource|GdImage|false The GD image resource or GdImage instance on success. False on failure.
    35093509 */
    35103510function wp_imagecreatetruecolor( $width, $height ) {
  • branches/5.6/tests/phpunit/tests/image/editorGd.php

    r49488 r49753  
    406406
    407407        /**
    408          * Test cropping an image
    409          */
    410         public function test_crop() {
     408         * Test cropping an image.
     409         *
     410         * @ticket 51937
     411         *
     412         * @dataProvider data_crop
     413         */
     414        public function test_crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
    411415                $file = DIR_TESTDATA . '/images/gradient-square.jpg';
    412416
     
    414418                $gd_image_editor->load();
    415419
    416                 $gd_image_editor->crop( 0, 0, 50, 50 );
     420                $gd_image_editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );
    417421
    418422                $this->assertSame(
    419423                        array(
    420                                 'width'  => 50,
    421                                 'height' => 50,
     424                                'width'  => (int) $src_w,
     425                                'height' => (int) $src_h,
    422426                        ),
    423427                        $gd_image_editor->get_size()
     428                );
     429        }
     430
     431        public function data_crop() {
     432                return array(
     433                        'src height and width must be greater than 0' => array(
     434                                'src_x' => 0,
     435                                'src_y' => 0,
     436                                'src_w' => 50,
     437                                'src_h' => 50,
     438                        ),
     439                        'src height and width can be string but must be greater than 0' => array(
     440                                'src_x' => 10,
     441                                'src_y' => '10',
     442                                'src_w' => '50',
     443                                'src_h' => '50',
     444                        ),
     445                        'dst height and width must be greater than 0' => array(
     446                                'src_x' => 10,
     447                                'src_y' => '10',
     448                                'src_w' => 150,
     449                                'src_h' => 150,
     450                                'dst_w' => 150,
     451                                'dst_h' => 150,
     452                        ),
     453                        'dst height and width can be string but must be greater than 0' => array(
     454                                'src_x' => 10,
     455                                'src_y' => '10',
     456                                'src_w' => 150,
     457                                'src_h' => 150,
     458                                'dst_w' => '150',
     459                                'dst_h' => '150',
     460                        ),
     461                );
     462        }
     463
     464        /**
     465         * Test should return WP_Error when dimensions are not integer or are <= 0.
     466         *
     467         * @ticket 51937
     468         *
     469         * @dataProvider data_crop_invalid_dimensions
     470         */
     471        public function test_crop_invalid_dimensions( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
     472                $file = DIR_TESTDATA . '/images/gradient-square.jpg';
     473
     474                $gd_image_editor = new WP_Image_Editor_GD( $file );
     475                $gd_image_editor->load();
     476
     477                $actual = $gd_image_editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );
     478
     479                $this->assertInstanceOf( 'WP_Error', $actual );
     480                $this->assertSame( 'image_crop_error', $actual->get_error_code() );
     481        }
     482
     483        public function data_crop_invalid_dimensions() {
     484                return array(
     485                        'src height must be greater than 0' => array(
     486                                'src_x' => 0,
     487                                'src_y' => 0,
     488                                'src_w' => 100,
     489                                'src_h' => 0,
     490                        ),
     491                        'src width must be greater than 0'  => array(
     492                                'src_x' => 10,
     493                                'src_y' => '10',
     494                                'src_w' => 0,
     495                                'src_h' => 100,
     496                        ),
     497                        'src height must be numeric and greater than 0' => array(
     498                                'src_x' => 10,
     499                                'src_y' => '10',
     500                                'src_w' => 100,
     501                                'src_h' => 'NaN',
     502                        ),
     503                        'dst height must be numeric and greater than 0' => array(
     504                                'src_x' => 0,
     505                                'src_y' => 0,
     506                                'src_w' => 100,
     507                                'src_h' => 50,
     508                                'dst_w' => '100',
     509                                'dst_h' => 'NaN',
     510                        ),
     511                        'src and dst height and width must be greater than 0' => array(
     512                                'src_x' => 0,
     513                                'src_y' => 0,
     514                                'src_w' => 0,
     515                                'src_h' => 0,
     516                                'dst_w' => 0,
     517                                'dst_h' => 0,
     518                        ),
     519                        'src and dst height and width can be string but must be greater than 0' => array(
     520                                'src_x' => 0,
     521                                'src_y' => 0,
     522                                'src_w' => '0',
     523                                'src_h' => '0',
     524                                'dst_w' => '0',
     525                                'dst_h' => '0',
     526                        ),
    424527                );
    425528        }
Note: See TracChangeset for help on using the changeset viewer.