Make WordPress Core

Changeset 62978


Ignore:
Timestamp:
08/03/2026 08:38:03 PM (3 months ago)
Author:
westonruter
Message:

Media: Normalize non-numeric attachment filesize metadata.

The stored filesize attachment metadata was read without validation in wp_prepare_attachment_for_js() and attachment_submitbox_metadata(). Attachment metadata is untyped, and the filesize key is commonly written by offloading plugins from a remote storage API response, so it can arrive as a numeric string, or be empty, non-numeric, or negative when the remote lookup fails. A numeric string was passed through verbatim, making filesizeInBytes a string in the media modal while the wp_filesize() branch of the very same conditional yielded an int; a non-numeric value such as 'unknown' was truthy and suppressed the fallback entirely, so size_format() returned false and the file size rendered empty even when the real file was readable.

Both call sites now only trust the stored value when it is numeric and casts to an integer greater than zero, and otherwise recompute the size with wp_filesize(). The fallback condition also replaces file_exists() with is_readable() guarded on a non-empty string, since get_attached_file() can be filtered to return a non-string. PHPUnit coverage is added for both functions.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/12632.
Follow-up to r34258, r52837, r62813, r62815.

Props mukesh27, westonruter.
See #65670.
Fixes #65686.

Location:
trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/media.php

    r62756 r62978  
    34263426        $file_size = false;
    34273427
    3428         if ( isset( $meta['filesize'] ) ) {
    3429                 $file_size = $meta['filesize'];
    3430         } elseif ( file_exists( $file ) ) {
     3428        if ( isset( $meta['filesize'] ) && is_numeric( $meta['filesize'] ) && (int) $meta['filesize'] > 0 ) {
     3429                $file_size = (int) $meta['filesize'];
     3430        } elseif ( is_string( $file ) && '' !== $file && is_readable( $file ) ) {
    34313431                $file_size = wp_filesize( $file );
    34323432        }
  • trunk/src/wp-includes/media.php

    r62935 r62978  
    47174717        $attached_file = get_attached_file( $attachment->ID );
    47184718
    4719         if ( isset( $meta['filesize'] ) ) {
    4720                 $bytes = $meta['filesize'];
    4721         } elseif ( file_exists( $attached_file ) ) {
     4719        if ( isset( $meta['filesize'] ) && is_numeric( $meta['filesize'] ) && (int) $meta['filesize'] > 0 ) {
     4720                $bytes = (int) $meta['filesize'];
     4721        } elseif ( is_string( $attached_file ) && '' !== $attached_file && is_readable( $attached_file ) ) {
    47224722                $bytes = wp_filesize( $attached_file );
    47234723        } else {
  • trunk/tests/phpunit/tests/media.php

    r62698 r62978  
    666666
    667667                $this->assertArrayHasKey( 'sizes', $prepped );
     668        }
     669
     670        /**
     671         * Tests that a `filesize` stored in the attachment metadata is normalized to a positive integer.
     672         *
     673         * When the stored value cannot be normalized, it should be treated as missing so that the
     674         * filesystem fallback runs instead.
     675         *
     676         * @ticket 65686
     677         *
     678         * @dataProvider data_wp_prepare_attachment_for_js_filesize
     679         *
     680         * @param mixed            $filesize The `filesize` value stored in the attachment metadata.
     681         * @param int<0, max>|null $expected The expected `filesizeInBytes` value, or null if it should not be set.
     682         */
     683        public function test_wp_prepare_attachment_for_js_filesize( $filesize, ?int $expected ) {
     684                $id = self::factory()->attachment->create_object(
     685                        array(
     686                                'file'           => 'test-image.jpg',
     687                                'post_title'     => 'Attachment Title',
     688                                'post_parent'    => 0,
     689                                'post_mime_type' => 'image/jpeg',
     690                        )
     691                );
     692                $this->assertIsInt( $id );
     693
     694                wp_update_attachment_metadata(
     695                        $id,
     696                        array(
     697                                'width'    => 50,
     698                                'height'   => 50,
     699                                'file'     => 'test-image.jpg',
     700                                'filesize' => $filesize,
     701                        )
     702                );
     703
     704                $post = get_post( $id );
     705                $this->assertInstanceOf( WP_Post::class, $post );
     706                $prepped = wp_prepare_attachment_for_js( $post );
     707                $this->assertIsArray( $prepped );
     708
     709                if ( null === $expected ) {
     710                        $this->assertArrayNotHasKey( 'filesizeInBytes', $prepped, 'The filesize should not have been set.' );
     711                        $this->assertArrayNotHasKey( 'filesizeHumanReadable', $prepped, 'The human readable filesize should not have been set.' );
     712                } else {
     713                        $this->assertSame( $expected, $prepped['filesizeInBytes'], 'The filesize was not normalized to an integer.' );
     714                        $this->assertSame( size_format( $expected ), $prepped['filesizeHumanReadable'], 'The human readable filesize did not match the normalized filesize.' );
     715                }
     716        }
     717
     718        /**
     719         * Data provider.
     720         *
     721         * @return array<non-falsy-string, array{ filesize: mixed, expected: int<0, max>|null }>
     722         */
     723        public function data_wp_prepare_attachment_for_js_filesize(): array {
     724                return array(
     725                        'an integer'                  => array(
     726                                'filesize' => 12345,
     727                                'expected' => 12345,
     728                        ),
     729                        'a numeric string'            => array(
     730                                'filesize' => '12345',
     731                                'expected' => 12345,
     732                        ),
     733                        'a float'                     => array(
     734                                'filesize' => 12345.6,
     735                                'expected' => 12345,
     736                        ),
     737                        'a float as a string'         => array(
     738                                'filesize' => '12345.6',
     739                                'expected' => 12345,
     740                        ),
     741                        'an exponential string'       => array(
     742                                'filesize' => '1e3',
     743                                'expected' => 1000,
     744                        ),
     745                        'a value smaller than a byte' => array(
     746                                'filesize' => 0.5,
     747                                'expected' => null,
     748                        ),
     749                        'zero'                        => array(
     750                                'filesize' => 0,
     751                                'expected' => null,
     752                        ),
     753                        'a negative integer'          => array(
     754                                'filesize' => -12345,
     755                                'expected' => null,
     756                        ),
     757                        'an empty string'             => array(
     758                                'filesize' => '',
     759                                'expected' => null,
     760                        ),
     761                        'a non-numeric string'        => array(
     762                                'filesize' => 'not-a-number',
     763                                'expected' => null,
     764                        ),
     765                        'an array'                    => array(
     766                                'filesize' => array( 12345 ),
     767                                'expected' => null,
     768                        ),
     769                        'null'                        => array(
     770                                'filesize' => null,
     771                                'expected' => null,
     772                        ),
     773                        'false'                       => array(
     774                                'filesize' => false,
     775                                'expected' => null,
     776                        ),
     777                        'true'                        => array(
     778                                'filesize' => true,
     779                                'expected' => null,
     780                        ),
     781                );
     782        }
     783
     784        /**
     785         * Tests that an unusable `filesize` in the attachment metadata falls back to the size of the file.
     786         *
     787         * @ticket 65686
     788         *
     789         * @dataProvider data_wp_prepare_attachment_for_js_filesize_falls_back_to_the_file
     790         *
     791         * @param mixed $filesize The `filesize` value stored in the attachment metadata.
     792         */
     793        public function test_wp_prepare_attachment_for_js_filesize_falls_back_to_the_file( $filesize ) {
     794                $id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/images/canola.jpg' );
     795                $this->assertIsInt( $id );
     796                $post = get_post( $id );
     797                $this->assertInstanceOf( WP_Post::class, $post );
     798                $file = get_attached_file( $id );
     799                $this->assertIsString( $file );
     800
     801                $meta = wp_get_attachment_metadata( $id );
     802                $this->assertIsArray( $meta );
     803                $meta['filesize'] = $filesize;
     804                wp_update_attachment_metadata( $id, $meta );
     805
     806                $prepped = wp_prepare_attachment_for_js( $post );
     807                $this->assertIsArray( $prepped );
     808                $this->assertArrayHasKey( 'filesizeInBytes', $prepped );
     809
     810                $this->assertSame( wp_filesize( $file ), $prepped['filesizeInBytes'] );
     811        }
     812
     813        /**
     814         * Data provider.
     815         *
     816         * @return array<non-falsy-string, array{ filesize: mixed }>
     817         */
     818        public function data_wp_prepare_attachment_for_js_filesize_falls_back_to_the_file(): array {
     819                return array(
     820                        'a value smaller than a byte' => array( 'filesize' => 0.5 ),
     821                        'zero'                        => array( 'filesize' => 0 ),
     822                        'a negative integer'          => array( 'filesize' => -12345 ),
     823                        'an empty string'             => array( 'filesize' => '' ),
     824                        'a non-numeric string'        => array( 'filesize' => 'not-a-number' ),
     825                        'an array'                    => array( 'filesize' => array( 12345 ) ),
     826                        'null'                        => array( 'filesize' => null ),
     827                        'false'                       => array( 'filesize' => false ),
     828                        'true'                        => array( 'filesize' => true ),
     829                );
    668830        }
    669831
Note: See TracChangeset for help on using the changeset viewer.