Skip to content

Commit 7caa95f

Browse files
authored
Fix: image cdn defensive check when trimming photon url (#44822)
* Add validation for image URL before trimming * changelog * Added tests * Phan
1 parent 22688d6 commit 7caa95f

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Image CDN: Added defensive check for is_string before trimming photon url

projects/packages/image-cdn/src/class-image-cdn-core.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public static function setup() {
5454
* @return string The raw final URL. You should run this through esc_url() before displaying it.
5555
*/
5656
public static function cdn_url( $image_url, $args = array(), $scheme = null ) {
57+
if ( ! is_string( $image_url ) || empty( $image_url ) ) {
58+
return '';
59+
}
5760
$image_url = trim( $image_url );
5861

5962
if ( ! defined( 'IS_WPCOM' ) || ! \IS_WPCOM ) {

projects/packages/image-cdn/tests/php/Image_CDN_Core_Test.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,18 @@ public function test_photonizing_check_extensions( $image_url, $expected ) {
450450
$this->assertEquals( $expected, Image_CDN_Core::cdn_url( $image_url, array( 'w' => 500 ) ) );
451451
}
452452

453+
/**
454+
* Tests that the cdn_url method returns an empty string when the image URL is empty or invalid.
455+
*
456+
* @since $$next-version$$
457+
*/
458+
public function test_cdn_url_empty_invalid_url() {
459+
$this->assertSame( '', Image_CDN_Core::cdn_url( '' ) );
460+
$this->assertSame( '', Image_CDN_Core::cdn_url( null ) );
461+
$this->assertSame( '', Image_CDN_Core::cdn_url( 123 ) ); // @phan-suppress-current-line PhanTypeMismatchArgument
462+
$this->assertSame( '', Image_CDN_Core::cdn_url( array() ) );
463+
}
464+
453465
/**
454466
* Data provider for test_photon_banned_domains_banned
455467
*/

0 commit comments

Comments
 (0)