Skip to content

Commit c2f2558

Browse files
committed
Media: Guard against false return values from wp_get_attachment_image_src() and wp_getimagesize().
* Add `is_array()` checks before accessing return values from `wp_get_attachment_image_src()` in `get_oembed_response_data_rich()`, `wp_playlist_shortcode()`, and `wp_prepare_attachment_for_js()`. * Guard `wp_getimagesize()` calls within `wp_get_attachment_image_src()` itself. * Ensure `wp_get_attachment_image_src()` always returns the expected `array{0: string, 1: int, 2: int, 3: bool}` type or `false` by normalizing the filter result with explicit type casting and default values. * Add `@phpstan-return` annotations to both `wp_get_attachment_image_src()` and `wp_getimagesize()` for the specific array shapes. Developed in WordPress#11073 Props hbhalodia, westonruter, mukesh27, edent, ozgursar, roshniahuja14. Fixes #64742. git-svn-id: https://develop.svn.wordpress.org/trunk@62176 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 3b3b726 commit c2f2558

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

src/wp-includes/embed.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -739,10 +739,13 @@ function get_oembed_response_data_rich( $data, $post, $width, $height ) {
739739
}
740740

741741
if ( $thumbnail_id ) {
742-
list( $thumbnail_url, $thumbnail_width, $thumbnail_height ) = wp_get_attachment_image_src( $thumbnail_id, array( $width, 0 ) );
743-
$data['thumbnail_url'] = $thumbnail_url;
744-
$data['thumbnail_width'] = $thumbnail_width;
745-
$data['thumbnail_height'] = $thumbnail_height;
742+
$thumbnail_src = wp_get_attachment_image_src( $thumbnail_id, array( $width, 0 ) );
743+
744+
if ( is_array( $thumbnail_src ) ) {
745+
$data['thumbnail_url'] = $thumbnail_src[0];
746+
$data['thumbnail_width'] = $thumbnail_src[1];
747+
$data['thumbnail_height'] = $thumbnail_src[2];
748+
}
746749
}
747750

748751
return $data;

src/wp-includes/media.php

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -972,12 +972,15 @@ function wp_get_registered_image_subsizes() {
972972
* @type int $2 Image height in pixels.
973973
* @type bool $3 Whether the image is a resized image.
974974
* }
975+
* @phpstan-return array{ 0: string, 1: int, 2: int, 3: bool }|false
975976
*/
976977
function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) {
977978
// Get a thumbnail or intermediate image if there is one.
978979
$image = image_downsize( $attachment_id, $size );
979980
if ( ! $image ) {
980-
$src = false;
981+
$src = false;
982+
$width = 0;
983+
$height = 0;
981984

982985
if ( $icon ) {
983986
$src = wp_mime_type_icon( $attachment_id, '.svg' );
@@ -988,7 +991,11 @@ function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon
988991

989992
$src_file = $icon_dir . '/' . wp_basename( $src );
990993

991-
list( $width, $height ) = wp_getimagesize( $src_file );
994+
$image_size = wp_getimagesize( $src_file );
995+
if ( is_array( $image_size ) ) {
996+
$width = $image_size[0];
997+
$height = $image_size[1];
998+
}
992999

9931000
$ext = strtolower( substr( $src_file, -4 ) );
9941001

@@ -997,7 +1004,11 @@ function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon
9971004
$width = 48;
9981005
$height = 64;
9991006
} else {
1000-
list( $width, $height ) = wp_getimagesize( $src_file );
1007+
$image_size = wp_getimagesize( $src_file );
1008+
if ( is_array( $image_size ) ) {
1009+
$width = $image_size[0];
1010+
$height = $image_size[1];
1011+
}
10011012
}
10021013
}
10031014
}
@@ -1024,7 +1035,16 @@ function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon
10241035
* an array of width and height values in pixels (in that order).
10251036
* @param bool $icon Whether the image should be treated as an icon.
10261037
*/
1027-
return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
1038+
$source = apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
1039+
if ( is_array( $source ) && isset( $source[0] ) && is_string( $source[0] ) ) {
1040+
return array(
1041+
$source[0],
1042+
(int) ( $source[1] ?? 0 ),
1043+
(int) ( $source[2] ?? 0 ),
1044+
(bool) ( $source[3] ?? false ),
1045+
);
1046+
}
1047+
return false;
10281048
}
10291049

10301050
/**
@@ -3230,10 +3250,23 @@ function wp_playlist_shortcode( $attr ) {
32303250
if ( $atts['images'] ) {
32313251
$thumb_id = get_post_thumbnail_id( $attachment->ID );
32323252
if ( ! empty( $thumb_id ) ) {
3233-
list( $src, $width, $height ) = wp_get_attachment_image_src( $thumb_id, 'full' );
3234-
$track['image'] = compact( 'src', 'width', 'height' );
3235-
list( $src, $width, $height ) = wp_get_attachment_image_src( $thumb_id, 'thumbnail' );
3236-
$track['thumb'] = compact( 'src', 'width', 'height' );
3253+
$image_src_full = wp_get_attachment_image_src( $thumb_id, 'full' );
3254+
if ( is_array( $image_src_full ) ) {
3255+
$track['image'] = array(
3256+
'src' => $image_src_full[0],
3257+
'width' => $image_src_full[1],
3258+
'height' => $image_src_full[2],
3259+
);
3260+
}
3261+
3262+
$image_src_thumb = wp_get_attachment_image_src( $thumb_id, 'thumbnail' );
3263+
if ( is_array( $image_src_thumb ) ) {
3264+
$track['thumb'] = array(
3265+
'src' => $image_src_thumb[0],
3266+
'width' => $image_src_thumb[1],
3267+
'height' => $image_src_thumb[2],
3268+
);
3269+
}
32373270
} else {
32383271
$src = wp_mime_type_icon( $attachment->ID, '.svg' );
32393272
$width = 48;
@@ -4711,10 +4744,23 @@ function wp_prepare_attachment_for_js( $attachment ) {
47114744

47124745
$id = get_post_thumbnail_id( $attachment->ID );
47134746
if ( ! empty( $id ) ) {
4714-
list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'full' );
4715-
$response['image'] = compact( 'src', 'width', 'height' );
4716-
list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'thumbnail' );
4717-
$response['thumb'] = compact( 'src', 'width', 'height' );
4747+
$response_image_full = wp_get_attachment_image_src( $id, 'full' );
4748+
if ( is_array( $response_image_full ) ) {
4749+
$response['image'] = array(
4750+
'src' => $response_image_full[0],
4751+
'width' => $response_image_full[1],
4752+
'height' => $response_image_full[2],
4753+
);
4754+
}
4755+
4756+
$response_image_thumb = wp_get_attachment_image_src( $id, 'thumbnail' );
4757+
if ( is_array( $response_image_thumb ) ) {
4758+
$response['thumb'] = array(
4759+
'src' => $response_image_thumb[0],
4760+
'width' => $response_image_thumb[1],
4761+
'height' => $response_image_thumb[2],
4762+
);
4763+
}
47184764
} else {
47194765
$src = wp_mime_type_icon( $attachment->ID, '.svg' );
47204766
$width = 48;
@@ -5724,6 +5770,7 @@ function wp_show_heic_upload_error( $plupload_settings ) {
57245770
* @param string $filename The file path.
57255771
* @param array $image_info Optional. Extended image information (passed by reference).
57265772
* @return array|false Array of image information or false on failure.
5773+
* @phpstan-return array{ 0: int, 1: int, 2: int, 3: string, mime: string, bits?: int, channels?: int }|false
57275774
*/
57285775
function wp_getimagesize( $filename, ?array &$image_info = null ) {
57295776
// Don't silence errors when in debug mode, unless running unit tests.

0 commit comments

Comments
 (0)