Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/wp-includes/class-wp-customize-widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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'];
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/wp-includes/widgets/class-wp-widget-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand All @@ -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;
}

Expand Down
44 changes: 44 additions & 0 deletions tests/phpunit/tests/customize/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<!-- wp:paragraph {"style":{"css":"color: red;"}} --><p>Hello</p><!-- /wp:paragraph -->',
),
);

$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
Expand Down
66 changes: 66 additions & 0 deletions tests/phpunit/tests/widgets/wpWidgetBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class Tests_Widgets_wpWidgetBlock extends WP_UnitTestCase {
'after_title' => "</h2>\n",
);

/**
* Block content carrying custom CSS in its attributes.
*
* @var string
*/
const CONTENT_WITH_CUSTOM_CSS = '<!-- wp:paragraph {"style":{"css":"color: red;"}} --><p>Hello</p><!-- /wp:paragraph -->';

/**
* Creates a published post shared by the tests so dynamic blocks (such as core/calendar) have content.
*
Expand Down Expand Up @@ -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( '<p>Hello</p>', $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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
Loading