Make WordPress Core

Changeset 54537


Ignore:
Timestamp:
10/17/2022 12:31:51 PM (18 months ago)
Author:
audrasjb
Message:

Comments: Apply kses when editing comments.

Props davidbaumwald, xknown, peterwilsoncc, paulkevan.
Merges [54527] to the 6.0 branch.

Location:
branches/6.0
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/6.0

  • branches/6.0/src/wp-includes/comment.php

    r53299 r54537  
    24652465    }
    24662466
     2467    $filter_comment = false;
     2468    if ( ! has_filter( 'pre_comment_content', 'wp_filter_kses' ) ) {
     2469        $filter_comment = ! user_can( isset( $comment['user_id'] ) ? $comment['user_id'] : 0, 'unfiltered_html' );
     2470    }
     2471
     2472    if ( $filter_comment ) {
     2473        add_filter( 'pre_comment_content', 'wp_filter_kses' );
     2474    }
     2475
    24672476    // Escape data pulled from DB.
    24682477    $comment = wp_slash( $comment );
     
    24742483
    24752484    $commentarr = wp_filter_comment( $commentarr );
     2485
     2486    if ( $filter_comment ) {
     2487        remove_filter( 'pre_comment_content', 'wp_filter_kses' );
     2488    }
    24762489
    24772490    // Now extract the merged array.
  • branches/6.0/tests/phpunit/tests/comment.php

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

    r52389 r54537  
    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.