Skip to content

Commit d337a43

Browse files
committed
Escape output from shared block before display
1 parent 29c4ae7 commit d337a43

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

includes/Blocks/SharedBlock.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,16 @@ function () use ( $block_support_styles ) {
126126
);
127127
}
128128

129-
return $block_data['html'];
129+
/**
130+
* Ensure Shared block output only contain allowed HTML tags and attributes.
131+
*
132+
* Since the shortcodes and embed from the original source have already been processed, we temporarily allow
133+
* the `iframe` tag in the output.
134+
*/
135+
add_filter( 'wp_kses_allowed_html', [ Helpers::class, 'kses_post_iframe_tag' ], 10, 2 );
136+
$html = wp_kses_post( $block_data['html'] );
137+
remove_filter( 'wp_kses_allowed_html', [ Helpers::class, 'kses_post_iframe_tag' ] );
138+
return $html;
130139
}
131140

132141
// When displaying excerpt content, prepare excerpt content and return the custom view.

includes/Helpers.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,29 @@ public static function validate_shared_block_preview_request( string $request_to
226226

227227
return hash_equals( $token, $request_token );
228228
}
229+
230+
/**
231+
* Add iframe tag to the list of allowed tags when using kses with the `post` context.
232+
*
233+
* @param array $tags List of allowed tags and attributes.
234+
* @param string $context Context name.
235+
*
236+
* @return array List of allowed tags and attributes.
237+
*/
238+
public static function kses_post_iframe_tag( $tags, $context ) {
239+
if ( 'post' === $context ) {
240+
$tags['iframe'] = [
241+
'src' => true,
242+
'height' => true,
243+
'width' => true,
244+
'title' => true,
245+
'allow' => true,
246+
'loading' => true,
247+
'frameborder' => true,
248+
'allowfullscreen' => true,
249+
];
250+
}
251+
252+
return $tags;
253+
}
229254
}

views/preview.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
if ( ! defined( 'ABSPATH' ) ) {
1111
die( '-1' );
1212
}
13+
14+
use Beapi\MultisiteSharedBlocks\Helpers;
15+
1316
?>
1417
<!DOCTYPE html>
1518
<html>
@@ -33,8 +36,15 @@
3336
$output .= render_block( $block );
3437
}
3538

36-
echo $output; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
37-
wp_footer();
39+
/**
40+
* Ensure Shared block output only contain allowed HTML tags and attributes.
41+
*
42+
* Since the shortcodes and embed from the original source have already been processed, we temporarily allow
43+
* the `iframe` tag in the output.
44+
*/
45+
add_filter( 'wp_kses_allowed_html', [ Helpers::class, 'kses_post_iframe_tag' ], 10, 2 );
46+
echo wp_kses_post( $output );
47+
remove_filter( 'wp_kses_allowed_html', [ Helpers::class, 'kses_post_iframe_tag' ] );
3848
?>
3949
</body>
4050
</html>

0 commit comments

Comments
 (0)