diff --git a/inc/media_offload.php b/inc/media_offload.php index c0080bcb..785c2a2b 100644 --- a/inc/media_offload.php +++ b/inc/media_offload.php @@ -6,6 +6,7 @@ * @author Optimole */ +use OptimoleWP\Media\UploadsPath; use OptimoleWP\Offload\Loader; use Optimole\Sdk\Exception\InvalidArgumentException; use Optimole\Sdk\Exception\InvalidUploadApiResponseException; @@ -1463,7 +1464,8 @@ public function generate_image_meta( $meta, $attachment_id ) { do_action( 'optml_log', 'file after replace' ); do_action( 'optml_log', $local_file ); } - if ( ! file_exists( $local_file ) ) { + // The path comes from attachment metadata, so a file outside the uploads directory is treated as missing. + if ( ! file_exists( $local_file ) || ! UploadsPath::contains( $local_file ) ) { update_post_meta( $attachment_id, self::META_KEYS['offload_error'], 'true' ); do_action( 'optml_log', 'missing file' ); do_action( 'optml_log', $local_file ); @@ -1616,7 +1618,8 @@ public function generate_image_meta( $meta, $attachment_id ) { if ( isset( $meta['sizes'] ) ) { foreach ( $meta['sizes'] as $key => $value ) { $generated_image_size_path = str_replace( $original_name, $meta['sizes'][ $key ]['file'], $local_file ); - file_exists( $generated_image_size_path ) && unlink( $generated_image_size_path ); + // Size files come from attachment metadata too, so only those inside the uploads directory are deleted. + file_exists( $generated_image_size_path ) && UploadsPath::contains( $generated_image_size_path ) && unlink( $generated_image_size_path ); $meta['sizes'][ $key ]['file'] = $file_name; } } @@ -1626,7 +1629,8 @@ public function generate_image_meta( $meta, $attachment_id ) { if ( isset( $meta['original_image'] ) ) { $ext = $this->get_ext( $local_file ); $scaled_path = str_replace( '.' . $ext, '-scaled.' . $ext, $local_file ); - file_exists( $scaled_path ) && unlink( $scaled_path ); + // The extension is replaced in the whole path, so folder names change too. + file_exists( $scaled_path ) && UploadsPath::contains( $scaled_path ) && unlink( $scaled_path ); } $duplicated_images = apply_filters( 'optml_offload_duplicated_images', [], $attachment_id ); diff --git a/inc/media_rename/attachment_db_renamer.php b/inc/media_rename/attachment_db_renamer.php index 5d5d9aba..ce992e6d 100644 --- a/inc/media_rename/attachment_db_renamer.php +++ b/inc/media_rename/attachment_db_renamer.php @@ -26,6 +26,13 @@ class Optml_Attachment_Db_Renamer { */ private $skip_columns = [ 'user_pass' ]; + /** + * Size suffixes, such as `150x150`, whose URLs are renamed, as keys; null renames every size + * + * @var array|null + */ + private $renamed_size_suffixes = null; + /** * Handle image size variations * @@ -35,9 +42,13 @@ class Optml_Attachment_Db_Renamer { /** * Constructor + * + * @param bool $skip_sizes Whether size and scaled variations are left unchanged. + * @param string[]|null $renamed_size_suffixes Size suffixes, such as `150x150`, whose URLs are renamed; null renames every size. */ - public function __construct( $skip_sizes = false ) { + public function __construct( $skip_sizes = false, ?array $renamed_size_suffixes = null ) { $this->handle_image_sizes = ! $skip_sizes; + $this->renamed_size_suffixes = null === $renamed_size_suffixes ? null : array_flip( $renamed_size_suffixes ); } /** @@ -479,6 +490,9 @@ private function replace_image_urls( $content, $old_url, $new_url ) { function ( $matches ) use ( $old_base, $new_base, $old_domain, $new_domain, $old_dir, $new_dir, $old_ext, $new_ext ) { // Extract the size part (e.g., -300x200) $size_part = substr( $matches[0], strlen( $old_domain . $old_dir . '/' . $old_base ), -strlen( '.' . $old_ext ) ); + if ( null !== $this->renamed_size_suffixes && ! isset( $this->renamed_size_suffixes[ ltrim( $size_part, '-' ) ] ) ) { + return $matches[0]; + } // Build the new URL with the same size return $new_domain . $new_dir . '/' . $new_base . $size_part . '.' . $new_ext; @@ -505,6 +519,9 @@ function ( $matches ) use ( $new_base, $new_domain, $new_dir, $new_ext ) { function ( $matches ) use ( $old_base, $new_base, $old_domain, $new_domain, $old_dir, $new_dir, $old_ext, $new_ext ) { // Extract the size part (e.g., -300x200) $size_part = substr( $matches[0], strlen( str_replace( '/', '\/', $old_domain . $old_dir . '/' . $old_base ) ), -strlen( '.' . $old_ext ) ); + if ( null !== $this->renamed_size_suffixes && ! isset( $this->renamed_size_suffixes[ ltrim( $size_part, '-' ) ] ) ) { + return $matches[0]; + } // Build the new URL with the same size return str_replace( '/', '\/', $new_domain . $new_dir . '/' . $new_base . $size_part . '.' . $new_ext ); diff --git a/inc/media_rename/attachment_rename.php b/inc/media_rename/attachment_rename.php index 15cba758..94d5f2fe 100644 --- a/inc/media_rename/attachment_rename.php +++ b/inc/media_rename/attachment_rename.php @@ -1,5 +1,7 @@ + */ + private $moved_size_suffixes = []; + /** * Constructor. * @@ -65,6 +74,11 @@ public function rename() { global $wp_filesystem; + // Paths come from attachment metadata, so only files inside the uploads directory are moved. + if ( ! UploadsPath::contains( $file_path ) || ! UploadsPath::contains( $new_file_path ) ) { + return new WP_Error( 'optml_attachment_path_outside_uploads', __( 'Error renaming file.', 'optimole-wp' ) ); + } + // Bail if original file doesn't exist. if ( ! $wp_filesystem->exists( $file_path ) ) { return new WP_Error( 'optml_attachment_file_not_found', __( 'Error renaming file.', 'optimole-wp' ) ); @@ -87,7 +101,9 @@ public function rename() { $new_scaled_file_path = $base_dir . $scaled_new_file_with_ext; // Move the scaled image. We also override any leftover scaled files. - $wp_filesystem->move( $scaled_old_file_path, $new_scaled_file_path, true ); + if ( UploadsPath::contains( $scaled_old_file_path ) && UploadsPath::contains( $new_scaled_file_path ) ) { + $wp_filesystem->move( $scaled_old_file_path, $new_scaled_file_path, true ); + } } // Update attachment metadata @@ -100,7 +116,7 @@ public function rename() { try { $skip_sizes = ! $this->attachment->is_image(); - $replacer = new Optml_Attachment_Db_Renamer( $skip_sizes ); + $replacer = new Optml_Attachment_Db_Renamer( $skip_sizes, $this->moved_size_suffixes ); $old_url = $this->attachment->get_main_url(); $new_url = $this->get_new_url( $new_unique_filename ); @@ -186,6 +202,9 @@ private function update_attachment_metadata( $new_path ) { $old_size_file_path = sprintf( '%s/%s', $this->attachment->get_dir_path(), $size_data['file'] ); $new_size_file_path = sprintf( '%s/%s', $this->attachment->get_dir_path(), $new_size_file ); + if ( ! UploadsPath::contains( $old_size_file_path ) || ! UploadsPath::contains( $new_size_file_path ) ) { + continue; + } $move = $wp_filesystem->move( $old_size_file_path, $new_size_file_path ); @@ -194,6 +213,11 @@ private function update_attachment_metadata( $new_path ) { $metadata['sizes'][ $size ]['file'] = $new_size_file; $already_moved_paths = array_unique( $already_moved_paths ); } + // Links are renamed only for size files this rename created, so other links keep working. A path + // moved for an earlier size creates no file with this suffix. + if ( $move ) { + $this->moved_size_suffixes[] = $size_suffix; + } } } diff --git a/inc/media_rename/attachment_replace.php b/inc/media_rename/attachment_replace.php index 31424def..b79500c8 100644 --- a/inc/media_rename/attachment_replace.php +++ b/inc/media_rename/attachment_replace.php @@ -1,5 +1,7 @@ file['name'] ); @@ -186,7 +193,8 @@ private function remove_all_image_sizes() { global $wp_filesystem; foreach ( $all_image_sizes_paths as $path ) { - if ( file_exists( $path ) ) { + // Paths come from attachment metadata, so only files inside the uploads directory are deleted. + if ( file_exists( $path ) && UploadsPath::contains( $path ) ) { $wp_filesystem->delete( $path ); } } @@ -223,7 +231,7 @@ private function handle_scaled_images() { // replace the old year/month/item-scaled.ext with the new year/month/item.ext $scaled_path = str_replace( $unscaled_file, $old_scaled_file, $this->attachment->get_source_file_path() ); - if ( file_exists( $scaled_path ) ) { + if ( file_exists( $scaled_path ) && UploadsPath::contains( $scaled_path ) ) { $wp_filesystem->delete( $scaled_path ); } diff --git a/inc/v2/Media/UploadsPath.php b/inc/v2/Media/UploadsPath.php new file mode 100644 index 00000000..1e4ba22c --- /dev/null +++ b/inc/v2/Media/UploadsPath.php @@ -0,0 +1,43 @@ +test_rename( self::$scaled_id, self::$scaled_model, 'big-file-rename', true ); } + public function test_rename_leaves_size_files_outside_uploads_in_place(): void { + $id = self::factory()->attachment->create_upload_object( OPTML_PATH . 'tests/assets/rename-unscaled.jpg' ); + $outside = self::outside_uploads_file( 'optml-outside-size.txt' ); + $meta = wp_get_attachment_metadata( $id ); + $meta['sizes']['thumbnail']['file'] = self::relative_from( ( new Optml_Attachment_Model( $id ) )->get_dir_path(), $outside ); + wp_update_attachment_metadata( $id, $meta ); + + ( new Optml_Attachment_Rename( $id, 'outside-size-renamed' ) )->rename(); + + $this->assertFileExists( $outside, 'A size file outside uploads must not be moved.' ); + wp_delete_post( $id, true ); + unlink( $outside ); + } + + /** + * Tampered metadata can change the size numbers too. + * + * @return array + */ + public function size_width_changes(): array { + return [ + 'same width' => [ 0 ], + 'changed width' => [ 1 ], + ]; + } + + /** + * @dataProvider size_width_changes + */ + public function test_rename_keeps_links_to_a_size_left_outside_uploads( int $width_change ): void { + $id = self::factory()->attachment->create_upload_object( OPTML_PATH . 'tests/assets/rename-unscaled.jpg' ); + $thumbnail_url = wp_get_attachment_image_url( $id, 'thumbnail' ); + $medium_url = wp_get_attachment_image_url( $id, 'medium' ); + $post_id = $this->post_linking( $thumbnail_url, $medium_url ); + $outside = self::outside_uploads_file( 'optml-outside-linked-size.txt' ); + $meta = wp_get_attachment_metadata( $id ); + $thumbnail = $meta['sizes']['thumbnail']['file']; + $meta['sizes']['thumbnail']['file'] = self::relative_from( ( new Optml_Attachment_Model( $id ) )->get_dir_path(), $outside ); + $meta['sizes']['thumbnail']['width'] += $width_change; + wp_update_attachment_metadata( $id, $meta ); + + ( new Optml_Attachment_Rename( $id, 'outside-linked-renamed' ) )->rename(); + clean_post_cache( $post_id ); + + $content = get_post( $post_id )->post_content; + $this->assertStringContainsString( $thumbnail_url, $content ); + $this->assertStringContainsString( wp_json_encode( $thumbnail_url ), $content ); + $this->assertStringNotContainsString( $medium_url, $content ); + $this->assertStringNotContainsString( wp_json_encode( $medium_url ), $content ); + $this->assertFileExists( ( new Optml_Attachment_Model( $id ) )->get_dir_path() . '/' . $thumbnail ); + wp_delete_post( $id, true ); + unlink( $outside ); + } + + public function test_rename_updates_links_to_a_size_moved_under_another_name(): void { + $id = self::factory()->attachment->create_upload_object( OPTML_PATH . 'tests/assets/rename-unscaled.jpg' ); + $thumbnail_url = wp_get_attachment_image_url( $id, 'thumbnail' ); + $post_id = $this->post_linking( $thumbnail_url ); + $outside = self::outside_uploads_file( 'optml-outside-twin-size.txt' ); + $meta = wp_get_attachment_metadata( $id ); + $meta['sizes']['optml_twin'] = $meta['sizes']['thumbnail']; + $meta['sizes']['thumbnail']['file'] = self::relative_from( ( new Optml_Attachment_Model( $id ) )->get_dir_path(), $outside ); + wp_update_attachment_metadata( $id, $meta ); + + ( new Optml_Attachment_Rename( $id, 'outside-twin-renamed' ) )->rename(); + clean_post_cache( $post_id ); + + $this->assertStringNotContainsString( $thumbnail_url, get_post( $post_id )->post_content ); + wp_delete_post( $id, true ); + unlink( $outside ); + } + + public function test_rename_keeps_links_to_a_size_whose_file_another_size_moved(): void { + $id = self::factory()->attachment->create_upload_object( OPTML_PATH . 'tests/assets/rename-unscaled.jpg' ); + $thumbnail_url = wp_get_attachment_image_url( $id, 'thumbnail' ); + $post_id = $this->post_linking( $thumbnail_url ); + $outside = self::outside_uploads_file( 'optml-outside-shared-size.txt' ); + $meta = wp_get_attachment_metadata( $id ); + $thumbnail = $meta['sizes']['thumbnail']; + // A size with the thumbnail dimensions that names the medium file, which the medium size moves first. + $meta['sizes']['optml_twin'] = array_merge( $meta['sizes']['medium'], [ 'width' => $thumbnail['width'], 'height' => $thumbnail['height'] ] ); + $meta['sizes']['thumbnail']['file'] = self::relative_from( ( new Optml_Attachment_Model( $id ) )->get_dir_path(), $outside ); + wp_update_attachment_metadata( $id, $meta ); + + ( new Optml_Attachment_Rename( $id, 'outside-shared-renamed' ) )->rename(); + clean_post_cache( $post_id ); + + $this->assertStringContainsString( $thumbnail_url, get_post( $post_id )->post_content ); + $this->assertFileExists( ( new Optml_Attachment_Model( $id ) )->get_dir_path() . '/' . $thumbnail['file'] ); + wp_delete_post( $id, true ); + unlink( $outside ); + } + + /** + * Create a post that links each URL as an image and in JSON block attributes. + * + * The content is written directly, because saving a post strips the escaped slashes of the JSON. + * + * @param string ...$urls Image URLs. + * + * @return int The post ID. + */ + private function post_linking( string ...$urls ): int { + global $wpdb; + + $content = ''; + foreach ( $urls as $url ) { + $content .= ''; + } + $post_id = self::factory()->post->create(); + $wpdb->update( $wpdb->posts, [ 'post_content' => $content ], [ 'ID' => $post_id ] ); + + return $post_id; + } + + public function test_rename_refuses_a_main_file_outside_uploads(): void { + $id = self::factory()->attachment->create_upload_object( OPTML_PATH . 'tests/assets/rename-unscaled.jpg' ); + $outside = self::outside_uploads_file( 'optml-outside-main.jpg' ); + update_post_meta( $id, '_wp_attached_file', self::relative_from( wp_get_upload_dir()['basedir'], $outside ) ); + + $result = ( new Optml_Attachment_Rename( $id, 'outside-main-renamed' ) )->rename(); + + $this->assertWPError( $result ); + $this->assertFileExists( $outside, 'A main file outside uploads must not be moved.' ); + wp_delete_post( $id, true ); + unlink( $outside ); + } + private function test_rename( $id, $model, $new_filename, $scaled = false ) { $renamer = new Optml_Attachment_Rename( $id, $new_filename ); $result = $renamer->rename(); diff --git a/tests/media_rename/test-attachment-replace.php b/tests/media_rename/test-attachment-replace.php index b5007b14..b419b8d0 100644 --- a/tests/media_rename/test-attachment-replace.php +++ b/tests/media_rename/test-attachment-replace.php @@ -1,4 +1,6 @@ unscaled protected static $unscaled_scaled_id; // unscaled -> scaled protected static $scaled_scaled_id; // scaled -> scaled @@ -109,6 +113,131 @@ private function test_replace_unscaled_to_unscaled() { $this->do_replace_test( self::$unscaled_unscaled_id, $replace_file, false, false ); } + public function test_replace_does_not_delete_size_files_outside_uploads() { + global $wp_filesystem; + + $id = self::factory()->attachment->create_upload_object( OPTML_PATH . 'tests/assets/sample-test.jpg' ); + $outside = self::outside_uploads_file( 'optml-outside-replace.txt' ); + $meta = wp_get_attachment_metadata( $id ); + $meta['sizes']['thumbnail']['file'] = self::relative_from( ( new Optml_Attachment_Model( $id ) )->get_dir_path(), $outside ); + wp_update_attachment_metadata( $id, $meta ); + + $tmp_file = self::FILESTASH . 'replace-outside.jpg'; + $wp_filesystem->copy( OPTML_PATH . 'tests/assets/small-1.jpg', $tmp_file, true ); + ( new Optml_Attachment_Replace( + $id, + [ + 'name' => 'replace-outside.jpg', + 'type' => 'image/jpeg', + 'tmp_name' => $tmp_file, + ] + ) )->replace(); + + $this->assertFileExists( $outside, 'A size file outside uploads must not be deleted.' ); + wp_delete_post( $id, true ); + unlink( $outside ); + } + + public function test_replace_refuses_a_main_file_outside_uploads() { + global $wp_filesystem; + + $id = self::factory()->attachment->create_upload_object( OPTML_PATH . 'tests/assets/sample-test.jpg' ); + $outside = self::outside_uploads_file( 'optml-outside-replace-main.jpg', OPTML_PATH . 'tests/assets/sample-test.jpg' ); + $before = md5_file( $outside ); + update_post_meta( $id, '_wp_attached_file', self::relative_from( wp_get_upload_dir()['basedir'], $outside ) ); + + $tmp_file = self::FILESTASH . 'replace-outside-main.jpg'; + $wp_filesystem->copy( OPTML_PATH . 'tests/assets/small-1.jpg', $tmp_file, true ); + $result = ( new Optml_Attachment_Replace( + $id, + [ + 'name' => 'replace-outside-main.jpg', + 'type' => 'image/jpeg', + 'tmp_name' => $tmp_file, + ] + ) )->replace(); + + $this->assertWPError( $result ); + $this->assertSame( 'optml_attachment_path_outside_uploads', $result->get_error_code() ); + $this->assertSame( $before, md5_file( $outside ), 'A main file outside uploads must not be overwritten.' ); + wp_delete_post( $id, true ); + unlink( $outside ); + file_exists( $tmp_file ) && unlink( $tmp_file ); + } + + public function test_replace_refuses_an_original_image_outside_uploads() { + global $wp_filesystem; + + $id = self::factory()->attachment->create_upload_object( OPTML_PATH . 'tests/assets/sample-test.jpg' ); + $outside = self::outside_uploads_file( 'optml-outside-replace-original.jpg', OPTML_PATH . 'tests/assets/sample-test.jpg' ); + $before = md5_file( $outside ); + $meta = wp_get_attachment_metadata( $id ); + $meta['original_image'] = self::relative_from( dirname( get_attached_file( $id ) ), $outside ); + wp_update_attachment_metadata( $id, $meta ); + + $tmp_file = self::FILESTASH . 'replace-outside-original.jpg'; + $wp_filesystem->copy( OPTML_PATH . 'tests/assets/small-1.jpg', $tmp_file, true ); + $result = ( new Optml_Attachment_Replace( + $id, + [ + 'name' => 'replace-outside-original.jpg', + 'type' => 'image/jpeg', + 'tmp_name' => $tmp_file, + ] + ) )->replace(); + + $this->assertWPError( $result ); + $this->assertSame( 'optml_attachment_path_outside_uploads', $result->get_error_code() ); + $this->assertSame( $before, md5_file( $outside ), 'An original image outside uploads must not be overwritten.' ); + wp_delete_post( $id, true ); + unlink( $outside ); + file_exists( $tmp_file ) && unlink( $tmp_file ); + } + + public function test_replace_keeps_a_scaled_file_outside_uploads() { + global $wp_filesystem; + + $id = self::factory()->attachment->create_upload_object( OPTML_PATH . 'tests/assets/sample-test.jpg' ); + // The uploads folder name holds the file name, so the old scaled path built from it points to another folder. + $basedir = WP_CONTENT_DIR . '/optml-replace-photo.jpg'; + $outside = WP_CONTENT_DIR . '/optml-replace-photo-scaled.jpg/replace-photo-scaled.jpg'; + wp_mkdir_p( $basedir ); + wp_mkdir_p( dirname( $outside ) ); + copy( OPTML_PATH . 'tests/assets/sample-test.jpg', $basedir . '/replace-photo.jpg' ); + copy( OPTML_PATH . 'tests/assets/sample-test.jpg', $basedir . '/replace-photo-scaled.jpg' ); + file_put_contents( $outside, 'keep' ); + $uploads = static function ( array $dir ) use ( $basedir ): array { + $dir['basedir'] = $basedir; + + return $dir; + }; + add_filter( 'upload_dir', $uploads ); + update_post_meta( $id, '_wp_attached_file', 'replace-photo-scaled.jpg' ); + $meta = wp_get_attachment_metadata( $id ); + $meta['file'] = 'replace-photo-scaled.jpg'; + $meta['original_image'] = 'replace-photo.jpg'; + wp_update_attachment_metadata( $id, $meta ); + + $tmp_file = self::FILESTASH . 'replace-photo.jpg'; + $wp_filesystem->copy( OPTML_PATH . 'tests/assets/small-1.jpg', $tmp_file, true ); + ( new Optml_Attachment_Replace( + $id, + [ + 'name' => 'replace-photo.jpg', + 'type' => 'image/jpeg', + 'tmp_name' => $tmp_file, + ] + ) )->replace(); + remove_filter( 'upload_dir', $uploads ); + + $this->assertFileExists( $outside, 'A scaled file outside uploads must not be deleted.' ); + wp_delete_post( $id, true ); + unlink( $outside ); + rmdir( dirname( $outside ) ); + array_map( 'unlink', glob( $basedir . '/*' ) ); + rmdir( $basedir ); + } + /** * A 0600 upload tmp file must not leave the replaced attachment unreadable to the web server. */ diff --git a/tests/media_rename/test-db-renamer.php b/tests/media_rename/test-db-renamer.php index 34776908..13b1e202 100644 --- a/tests/media_rename/test-db-renamer.php +++ b/tests/media_rename/test-db-renamer.php @@ -284,6 +284,20 @@ public function test_static__ensure_sizes() { } } + public function test_static__renames_only_listed_sizes() { + $old_url = 'http://om-wp.test/wp-content/uploads/2025/03/asdfgh.jpg'; + $new_url = 'http://om-wp.test/wp-content/uploads/2025/03/listed-sizes.jpg'; + + $listed = self::$replace_method->invoke( new Optml_Attachment_Db_Renamer( false, [ '300x200' ] ), self::POST_CONTENT, $old_url, $new_url ); + $none = self::$replace_method->invoke( new Optml_Attachment_Db_Renamer( false, [] ), self::POST_CONTENT, $old_url, $new_url ); + + $this->assertStringContainsString( 'listed-sizes-300x200.jpg', $listed ); + $this->assertStringContainsString( 'asdfgh-150x150.jpg', $listed ); + $this->assertStringNotContainsString( 'listed-sizes-150x150.jpg', $listed ); + $this->assertStringContainsString( 'asdfgh-300x200.jpg', $none ); + $this->assertStringContainsString( 'asdfgh-150x150.jpg', $none ); + } + public function test_db__post_content_video_replacement() { $initial_url = 'http://om-wp.test/wp-content/uploads/2025/03/video-file.mp4'; $new_url = 'http://example.com/wp-content/uploads/2020/01/new-home-made-tutorial.mp4'; diff --git a/tests/outside-uploads-files.php b/tests/outside-uploads-files.php new file mode 100644 index 00000000..769e31c3 --- /dev/null +++ b/tests/outside-uploads-files.php @@ -0,0 +1,36 @@ + '; use Optml_Dam_Offload_Utils; + use Optml_Test_Outside_Uploads; public static $files = [ 'sample-test', '1PQ7p', @@ -255,6 +258,63 @@ public function test_upload_during_rollback_stays_local(): void { $this->assertFileExists( get_attached_file( $attachment_id ) ); } + public function test_offload_treats_a_main_file_outside_uploads_as_missing(): void { + Optml_Media_Offload::instance()->rollback_and_update_images( [ self::$sample_attachement ] ); + $outside = self::outside_uploads_file( 'optml-outside-offload.jpg', OPTML_PATH . 'tests/assets/sample-test.jpg' ); + update_post_meta( self::$sample_attachement, '_wp_attached_file', self::relative_from( wp_get_upload_dir()['basedir'], $outside ) ); + + Optml_Media_Offload::instance()->upload_and_update_existing_images( [ self::$sample_attachement ] ); + + $this->assertSame( 'true', get_post_meta( self::$sample_attachement, Optml_Media_Offload::META_KEYS['offload_error'], true ) ); + $this->assertFileExists( $outside, 'A main file outside uploads must not be offloaded and deleted.' ); + unlink( $outside ); + } + + public function test_offload_keeps_size_files_outside_uploads(): void { + Optml_Media_Offload::instance()->rollback_and_update_images( [ self::$sample_attachement ] ); + $outside = self::outside_uploads_file( 'optml-outside-offload-size.jpg' ); + $meta = wp_get_attachment_metadata( self::$sample_attachement ); + $meta['sizes']['thumbnail']['file'] = self::relative_from( dirname( get_attached_file( self::$sample_attachement ) ), $outside ); + wp_update_attachment_metadata( self::$sample_attachement, $meta ); + + Optml_Media_Offload::instance()->upload_and_update_existing_images( [ self::$sample_attachement ] ); + + $this->assertTrue( Optml_Media_Offload::is_uploaded_image( wp_get_attachment_metadata( self::$sample_attachement )['file'] ) ); + $this->assertFileExists( $outside, 'A size file outside uploads must not be deleted.' ); + unlink( $outside ); + } + + public function test_offload_keeps_a_scaled_file_outside_uploads(): void { + Optml_Media_Offload::instance()->rollback_and_update_images( [ self::$sample_attachement ] ); + // The uploads folder name holds the extension, so the scaled path built from it points to another folder. + $basedir = WP_CONTENT_DIR . '/optml-media.jpg'; + $outside = WP_CONTENT_DIR . '/optml-media-scaled.jpg/sample-test-scaled.jpg'; + wp_mkdir_p( $basedir ); + wp_mkdir_p( dirname( $outside ) ); + copy( get_attached_file( self::$sample_attachement ), $basedir . '/sample-test.jpg' ); + file_put_contents( $outside, 'keep' ); + $uploads = static function ( array $dir ) use ( $basedir ): array { + $dir['basedir'] = $basedir; + + return $dir; + }; + add_filter( 'upload_dir', $uploads ); + update_post_meta( self::$sample_attachement, '_wp_attached_file', 'sample-test.jpg' ); + $meta = wp_get_attachment_metadata( self::$sample_attachement ); + $meta['original_image'] = 'sample-test-original.jpg'; + wp_update_attachment_metadata( self::$sample_attachement, $meta ); + + Optml_Media_Offload::instance()->upload_and_update_existing_images( [ self::$sample_attachement ] ); + remove_filter( 'upload_dir', $uploads ); + + $this->assertTrue( Optml_Media_Offload::is_uploaded_image( wp_get_attachment_metadata( self::$sample_attachement )['file'] ) ); + $this->assertFileExists( $outside, 'A scaled file outside uploads must not be deleted.' ); + unlink( $outside ); + rmdir( dirname( $outside ) ); + file_exists( $basedir . '/sample-test.jpg' ) && unlink( $basedir . '/sample-test.jpg' ); + rmdir( $basedir ); + } + public function test_retryable_error() { Optml_Media_Offload::instance()->rollback_and_update_images([self::$sample_attachement]); self::$error_mark = true; diff --git a/tests/test-uploads-path.php b/tests/test-uploads-path.php new file mode 100644 index 00000000..5fa0ce51 --- /dev/null +++ b/tests/test-uploads-path.php @@ -0,0 +1,51 @@ +assertTrue( UploadsPath::contains( wp_get_upload_dir()['basedir'] . '/photo.jpg' ) ); + } + + public function test_file_reached_with_dot_dot_is_refused(): void { + $this->assertFalse( UploadsPath::contains( wp_get_upload_dir()['basedir'] . '/../wp-config.php' ) ); + } + + public function test_path_ending_in_dot_dot_is_refused(): void { + $this->assertFalse( UploadsPath::contains( wp_get_upload_dir()['basedir'] . '/..' ) ); + $this->assertFalse( UploadsPath::contains( wp_get_upload_dir()['basedir'] . '/.' ) ); + } + + public function test_backslash_in_a_folder_name_is_not_taken_as_a_separator(): void { + if ( '\\' === DIRECTORY_SEPARATOR ) { + $this->markTestSkipped( 'A backslash is a separator on Windows.' ); + } + $basedir = wp_get_upload_dir()['basedir']; + $sibling = $basedir . '\\private'; + $child = $basedir . '/a\\b'; + wp_mkdir_p( $sibling ); + wp_mkdir_p( $child ); + + $sibling_file = UploadsPath::contains( $basedir . '/../' . basename( $sibling ) . '/victim.jpg' ); + $child_file = UploadsPath::contains( $child . '/photo.jpg' ); + rmdir( $sibling ); + rmdir( $child ); + + $this->assertFalse( $sibling_file ); + $this->assertTrue( $child_file ); + } + + public function test_file_in_a_folder_that_does_not_exist_is_refused(): void { + $this->assertFalse( UploadsPath::contains( wp_get_upload_dir()['basedir'] . '/optml-missing-folder/photo.jpg' ) ); + } +}