Make WordPress Core

Changeset 54527


Ignore:
Timestamp:
10/17/2022 11:29:55 AM (4 years ago)
Author:
audrasjb
Message:

Comments: Apply kses when editing comments.

Props davidbaumwald, xknown, peterwilsoncc, paulkevan.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r54489 r54527  
    25002500        }
    25012501
     2502        $filter_comment = false;
     2503        if ( ! has_filter( 'pre_comment_content', 'wp_filter_kses' ) ) {
     2504                $filter_comment = ! user_can( isset( $comment['user_id'] ) ? $comment['user_id'] : 0, 'unfiltered_html' );
     2505        }
     2506
     2507        if ( $filter_comment ) {
     2508                add_filter( 'pre_comment_content', 'wp_filter_kses' );
     2509        }
     2510
    25022511        // Escape data pulled from DB.
    25032512        $comment = wp_slash( $comment );
    … …  
    25092518
    25102519        $commentarr = wp_filter_comment( $commentarr );
     2520
     2521        if ( $filter_comment ) {
     2522                remove_filter( 'pre_comment_content', 'wp_filter_kses' );
     2523        }
    25112524
    25122525        // Now extract the merged array.
  • trunk/tests/phpunit/tests/comment.php

    r54489 r54527  
    8080                $comment = get_comment( $comments[0] );
    8181                $this->assertEquals( $post2->ID, $comment->comment_post_ID );
     82        }
     83
     84        public function test_update_comment_from_privileged_user_by_privileged_user() {
     85                $admin_id_1 = self::factory()->user->create( array( 'role' => 'administrator' ) );
     86                wp_set_current_user( $admin_id_1 );
     87
     88                $comment_id = wp_new_comment(
     89                        array(
     90                                'comment_post_ID'      => self::$post_id,
     91                                'comment_author'       => 'Author',
     92                                'comment_author_url'   => 'http://example.localhost/',
     93                                'comment_author_email' => 'test@test.com',
     94                                'user_id'              => $admin_id_1,
     95                                'comment_content'      => 'This is a comment',
     96                        )
     97                );
     98
     99                wp_set_current_user( 0 );
     100
     101                $admin_id_2 = self::factory()->user->create(
     102                        array(
     103                                'role'       => 'administrator',
     104                                'user_login' => 'test_wp_admin_get',
     105                                'user_pass'  => 'password',
     106                                'user_email' => 'testadmin@test.com',
     107                        )
     108                );
     109
     110                wp_set_current_user( $admin_id_2 );
     111
     112                wp_update_comment(
     113                        array(
     114                                'comment_ID'      => $comment_id,
     115                                'comment_content' => 'new comment <img onerror=demo src=x>',
     116                        )
     117                );
     118
     119                $comment          = get_comment( $comment_id );
     120                $expected_content = is_multisite()
     121                        ? 'new comment '
     122                        : 'new comment <img onerror=demo src=x>';
     123
     124                $this->assertSame( $expected_content, $comment->comment_content );
     125
     126                wp_set_current_user( 0 );
     127        }
     128
     129        public function test_update_comment_from_unprivileged_user_by_privileged_user() {
     130                wp_set_current_user( self::$user_id );
     131
     132                $comment_id = wp_new_comment(
     133                        array(
     134                                'comment_post_ID'      => self::$post_id,
     135                                'comment_author'       => 'Author',
     136                                'comment_author_url'   => 'http://example.localhost/',
     137                                'comment_author_email' => 'test@test.com',
     138                                'user_id'              => self::$user_id,
     139                                'comment_content'      => '<a href="http://example.localhost/something.html">click</a>',
     140                        )
     141                );
     142
     143                wp_set_current_user( 0 );
     144
     145                $admin_id = self::factory()->user->create(
     146                        array(
     147                                'role'       => 'administrator',
     148                                'user_login' => 'test_wp_admin_get',
     149                                'user_pass'  => 'password',
     150                                'user_email' => 'testadmin@test.com',
     151                        )
     152                );
     153
     154                wp_set_current_user( $admin_id );
     155
     156                wp_update_comment(
     157                        array(
     158                                'comment_ID'      => $comment_id,
     159                                'comment_content' => '<a href="http://example.localhost/something.html" disallowed=attribute>click</a>',
     160                        )
     161                );
     162
     163                $comment = get_comment( $comment_id );
     164                $this->assertEquals( '<a href="http://example.localhost/something.html" rel="nofollow ugc">click</a>', $comment->comment_content, 'Comment: ' . $comment->comment_content );
     165                wp_set_current_user( 0 );
    82166        }
    83167
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r54090 r54527  
    29732973                                        'author_name'       => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
    29742974                                        'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     2975                                        'author'            => self::$editor_id,
    29752976                                ),
    29762977                                array(
    … …  
    29812982                                        'author_name'       => 'div strong',
    29822983                                        'author_user_agent' => 'div strong',
     2984                                        'author'            => self::$editor_id,
    29832985                                )
    29842986                        );
    … …  
    29902992                                        'author_name'       => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
    29912993                                        'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     2994                                        'author'            => self::$editor_id,
    29922995                                ),
    29932996                                array(
    … …  
    29983001                                        'author_name'       => 'div strong',
    29993002                                        'author_user_agent' => 'div strong',
     3003                                        'author'            => self::$editor_id,
    30003004                                )
    30013005                        );
    … …  
    30123016                                'author_name'       => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
    30133017                                'author_user_agent' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
     3018                                'author'            => self::$superadmin_id,
    30143019                        ),
    30153020                        array(
    … …  
    30203025                                'author_name'       => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
    30213026                                'author_user_agent' => '\\\&\\\ &amp; &invalid; &lt; &lt; &amp;lt;',
     3027                                'author'            => self::$superadmin_id,
    30223028                        )
    30233029                );
    … …  
    30333039                                'author_name'       => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
    30343040                                'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     3041                                'author'            => self::$superadmin_id,
    30353042                        ),
    30363043                        array(
    … …  
    30413048                                'author_name'       => 'div strong',
    30423049                                'author_user_agent' => 'div strong',
     3050                                'author'            => self::$superadmin_id,
    30433051                        )
    30443052                );
Note: See TracChangeset for help on using the changeset viewer.