Skip to content
Merged
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
16 changes: 10 additions & 6 deletions lib/experimental/dashboard-widgets/widget-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ function gutenberg_translate_widget_metadata( $widget ) {

/**
* Constrains a widget help note to its allowed shape: `content` keeps
* only `em`/`strong` markup, and links missing a `label` or `href` are
* dropped.
* only `em`/`strong` markup, and links are dropped unless they carry a
* `label` and an `href` that survives `esc_url_raw()`.
*
* @param array|null $help Help note from the build manifest.
* @return array|null Sanitized help note, or null when there is no content.
Expand All @@ -91,10 +91,14 @@ function gutenberg_sanitize_widget_help( $help ) {
$links = array();
foreach ( $help['links'] as $link ) {
if ( is_array( $link ) && ! empty( $link['label'] ) && ! empty( $link['href'] ) ) {
$links[] = array(
'label' => $link['label'],
'href' => $link['href'],
);
$href = esc_url_raw( $link['href'] );

if ( $href ) {
$links[] = array(
'label' => $link['label'],
'href' => $href,
);
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion phpunit/experimental/widget-types-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public function test_translate_widget_metadata_localizes_strings() {

/**
* The help content keeps minimal emphasis, everything else is stripped,
* and malformed links are dropped.
* and links are dropped when malformed or when their href does not
* survive esc_url_raw().
*/
public function test_sanitize_widget_help_constrains_markup_and_links() {
$help = gutenberg_sanitize_widget_help(
Expand All @@ -81,6 +82,10 @@ public function test_sanitize_widget_help_constrains_markup_and_links() {
'href' => 'site-health.php',
),
array( 'label' => 'Missing href' ),
array(
'label' => 'Unsafe protocol',
'href' => 'javascript:alert(1)',
),
),
)
);
Expand Down
Loading