diff --git a/src/wp-includes/class-wp-customize-widgets.php b/src/wp-includes/class-wp-customize-widgets.php index d7cd0d5de15dd..39ffb27b599a1 100644 --- a/src/wp-includes/class-wp-customize-widgets.php +++ b/src/wp-includes/class-wp-customize-widgets.php @@ -1449,6 +1449,7 @@ protected function get_instance_hash_key( $serialized_instance ) { * * @since 3.9.0 * @since 5.8.0 Added the `$id_base` parameter. + * @since 7.3.0 Custom CSS is stripped from the 'block' widget content for users without the `edit_css` capability. * * @global WP_Widget_Factory $wp_widget_factory * @@ -1474,6 +1475,11 @@ public function sanitize_widget_instance( $value, $id_base = null ) { $value['raw_instance']['content'] = wp_kses_post( $value['raw_instance']['content'] ); } + if ( 'block' === $id_base && ! current_user_can( 'edit_css' ) ) { + // wp_strip_custom_css_from_blocks() expects slashed content. + $value['raw_instance']['content'] = wp_unslash( wp_strip_custom_css_from_blocks( wp_slash( $value['raw_instance']['content'] ) ) ); + } + return $value['raw_instance']; } } diff --git a/src/wp-includes/widgets/class-wp-widget-block.php b/src/wp-includes/widgets/class-wp-widget-block.php index 44371825ac57c..017211482611b 100644 --- a/src/wp-includes/widgets/class-wp-widget-block.php +++ b/src/wp-includes/widgets/class-wp-widget-block.php @@ -171,6 +171,7 @@ private function get_dynamic_classname( $content ) { * Handles updating settings for the current Block widget instance. * * @since 5.8.0 + * @since 7.3.0 Custom CSS is stripped from the content for users without the `edit_css` capability. * @param array $new_instance New settings for this instance as input by the user via * WP_Widget::form(). @@ -186,6 +187,11 @@ public function update( $new_instance, $old_instance ) { $instance['content'] = wp_kses_post( $new_instance['content'] ); } + if ( ! current_user_can( 'edit_css' ) ) { + // wp_strip_custom_css_from_blocks() expects slashed content. + $instance['content'] = wp_unslash( wp_strip_custom_css_from_blocks( wp_slash( $instance['content'] ) ) ); + } + return $instance; } diff --git a/tests/phpunit/tests/customize/widgets.php b/tests/phpunit/tests/customize/widgets.php index dc23c0acf53de..3b47235935bb4 100644 --- a/tests/phpunit/tests/customize/widgets.php +++ b/tests/phpunit/tests/customize/widgets.php @@ -532,6 +532,50 @@ public function test_sanitize_widget_instance_raw_instance() { $this->assertSame( $unsanitized_from_js, $block_instance ); } + /** + * Custom CSS in a block widget's 'raw_instance' content should be stripped + * for a user without the edit_css capability. + * + * @ticket 64771 + * + * @covers WP_Customize_Widgets::sanitize_widget_instance + */ + public function test_sanitize_widget_instance_raw_instance_strips_custom_css_without_edit_css() { + remove_action( 'widgets_init', array( $this, 'remove_widgets_block_editor' ) ); + $this->do_customize_boot_actions(); + + add_filter( 'map_meta_cap', array( $this, 'revoke_edit_css_cap' ), 10, 2 ); + $this->assertFalse( current_user_can( 'edit_css' ) ); + + $value = array( + 'raw_instance' => array( + 'content' => '
Hello
', + ), + ); + + $sanitized = $this->manager->widgets->sanitize_widget_instance( $value, 'block' ); + remove_filter( 'map_meta_cap', array( $this, 'revoke_edit_css_cap' ), 10 ); + + $blocks = parse_blocks( $sanitized['content'] ); + $this->assertSame( 'core/paragraph', $blocks[0]['blockName'] ); + $this->assertArrayNotHasKey( 'style', $blocks[0]['attrs'], 'Custom CSS should be stripped for a user without edit_css.' ); + } + + /** + * Revoke edit_css cap via map_meta_cap. + * + * @param array $caps Returns the user's actual capabilities. + * @param string $cap Capability name. + * @return array Caps. + */ + public function revoke_edit_css_cap( $caps, $cap ) { + if ( 'edit_css' === $cap ) { + $caps = array_diff( $caps, array( 'unfiltered_html' ) ); + $caps[] = 'do_not_allow'; + } + return $caps; + } + /** * There should NOT be a 'raw_instance' key when the block editor is enabled * but the widget does not support them because `show_instance_in_rest` on diff --git a/tests/phpunit/tests/widgets/wpWidgetBlock.php b/tests/phpunit/tests/widgets/wpWidgetBlock.php index 60d7d769585e7..09ce0bfcab32a 100644 --- a/tests/phpunit/tests/widgets/wpWidgetBlock.php +++ b/tests/phpunit/tests/widgets/wpWidgetBlock.php @@ -27,6 +27,13 @@ class Tests_Widgets_wpWidgetBlock extends WP_UnitTestCase { 'after_title' => "\n", ); + /** + * Block content carrying custom CSS in its attributes. + * + * @var string + */ + const CONTENT_WITH_CUSTOM_CSS = 'Hello
'; + /** * Creates a published post shared by the tests so dynamic blocks (such as core/calendar) have content. * @@ -360,6 +367,50 @@ public function test_update_without_unfiltered_html_cap() { ); } + /** + * Tests that update() strips custom CSS from the content when the user lacks edit_css. + * + * @ticket 64771 + * + * @covers WP_Widget_Block::update + */ + public function test_update_strips_custom_css_without_edit_css() { + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); + + add_filter( 'map_meta_cap', array( $this, 'revoke_edit_css_cap' ), 10, 2 ); + $this->assertFalse( current_user_can( 'edit_css' ) ); + + $widget = new WP_Widget_Block(); + $result = $widget->update( array( 'content' => self::CONTENT_WITH_CUSTOM_CSS ), array() ); + remove_filter( 'map_meta_cap', array( $this, 'revoke_edit_css_cap' ), 10 ); + + $blocks = parse_blocks( $result['content'] ); + $this->assertSame( 'core/paragraph', $blocks[0]['blockName'] ); + $this->assertArrayNotHasKey( 'style', $blocks[0]['attrs'], 'Custom CSS should be stripped for a user without edit_css.' ); + $this->assertStringContainsString( 'Hello
', $result['content'] ); + } + + /** + * Tests that update() keeps custom CSS in the content when the user has edit_css. + * + * @ticket 64771 + * + * @covers WP_Widget_Block::update + */ + public function test_update_keeps_custom_css_with_edit_css() { + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + if ( is_multisite() ) { + grant_super_admin( $admin_id ); + } + wp_set_current_user( $admin_id ); + $this->assertTrue( current_user_can( 'edit_css' ) ); + + $widget = new WP_Widget_Block(); + $result = $widget->update( array( 'content' => self::CONTENT_WITH_CUSTOM_CSS ), array() ); + + $this->assertSame( self::CONTENT_WITH_CUSTOM_CSS, $result['content'] ); + } + /** * Grants the unfiltered_html capability via map_meta_cap. * @@ -388,6 +439,21 @@ public function revoke_unfiltered_html_cap( $caps, $cap ) { return $caps; } + /** + * Revokes the edit_css capability via map_meta_cap. + * + * @param string[] $caps Capabilities. + * @param string $cap Capability name. + * @return string[] Filtered capabilities. + */ + public function revoke_edit_css_cap( $caps, $cap ) { + if ( 'edit_css' === $cap ) { + $caps = array_diff( $caps, array( 'unfiltered_html' ) ); + $caps[] = 'do_not_allow'; + } + return $caps; + } + /** * Tests that form() outputs the content textarea with saved and default values. *