#65686 closed defect (bug) (fixed)
Normalize non-integer filesize attachment metadata in the admin
| Reported by: | mukesh27 | Owned by: | westonruter |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.1 |
| Component: | Media | Version: | 7.0 |
| Severity: | normal | Keywords: | has-patch |
| Cc: | Focuses: |
Description
Follow-up to [62815] / #65670, which normalized the stored filesize attachment metadata in WP_REST_Attachments_Controller::get_attachment_filesize(). The same unguarded read still exists in two admin-facing places:
wp_prepare_attachment_for_js()inwp-includes/media.phpattachment_submitbox_metadata()inwp-admin/includes/media.php
Both do:
if ( isset( $meta['filesize'] ) ) { $bytes = $meta['filesize']; } elseif ( file_exists( $attached_file ) ) { $bytes = wp_filesize( $attached_file ); }
Attachment metadata is untyped, and since [52837] the filesize key is commonly written by third-party code — offloading plugins (S3, GCS, CDNs) populate it from a remote storage API response, where it typically arrives as a string, and can be absent, empty, or non-numeric when the remote lookup fails.
Consequences today:
- A numeric string (
'123456') is passed through verbatim, sofilesizeInBytesis a string in the JSON handed to the media modal, while thewp_filesize()branch of the very same conditional yields anint. After [62815] the REST response for the same attachment is now anint, so the two representations of one value disagree. - A non-numeric string (
'unknown') is truthy, so thefile_exists()fallback is skipped even when the real file is present and readable.size_format()then returnsfalse, andfilesizeHumanReadableisfalse/ "File size:" renders empty — instead of the correct size that was onewp_filesize()call away. - Negative or zero-ish values (
-1,'0','') likewise suppress the fallback or produce a nonsensical size.
Proposed fix, mirroring [62815] exactly:
if ( isset( $meta['filesize'] ) && is_numeric( $meta['filesize'] ) && $meta['filesize'] > 0 ) { $bytes = (int) $meta['filesize']; } elseif ( file_exists( $attached_file ) ) { $bytes = wp_filesize( $attached_file ); }
Numeric values are trusted and cast; anything else falls through to the real file size. This keeps filesizeInBytes a non-negative-int, consistent with wp_filesize() (see [62813]) and with the REST filesize field.
No back-compat break is expected: the only behavior change for existing valid metadata is '123456' becoming 123456, which is what every consumer already assumes.
Change History (10)
This ticket was mentioned in PR #12632 on WordPress/wordpress-develop by @mukesh27.
4 months ago
#2
- Keywords has-patch added; needs-patch removed
This ticket was mentioned in Slack in #core-test by noruzzaman. View the logs.
3 months ago
@wildworks commented on PR #12632:
3 months ago
#4
@mukeshpanchal27, Is this PR ready for review?
@mukesh27 commented on PR #12632:
3 months ago
#5
Is this PR ready for review?
Yes @t-hamano
@westonruter commented on PR #12632:
3 months ago
#7
I tried using a plugin to filter the attachment metadata to use 'unknown' as the filesize:
<details><summary>Details</summary>
<?php /** * Plugin Name: Unknown Filesize Metadata * Plugin URI: https://core.trac.wordpress.org/ticket/65686 * Description: Filters attachment metadata to supply the non-numeric string 'unknown' as the filesize, simulating an offloading plugin whose remote storage lookup failed. See Trac #65686. * Version: 0.1.0 * Requires at least: 7.0 * Requires PHP: 7.2 * Author: Weston Ruter * License: GPLv2 or later * License URI: https://www.gnu.org/licenses/gpl-2.0.html * * @package UnknownFilesizeMetadata */ namespace UnknownFilesizeMetadata; /** * Supplies a non-numeric filesize in attachment metadata. * * @param mixed $data Attachment metadata. Normally an array, but other plugins may return anything. * @return mixed Filtered attachment metadata. */ function filter_wp_get_attachment_metadata( $data ) { if ( is_array( $data ) ) { $data['filesize'] = 'unknown'; } return $data; } add_filter( 'wp_get_attachment_metadata', __NAMESPACE__ . '\filter_wp_get_attachment_metadata' );
</details>
In the media modal, the file size correctly goes from false to the expected value as read from disk:
Before ❌ | After ✅
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Trac ticket: https://core.trac.wordpress.org/ticket/65686
## Use of AI Tools
AI assistance: Yes
Tool(s): Claude
Model(s): GPT-5.1
Used for: For drafting PR details only.