Image editor: fatal error when attachment metadata has no sizes array - #12744
Image editor: fatal error when attachment metadata has no sizes array#12744josephscott wants to merge 7 commits into
sizes array#12744Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @nazmulasif. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Pull request overview
Fixes a fatal error path in the image editor when an attachment’s metadata lacks a usable sizes array, ensuring wp_save_image() / wp_restore_image() can proceed without type errors and adding regression coverage in the Ajax image editor test suite.
Changes:
- Normalize
$meta['sizes']to an array inwp_save_image()before it’s passed toarray_merge(). - Normalize
$metaand$meta['sizes']inwp_restore_image()to avoid “Cannot use a scalar value as an array” fatals. - Add PHPUnit data-driven tests covering missing / scalar / null
sizesmetadata during save and restore flows.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
tests/phpunit/tests/ajax/wpAjaxImageEditor.php |
Adds regression tests (data provider + two test methods) to ensure saving/restoring doesn’t fatal when sizes metadata is missing or invalid. |
src/wp-admin/includes/image-edit.php |
Hardens wp_save_image() and wp_restore_image() against missing/non-array sizes metadata. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Test ReportPatch tested: #12744 Environment
Steps taken
Expected result
Additional Notes
Screenshots/Screencast with results
Support Content |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Weston Ruter <westonruter@gmail.com>
Co-authored-by: Weston Ruter <westonruter@gmail.com>
Co-authored-by: Weston Ruter <westonruter@gmail.com>
Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>
Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (3)
src/wp-admin/includes/image-edit.php:835
wp_restore_image()now normalizes$meta['sizes']to an array, but it can still fatal later ifsizesis an array containing non-array entries (e.g.array( 'thumbnail' => 'not-an-array' )), because the function accesses$meta['sizes'][ $default_size ]['file']without verifying the per-size value is an array. Consider filteringsizesto arrays only so corrupted entries don’t trigger a TypeError during restore.
if ( ! isset( $meta['sizes'] ) || ! is_array( $meta['sizes'] ) ) {
$meta['sizes'] = array();
}
src/wp-admin/includes/image-edit.php:997
wp_save_image()now ensures$meta['sizes']is an array, but ifsizesis an array with non-array entries (e.g.array( 'thumbnail' => 'not-an-array' )), the function later accesses$meta['sizes'][ $size ]['file']and can still fatal. Filtering$meta['sizes']to array entries here would prevent TypeErrors when metadata has been corrupted by plugins/filters.
if ( ! isset( $meta['sizes'] ) || ! is_array( $meta['sizes'] ) ) {
$meta['sizes'] = array();
}
tests/phpunit/tests/ajax/wpAjaxImageEditor.php:322
- The data provider covers missing/scalar
sizes, but not the case wheresizesis an array containing invalid (non-array) per-size entries (which can still lead to fatals when code accesses$meta['sizes'][ $size ]['file']). Adding a case likearray( 'sizes' => array( 'thumbnail' => 'not-an-array' ) )would protect against regressions for that failure mode.
'string sizes' => array( array( 'sizes' => 'not-an-array' ) ),
'boolean sizes' => array( array( 'sizes' => false ) ),
irozum
left a comment
There was a problem hiding this comment.
This fixes the reported fatal correctly: wp_save_image() no longer passes a non-array $meta['sizes'] into array_merge(), and the PR also closes the analogous gap in wp_restore_image() where the same malformed metadata would hit $meta['sizes'][ $default_size ] = $data and raise "Cannot use a scalar value as an array" (or the PHP 8.1+ deprecation/PHP 9 error for false). Good catch extending the fix there rather than stopping at the one line from the report.
Ran the full Tests_Ajax_wpAjaxImageEditor class (--group ajax) and the 65748 ticket group — 190 tests, all green, plus PHPCS lint:errors and PHPStan clean. Traced every remaining use of $meta['sizes'] after both new guards (the array_merge call and the foreach ( $default_sizes ...) loop in wp_restore_image) and confirmed they're all safe once the guard runs. No backward-compat concern here since the prior behavior was a fatal error — nothing could have been relying on it.
One thing I couldn't fully verify from the diff alone: the data provider only exercises sizes being absent, null, '', a non-array string, and false — it doesn't cover sizes being an array containing non-array entries (e.g. array( 'thumbnail' => 'not-an-array' )), which line 880's $meta['sizes'][ $default_size ]['file'] would presumably also choke on. Probably out of scope for this ticket's fatal (the report was specifically about $meta['sizes'] itself, not its members), but worth a quick gut-check on whether that's a live path.
https://core.trac.wordpress.org/ticket/65748
AI assistance: Yes
Tool(s): Claude
Model(s): Opus 5
Used for: Writing the tests
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.