Skip to content

Commit dd245d0

Browse files
committed
Allow use of '<' as a comparator
1 parent ec4a67a commit dd245d0

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

‎add-admin-css.php‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,13 +618,20 @@ public function add_css() {
618618
*
619619
* @since 1.0
620620
*
621-
* @param string $files CSS code (without `<style>` tag).
621+
* @param string $files CSS styles (without `<style>` tag).
622622
*/
623623
$css = trim( apply_filters( 'c2c_add_admin_css', $css ) );
624624

625-
if ( $css ) {
625+
if ( $css && is_string( $css ) ) {
626+
// Encode '<' to prevent tag injection.
627+
$sanitized_css = str_replace( '<', '&lt;', wp_check_invalid_utf8( $css ) );
628+
// Allow use of '<' in comparators.
629+
$sanitized_css = str_replace( '&lt; ', '< ', $sanitized_css );
630+
$sanitized_css = str_replace( '&lt;= ', '<= ', $sanitized_css );
631+
626632
echo "<style>\n";
627-
echo str_replace( '<', '&lt;', wp_check_invalid_utf8( $css ) ) . "\n";
633+
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Sanitized above.
634+
echo $sanitized_css . "\n";
628635
echo "</style>\n";
629636
}
630637
}

‎tests/phpunit/tests/add-admin-css.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ public static function get_css_file_links2() {
9898
);
9999
}
100100

101+
public static function get_css_with_special_characters() {
102+
return [
103+
[ '.button { &:hover { background-color: #000; } }' ],
104+
[ '@media (width < 600px) { .button { background-color: #0f0; } }' ],
105+
[ '@media (width >= 600px) { .button { background-color: #f00; } }' ],
106+
[ '.quote { font-family: "Comic Sans MS", cursive; }' ],
107+
[ "a[target='_blank'] { color: blue; }" ],
108+
];
109+
}
101110

102111
//
103112
//
@@ -452,6 +461,27 @@ public function test_add_css_to_head_with_just_css( $expected = false ) {
452461
return $out;
453462
}
454463

464+
/**
465+
* @dataProvider get_css_with_special_characters
466+
*/
467+
public function test_add_css_allows_valid_css_characters( $css ) {
468+
$this->set_option( [ 'css' => $css, 'files' => [] ] );
469+
$this->test_turn_on_admin();
470+
471+
$this->assertStringContainsString( $css, $this->get_action_output() );
472+
}
473+
474+
public function test_add_css_prevents_markup_injection() {
475+
$injection = '<script>alert("Hello, world!");</script>';
476+
$css = $this->add_css( $injection, '' );
477+
478+
$this->set_option( [ 'css' => $css, 'files' => [] ] );
479+
$this->test_turn_on_admin();
480+
481+
$this->assertStringNotContainsString( $injection, $this->get_action_output() );
482+
$this->assertStringContainsString( str_replace( '<', '&lt;', $injection ), $this->get_action_output() );
483+
}
484+
455485
public function test_add_css_to_head_with_just_files( $expected = false ) {
456486
// Examples of different types of references.
457487
$files = array(

0 commit comments

Comments
 (0)