Commit 7e14baf290a for woocommerce
commit 7e14baf290a5898bc80db4af6dc7448c496f9c9b
Author: Abdalsalaam Halawa <abdalsalaamnafez@gmail.com>
Date: Thu Nov 26 14:22:18 2026 +0200
Delete the attachment when a CSV import upload is rejected (#70966)
* Delete the attachment when a CSV import upload is rejected
handle_csv_upload() passed the $_FILES entry's missing "id" key to
wp_delete_attachment(), so a file rejected by wc_is_file_valid_csv()
left its attachment post and its file in uploads/wc-imports behind.
Use the attachment id from wp_import_handle_upload() and remove the
file if it is still there.
* Keep a vetoed attachment's file and test the rejected upload cleanup
The file is now deleted on its own only when no attachment was created.
When an attachment exists, wp_delete_attachment() removes the file with
it, so a pre_delete_attachment veto keeps both. The cleanup moved into
discard_rejected_upload(), with tests for the delete, the veto and the
file without an attachment.
* Drop redundant guards from the rejected upload cleanup
wp_import_handle_upload() returns the attachment id as an int and always
returns the file path when there is no error, and wp_delete_file() ignores
a missing file.
---------
Co-authored-by: Taha Paksu <3295+tpaksu@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/fix-csv-upload-helper-rejected-file-cleanup b/plugins/woocommerce/changelog/fix-csv-upload-helper-rejected-file-cleanup
new file mode 100644
index 00000000000..f8c4900e76b
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-csv-upload-helper-rejected-file-cleanup
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Remove the uploaded file and its attachment when a CSV import upload is rejected as not a valid CSV.
diff --git a/plugins/woocommerce/src/Internal/Admin/ImportExport/CSVUploadHelper.php b/plugins/woocommerce/src/Internal/Admin/ImportExport/CSVUploadHelper.php
index 04320db4f60..5157bb2705c 100644
--- a/plugins/woocommerce/src/Internal/Admin/ImportExport/CSVUploadHelper.php
+++ b/plugins/woocommerce/src/Internal/Admin/ImportExport/CSVUploadHelper.php
@@ -120,13 +120,28 @@ class CSVUploadHelper {
}
if ( ! wc_is_file_valid_csv( $upload['file'], false ) ) {
- wp_delete_attachment( $file['id'], true );
+ $this->discard_rejected_upload( $upload );
throw new \Exception( esc_html__( 'Invalid file type for a CSV import.', 'woocommerce' ) );
}
return $upload;
}
+ /**
+ * Remove an upload that failed the CSV check: its attachment, which takes
+ * the file with it, or the file alone when no attachment was created.
+ *
+ * @param array $upload Result of wp_import_handle_upload().
+ */
+ private function discard_rejected_upload( array $upload ): void {
+ if ( ! empty( $upload['id'] ) ) {
+ // A pre_delete_attachment veto keeps the attachment, and its file with it.
+ wp_delete_attachment( $upload['id'], true );
+ return;
+ }
+ wp_delete_file( $upload['file'] );
+ }
+
/**
* Hooked onto 'upload_dir' to override the default upload directory for a CSV upload.
*
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/ImportExport/CSVUploadHelperTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/ImportExport/CSVUploadHelperTest.php
index 3332f06f047..f402634baf1 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/ImportExport/CSVUploadHelperTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/ImportExport/CSVUploadHelperTest.php
@@ -189,4 +189,79 @@ class CSVUploadHelperTest extends WC_Unit_Test_Case {
$this->sut->handle_csv_upload( 'products', 'import' );
}
+
+ /**
+ * Store a file in the import directory, optionally as an attachment, the way
+ * wp_import_handle_upload() does.
+ *
+ * @param bool $with_attachment Whether to create an attachment for the file.
+ * @return array{file: string, id?: int} The upload result.
+ */
+ private function store_upload( bool $with_attachment ): array {
+ $file = trailingslashit( $this->sut->get_import_dir( true ) ) . 'product-rejected-' . wp_generate_password( 6, false ) . '.txt';
+ file_put_contents( $file, "sku,name\nT-1,Test\n" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
+ $upload = array( 'file' => $file );
+ if ( $with_attachment ) {
+ $upload['id'] = wp_insert_attachment(
+ array(
+ 'post_title' => 'product-rejected.txt',
+ 'post_mime_type' => 'text/plain',
+ 'post_status' => 'private',
+ ),
+ $file
+ );
+ }
+ return $upload;
+ }
+
+ /**
+ * Call the private cleanup run when an upload fails the CSV check.
+ *
+ * @param array $upload The upload result.
+ */
+ private function discard_rejected_upload( array $upload ): void {
+ $method = new \ReflectionMethod( CSVUploadHelper::class, 'discard_rejected_upload' );
+ $method->setAccessible( true );
+ $method->invoke( $this->sut, $upload );
+ }
+
+ /**
+ * @testdox Should delete the attachment and its file when an upload fails the CSV check.
+ */
+ public function test_discard_rejected_upload_deletes_attachment_and_file(): void {
+ $upload = $this->store_upload( true );
+
+ $this->discard_rejected_upload( $upload );
+
+ $this->assertNull( get_post( $upload['id'] ), 'The attachment should be deleted' );
+ $this->assertFileDoesNotExist( $upload['file'], 'The stored file should be deleted with the attachment' );
+ }
+
+ /**
+ * @testdox Should keep the attachment and its file when a pre_delete_attachment filter vetoes the delete.
+ */
+ public function test_discard_rejected_upload_respects_a_delete_veto(): void {
+ $upload = $this->store_upload( true );
+ add_filter( 'pre_delete_attachment', '__return_false' );
+
+ try {
+ $this->discard_rejected_upload( $upload );
+ } finally {
+ remove_filter( 'pre_delete_attachment', '__return_false' );
+ }
+
+ $this->assertInstanceOf( \WP_Post::class, get_post( $upload['id'] ), 'A vetoed attachment should stay' );
+ $this->assertFileExists( $upload['file'], 'The file of a kept attachment should stay' );
+ }
+
+ /**
+ * @testdox Should delete the file when an upload failing the CSV check has no attachment.
+ */
+ public function test_discard_rejected_upload_deletes_a_file_without_attachment(): void {
+ $upload = $this->store_upload( false );
+
+ $this->discard_rejected_upload( $upload );
+
+ $this->assertFileDoesNotExist( $upload['file'], 'The stored file should be deleted' );
+ }
}