Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boost: Fix photon URL check #39635

Merged
merged 8 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Added a public method to check if a URL is CDN url
29 changes: 23 additions & 6 deletions projects/packages/image-cdn/src/class-image-cdn-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,12 @@ public static function cdn_url( $image_url, $args = array(), $scheme = null ) {
}
}

/** This filter is documented below. */
$custom_photon_url = apply_filters( 'jetpack_photon_domain', '', $image_url );
$custom_photon_url = esc_url( $custom_photon_url );

// You can't run a Photon URL through Photon again because query strings are stripped.
// So if the image is already a Photon URL, append the new arguments to the existing URL.
// Alternately, if it's a *.files.wordpress.com url or an image on a private WordPress.com Simple site,
// then keep the domain as is.
if (
in_array( $image_url_parts['host'], array( 'i0.wp.com', 'i1.wp.com', 'i2.wp.com' ), true )
|| wp_parse_url( $custom_photon_url, PHP_URL_HOST ) === $image_url_parts['host']
self::is_cdn_url( $image_url )
|| $is_wpcom_image
|| $is_wpcom_private_site
) {
Expand Down Expand Up @@ -245,6 +240,28 @@ public static function cdn_url( $image_url, $args = array(), $scheme = null ) {
return self::cdn_url_scheme( $photon_url, $scheme );
}

/**
* Checks if a given URL is a Photon URL.
*
* @since $$next-version$$
* @param string $url The URL to check.
haqadn marked this conversation as resolved.
Show resolved Hide resolved
* @return bool True if the URL is a Photon URL, false otherwise.
*/
public static function is_cdn_url( $url ) {
$parsed_url = wp_parse_url( $url );

if ( ! $parsed_url ) {
return false;
}

// See usage in ::cdn_url for documentation of this filter
$custom_photon_url = apply_filters( 'jetpack_photon_domain', '', $url );
$custom_photon_url = esc_url( $custom_photon_url );

return in_array( $parsed_url['host'], array( 'i0.wp.com', 'i1.wp.com', 'i2.wp.com' ), true )
|| wp_parse_url( $custom_photon_url, PHP_URL_HOST ) === $parsed_url['host'];
}

/**
* Parses WP.com-hosted image args to replicate the crop.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,24 @@ public function test_photon_url_filter_network_path_photonized_to_https() {
$this->assertEquals( 'https://photon.test/example.com/img.jpg', $url );
}

/**
* @covers ::Image_CDN_Core::is_cdn_url
* @since $$next-version$$
* @group jetpack_photon_filter_network_path
*/
public function test_is_cdn_url_method() {
$this->apply_custom_domain( '//photon.test' );
$this->assertTrue( Image_CDN_Core::is_cdn_url( '//photon.test/example.com/img.jpg' ) );

$this->assertTrue( Image_CDN_Core::is_cdn_url( 'https://i0.wp.com/example.com/img.jpg' ) );
$this->assertTrue( Image_CDN_Core::is_cdn_url( 'http://i1.wp.com/example.com/img.jpg' ) );
$this->assertTrue( Image_CDN_Core::is_cdn_url( '//i2.wp.com/example.com/img.jpg' ) );
$this->assertFalse( Image_CDN_Core::is_cdn_url( '//i3.wp.com/example.com/img.jpg' ) );
$this->assertFalse( Image_CDN_Core::is_cdn_url( 'http://example.com/img.jpg' ) );
$this->assertFalse( Image_CDN_Core::is_cdn_url( 'https://example.com/img.jpg' ) );
$this->assertFalse( Image_CDN_Core::is_cdn_url( '//example.com/img.jpg' ) );
}

/**
* @author aduth
* @covers ::Image_CDN_Core::cdn_url_scheme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ public static function handle_proxy() {
wp_send_json_error( 'Invalid URL', 400 );
}

$photon_url = Image_CDN_Core::cdn_url( $proxy_url );
$photon_url_domain = wp_parse_url( $photon_url, PHP_URL_HOST );
$photon_domain = wp_parse_url( apply_filters( 'jetpack_photon_domain', 'https://i0.wp.com' ), PHP_URL_HOST );
if ( $photon_url_domain !== $photon_domain ) {
$photon_url = Image_CDN_Core::cdn_url( $proxy_url );
if ( ! Image_CDN_Core::is_cdn_url( $proxy_url ) ) {
wp_send_json_error( 'Failed to proxy the image.', 400 );
}

Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/boost/changelog/improve-cdn-url-check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Image Guide: Improve check for Jetpack Image CDN URLs
Loading