Make WordPress Core

Ticket #37134: 37134.10.diff

File 37134.10.diff, 5.0 KB (added by azaozz, 3 years ago)
  • src/wp-includes/kses.php

     
    23022302                }
    23032303
    23042304                if ( $found ) {
     2305                        // Check for any CSS containing \ ( & } = or comments, except for url() usage checked above.
     2306                        $allow_css = ! preg_match( '%[\\\(&=}]|/\*%', $css_test_string );
     2307
    23052308                        /**
    2306                          * Filters the regex limiting the list of characters not allowed in CSS rules.
     2309                         * Filters the check for unsafe CSS in `safecss_filter_attr`.
    23072310                         *
    2308                          * Default behaviour is to remove any CSS containing \ ( & } = or comments,
    2309                          * except for url() usage.
     2311                         * Enables developers to determine whether a section of CSS should be allowed or discarded.
     2312                         * By default, the value will be false if the part contains \ ( & } = or comments.
     2313                         * Return true to allow the CSS part to be included in the output.
    23102314                         *
    23112315                         * @since 5.5.0
    23122316                         *
    2313                          * @param string $regex           Regex pattern of disallowed characters in CSS rules.
    2314                          *                                Default is '%[\\\(&=}]|/\*%'.
    2315                          * @param string $css_test_string CSS value to test.
     2317                         * @param bool   $allow_css       Whether the CSS in the test string is considered safe.
     2318                         * @param string $css_test_string The css string to test.
    23162319                         */
    2317                         $disallowed_chars = apply_filters( 'safe_style_disallowed_chars', '%[\\\(&=}]|/\*%', $css_test_string );
    2318                         if ( ! preg_match( $disallowed_chars, $css_test_string ) ) {
     2320                        $allow_css = apply_filters( 'safecss_filter_attr_allow_css', $allow_css, $css_test_string );
     2321
     2322                         // Only add the css part if it passes the regex check.
     2323                        if ( $allow_css ) {
    23192324                                if ( '' !== $css ) {
    23202325                                        $css .= ';';
    23212326                                }
     2327
    23222328                                $css .= $css_item;
    23232329                        }
    23242330                }
  • tests/phpunit/tests/kses.php

     
    12631263        }
    12641264
    12651265        /**
    1266          * Filter for disallowed characters never matches thus allowing all characters.
    1267          */
    1268         function _safe_style_disallowed_chars_filter( $regex ) {
    1269                 return '%a^%'; // Regex with no matches.
    1270 
    1271         }
    1272         /**
    1273          * Testing the safecss_filter_attr() function with the safe_style_disallowed_chars filter.
     1266         * Testing the safecss_filter_attr() function with the safecss_filter_attr_allow_css filter.
    12741267         *
    12751268         * @ticket 37134
    12761269         *
     
    12801273         * @param string $expected Expected string of CSS rules.
    12811274         */
    12821275        public function test_safecss_filter_attr_filtered( $css, $expected ) {
    1283                 add_filter( 'safe_style_disallowed_chars', array( $this, '_safe_style_disallowed_chars_filter' ) );
     1276                add_filter( 'safecss_filter_attr_allow_css', '__return_true' );
    12841277                $this->assertSame( $expected, safecss_filter_attr( $css ) );
    1285                 remove_filter( 'safe_style_disallowed_chars', array( $this, '_safe_style_disallowed_chars_filter' ) );
     1278                remove_filter( 'safecss_filter_attr_allow_css', '__return_true' );
    12861279        }
    12871280
    12881281        /**
     
    13031296                                'css'      => 'margin-top: 2px',
    13041297                                'expected' => 'margin-top: 2px',
    13051298                        ),
    1306                         // Backslash \ can be allowed with the 'safe_style_disallowed_chars' filter.
     1299                        // Backslash \ can be allowed with the 'safecss_filter_attr_allow_css' filter.
    13071300                        array(
    13081301                                'css'      => 'margin-top: \2px',
    13091302                                'expected' => 'margin-top: \2px',
    13101303                        ),
    1311                         // Curly bracket } can be allowed with the 'safe_style_disallowed_chars' filter.
     1304                        // Curly bracket } can be allowed with the 'safecss_filter_attr_allow_css' filter.
    13121305                        array(
    13131306                                'css'      => 'margin-bottom: 2px}',
    13141307                                'expected' => 'margin-bottom: 2px}',
    13151308                        ),
    1316                         // Parenthesis ) can be allowed with the 'safe_style_disallowed_chars' filter.
     1309                        // Parenthesis ) can be allowed with the 'safecss_filter_attr_allow_css' filter.
    13171310                        array(
    13181311                                'css'      => 'margin-bottom: 2px)',
    13191312                                'expected' => 'margin-bottom: 2px)',
    13201313                        ),
    1321                         // Ampersand & can be allowed with the 'safe_style_disallowed_chars' filter.
     1314                        // Ampersand & can be allowed with the 'safecss_filter_attr_allow_css' filter.
    13221315                        array(
    13231316                                'css'      => 'margin-bottom: 2px&',
    13241317                                'expected' => 'margin-bottom: 2px&',
    13251318                        ),
    1326                         // Expressions can be allowed with the 'safe_style_disallowed_chars' filter.
     1319                        // Expressions can be allowed with the 'safecss_filter_attr_allow_css' filter.
    13271320                        array(
    13281321                                'css'      => 'height: expression( body.scrollTop + 50 + "px" )',
    13291322                                'expected' => 'height: expression( body.scrollTop + 50 + "px" )',
    13301323                        ),
    1331                         // RGB color values can be allowed with the 'safe_style_disallowed_chars' filter.
     1324                        // RGB color values can be allowed with the 'safecss_filter_attr_allow_css' filter.
    13321325                        array(
    13331326                                'css'      => 'color: rgb( 100, 100, 100 )',
    13341327                                'expected' => 'color: rgb( 100, 100, 100 )',
    13351328                        ),
    1336                         // RGBA color values can be allowed with the 'safe_style_disallowed_chars' filter.
     1329                        // RGBA color values can be allowed with the 'safecss_filter_attr_allow_css' filter.
    13371330                        array(
    13381331                                'css'      => 'color: rgb( 100, 100, 100, .4 )',
    13391332                                'expected' => 'color: rgb( 100, 100, 100, .4 )',