Skip to content

Conversation

@ramonjd
Copy link
Member

@ramonjd ramonjd commented Oct 25, 2025

Problem

The prepare_item_for_database() method returns an object, but wp_slash() expects a string or array. Without casting the object to an array, wp_slash() would return the object unchanged, meaning string values within the object wouldn't be properly escaped for database insertion.

Props to @justlevine for spotting it

Solution

Cast the object to an array before passing it to wp_slash():

// Before
$new_attachment_id = wp_insert_attachment( wp_slash( $new_attachment_post ), $saved['path'], 0, true );

// After  
$new_attachment_id = wp_insert_attachment( wp_slash( (array) $new_attachment_post ), $saved['path'], 0, true );

Testing

Ensure no regressions to the media/edit endpoint.

  • In the block editor, add an image block, then edit the image using the cropper
  • Verify the image is saved correctly.
Kapture.2025-10-25.at.12.55.35.mp4

To be sure, you can copy that network request as cURL or fetch and add a title or description with apostrophes to the body payload, e.g.,

 "body": "{\"src\":\"http://localhost:4013/wp-content/uploads/2025/10/homemade-smash-burgers-1-edited.jpg\",\"modifiers\":[{\"type\":\"rotate\",\"args\":{\"angle\":90}}],\"title\": \"burger'licious\"}",

The results of wp_slash( (array) $new_attachment_post ) should be

Array ( 'post_title' => burger\\'licious...

Unit tests

Run the new unit test: npm run test:php -- --filter test_edit_image_wp_slash_with_object_cast
Run all attachment controller tests: npm run test:php -- --filter WP_Test_REST_Attachments_Controller

cc @justlevine

Trac ticket: https://core.trac.wordpress.org/ticket/64149


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.

@ramonjd ramonjd self-assigned this Oct 25, 2025
@ramonjd ramonjd added the bug label Oct 25, 2025
@github-actions
Copy link

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@github-actions
Copy link

github-actions bot commented Oct 26, 2025

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props ramonopoly, westonruter, mukesh27, justlevine.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. This is something else that would have been caught by static analysis. PHPStan would have blocked it, and even PhpStorm complains:

Image

@justlevine
Copy link

This is something else that would have been caught by static analysis. PHPStan would have blocked it,

@westonruter not hypothetical. it was caught by PHPstan , showed up when I was rebasing #7619 on trunk 🚀

@ramonjd
Copy link
Member Author

ramonjd commented Oct 26, 2025

Good catch. This is something else that would have been caught by static analysis. PHPStan would have blocked it, and even PhpStorm complains:

Thanks @westonruter and @justlevine - I need to double check my local set up.

ramonjd and others added 2 commits October 27, 2025 07:38
This change modifies the `wp_insert_attachment` call to ensure that the attachment post data is properly cast to an array before being passed to `wp_slash()`, enhancing data integrity during database insertion. Additionally, a new PHPUnit test is added to verify that the data is correctly slashed and formatted as an array, ensuring proper escaping of string values.
Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>
@ramonjd ramonjd force-pushed the fix/attachments-controller-cast-args-to-array-edit-media-item branch from 70288c0 to 6b25134 Compare October 26, 2025 20:39
@ramonjd
Copy link
Member Author

ramonjd commented Oct 26, 2025

Committed in r61065

@ramonjd ramonjd closed this Oct 26, 2025
@ramonjd ramonjd deleted the fix/attachments-controller-cast-args-to-array-edit-media-item branch October 26, 2025 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants