From 56f26f3c1a6169c72c6678049ac2cbbc39479334 Mon Sep 17 00:00:00 2001 From: Adnan Haque Date: Fri, 27 Sep 2024 17:21:13 +0600 Subject: [PATCH 1/5] rawurlencode each path part in the image URL --- .../image-cdn/src/class-image-cdn-core.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/projects/packages/image-cdn/src/class-image-cdn-core.php b/projects/packages/image-cdn/src/class-image-cdn-core.php index d577eeb68dfe2..5a1b2f7bb8709 100644 --- a/projects/packages/image-cdn/src/class-image-cdn-core.php +++ b/projects/packages/image-cdn/src/class-image-cdn-core.php @@ -196,7 +196,7 @@ public static function cdn_url( $image_url, $args = array(), $scheme = null ) { } } - $image_host_path = $image_url_parts['host'] . $image_url_parts['path']; + $image_host_path = $image_url_parts['host'] . static::escae_path( $image_url_parts['path'] ); /** * Filters the domain used by the Photon module. @@ -245,6 +245,22 @@ public static function cdn_url( $image_url, $args = array(), $scheme = null ) { return self::cdn_url_scheme( $photon_url, $scheme ); } + /** + * URL-encodes each path component. + * + * Example: + * Input: "foo/bar baz/baz" + * Output: "foo/bar%20baz/baz" + * + * @param string $path The path to escape. + * @return string The escaped path. + */ + private static function escae_path( $path ) { + $parts = explode( '/', $path ); + $parts = array_map( 'rawurlencode', $parts ); + return implode( '/', $parts ); + } + /** * Parses WP.com-hosted image args to replicate the crop. * From 7c3c441b860f45415f73ff8a31bb81808995b086 Mon Sep 17 00:00:00 2001 From: Adnan Haque Date: Fri, 27 Sep 2024 17:41:04 +0600 Subject: [PATCH 2/5] Fix a typo --- projects/packages/image-cdn/src/class-image-cdn-core.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/projects/packages/image-cdn/src/class-image-cdn-core.php b/projects/packages/image-cdn/src/class-image-cdn-core.php index 5a1b2f7bb8709..aa058b0732076 100644 --- a/projects/packages/image-cdn/src/class-image-cdn-core.php +++ b/projects/packages/image-cdn/src/class-image-cdn-core.php @@ -196,8 +196,7 @@ public static function cdn_url( $image_url, $args = array(), $scheme = null ) { } } - $image_host_path = $image_url_parts['host'] . static::escae_path( $image_url_parts['path'] ); - + $image_host_path = $image_url_parts['host'] . static::escape_path( $image_url_parts['path'] ); /** * Filters the domain used by the Photon module. * @@ -255,7 +254,7 @@ public static function cdn_url( $image_url, $args = array(), $scheme = null ) { * @param string $path The path to escape. * @return string The escaped path. */ - private static function escae_path( $path ) { + private static function escape_path( $path ) { $parts = explode( '/', $path ); $parts = array_map( 'rawurlencode', $parts ); return implode( '/', $parts ); From a48ad17b18f4813020337c7b0cdad36acdb0c166 Mon Sep 17 00:00:00 2001 From: Adnan Haque Date: Fri, 27 Sep 2024 17:41:17 +0600 Subject: [PATCH 3/5] Add a test --- .../image-cdn/tests/php/test_class.image_cdn_core.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/projects/packages/image-cdn/tests/php/test_class.image_cdn_core.php b/projects/packages/image-cdn/tests/php/test_class.image_cdn_core.php index 204075146cd8c..648b9476d1951 100644 --- a/projects/packages/image-cdn/tests/php/test_class.image_cdn_core.php +++ b/projects/packages/image-cdn/tests/php/test_class.image_cdn_core.php @@ -265,6 +265,17 @@ 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::cdn_url + * @since $$next-version$$ + * @group jetpack_photon_filter_url_encoding + */ + public function test_photon_url_filter_url_encodes_path_parts() { + $url = Image_CDN_Core::cdn_url( '//example.com/narrow no-break space/name with spaces.jpg', array(), 'https' ); + + $this->assertEquals( 'https://i0.wp.com/example.com/narrow%E2%80%AFno-break%E2%80%AFspace/name%20with%20spaces.jpg', $url ); + } + /** * @author aduth * @covers ::Image_CDN_Core::cdn_url_scheme From 1e7f60638c7e2e6b42ba32d1ac35ac9c8acfb1f0 Mon Sep 17 00:00:00 2001 From: Adnan Haque Date: Fri, 27 Sep 2024 17:44:18 +0600 Subject: [PATCH 4/5] changelog --- projects/packages/image-cdn/changelog/fix-photon-url-encoding | 4 ++++ projects/plugins/boost/changelog/fix-photon-url-encoding | 4 ++++ projects/plugins/jetpack/changelog/fix-photon-url-encoding | 4 ++++ 3 files changed, 12 insertions(+) create mode 100644 projects/packages/image-cdn/changelog/fix-photon-url-encoding create mode 100644 projects/plugins/boost/changelog/fix-photon-url-encoding create mode 100644 projects/plugins/jetpack/changelog/fix-photon-url-encoding diff --git a/projects/packages/image-cdn/changelog/fix-photon-url-encoding b/projects/packages/image-cdn/changelog/fix-photon-url-encoding new file mode 100644 index 0000000000000..c23cebccb8444 --- /dev/null +++ b/projects/packages/image-cdn/changelog/fix-photon-url-encoding @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +URL encode path parts of an image diff --git a/projects/plugins/boost/changelog/fix-photon-url-encoding b/projects/plugins/boost/changelog/fix-photon-url-encoding new file mode 100644 index 0000000000000..4f7de91427b89 --- /dev/null +++ b/projects/plugins/boost/changelog/fix-photon-url-encoding @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Image CDN: URL encode image path parts for RSS feed compatibility diff --git a/projects/plugins/jetpack/changelog/fix-photon-url-encoding b/projects/plugins/jetpack/changelog/fix-photon-url-encoding new file mode 100644 index 0000000000000..de3a486e45325 --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-photon-url-encoding @@ -0,0 +1,4 @@ +Significance: patch +Type: compat + +Image CDN: URL encode image path parts for RSS feed compatibility From 98d10b41ce1e9717df97aa5bddaa3e9b2d99cd09 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Mon, 7 Oct 2024 17:35:04 +0300 Subject: [PATCH 5/5] Add comment to clarify space usage --- .../packages/image-cdn/tests/php/test_class.image_cdn_core.php | 1 + 1 file changed, 1 insertion(+) diff --git a/projects/packages/image-cdn/tests/php/test_class.image_cdn_core.php b/projects/packages/image-cdn/tests/php/test_class.image_cdn_core.php index 2560849332836..d08e3ee153e20 100644 --- a/projects/packages/image-cdn/tests/php/test_class.image_cdn_core.php +++ b/projects/packages/image-cdn/tests/php/test_class.image_cdn_core.php @@ -289,6 +289,7 @@ public function test_is_cdn_url_method() { * @group jetpack_photon_filter_url_encoding */ public function test_photon_url_filter_url_encodes_path_parts() { + // The first two spaces are not standard spaces - https://www.compart.com/en/unicode/U+202F $url = Image_CDN_Core::cdn_url( '//example.com/narrow no-break space/name with spaces.jpg', array(), 'https' ); $this->assertEquals( 'https://i0.wp.com/example.com/narrow%E2%80%AFno-break%E2%80%AFspace/name%20with%20spaces.jpg', $url );