Make WordPress Core


Ignore:
Timestamp:
05/16/2023 02:26:01 PM (3 years ago)
Author:
audrasjb
Message:

Media: Prevent CSRF setting attachment thumbnails.

Props martinkrcho, paulkevan, peterwilsoncc, xknown, peterwilsoncc.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php

    r54722 r55764  
    102102                $this->assertSame( $expected, $response['data'] );
    103103        }
     104
     105        public function test_wp_ajax_set_attachment_thumbnail_success() {
     106                // Become an administrator.
     107                $post    = $_POST;
     108                $user_id = self::factory()->user->create(
     109                        array(
     110                                'role'       => 'administrator',
     111                                'user_login' => 'user_36578_administrator',
     112                                'user_email' => 'user_36578_administrator@example.com',
     113                        )
     114                );
     115                wp_set_current_user( $user_id );
     116                $_POST = array_merge( $_POST, $post );
     117
     118                // Upload the attachment itself.
     119                $filename = DIR_TESTDATA . '/uploads/small-audio.mp3';
     120                $contents = file_get_contents( $filename );
     121
     122                $upload     = wp_upload_bits( wp_basename( $filename ), null, $contents );
     123                $attachment = $this->_make_attachment( $upload );
     124
     125                // Upload the thumbnail.
     126                $filename = DIR_TESTDATA . '/images/waffles.jpg';
     127                $contents = file_get_contents( $filename );
     128
     129                $upload    = wp_upload_bits( wp_basename( $filename ), null, $contents );
     130                $thumbnail = $this->_make_attachment( $upload );
     131
     132                // Set up a default request.
     133                $_POST['_ajax_nonce']  = wp_create_nonce( 'set-attachment-thumbnail' );
     134                $_POST['thumbnail_id'] = $thumbnail;
     135                $_POST['urls']         = array( wp_get_attachment_url( $attachment ) );
     136
     137                // Make the request.
     138                try {
     139                        $this->_handleAjax( 'set-attachment-thumbnail' );
     140                } catch ( WPAjaxDieContinueException $e ) {
     141                        unset( $e );
     142                }
     143
     144                // Get the response.
     145                $response = json_decode( $this->_last_response, true );
     146
     147                // Ensure everything is correct.
     148                $this->assertTrue( $response['success'] );
     149        }
     150
     151        public function test_wp_ajax_set_attachment_thumbnail_missing_nonce() {
     152                // Become an administrator.
     153                $post    = $_POST;
     154                $user_id = self::factory()->user->create(
     155                        array(
     156                                'role'       => 'administrator',
     157                                'user_login' => 'user_36578_administrator',
     158                                'user_email' => 'user_36578_administrator@example.com',
     159                        )
     160                );
     161                wp_set_current_user( $user_id );
     162                $_POST = array_merge( $_POST, $post );
     163
     164                // Upload the attachment itself.
     165                $filename = DIR_TESTDATA . '/uploads/small-audio.mp3';
     166                $contents = file_get_contents( $filename );
     167
     168                $upload     = wp_upload_bits( wp_basename( $filename ), null, $contents );
     169                $attachment = $this->_make_attachment( $upload );
     170
     171                // Upload the thumbnail.
     172                $filename = DIR_TESTDATA . '/images/waffles.jpg';
     173                $contents = file_get_contents( $filename );
     174
     175                $upload    = wp_upload_bits( wp_basename( $filename ), null, $contents );
     176                $thumbnail = $this->_make_attachment( $upload );
     177
     178                // Set up a default request.
     179                $_POST['thumbnail_id'] = $thumbnail;
     180                $_POST['urls']         = array( wp_get_attachment_url( $attachment ) );
     181
     182                // Make the request.
     183                try {
     184                        $this->_handleAjax( 'set-attachment-thumbnail' );
     185                } catch ( WPAjaxDieContinueException $e ) {
     186                        unset( $e );
     187                }
     188
     189                // Get the response.
     190                $response = json_decode( $this->_last_response, true );
     191
     192                // Check that success is false without sending nonce.
     193                $this->assertFalse( $response['success'] );
     194        }
    104195}
Note: See TracChangeset for help on using the changeset viewer.