Skip to content

Commit f5a0517

Browse files
committed
Media / Attachments REST API endpoint: cast args to array before sending to wp_slash > wp_insert_attachment
This commit casts the object returned by `prepare_item_for_database()` to an array. Without doing so, `wp_slash()` returns the object unchanged, meaning string values within the object wouldn't be properly escaped for database insertion. Follow-up to [64035] Props ramonopoly, westonruter, mukesh27, justlevine. Fixes #64149. git-svn-id: https://develop.svn.wordpress.org/trunk@61065 602fd350-edb4-49c9-b593-d223f7449a82
1 parent bc3a185 commit f5a0517

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ public function edit_media_item( $request ) {
777777
$new_attachment_post->post_parent = $new_attachment_post->post_parent ?? 0;
778778

779779
// Insert the new attachment post.
780-
$new_attachment_id = wp_insert_attachment( wp_slash( $new_attachment_post ), $saved['path'], 0, true );
780+
$new_attachment_id = wp_insert_attachment( wp_slash( (array) $new_attachment_post ), $saved['path'], 0, true );
781781

782782
if ( is_wp_error( $new_attachment_id ) ) {
783783
if ( 'db_update_error' === $new_attachment_id->get_error_code() ) {

tests/phpunit/tests/rest-api/rest-attachments-controller.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3097,4 +3097,58 @@ public function test_edit_image_vertical_flip_only() {
30973097
// The controller converts the integer values to booleans: 0 !== (int) 1 = true.
30983098
$this->assertSame( array( true, false ), WP_Image_Editor_Mock::$spy['flip'][0], 'Vertical flip of the image is not identical.' );
30993099
}
3100+
3101+
/**
3102+
* Test that wp_slash() is properly applied when creating edited images.
3103+
*
3104+
* This test verifies that the object returned by prepare_item_for_database()
3105+
* is properly cast to an array before being passed to wp_slash(), ensuring
3106+
* that string values are properly escaped for database insertion.
3107+
*
3108+
* @ticket 64149
3109+
* @requires function imagejpeg
3110+
*/
3111+
public function test_edit_image_wp_slash_with_object_cast() {
3112+
wp_set_current_user( self::$superadmin_id );
3113+
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
3114+
3115+
// Create a mock to capture the data passed to wp_insert_attachment.
3116+
$captured_data = null;
3117+
3118+
// Mock wp_insert_attachment to capture the data being passed.
3119+
add_filter(
3120+
'wp_insert_attachment_data',
3121+
static function ( $data ) use ( &$captured_data ) {
3122+
$captured_data = $data;
3123+
return $data;
3124+
},
3125+
10,
3126+
1
3127+
);
3128+
3129+
$params = array(
3130+
'rotation' => 60,
3131+
'src' => wp_get_attachment_image_url( $attachment, 'full' ),
3132+
'title' => 'Test Title with "quotes" and \'apostrophes\'',
3133+
'caption' => 'Test Caption with "quotes" and \'apostrophes\'',
3134+
'description' => 'Test Description with "quotes" and \'apostrophes\'',
3135+
);
3136+
3137+
$request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" );
3138+
$request->set_body_params( $params );
3139+
$response = rest_do_request( $request );
3140+
3141+
$this->assertSame( 201, $response->get_status() );
3142+
3143+
// Verify that the data was properly slashed (escaped)
3144+
$this->assertNotNull( $captured_data, 'wp_insert_attachment was not called with data' );
3145+
3146+
// Check that quotes are properly escaped in the captured data.
3147+
$this->assertStringContainsString( 'Test Title with \"quotes\"', $captured_data['post_title'] ?? '', 'Title quotes not properly escaped' );
3148+
$this->assertStringContainsString( 'Test Caption with \"quotes\"', $captured_data['post_excerpt'] ?? '', 'Caption quotes not properly escaped' );
3149+
$this->assertStringContainsString( 'Test Description with \"quotes\"', $captured_data['post_content'] ?? '', 'Description quotes not properly escaped' );
3150+
3151+
// Verify that the data is an array (not an object).
3152+
$this->assertIsArray( $captured_data, 'Data passed to wp_insert_attachment should be an array' );
3153+
}
31003154
}

0 commit comments

Comments
 (0)