diff --git a/src/wp-admin/includes/class-custom-background.php b/src/wp-admin/includes/class-custom-background.php index e42a38f4eb689..2d3d9357c30fc 100644 --- a/src/wp-admin/includes/class-custom-background.php +++ b/src/wp-admin/includes/class-custom-background.php @@ -247,7 +247,7 @@ public function admin_page() { $message = sprintf( /* translators: %s: URL to background image configuration in Customizer. */ __( 'You can now manage and live-preview Custom Backgrounds in the Customizer.' ), - admin_url( 'customize.php?autofocus[control]=background_image' ) + esc_url( admin_url( 'customize.php?autofocus[control]=background_image' ) ) ); wp_admin_notice( $message, @@ -308,7 +308,7 @@ public function admin_page() { . " background-attachment: $background_attachment;"; } ?> -
+

@@ -431,8 +431,8 @@ public function admin_page() { ); ?> - -
+ +
@@ -440,7 +440,7 @@ public function admin_page() {
@@ -451,8 +451,8 @@ public function admin_page() { - -
+ +
@@ -472,8 +472,8 @@ public function admin_page() { - -
+ +
@@ -487,10 +487,11 @@ public function admin_page() { -> + + data-default-color="#">
diff --git a/tests/phpunit/tests/admin/customBackground.php b/tests/phpunit/tests/admin/customBackground.php new file mode 100644 index 0000000000000..804c21773fc39 --- /dev/null +++ b/tests/phpunit/tests/admin/customBackground.php @@ -0,0 +1,88 @@ +user->create( + array( + 'role' => 'administrator', + ) + ); + } + + public function set_up() { + parent::set_up(); + + wp_set_current_user( self::$admin_id ); + set_current_screen( 'appearance_page_custom-background' ); + + remove_theme_support( 'custom-background' ); + add_theme_support( + 'custom-background', + array( + 'default-position-x' => 'left', + 'default-position-y' => 'top', + 'default-size' => 'auto', + 'default-repeat' => 'repeat', + 'default-attachment' => 'scroll', + ) + ); + } + + public function tear_down() { + remove_filter( 'theme_mod_background_position_x', array( $this, 'filter_background_position_x' ) ); + remove_theme_mod( 'background_image' ); + remove_theme_mod( 'background_image_thumb' ); + remove_theme_support( 'custom-background' ); + set_current_screen(); + wp_set_current_user( 0 ); + + parent::tear_down(); + } + + /** + * @ticket 57268 + */ + public function test_admin_page_escapes_background_styles() { + set_theme_mod( 'background_image', 'https://example.org/background.jpg' ); + set_theme_mod( 'background_image_thumb', 'https://example.org/background.jpg' ); + set_theme_mod( 'background_size', 'cover' ); + set_theme_mod( 'background_repeat', 'repeat' ); + set_theme_mod( 'background_attachment', 'scroll' ); + + add_filter( 'theme_mod_background_position_x', array( $this, 'filter_background_position_x' ) ); + + $custom_background = new Custom_Background(); + + ob_start(); + $custom_background->admin_page(); + $output = ob_get_clean(); + + $dom = new DOMDocument(); + + libxml_use_internal_errors( true ); + $dom->loadHTML( '' . $output . '' ); + libxml_clear_errors(); + + $image = $dom->getElementById( 'custom-background-image' ); + + $this->assertInstanceOf( DOMElement::class, $image ); + $this->assertFalse( $image->hasAttribute( 'onmouseover' ) ); + } + + public function filter_background_position_x() { + return 'left" onmouseover="alert(1)'; + } +}