Skip to content

Commit 0365248

Browse files
authored
Validate urls are from expected sources. (#14866) (#14869)
1 parent 184cfff commit 0365248

File tree

5 files changed

+83
-3
lines changed

5 files changed

+83
-3
lines changed

class.jetpack-gutenberg.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,4 +859,71 @@ public static function get_extensions_preset_for_variation( $preset_extensions_m
859859

860860
return $preset_extensions;
861861
}
862+
863+
/**
864+
* Validate a URL used in a SSR block.
865+
*
866+
* @since 8.3.0
867+
*
868+
* @param string $url URL saved as an attribute in block.
869+
* @param array $allowed Array of allowed hosts for that block, or regexes to check against.
870+
* @param bool $is_regex Array of regexes matching the URL that could be used in block.
871+
*
872+
* @return bool|string
873+
*/
874+
public static function validate_block_embed_url( $url, $allowed = array(), $is_regex = false ) {
875+
if (
876+
empty( $url )
877+
|| ! is_array( $allowed )
878+
|| empty( $allowed )
879+
) {
880+
return false;
881+
}
882+
883+
$url_components = wp_parse_url( $url );
884+
885+
// Bail early if we cannot find a host.
886+
if ( empty( $url_components['host'] ) ) {
887+
return false;
888+
}
889+
890+
// Normalize URL.
891+
$url = sprintf(
892+
'%s://%s%s%s',
893+
$url_components['scheme'],
894+
$url_components['host'],
895+
$url_components['path'] ? $url_components['path'] : '/',
896+
$url_components['query'] ? '?' . $url_components['query'] : ''
897+
);
898+
899+
if ( ! empty( $url_components['fragment'] ) ) {
900+
$url = $url . '#' . rawurlencode( $url_components['fragment'] );
901+
}
902+
903+
/*
904+
* If we're using a whitelist of hosts,
905+
* check if the URL belongs to one of the domains allowed for that block.
906+
*/
907+
if (
908+
false === $is_regex
909+
&& in_array( $url_components['host'], $allowed, true )
910+
) {
911+
return $url;
912+
}
913+
914+
/*
915+
* If we are using an array of regexes to check against,
916+
* loop through that.
917+
*/
918+
if ( true === $is_regex ) {
919+
foreach ( $allowed as $regex ) {
920+
if ( 1 === preg_match( $regex, $url ) ) {
921+
return $url;
922+
}
923+
}
924+
}
925+
926+
return false;
927+
}
928+
862929
}

extensions/blocks/calendly/calendly.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ function load_assets( $attr, $content ) {
8282
if ( is_admin() ) {
8383
return;
8484
}
85-
$url = get_attribute( $attr, 'url' );
85+
$url = \Jetpack_Gutenberg::validate_block_embed_url(
86+
get_attribute( $attr, 'url' ),
87+
array( 'calendly.com' )
88+
);
8689
if ( empty( $url ) ) {
8790
return;
8891
}

extensions/blocks/eventbrite/eventbrite.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ function jetpack_render_eventbrite_block( $attr, $content ) {
2727
return '';
2828
}
2929

30+
$attr['url'] = Jetpack_Gutenberg::validate_block_embed_url(
31+
$attr['url'],
32+
array( '#^https?:\/\/(?:[0-9a-z]+\.)?eventbrite\.(?:com|co\.uk|com\.ar|com\.au|be|com\.br|ca|cl|co|dk|de|es|fi|fr|hk|ie|it|com\.mx|nl|co\.nz|at|com\.pe|pt|ch|sg|se)\/e\/[^\/]*?(?:\d+)\/?(?:\?[^\/]*)?$#' ),
33+
true
34+
);
35+
3036
$widget_id = wp_unique_id( 'eventbrite-widget-' );
3137

3238
wp_enqueue_script( 'eventbrite-widget', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', array(), JETPACK__VERSION, true );

extensions/blocks/gif/gif.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
function jetpack_gif_block_render( $attr ) {
2525
$padding_top = isset( $attr['paddingTop'] ) ? $attr['paddingTop'] : 0;
2626
$style = 'padding-top:' . $padding_top;
27-
$giphy_url = isset( $attr['giphyUrl'] ) ? $attr['giphyUrl'] : null;
27+
$giphy_url = isset( $attr['giphyUrl'] )
28+
? Jetpack_Gutenberg::validate_block_embed_url( $attr['giphyUrl'], array( 'giphy.com' ) )
29+
: null;
2830
$search_text = isset( $attr['searchText'] ) ? $attr['searchText'] : '';
2931
$caption = isset( $attr['caption'] ) ? $attr['caption'] : null;
3032

extensions/blocks/google-calendar/google-calendar.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ function register_block() {
3737
function load_assets( $attr ) {
3838
$width = isset( $attr['width'] ) ? $attr['width'] : '800';
3939
$height = isset( $attr['height'] ) ? $attr['height'] : '600';
40-
$url = isset( $attr['url'] ) ? $attr['url'] : '';
40+
$url = isset( $attr['url'] )
41+
? \Jetpack_Gutenberg::validate_block_embed_url( $attr['url'], array( 'calendar.google.com' ) ) :
42+
'';
4143
$classes = \Jetpack_Gutenberg::block_classes( 'google-calendar', $attr );
4244

4345
if ( empty( $url ) ) {

0 commit comments

Comments
 (0)