Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (9 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/kses.php

    r40637 r42343  
    1616        $attributes = array(
    1717            'class' => 'classname',
    18             'id' => 'id',
     18            'id'    => 'id',
    1919            'style' => 'color: red;',
    2020            'style' => 'color: red',
     
    2525
    2626        foreach ( $attributes as $name => $value ) {
    27             $string = "<address $name='$value'>1 WordPress Avenue, The Internet.</address>";
     27            $string        = "<address $name='$value'>1 WordPress Avenue, The Internet.</address>";
    2828            $expect_string = "<address $name='" . str_replace( '; ', ';', trim( $value, ';' ) ) . "'>1 WordPress Avenue, The Internet.</address>";
    2929            $this->assertEquals( $expect_string, wp_kses( $string, $allowedposttags ) );
     
    3838
    3939        $attributes = array(
    40             'class' => 'classname',
    41             'id' => 'id',
    42             'style' => 'color: red;',
    43             'title' => 'title',
    44             'href' => 'http://example.com',
    45             'rel' => 'related',
    46             'rev' => 'revision',
    47             'name' => 'name',
     40            'class'  => 'classname',
     41            'id'     => 'id',
     42            'style'  => 'color: red;',
     43            'title'  => 'title',
     44            'href'   => 'http://example.com',
     45            'rel'    => 'related',
     46            'rev'    => 'revision',
     47            'name'   => 'name',
    4848            'target' => '_blank',
    4949        );
    5050
    5151        foreach ( $attributes as $name => $value ) {
    52             $string = "<a $name='$value'>I link this</a>";
     52            $string        = "<a $name='$value'>I link this</a>";
    5353            $expect_string = "<a $name='" . trim( $value, ';' ) . "'>I link this</a>";
    5454            $this->assertEquals( $expect_string, wp_kses( $string, $allowedposttags ) );
     
    6464        $attributes = array(
    6565            'class' => 'classname',
    66             'id' => 'id',
     66            'id'    => 'id',
    6767            'style' => 'color: red;',
    6868            'title' => 'title',
     
    7070
    7171        foreach ( $attributes as $name => $value ) {
    72             $string = "<abbr $name='$value'>WP</abbr>";
     72            $string        = "<abbr $name='$value'>WP</abbr>";
    7373            $expect_string = "<abbr $name='" . trim( $value, ';' ) . "'>WP</abbr>";
    7474            $this->assertEquals( $expect_string, wp_kses( $string, $allowedposttags ) );
     
    143143            if ( ! empty( $result ) && $result != 'alert(1);' && $result != 'alert(1)' ) {
    144144                switch ( $k ) {
    145                     case 6: $this->assertEquals( 'javascript&amp;#0000058alert(1);', $result ); break;
     145                    case 6:
     146                        $this->assertEquals( 'javascript&amp;#0000058alert(1);', $result );
     147                        break;
    146148                    case 12:
    147149                        $this->assertEquals( str_replace( '&', '&amp;', $x ), $result );
    148150                        break;
    149                     case 22: $this->assertEquals( 'javascript&amp;#0000058alert(1);', $result ); break;
    150                     case 23: $this->assertEquals( 'javascript&amp;#0000058alert(1)//?:', $result ); break;
    151                     case 24: $this->assertEquals( 'feed:alert(1)', $result ); break;
    152                     default: $this->fail( "wp_kses_bad_protocol failed on $x. Result: $result" );
     151                    case 22:
     152                        $this->assertEquals( 'javascript&amp;#0000058alert(1);', $result );
     153                        break;
     154                    case 23:
     155                        $this->assertEquals( 'javascript&amp;#0000058alert(1)//?:', $result );
     156                        break;
     157                    case 24:
     158                        $this->assertEquals( 'feed:alert(1)', $result );
     159                        break;
     160                    default:
     161                        $this->fail( "wp_kses_bad_protocol failed on $x. Result: $result" );
    153162                }
    154163            }
     
    167176        foreach ( $safe as $x ) {
    168177            $result = wp_kses_bad_protocol( wp_kses_normalize_entities( $x ), array( 'http', 'https', 'dummy' ) );
    169             if ( $result != $x && $result != 'http://example.org/' )
     178            if ( $result != $x && $result != 'http://example.org/' ) {
    170179                $this->fail( "wp_kses_bad_protocol incorrectly blocked $x" );
     180            }
    171181        }
    172182    }
     
    175185        $xss = simplexml_load_file( DIR_TESTDATA . '/formatting/xssAttacks.xml' );
    176186        foreach ( $xss->attack as $attack ) {
    177             if ( in_array( $attack->name, array( 'IMG Embedded commands 2', 'US-ASCII encoding', 'OBJECT w/Flash 2', 'Character Encoding Example' ) ) )
     187            if ( in_array( $attack->name, array( 'IMG Embedded commands 2', 'US-ASCII encoding', 'OBJECT w/Flash 2', 'Character Encoding Example' ) ) ) {
    178188                continue;
     189            }
    179190
    180191            $code = (string) $attack->code;
    181192
    182             if ( $code == 'See Below' )
     193            if ( $code == 'See Below' ) {
    183194                continue;
     195            }
    184196
    185197            if ( substr( $code, 0, 4 ) == 'perl' ) {
    186                 $pos = strpos( $code, '"' ) + 1;
    187                 $code = substr( $code, $pos, strrpos($code, '"') - $pos );
     198                $pos  = strpos( $code, '"' ) + 1;
     199                $code = substr( $code, $pos, strrpos( $code, '"' ) - $pos );
    188200                $code = str_replace( '\0', "\0", $code );
    189201            }
     
    191203            $result = trim( wp_kses_data( $code ) );
    192204
    193             if ( $result == '' || $result == 'XSS' || $result == 'alert("XSS");' || $result == "alert('XSS');" )
     205            if ( $result == '' || $result == 'XSS' || $result == 'alert("XSS");' || $result == "alert('XSS');" ) {
    194206                continue;
     207            }
    195208
    196209            switch ( $attack->name ) {
    197210                case 'XSS Locator':
    198                     $this->assertEquals('\';alert(String.fromCharCode(88,83,83))//\\\';alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//\\";alert(String.fromCharCode(88,83,83))//--&gt;"&gt;\'&gt;alert(String.fromCharCode(88,83,83))=&amp;{}', $result);
     211                    $this->assertEquals( '\';alert(String.fromCharCode(88,83,83))//\\\';alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//\\";alert(String.fromCharCode(88,83,83))//--&gt;"&gt;\'&gt;alert(String.fromCharCode(88,83,83))=&amp;{}', $result );
    199212                    break;
    200213                case 'XSS Quick Test':
    201                     $this->assertEquals('\'\';!--"=&amp;{()}', $result);
     214                    $this->assertEquals( '\'\';!--"=&amp;{()}', $result );
    202215                    break;
    203216                case 'SCRIPT w/Alert()':
     
    205218                    break;
    206219                case 'SCRIPT w/Char Code':
    207                     $this->assertEquals('alert(String.fromCharCode(88,83,83))', $result);
     220                    $this->assertEquals( 'alert(String.fromCharCode(88,83,83))', $result );
    208221                    break;
    209222                case 'IMG STYLE w/expression':
    210                     $this->assertEquals('exp/*', $result);
     223                    $this->assertEquals( 'exp/*', $result );
    211224                    break;
    212225                case 'List-style-image':
    213                     $this->assertEquals('li {list-style-image: url("javascript:alert(\'XSS\')");}XSS', $result);
     226                    $this->assertEquals( 'li {list-style-image: url("javascript:alert(\'XSS\')");}XSS', $result );
    214227                    break;
    215228                case 'STYLE':
    216                     $this->assertEquals( "alert('XSS');", $result);
     229                    $this->assertEquals( "alert('XSS');", $result );
    217230                    break;
    218231                case 'STYLE w/background-image':
    219                     $this->assertEquals('.XSS{background-image:url("javascript:alert(\'XSS\')");}<A></A>', $result);
     232                    $this->assertEquals( '.XSS{background-image:url("javascript:alert(\'XSS\')");}<A></A>', $result );
    220233                    break;
    221234                case 'STYLE w/background':
    222                     $this->assertEquals('BODY{background:url("javascript:alert(\'XSS\')")}', $result);
     235                    $this->assertEquals( 'BODY{background:url("javascript:alert(\'XSS\')")}', $result );
    223236                    break;
    224237                case 'Remote Stylesheet 2':
     
    229242                    break;
    230243                case 'Remote Stylesheet 4':
    231                     $this->assertEquals('BODY{-moz-binding:url("http://ha.ckers.org/xssmoz.xml#xss")}', $result);
     244                    $this->assertEquals( 'BODY{-moz-binding:url("http://ha.ckers.org/xssmoz.xml#xss")}', $result );
    232245                    break;
    233246                case 'XML data island w/CDATA':
    234                     $this->assertEquals( "&lt;![CDATA[]]&gt;", $result );
     247                    $this->assertEquals( '&lt;![CDATA[]]&gt;', $result );
    235248                    break;
    236249                case 'XML data island w/comment':
     
    256269                    break;
    257270                case 'Escaping JavaScript escapes':
    258                     $this->assertEquals('\";alert(\'XSS\');//', $result);
     271                    $this->assertEquals( '\";alert(\'XSS\');//', $result );
    259272                    break;
    260273                case 'STYLE w/broken up JavaScript':
     
    277290                    break;
    278291                case 'Malformed IMG Tags':
    279                     $this->assertEquals('alert("XSS")"&gt;', $result);
     292                    $this->assertEquals( 'alert("XSS")"&gt;', $result );
    280293                    break;
    281294                case 'No Quotes/Semicolons':
     
    304317
    305318    function _wp_kses_allowed_html_filter( $html, $context ) {
    306         if ( 'post' == $context )
     319        if ( 'post' == $context ) {
    307320            return array( 'a' => array( 'href' => true ) );
    308         else
     321        } else {
    309322            return array( 'a' => array( 'href' => false ) );
     323        }
    310324    }
    311325
     
    318332        $this->assertEquals( $allowedposttags, wp_kses_allowed_html( 'post' ) );
    319333
    320         $tags = wp_kses_allowed_html( 'post' ) ;
     334        $tags = wp_kses_allowed_html( 'post' );
    321335
    322336        foreach ( $tags as $tag ) {
     
    341355        $custom_tags = array(
    342356            'a' => array(
    343                 'href' => true,
    344                 'rel' => true,
    345                 'rev' => true,
    346                 'name' => true,
     357                'href'   => true,
     358                'rel'    => true,
     359                'rev'    => true,
     360                'name'   => true,
    347361                'target' => true,
    348362            ),
     
    362376
    363377    function test_hyphenated_tag() {
    364         $string = "<hyphenated-tag attribute=\"value\" otherattribute=\"value2\">Alot of hyphens.</hyphenated-tag>";
    365         $custom_tags = array(
     378        $string                 = '<hyphenated-tag attribute="value" otherattribute="value2">Alot of hyphens.</hyphenated-tag>';
     379        $custom_tags            = array(
    366380            'hyphenated-tag' => array(
    367381                'attribute' => true,
     
    370384        $expect_stripped_string = 'Alot of hyphens.';
    371385
    372         $expect_valid_string = "<hyphenated-tag attribute=\"value\">Alot of hyphens.</hyphenated-tag>";
     386        $expect_valid_string = '<hyphenated-tag attribute="value">Alot of hyphens.</hyphenated-tag>';
    373387        $this->assertEquals( $expect_stripped_string, wp_kses_post( $string ) );
    374388        $this->assertEquals( $expect_valid_string, wp_kses( $string, $custom_tags ) );
     
    563577            array(
    564578                '<a title="hello"disabled href=# id=\'my_id\'>',
    565                 array( '<a ', 'title="hello"', 'disabled ', 'href=# ', "id='my_id'", ">" ),
     579                array( '<a ', 'title="hello"', 'disabled ', 'href=# ', "id='my_id'", '>' ),
    566580            ),
    567581            array(
     
    684698     */
    685699    function test_wp_kses_attr_no_attributes_allowed_with_empty_array() {
    686         $element = 'foo';
     700        $element   = 'foo';
    687701        $attribute = 'title="foo" class="bar"';
    688702
     
    694708     */
    695709    function test_wp_kses_attr_no_attributes_allowed_with_true() {
    696         $element = 'foo';
     710        $element   = 'foo';
    697711        $attribute = 'title="foo" class="bar"';
    698712
     
    704718     */
    705719    function test_wp_kses_attr_single_attribute_is_allowed() {
    706         $element = 'foo';
     720        $element   = 'foo';
    707721        $attribute = 'title="foo" class="bar"';
    708722
Note: See TracChangeset for help on using the changeset viewer.