Make WordPress Core

Changeset 61065


Ignore:
Timestamp:
10/26/2025 09:06:35 PM (13 months ago)
Author:
ramonopoly
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r60917 r61065  
    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 ) ) {
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r60917 r61065  
    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}
Note: See TracChangeset for help on using the changeset viewer.