From 9b21c707aea74a4be566a54bcb8f0d5f81f8b34f Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Tue, 14 Jan 2025 11:29:53 +0200 Subject: [PATCH] Image CDN: Prevent URLs from being double encoded (#40886) --- .../packages/image-cdn/changelog/fix-double-encoding | 4 ++++ .../packages/image-cdn/src/class-image-cdn-core.php | 1 + .../image-cdn/tests/php/test_class.image_cdn_core.php | 10 ++++++++++ .../boost/changelog/fix-image-cdn-double-encoding | 4 ++++ .../jetpack/changelog/fix-image-cdn-double-encoding | 4 ++++ 5 files changed, 23 insertions(+) create mode 100644 projects/packages/image-cdn/changelog/fix-double-encoding create mode 100644 projects/plugins/boost/changelog/fix-image-cdn-double-encoding create mode 100644 projects/plugins/jetpack/changelog/fix-image-cdn-double-encoding diff --git a/projects/packages/image-cdn/changelog/fix-double-encoding b/projects/packages/image-cdn/changelog/fix-double-encoding new file mode 100644 index 0000000000000..cacc8cdc6aefb --- /dev/null +++ b/projects/packages/image-cdn/changelog/fix-double-encoding @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +General: Ensure that double encoding doesn't happen. 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 693a4d1431c7b..49c472bb292b1 100644 --- a/projects/packages/image-cdn/src/class-image-cdn-core.php +++ b/projects/packages/image-cdn/src/class-image-cdn-core.php @@ -273,6 +273,7 @@ public static function is_cdn_url( $url ) { */ private static function escape_path( $path ) { $parts = explode( '/', $path ); + $parts = array_map( 'rawurldecode', $parts ); $parts = array_map( 'rawurlencode', $parts ); return implode( '/', $parts ); } 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 0b05e96c3b837..57f205e01d0c4 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 @@ -295,6 +295,16 @@ public function test_photon_url_filter_url_encodes_path_parts() { $this->assertEquals( 'https://i0.wp.com/example.com/narrow%E2%80%AFno-break%E2%80%AFspace/name%20with%20spaces.jpg', $url ); } + /** + * @covers Automattic\Jetpack\Image_CDN\Image_CDN_Core::cdn_url + * @since $$next-version$$ + * @group jetpack_photon_filter_url_encoding + */ + public function test_photon_url_filter_encoded_url_should_not_be_encoded_again() { + $url = Image_CDN_Core::cdn_url( '//example.com/image%20with%20spaces.jpg', array(), 'https' ); + $this->assertEquals( 'https://i0.wp.com/example.com/image%20with%20spaces.jpg', $url ); + } + /** * @author aduth * @covers Automattic\Jetpack\Image_CDN\Image_CDN_Core::cdn_url_scheme diff --git a/projects/plugins/boost/changelog/fix-image-cdn-double-encoding b/projects/plugins/boost/changelog/fix-image-cdn-double-encoding new file mode 100644 index 0000000000000..da2382199f128 --- /dev/null +++ b/projects/plugins/boost/changelog/fix-image-cdn-double-encoding @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Image CDN: Ensure that double encoding doesn't happen. diff --git a/projects/plugins/jetpack/changelog/fix-image-cdn-double-encoding b/projects/plugins/jetpack/changelog/fix-image-cdn-double-encoding new file mode 100644 index 0000000000000..101848bfb4810 --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-image-cdn-double-encoding @@ -0,0 +1,4 @@ +Significance: patch +Type: bugfix + +Photon: Fix double encoding image urls.