Skip to content

Commit de917d2

Browse files
committed
fix: Introduce sanitize_provider() to validate and normalize oEmbed provider data
1 parent 8427433 commit de917d2

1 file changed

Lines changed: 33 additions & 9 deletions

File tree

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

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class WP_oEmbed {
2424
*
2525
* @since 2.9.0
2626
* @var array<string, array{ 0: string, 1: bool }> An associative array mapping URL patterns to provider data.
27-
* Each entry's value is an array with the provider endpoint URL
28-
* string at index 0 and a boolean regex flag at index 1.
27+
* Each entry's value is an array with the provider endpoint URL
28+
* string at index 0 and a boolean regex flag at index 1.
2929
*/
3030
public $providers = array();
3131

@@ -230,7 +230,8 @@ public function __construct() {
230230
*/
231231
$providers = apply_filters( 'oembed_providers', $providers );
232232
foreach ( $providers as $matchmask => $data ) {
233-
if ( ! is_array( $data ) || ! isset( $data[0] ) || ! is_string( $data[0] ) ) {
233+
$provider = $this->sanitize_provider( $data );
234+
if ( null === $provider ) {
234235
_doing_it_wrong(
235236
__METHOD__,
236237
sprintf(
@@ -241,8 +242,7 @@ public function __construct() {
241242
'7.1.0'
242243
);
243244
} else {
244-
$data[1] = (bool) ( $data[1] ?? false );
245-
$this->providers[ $matchmask ] = $data;
245+
$this->providers[ $matchmask ] = array( $provider['endpoint'], $provider['is_regex'] );
246246
}
247247
}
248248

@@ -267,6 +267,28 @@ public function __call( $name, $arguments ) {
267267
return false;
268268
}
269269

270+
/**
271+
* Sanitizes and normalizes a single oEmbed provider entry.
272+
*
273+
* Validates that the provider data is an array with a string endpoint URL at index 0,
274+
* and normalizes the optional regex flag at index 1 to a boolean.
275+
*
276+
* @since 7.1.0
277+
*
278+
* @param mixed $data The raw provider data to sanitize.
279+
* @return array{ endpoint: string, is_regex: bool }|null Normalized provider array, or null if malformed.
280+
*/
281+
private function sanitize_provider( $data ) {
282+
if ( ! is_array( $data ) || ! isset( $data[0] ) || ! is_string( $data[0] ) ) {
283+
return null;
284+
}
285+
286+
return array(
287+
'endpoint' => $data[0],
288+
'is_regex' => (bool) ( $data[1] ?? false ),
289+
);
290+
}
291+
270292
/**
271293
* Takes a URL and returns the corresponding oEmbed provider's URL, if there is one.
272294
*
@@ -294,17 +316,19 @@ public function get_provider( $url, $args = '' ) {
294316
}
295317

296318
foreach ( $this->providers as $matchmask => $data ) {
297-
$providerurl = $data[0];
298-
$regex = $data[1] ?? false;
319+
$provider_data = $this->sanitize_provider( $data );
320+
if ( null === $provider_data ) {
321+
continue;
322+
}
299323

300324
// Turn the asterisk-type provider URLs into regex.
301-
if ( ! $regex ) {
325+
if ( ! $provider_data['is_regex'] ) {
302326
$matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
303327
$matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
304328
}
305329

306330
if ( preg_match( $matchmask, $url ) ) {
307-
$provider = str_replace( '{format}', 'json', $providerurl ); // JSON is easier to deal with than XML.
331+
$provider = str_replace( '{format}', 'json', $provider_data['endpoint'] ); // JSON is easier to deal with than XML.
308332
break;
309333
}
310334
}

0 commit comments

Comments
 (0)