Make WordPress Core

Ticket #38704: 38704.diff

File 38704.diff, 14.5 KB (added by jnylen0, 9 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
    index e6f7920..3eb8737 100644
    a b class WP_REST_Comments_Controller extends WP_REST_Controller { 
    520520                 */
    521521                $prepared_comment = apply_filters( 'rest_pre_insert_comment', $prepared_comment, $request );
    522522
    523                 $comment_id = wp_insert_comment( $prepared_comment );
     523                $comment_id = wp_insert_comment( wp_filter_comment( wp_slash( (array) $prepared_comment ) ) );
    524524
    525525                if ( ! $comment_id ) {
    526526                        return new WP_Error( 'rest_comment_failed_create', __( 'Creating comment failed.' ), array( 'status' => 500 ) );
    class WP_REST_Comments_Controller extends WP_REST_Controller { 
    644644                                return new WP_Error( $error_code, __( 'Comment field exceeds maximum length allowed.' ), array( 'status' => 400 ) );
    645645                        }
    646646
    647                         $updated = wp_update_comment( $prepared_args );
     647                        $updated = wp_update_comment( wp_slash( (array) $prepared_args ) );
    648648
    649649                        if ( 0 === $updated ) {
    650650                                return new WP_Error( 'rest_comment_failed_edit', __( 'Updating comment failed.' ), array( 'status' => 500 ) );
    class WP_REST_Comments_Controller extends WP_REST_Controller { 
    995995                 * the 'content.raw' properties of the Request object.
    996996                 */
    997997                if ( isset( $request['content'] ) && is_string( $request['content'] ) ) {
    998                         $prepared_comment['comment_content'] = wp_filter_kses( $request['content'] );
     998                        $prepared_comment['comment_content'] = $request['content'];
    999999                } elseif ( isset( $request['content']['raw'] ) && is_string( $request['content']['raw'] ) ) {
    1000                         $prepared_comment['comment_content'] = wp_filter_kses( $request['content']['raw'] );
     1000                        $prepared_comment['comment_content'] = $request['content']['raw'];
    10011001                }
    10021002
    10031003                if ( isset( $request['post'] ) ) {
  • tests/phpunit/tests/rest-api/rest-comments-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php
    index a8526c8..5abccb5 100644
    a b  
    1010  * @group restapi
    1111  */
    1212class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase {
    13 
     13        protected static $superadmin_id;
    1414        protected static $admin_id;
     15        protected static $editor_id;
    1516        protected static $subscriber_id;
    1617        protected static $author_id;
    1718
    class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    2526        protected $endpoint;
    2627
    2728        public static function wpSetUpBeforeClass( $factory ) {
     29                self::$superadmin_id = $factory->user->create( array(
     30                        'role'       => 'administrator',
     31                        'user_login' => 'superadmin',
     32                ) );
    2833                self::$admin_id = $factory->user->create( array(
    2934                        'role' => 'administrator',
    3035                ) );
     36                self::$editor_id = $factory->user->create( array(
     37                        'role' => 'editor',
     38                ) );
    3139                self::$subscriber_id = $factory->user->create( array(
    3240                        'role' => 'subscriber',
    3341                ) );
    class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    7987        public function setUp() {
    8088                parent::setUp();
    8189                $this->endpoint = new WP_REST_Comments_Controller;
     90                if ( is_multisite() ) {
     91                        update_site_option( 'site_admins', array( 'superadmin' ) );
     92                }
    8293        }
    8394
    8495        public function tearDown() {
    class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase 
    18801891                $this->assertErrorResponse( 'comment_content_column_length', $response, 400 );
    18811892        }
    18821893
     1894        public function verify_comment_roundtrip( $input = array(), $expected_output = array() ) {
     1895                // Create the comment
     1896                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1897                $request->set_param( 'author_email', 'cbg@androidsdungeon.com' );
     1898                $request->set_param( 'post', self::$post_id );
     1899                foreach ( $input as $name => $value ) {
     1900                        $request->set_param( $name, $value );
     1901                }
     1902                $response = $this->server->dispatch( $request );
     1903                $this->assertEquals( 201, $response->get_status() );
     1904                $actual_output = $response->get_data();
     1905
     1906                // Compare expected API output to actual API output
     1907                $this->assertEquals( $expected_output['content']['raw']     , $actual_output['content']['raw'] );
     1908                $this->assertEquals( $expected_output['content']['rendered'], trim( $actual_output['content']['rendered'] ) );
     1909                $this->assertEquals( $expected_output['author_name']        , $actual_output['author_name'] );
     1910                $this->assertEquals( $expected_output['author_user_agent']  , $actual_output['author_user_agent'] );
     1911
     1912                // Compare expected API output to WP internal values
     1913                $comment = get_comment( $actual_output['id'] );
     1914                $this->assertEquals( $expected_output['content']['raw']   , $comment->comment_content );
     1915                $this->assertEquals( $expected_output['author_name']      , $comment->comment_author );
     1916                $this->assertEquals( $expected_output['author_user_agent'], $comment->comment_agent );
     1917
     1918                // Update the comment
     1919                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $actual_output['id'] ) );
     1920                foreach ( $input as $name => $value ) {
     1921                        $request->set_param( $name, $value );
     1922                }
     1923                // FIXME at least one value must change, or update fails
     1924                // See https://core.trac.wordpress.org/ticket/38700
     1925                $request->set_param( 'author_ip', '127.0.0.2' );
     1926                $response = $this->server->dispatch( $request );
     1927                $this->assertEquals( 200, $response->get_status() );
     1928                $actual_output = $response->get_data();
     1929
     1930                // Compare expected API output to actual API output
     1931                $this->assertEquals( $expected_output['content']['raw']     , $actual_output['content']['raw'] );
     1932                $this->assertEquals( $expected_output['content']['rendered'], trim( $actual_output['content']['rendered'] ) );
     1933                $this->assertEquals( $expected_output['author_name']        , $actual_output['author_name'] );
     1934                $this->assertEquals( $expected_output['author_user_agent']  , $actual_output['author_user_agent'] );
     1935
     1936                // Compare expected API output to WP internal values
     1937                $comment = get_comment( $actual_output['id'] );
     1938                $this->assertEquals( $expected_output['content']['raw']   , $comment->comment_content );
     1939                $this->assertEquals( $expected_output['author_name']      , $comment->comment_author );
     1940                $this->assertEquals( $expected_output['author_user_agent'], $comment->comment_agent );
     1941        }
     1942
     1943        public function test_comment_roundtrip_as_editor_1() {
     1944                wp_set_current_user( self::$editor_id );
     1945                $this->assertEquals( ! is_multisite(), current_user_can( 'unfiltered_html' ) );
     1946                $this->verify_comment_roundtrip( array(
     1947                        'content'           => '\o/ ¯\_(ツ)_/¯ 🚢',
     1948                        'author_name'       => '\o/ ¯\_(ツ)_/¯ 🚢',
     1949                        'author_user_agent' => '\o/ ¯\_(ツ)_/¯ 🚢',
     1950                ), array(
     1951                        'content' => array(
     1952                                'raw'      => '\o/ ¯\_(ツ)_/¯ 🚢',
     1953                                'rendered' => '<p>\o/ ¯\_(ツ)_/¯ 🚢</p>',
     1954                        ),
     1955                        'author_name'       => '\o/ ¯\_(ツ)_/¯ 🚢',
     1956                        'author_user_agent' => '\o/ ¯\_(ツ)_/¯ 🚢',
     1957                ) );
     1958        }
     1959
     1960        public function test_comment_roundtrip_as_editor_2() {
     1961                wp_set_current_user( self::$editor_id );
     1962                if ( is_multisite() ) {
     1963                        $this->assertFalse( current_user_can( 'unfiltered_html' ) );
     1964                        $this->verify_comment_roundtrip( array(
     1965                                'content'           => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
     1966                                'author_name'       => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
     1967                                'author_user_agent' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
     1968                        ), array(
     1969                                'content' => array(
     1970                                        'raw'      => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
     1971                                        'rendered' => '<p>\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;</p>',
     1972                                ),
     1973                                'author_name'       => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
     1974                                'author_user_agent' => '\\\&\\\ &amp; &invalid; &lt; &lt; &amp;lt;',
     1975                        ) );
     1976                } else {
     1977                        $this->assertTrue( current_user_can( 'unfiltered_html' ) );
     1978                        $this->verify_comment_roundtrip( array(
     1979                                'content'           => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
     1980                                'author_name'       => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
     1981                                'author_user_agent' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
     1982                        ), array(
     1983                                'content' => array(
     1984                                        'raw'      => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
     1985                                        'rendered' => '<p>\\\&#038;\\\ &amp; &invalid; < &lt; &amp;lt;' . "\n</p>",
     1986                                ),
     1987                                'author_name'       => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
     1988                                'author_user_agent' => '\\\&\\\ &amp; &invalid; &lt; &lt; &amp;lt;',
     1989                        ) );
     1990                }
     1991        }
     1992
     1993        public function test_comment_roundtrip_as_editor_unfiltered_html_1() {
     1994                wp_set_current_user( self::$editor_id );
     1995                if ( is_multisite() ) {
     1996                        $this->assertFalse( current_user_can( 'unfiltered_html' ) );
     1997                        $this->verify_comment_roundtrip( array(
     1998                                'content'           => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     1999                                'author_name'       => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     2000                                'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     2001                        ), array(
     2002                                'content' => array(
     2003                                        'raw'      => 'div <strong>strong</strong> oh noes',
     2004                                        'rendered' => '<p>div <strong>strong</strong> oh noes</p>',
     2005                                ),
     2006                                'author_name'       => 'div strong',
     2007                                'author_user_agent' => 'div strong',
     2008                        ) );
     2009                } else {
     2010                        $this->assertTrue( current_user_can( 'unfiltered_html' ) );
     2011                        $this->verify_comment_roundtrip( array(
     2012                                'content'           => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     2013                                'author_name'       => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     2014                                'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     2015                        ), array(
     2016                                'content' => array(
     2017                                        'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     2018                                        'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
     2019                                ),
     2020                                'author_name'       => 'div strong',
     2021                                'author_user_agent' => 'div strong',
     2022                        ) );
     2023                }
     2024        }
     2025
     2026        public function test_comment_roundtrip_as_editor_unfiltered_html_2() {
     2027                wp_set_current_user( self::$editor_id );
     2028                if ( is_multisite() ) {
     2029                        $this->assertFalse( current_user_can( 'unfiltered_html' ) );
     2030                        $this->verify_comment_roundtrip( array(
     2031                                'content'           => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
     2032                                'author_name'       => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
     2033                                'author_user_agent' => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
     2034                        ), array(
     2035                                'content' => array(
     2036                                        'raw'      => '<a href="#" rel="nofollow">link</a>',
     2037                                        'rendered' => '<p><a href="#" rel="nofollow">link</a></p>',
     2038                                ),
     2039                                'author_name'       => 'link',
     2040                                'author_user_agent' => 'link',
     2041                        ) );
     2042                } else {
     2043                        $this->assertTrue( current_user_can( 'unfiltered_html' ) );
     2044                        $this->verify_comment_roundtrip( array(
     2045                                'content'           => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
     2046                                'author_name'       => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
     2047                                'author_user_agent' => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
     2048                        ), array(
     2049                                'content' => array(
     2050                                        'raw'      => '<a href="#" target="_blank" data-unfiltered=true rel="nofollow">link</a>',
     2051                                        'rendered' => '<p><a href="#" target="_blank" data-unfiltered=true rel="nofollow">link</a></p>',
     2052                                ),
     2053                                'author_name'       => 'link',
     2054                                'author_user_agent' => 'link',
     2055                        ) );
     2056                }
     2057        }
     2058
     2059        public function test_comment_roundtrip_as_superadmin_1() {
     2060                wp_set_current_user( self::$superadmin_id );
     2061                $this->assertTrue( current_user_can( 'unfiltered_html' ) );
     2062                $this->verify_comment_roundtrip( array(
     2063                        'content'           => '\o/ ¯\_(ツ)_/¯ 🚢',
     2064                        'author_name'       => '\o/ ¯\_(ツ)_/¯ 🚢',
     2065                        'author_user_agent' => '\o/ ¯\_(ツ)_/¯ 🚢',
     2066                ), array(
     2067                        'content' => array(
     2068                                'raw'      => '\o/ ¯\_(ツ)_/¯ 🚢',
     2069                                'rendered' => '<p>\o/ ¯\_(ツ)_/¯ 🚢</p>',
     2070                        ),
     2071                        'author_name'       => '\o/ ¯\_(ツ)_/¯ 🚢',
     2072                        'author_user_agent' => '\o/ ¯\_(ツ)_/¯ 🚢',
     2073                ) );
     2074        }
     2075
     2076        public function test_comment_roundtrip_as_superadmin_2() {
     2077                wp_set_current_user( self::$superadmin_id );
     2078                $this->assertTrue( current_user_can( 'unfiltered_html' ) );
     2079                $this->verify_comment_roundtrip( array(
     2080                        'content'           => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
     2081                        'author_name'       => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
     2082                        'author_user_agent' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
     2083                ), array(
     2084                        'content' => array(
     2085                                'raw'      => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
     2086                                'rendered' => '<p>\\\&#038;\\\ &amp; &invalid; < &lt; &amp;lt;' . "\n</p>",
     2087                        ),
     2088                        'author_name'       => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
     2089                        'author_user_agent' => '\\\&\\\ &amp; &invalid; &lt; &lt; &amp;lt;',
     2090                ) );
     2091        }
     2092
     2093        public function test_comment_roundtrip_as_superadmin_unfiltered_html_1() {
     2094                wp_set_current_user( self::$superadmin_id );
     2095                $this->assertTrue( current_user_can( 'unfiltered_html' ) );
     2096                $this->verify_comment_roundtrip( array(
     2097                        'content'           => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     2098                        'author_name'       => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     2099                        'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     2100                ), array(
     2101                        'content' => array(
     2102                                'raw'      => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
     2103                                'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
     2104                        ),
     2105                        'author_name'       => 'div strong',
     2106                        'author_user_agent' => 'div strong',
     2107                ) );
     2108        }
     2109
     2110        public function test_comment_roundtrip_as_superadmin_unfiltered_html_2() {
     2111                wp_set_current_user( self::$superadmin_id );
     2112                $this->assertTrue( current_user_can( 'unfiltered_html' ) );
     2113                $this->verify_comment_roundtrip( array(
     2114                        'content'           => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
     2115                        'author_name'       => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
     2116                        'author_user_agent' => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
     2117                ), array(
     2118                        'content' => array(
     2119                                'raw'      => '<a href="#" target="_blank" data-unfiltered=true rel="nofollow">link</a>',
     2120                                'rendered' => '<p><a href="#" target="_blank" data-unfiltered=true rel="nofollow">link</a></p>',
     2121                        ),
     2122                        'author_name'       => 'link',
     2123                        'author_user_agent' => 'link',
     2124                ) );
     2125        }
     2126
    18832127        public function test_delete_item() {
    18842128                wp_set_current_user( self::$admin_id );
    18852129