From a498b5af15552573c72deeb8b61ed89c0d50abb3 Mon Sep 17 00:00:00 2001 From: Javier Casares Date: Tue, 3 Nov 2026 16:44:19 +0000 Subject: [PATCH] v1.5.1 --- changelog.txt | 26 +++- idrivee2-media-upload.php | 4 +- includes/class-media-uploader.php | 221 +++++++++++++++++++++++++++++- readme.txt | 18 ++- vendor/composer/installed.php | 4 +- 5 files changed, 262 insertions(+), 11 deletions(-) diff --git a/changelog.txt b/changelog.txt index 40d23bc2..56161c91 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,22 @@ == Changelog == += 1.5.1 = + +_Release date: 2026-10-02_ + +**Fixed** + +* Remote name collisions no longer overwrite existing S3 objects: on the first upload of an attachment, the plugin probes the remote bucket (`headObject`) and, when the key is taken, renames the local files WordPress-style (`file.jpg` → `file-1.jpg`, `file-2.jpg`, …) before uploading, keeping metadata, `_wp_attached_file`, and GUID in sync. Previously a re-uploaded name silently overwrote the existing remote object (and every attachment pointing at it), because local files are deleted after upload and WordPress core deduplicates names against the local disk only. Re-uploads of an already-offloaded attachment keep their name (intentional overwrite). Non-404 `headObject` errors fall back to the previous upload-without-check behavior so uploads are never blocked + +**Compatibility** + +* WordPress: 5.3 - 7.1 (no new symbols) +* PHP: 8.1 - 8.5 + +**Tests** + +* PHPUnit: 95 tests, 207 assertions (7 new Media_Uploader tests covering rename-on-collision, local-candidate blocking, missing-original, and the `headObject` 404/NotFound/NoSuchKey/non-404 outcomes) + = 1.5.0 = _Release date: 2026-09-18_ @@ -7,6 +24,13 @@ _Release date: 2026-09-18_ **Highlights** * Multisite uninstall now cleans up every site, not just the current one +* Release artifact 7× smaller (37.5 MB → 5.3 MB) + +**Changed** + +* Release artifact slimmed from 37.5 MB to 5.3 MB: `bin/deploy.sh` now prunes the AWS SDK service models (`src/data`) to S3 only — the plugin never instantiates other clients, so ~250 MB of api models, docs, and endpoint tests for 450+ unused services is dead weight (the SDK ships both raw `.json` and compiled `.json.php` for every service). An offline smoke test (S3 presigned PutObject/HeadBucket/DeleteObject against the built ZIP) proves the slimmed artifact works at build time; `bin/preflight.sh` mirrors the prune and smoke so SDK layout changes fail pre-deploy +* `bin/deploy.sh`: replaced the `head -40` in the verify pipeline with `sed -n` — under `set -o pipefail` the SIGPIPE could abort the script after zipping but before restoring dev dependencies (exit 141, twice this session) +* `bin/deploy.sh`: exports `COMPOSER_ALLOW_SUPERUSER=1` — running as root without it makes composer disable the PHPCS installer plugin, leaving coding standards unregistered after deploy and failing the next preflight **Fixed** @@ -22,7 +46,7 @@ _Release date: 2026-09-18_ * PHP Coding Standards: 3.13.6 (0 errors) * WordPress Coding Standards: 3.4.1 (0 violations) * PHPStan: Level 9, 0 errors -* PHPUnit: 88 tests, 164 assertions (6 new uninstall tests: single-site opt-in/opt-out/corrupt, multisite per-site cleanup, empty site-list fallback) +* PHPUnit: 88 tests, 166 assertions (6 new uninstall tests: single-site opt-in/opt-out/corrupt, multisite per-site cleanup incl. no-limit `get_sites()` args assertions, empty site-list fallback) = 1.4.4 = diff --git a/idrivee2-media-upload.php b/idrivee2-media-upload.php index 4fa0dff5..b167e980 100644 --- a/idrivee2-media-upload.php +++ b/idrivee2-media-upload.php @@ -4,7 +4,7 @@ * Plugin URI: https://www.robotstxt.software/plugins/idrivee2-media-upload/ * Update URI: https://www.robotstxt.software/plugins/idrivee2-media-upload/ * Description: Uploads media files to iDrivee2 (S3-compatible) with enterprise-grade security and logging. - * Version: 1.5.0 + * Version: 1.5.1 * Requires at least: 5.3 * Requires PHP: 8.1 * Author: ROBOTSTXT @@ -35,7 +35,7 @@ if ( ! defined( 'ABSPATH' ) ) { * * @since 1.1.4 */ -define( 'IDRIVEE2_MEDIA_VERSION', '1.5.0' ); +define( 'IDRIVEE2_MEDIA_VERSION', '1.5.1' ); /** * Load Composer autoloader if available. diff --git a/includes/class-media-uploader.php b/includes/class-media-uploader.php index dc5e7b55..0f49a46c 100644 --- a/includes/class-media-uploader.php +++ b/includes/class-media-uploader.php @@ -94,7 +94,10 @@ class Media_Uploader { * * Uses the AWS CommandPool to upload the original file and all generated * thumbnail sizes in parallel, streaming each file directly from disk. - * No headObject pre-check is performed — files are assumed to be new. + * + * Before the first upload of an attachment, a remote name collision check + * runs (see maybe_rename_for_remote()) so an existing remote object is + * never silently overwritten by a re-uploaded file name. * * Concurrency defaults to 5 and can be overridden via the * IDRIVEE2_UPLOAD_CONCURRENCY constant in wp-config.php. @@ -139,12 +142,26 @@ class Media_Uploader { return $meta; } - // Build list of files: original + each thumbnail. $upload_dir = wp_upload_dir(); $basedir = $upload_dir['basedir']; + + $client = $this->client_factory->create(); + + /* + * First upload only: wp_unique_filename() deduplicates against the local + * uploads directory, but this plugin deletes local files after uploading, + * so a re-used name looks "new" locally while the remote object exists. + * Rename the files WordPress-style (file-1.jpg, ...) to avoid overwriting. + */ + if ( ! get_post_meta( $attachment_id, '_idrivee2_s3_base_url', true ) ) { + $meta = $this->maybe_rename_for_remote( $client, $meta, $basedir ); + } + + $meta_file = isset( $meta['file'] ) && is_string( $meta['file'] ) ? $meta['file'] : ''; $base_path = path_join( $basedir, $meta_file ); $meta_sizes = isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ? $meta['sizes'] : array(); + // Build list of files: original + each thumbnail. /* @var array $files */ $files = array( 'original' => $base_path ); foreach ( $meta_sizes as $size ) { @@ -154,7 +171,6 @@ class Media_Uploader { } // Open a stream for each file and build the AWS command list. - $client = $this->client_factory->create(); $bucket = $this->config->get_bucket(); $commands = array(); $key_map = array(); // int index -> file metadata. @@ -339,6 +355,205 @@ class Media_Uploader { return $meta; } + /** + * Rename attachment files when the remote key already exists. + * + * WordPress core deduplicates file names against the local uploads + * directory only (wp_unique_filename()). Because this plugin deletes local + * files after uploading, a re-uploaded name looks "new" locally while the + * remote object still exists, and a plain PutObject would silently + * overwrite it. + * + * When the remote key is taken, the original and all size files are renamed + * locally to the next free "-N" variant (file-1.jpg, file-2.jpg, ...) and + * the metadata is updated to match, mirroring what core would have done + * on a local collision. The updated meta is returned so the caller's + * meta_file/GUID handling and the persisted attachment metadata stay in + * sync with the uploaded keys. + * + * @since 1.5.1 + * + * @param \Aws\S3\S3Client $client S3 client. + * @param array $meta Attachment metadata. + * @param string $basedir Uploads base directory. + * @return array Updated metadata. + */ + private function maybe_rename_for_remote( \Aws\S3\S3Client $client, array $meta, string $basedir ): array { + $meta_file = isset( $meta['file'] ) && is_string( $meta['file'] ) ? $meta['file'] : ''; + if ( '' === $meta_file ) { + return $meta; + } + + $new_file = $this->unique_remote_file( $client, $meta_file, $basedir ); + if ( $new_file === $meta_file ) { + return $meta; + } + + $old_path = path_join( $basedir, $meta_file ); + $new_path = path_join( $basedir, $new_file ); + + // Security: refuse to rename files outside the uploads directory, + // mirroring the realpath() jail used for uploads in do_upload(). + $real_basedir = realpath( $basedir ); + if ( false === $real_basedir + || ! $this->is_inside_basedir( $real_basedir, dirname( $old_path ) ) + || ! $this->is_inside_basedir( $real_basedir, dirname( $new_path ) ) ) { + $this->logger->warning( 'Refusing rename outside uploads dir', array( 'path' => $old_path ) ); + return $meta; + } + + if ( file_exists( $old_path ) + // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename -- WP_Filesystem move() unavailable without credentials in this context + && ! rename( $old_path, $new_path ) ) { + $this->logger->warning( 'Rename failed after remote name collision, keeping original name', array( 'path' => $old_path ) ); + return $meta; + } + + // Base names without extension: "file.jpg" / "file-1.jpg" -> "file" / "file-1". + $old_name = basename( $meta_file ); + $new_name = basename( $new_file ); + $dot = strrpos( $old_name, '.' ); + $old_base = ( false === $dot ) ? $old_name : substr( $old_name, 0, $dot ); + $dot = strrpos( $new_name, '.' ); + $new_base = ( false === $dot ) ? $new_name : substr( $new_name, 0, $dot ); + + $sizes = isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ? $meta['sizes'] : array(); + foreach ( $sizes as $size_key => $size ) { + if ( ! is_array( $size ) || ! isset( $size['file'] ) || ! is_string( $size['file'] ) ) { + continue; + } + $size_file = $size['file']; + if ( ! str_starts_with( $size_file, $old_base ) ) { + continue; // Custom-named size: leave untouched. + } + $new_size_file = $new_base . substr( $size_file, strlen( $old_base ) ); + $old_size_path = path_join( dirname( $old_path ), $size_file ); + $new_size_path = path_join( dirname( $new_path ), $new_size_file ); + + // Security: same uploads-dir jail as for the original file. + if ( ! $this->is_inside_basedir( $real_basedir, dirname( $old_size_path ) ) + || ! $this->is_inside_basedir( $real_basedir, dirname( $new_size_path ) ) ) { + $this->logger->warning( 'Refusing rename outside uploads dir', array( 'path' => $old_size_path ) ); + continue; + } + + if ( file_exists( $old_size_path ) + // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename -- WP_Filesystem move() unavailable without credentials in this context + && ! rename( $old_size_path, $new_size_path ) ) { + $this->logger->warning( 'Rename failed for size file', array( 'path' => $old_size_path ) ); + } + $size['file'] = $new_size_file; + $sizes[ $size_key ] = $size; + } + + $meta['sizes'] = $sizes; + $meta['file'] = $new_file; + + $this->logger->info( + sprintf( 'Remote name collision: %s renamed to %s', $old_name, $new_name ), + array( 'original' => $meta_file ) + ); + + return $meta; + } + + /** + * Check whether a resolved directory stays inside the uploads base directory. + * + * @since 1.5.1 + * + * @param string $real_basedir Realpath of the uploads base directory. + * @param string $dir Directory to check. + * @return bool True when the directory resolves inside the uploads base directory. + */ + private function is_inside_basedir( string $real_basedir, string $dir ): bool { + $real = realpath( $dir ); + return false !== $real + && 0 === strpos( $real, $real_basedir . DIRECTORY_SEPARATOR ); + } + + /** + * Find an uploads-relative file name that is free remotely and locally. + * + * Mirrors the WordPress core naming scheme from wp_unique_filename(): + * file.jpg -> file-1.jpg -> file-2.jpg ... Candidates that exist locally + * are also skipped so the rename() in maybe_rename_for_remote() cannot + * clobber an existing local file. Gives up after 100 attempts and returns + * the original name. + * + * @since 1.5.1 + * + * @param \Aws\S3\S3Client $client S3 client. + * @param string $meta_file Uploads-relative file path (e.g. 2026/10/file.jpg). + * @param string $basedir Uploads base directory. + * @return string A free file path, or the original when everything is taken. + */ + private function unique_remote_file( \Aws\S3\S3Client $client, string $meta_file, string $basedir ): string { + if ( ! $this->remote_key_exists( $client, $meta_file ) ) { + return $meta_file; + } + + $name = basename( $meta_file ); + $dot = strrpos( $name, '.' ); + $ext = ( false === $dot ) ? '' : substr( $name, $dot ); + $base = ( '' === $ext ) ? $name : substr( $name, 0, -strlen( $ext ) ); + $dir = dirname( $meta_file ); + $prefix = ( '.' === $dir ) ? '' : $dir . '/'; + + for ( $i = 1; $i <= 100; ++$i ) { + $candidate = $prefix . $base . '-' . $i . $ext; + if ( file_exists( path_join( $basedir, $candidate ) ) ) { + continue; + } + if ( ! $this->remote_key_exists( $client, $candidate ) ) { + return $candidate; + } + } + + $this->logger->warning( 'No free remote file name found after 100 attempts, keeping original', array( 'file' => $meta_file ) ); + return $meta_file; + } + + /** + * Check whether an object key already exists in the remote bucket. + * + * Protected so unit-test doubles can stub the remote check without a + * live S3 connection. + * + * @since 1.5.1 + * + * @param \Aws\S3\S3Client $client S3 client. + * @param string $key Object key. + * @return bool True when the key exists. + */ + protected function remote_key_exists( \Aws\S3\S3Client $client, string $key ): bool { + try { + $client->headObject( + array( + 'Bucket' => $this->config->get_bucket(), + 'Key' => $key, + ) + ); + return true; + } catch ( \Aws\Exception\AwsException $e ) { + if ( 404 === $e->getStatusCode() + || in_array( $e->getAwsErrorCode(), array( 'NotFound', 'NoSuchKey' ), true ) ) { + return false; + } + + // Connection or permission errors: assume the key is free so + // uploads are not blocked (matches the pre-1.5.1 behavior). + $this->logger->warning( + 'headObject failed, assuming the key is free', + array( + 'key' => $key, + 'error' => $e->getAwsErrorMessage() ?? $e->getMessage(), + ) + ); + return false; + } + } + /** * Schedule files for deletion via the cron queue. * diff --git a/readme.txt b/readme.txt index 22353853..43ea4e45 100644 --- a/readme.txt +++ b/readme.txt @@ -3,9 +3,9 @@ Contributors: robotstxt, javiercasares Tags: media, upload, s3, cdn, storage, idrivee2, cloud Requires at least: 5.3 Tested up to: 7.1 -Stable tag: 1.5.0 +Stable tag: 1.5.1 Requires PHP: 8.1 -Version: 1.5.0 +Version: 1.5.1 License: GPL-3.0-or-later License URI: https://www.gnu.org/licenses/gpl-3.0.txt @@ -215,6 +215,14 @@ Updates are delivered through the [ROBOTSTXT Manager](https://www.robotstxt.soft == Changelog == += 1.5.1 = + +_Release date: 2026-10-02_ + +**Fixed** + +* Remote name collisions no longer overwrite existing S3 objects: files are renamed WordPress-style (file.jpg → file-1.jpg, …) before the first upload when the remote key is already taken + = 1.5.0 = _Release date: 2026-09-18_ @@ -223,6 +231,10 @@ _Release date: 2026-09-18_ * Multisite uninstall now cleans up all network sites (options, post meta, cron), honoring each site's own opt-in setting +**Changed** + +* Release ZIP slimmed from 37.5 MB to 5.3 MB by pruning unused AWS SDK service models (S3 kept); artifacts are smoke-tested offline at build time + **Compatibility** * WordPress: 5.3 - 7.1 @@ -233,7 +245,7 @@ _Release date: 2026-09-18_ * PHP Coding Standards: 3.13.6 (0 errors) * WordPress Coding Standards: 3.4.1 (0 violations) * PHPStan: Level 9, 0 errors -* PHPUnit: 88 tests, 164 assertions +* PHPUnit: 88 tests, 166 assertions = 1.4.4 = diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index b5e1f343..901507d4 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -3,7 +3,7 @@ 'name' => 'robotstxt/idrivee2-media-upload', 'pretty_version' => 'dev-main', 'version' => 'dev-main', - 'reference' => '46bc73f3950690042ec997023ac01ae9a531e89a', + 'reference' => '48796eaaa34126487203a27ed9b6ee0545fdaf7f', 'type' => 'wordpress-plugin', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), @@ -112,7 +112,7 @@ 'robotstxt/idrivee2-media-upload' => array( 'pretty_version' => 'dev-main', 'version' => 'dev-main', - 'reference' => '46bc73f3950690042ec997023ac01ae9a531e89a', + 'reference' => '48796eaaa34126487203a27ed9b6ee0545fdaf7f', 'type' => 'wordpress-plugin', 'install_path' => __DIR__ . '/../../', 'aliases' => array(),