Skip to content

Commit 2a76573

Browse files
committed
Fix: Add validation for oEmbed provider data to prevent PHP warnings
1 parent aa72dfe commit 2a76573

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/wp-includes/class-wp-oembed.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,14 @@ public function get_provider( $url, $args = '' ) {
273273
}
274274

275275
foreach ( $this->providers as $matchmask => $data ) {
276+
if (
277+
! is_array( $data ) ||
278+
count( $data ) < 2 ||
279+
! array_key_exists( 0, $data ) ||
280+
! array_key_exists( 1, $data )
281+
) {
282+
continue;
283+
}
276284
list( $providerurl, $regex ) = $data;
277285

278286
// Turn the asterisk-type provider URLs into regex.

tests/phpunit/tests/oembed/wpOembed.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,34 @@ public function test_wp_filter_pre_oembed_result_multisite_restores_state_if_no_
276276
$this->assertFalse( $actual );
277277
$this->assertSame( $current_blog_id, get_current_blog_id() );
278278
}
279+
280+
/**
281+
* @ticket 65068
282+
*
283+
* @covers ::get_provider
284+
*/
285+
public function test_get_provider_skips_malformed_provider_entries() {
286+
$warnings = array();
287+
288+
$error_handler = function ( $errno, $errstr ) use ( &$warnings ) {
289+
if ( E_WARNING === $errno ) {
290+
$warnings[] = $errstr;
291+
}
292+
return false;
293+
};
294+
295+
set_error_handler( $error_handler );
296+
297+
$this->oembed->providers['bad_provider'] = array(
298+
'url' => '#https?://example\.site/.*#i',
299+
'endpoint' => 'https://example.site/api/oembed',
300+
);
301+
302+
$result = $this->oembed->get_provider( 'https://en.wikipedia.org/wiki/Rickrolling' );
303+
304+
restore_error_handler();
305+
306+
$this->assertFalse( $result );
307+
$this->assertSame( array(), $warnings, 'PHP warnings were raised: ' . implode( ', ', $warnings ) );
308+
}
279309
}

0 commit comments

Comments
 (0)