Commit 4551485a83c for woocommerce
commit 4551485a83c2a65ecc48d9ac0461ce401f6a1aa6
Author: Luigi Teschio <gigitux@gmail.com>
Date: Fri Nov 20 17:37:46 2026 +0100
Remove customer passwords from deferred email actions (#71273)
Forwardport of 500-gh-Automattic/woocommerce.
Keep the 11.3.1 migration registration and callback names from the shipped migration fix.
Co-authored-by: Tom Cafferkey <tjcafferkey@gmail.com>
diff --git a/plugins/woocommerce/includes/class-wc-install.php b/plugins/woocommerce/includes/class-wc-install.php
index 6e31a9f62b4..1752c51daec 100644
--- a/plugins/woocommerce/includes/class-wc-install.php
+++ b/plugins/woocommerce/includes/class-wc-install.php
@@ -375,6 +375,9 @@ class WC_Install {
'wc_update_1130_delete_stock_notification_sync_notice_option',
'wc_update_1130_backfill_order_stats_payment_method',
),
+ '11.3.1' => array(
+ 'wc_update_1131_redact_deferred_customer_passwords',
+ ),
'11.4.0' => array(
'wc_update_1140_re_enable_autoload_options',
'wc_update_1140_add_mpn_to_product_lookup_table',
diff --git a/plugins/woocommerce/includes/wc-update-functions.php b/plugins/woocommerce/includes/wc-update-functions.php
index effde8e02b8..0accc208690 100644
--- a/plugins/woocommerce/includes/wc-update-functions.php
+++ b/plugins/woocommerce/includes/wc-update-functions.php
@@ -4031,6 +4031,113 @@ function wc_update_1130_set_legacy_variation_price_hash_option() {
}
}
+/**
+ * Remove customer passwords from previously stored deferred email actions.
+ *
+ * Scan 100 actions per run, including completed and failed actions.
+ *
+ * @since 11.3.1
+ * @return bool True when another batch remains.
+ */
+function wc_update_1131_redact_deferred_customer_passwords() {
+ global $wpdb;
+
+ $table = $wpdb->prefix . 'actionscheduler_actions';
+ $last_id_option = 'woocommerce_update_1131_last_deferred_email_action_id';
+ $batch_size = 100;
+ $hook = 'woocommerce_send_queued_transactional_email';
+
+ $redact_args = static function ( array &$args ) use ( &$redact_args ): bool {
+ $changed = false;
+ foreach ( $args as $key => $value ) {
+ if ( 'user_pass' === $key ) {
+ unset( $args[ $key ] );
+ $changed = true;
+ } elseif ( is_array( $value ) ) {
+ $changed = $redact_args( $args[ $key ] ) || $changed;
+ }
+ }
+ return $changed;
+ };
+
+ $redact_row = static function ( $row ) use ( $wpdb, $table, $hook, $redact_args ): bool {
+ $stored_args = ! empty( $row->extended_args ) ? $row->extended_args : $row->args;
+ $args = json_decode( $stored_args, true );
+ if ( ! is_array( $args ) ) {
+ return false;
+ }
+ if ( ! $redact_args( $args ) ) {
+ return true;
+ }
+
+ $encoded = wp_json_encode( $args );
+ if ( false === $encoded ) {
+ return false;
+ }
+
+ // Action Scheduler stores long arguments in extended_args and their hash in args.
+ $data = strlen( $encoded ) <= 191
+ ? array(
+ 'args' => $encoded,
+ 'extended_args' => null,
+ )
+ : array(
+ 'args' => md5( $encoded ),
+ 'extended_args' => $encoded,
+ );
+
+ for ( $attempt = 0; $attempt < 3; ++$attempt ) {
+ $updated = $wpdb->update(
+ $table,
+ $data,
+ array(
+ 'action_id' => $row->action_id,
+ 'hook' => $hook,
+ )
+ );
+
+ if ( false !== $updated ) {
+ return true;
+ }
+ }
+
+ return false;
+ };
+
+ $rows = $wpdb->get_results(
+ $wpdb->prepare(
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- The table name uses the current site's trusted prefix.
+ "SELECT action_id, args, extended_args FROM {$table} WHERE hook = %s AND action_id > %d ORDER BY action_id ASC LIMIT %d",
+ $hook,
+ (int) get_option( $last_id_option, 0 ),
+ $batch_size
+ )
+ );
+
+ if ( '' !== $wpdb->last_error ) {
+ wc_get_logger()->error( sprintf( 'Stopped redacting deferred email passwords: %s', $wpdb->last_error ), array( 'source' => 'wc-updater' ) );
+ return false;
+ }
+
+ foreach ( $rows as $row ) {
+ if ( ! $redact_row( $row ) ) {
+ wc_get_logger()->error( sprintf( 'Could not redact deferred email password from action #%d.', (int) $row->action_id ), array( 'source' => 'wc-updater' ) );
+ }
+ }
+
+ if ( count( $rows ) === $batch_size ) {
+ $last_id = (int) end( $rows )->action_id;
+ if ( (int) get_option( $last_id_option, 0 ) >= $last_id || update_option( $last_id_option, $last_id, false ) ) {
+ return true;
+ }
+ wc_get_logger()->error( 'Stopped redacting deferred email passwords: the progress cursor could not be saved.', array( 'source' => 'wc-updater' ) );
+ return false;
+ }
+
+ delete_option( $last_id_option );
+ return false;
+}
+
/**
* Delete the product attributes lookup rows of variations that are not published.
*
diff --git a/plugins/woocommerce/src/Internal/Email/DeferredEmailQueue.php b/plugins/woocommerce/src/Internal/Email/DeferredEmailQueue.php
index 34c61113b11..70d84c7ac9e 100644
--- a/plugins/woocommerce/src/Internal/Email/DeferredEmailQueue.php
+++ b/plugins/woocommerce/src/Internal/Email/DeferredEmailQueue.php
@@ -33,6 +33,11 @@ final class DeferredEmailQueue {
*/
private const QUEUED_OBJECT_KEY = '__woocommerce_deferred_email_object';
+ /**
+ * Argument keys whose values must never be persisted in the email queue.
+ */
+ private const SENSITIVE_ARG_KEYS = array( 'user_pass' );
+
/**
* Queue of email callbacks collected during the current request.
*
@@ -236,6 +241,8 @@ final class DeferredEmailQueue {
*/
private function prepare_arg_for_queue( $arg ) {
if ( is_array( $arg ) ) {
+ $arg = $this->sanitize_args_for_queue( $arg );
+
foreach ( $arg as $key => $value ) {
$arg[ $key ] = $this->prepare_arg_for_queue( $value );
}
@@ -269,6 +276,20 @@ final class DeferredEmailQueue {
return $arg;
}
+ /**
+ * Remove known sensitive fields before persisting email arguments.
+ *
+ * @param array $args Arguments at the current array depth.
+ * @return array
+ */
+ private function sanitize_args_for_queue( array $args ): array {
+ foreach ( self::SENSITIVE_ARG_KEYS as $key ) {
+ unset( $args[ $key ] );
+ }
+
+ return $args;
+ }
+
/**
* Restore queued arguments after Action Scheduler storage.
*
diff --git a/plugins/woocommerce/tests/php/src/Internal/Email/DeferredEmailQueueTest.php b/plugins/woocommerce/tests/php/src/Internal/Email/DeferredEmailQueueTest.php
index 224ab7cbf75..f90bb369764 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Email/DeferredEmailQueueTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Email/DeferredEmailQueueTest.php
@@ -113,6 +113,196 @@ class DeferredEmailQueueTest extends WC_Unit_Test_Case {
$this->assertSame( array( 42, 'extra' ), $action['args'][1] );
}
+ /**
+ * @testdox Customer passwords never enter persisted new-account email arguments.
+ */
+ public function test_new_account_email_omits_password_from_scheduled_args(): void {
+ $customer_data = array(
+ 'user_login' => 'new-customer',
+ 'user_pass' => 'secret-password',
+ );
+
+ $this->assertTrue( $this->sut->push( 'woocommerce_created_customer', array( 12, $customer_data, false ) ) );
+ $this->sut->dispatch();
+
+ $args = $this->get_scheduled_email_action()['args'][1];
+ $this->assertArrayNotHasKey( 'user_pass', $args[1] );
+ $this->assertSame( 'new-customer', $args[1]['user_login'] );
+ $this->assertArrayHasKey( 'user_pass', $customer_data, 'The original hook arguments must remain available to other callbacks.' );
+ }
+
+ /**
+ * @testdox Sensitive fields are omitted from nested arguments on any deferred email hook.
+ */
+ public function test_push_sanitizes_nested_sensitive_args(): void {
+ $args = array(
+ array(
+ 'user_pass' => 'secret-one',
+ 'nested' => array(
+ 'user_pass' => 'secret-two',
+ 'message' => 'Keep this',
+ ),
+ ),
+ );
+
+ $this->assertTrue( $this->sut->push( 'woocommerce_custom_email', $args ) );
+ $this->sut->dispatch();
+
+ $queued = $this->get_scheduled_email_action()['args'][1][0];
+ $this->assertArrayNotHasKey( 'user_pass', $queued );
+ $this->assertArrayNotHasKey( 'user_pass', $queued['nested'] );
+ $this->assertSame( 'Keep this', $queued['nested']['message'] );
+ $this->assertSame( 'secret-one', $args[0]['user_pass'], 'The original arguments must not be modified.' );
+ }
+
+ /**
+ * @testdox The upgrade removes passwords from both short and extended persisted actions.
+ */
+ public function test_upgrade_redacts_existing_deferred_customer_passwords(): void {
+ global $wpdb;
+
+ require_once WC_ABSPATH . 'includes/wc-update-functions.php';
+ $table = $wpdb->prefix . 'actionscheduler_actions';
+ $ids = array();
+ foreach ( array( false, true ) as $extended ) {
+ $args = wp_json_encode(
+ array(
+ 'woocommerce_created_customer',
+ array(
+ 12,
+ array(
+ 'user_pass' => 'secret-password',
+ 'user_login' => str_repeat( 'x', $extended ? 200 : 5 ),
+ ),
+ false,
+ ),
+ )
+ );
+ $this->assertIsString( $args );
+ $wpdb->insert(
+ $table,
+ array(
+ 'hook' => 'woocommerce_send_queued_transactional_email',
+ 'status' => $extended ? 'failed' : 'complete',
+ 'args' => $extended ? md5( $args ) : $args,
+ 'extended_args' => $extended ? $args : null,
+ )
+ );
+ $this->assertGreaterThan( 0, $wpdb->insert_id );
+ $ids[] = $wpdb->insert_id;
+ }
+
+ $this->assertFalse( \wc_update_1131_redact_deferred_customer_passwords() );
+
+ foreach ( $ids as $id ) {
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- The table name uses the current site's trusted prefix.
+ $row = $wpdb->get_row( $wpdb->prepare( "SELECT args, extended_args FROM {$table} WHERE action_id = %d", $id ) );
+ $encoded = $row->extended_args ?? $row->args;
+ $decoded = json_decode( $encoded, true );
+ $this->assertArrayNotHasKey( 'user_pass', $decoded[1][1] );
+ $this->assertArrayHasKey( 'user_login', $decoded[1][1] );
+ $this->assertStringNotContainsString( 'secret-password', $row->args );
+ $this->assertStringNotContainsString( 'secret-password', (string) $row->extended_args );
+ }
+ }
+
+ /**
+ * @testdox The upgrade removes passwords at every depth from custom deferred email actions.
+ */
+ public function test_upgrade_redacts_nested_custom_email_passwords(): void {
+ global $wpdb;
+
+ require_once WC_ABSPATH . 'includes/wc-update-functions.php';
+ $table = $wpdb->prefix . 'actionscheduler_actions';
+ $ids = array();
+ foreach ( array( false, true ) as $extended ) {
+ $args = wp_json_encode(
+ array(
+ 'myplugin_customer_welcome',
+ array(
+ array(
+ 'account' => array(
+ 'user_pass' => 'plaintext-secret',
+ 'nested' => array(
+ 'user_pass' => 'another-secret',
+ 'message' => str_repeat( 'Keep this', $extended ? 30 : 1 ),
+ ),
+ ),
+ ),
+ ),
+ )
+ );
+ $this->assertIsString( $args );
+ $wpdb->insert(
+ $table,
+ array(
+ 'hook' => 'woocommerce_send_queued_transactional_email',
+ 'status' => 'complete',
+ 'args' => $extended ? md5( $args ) : $args,
+ 'extended_args' => $extended ? $args : null,
+ )
+ );
+ $this->assertGreaterThan( 0, $wpdb->insert_id );
+ $ids[] = $wpdb->insert_id;
+ }
+
+ $this->assertFalse( \wc_update_1131_redact_deferred_customer_passwords() );
+
+ foreach ( $ids as $id ) {
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- The table name uses the current site's trusted prefix.
+ $row = $wpdb->get_row( $wpdb->prepare( "SELECT args, extended_args FROM {$table} WHERE action_id = %d", $id ) );
+ $decoded = json_decode( $row->extended_args ?? $row->args, true );
+ $account = $decoded[1][0]['account'];
+ $this->assertSame( 'myplugin_customer_welcome', $decoded[0] );
+ $this->assertArrayNotHasKey( 'user_pass', $account );
+ $this->assertArrayNotHasKey( 'user_pass', $account['nested'] );
+ $this->assertStringContainsString( 'Keep this', $account['nested']['message'] );
+ $this->assertStringNotContainsString( 'plaintext-secret', $row->args . $row->extended_args );
+ $this->assertStringNotContainsString( 'another-secret', $row->args . $row->extended_args );
+ }
+ }
+
+ /**
+ * @testdox The upgrade processes at most 100 actions per invocation and resumes at the next ID.
+ */
+ public function test_upgrade_redacts_actions_in_bounded_batches(): void {
+ global $wpdb;
+
+ require_once WC_ABSPATH . 'includes/wc-update-functions.php';
+ $table = $wpdb->prefix . 'actionscheduler_actions';
+ $args = wp_json_encode( array( 'woocommerce_created_customer', array( 12, array( 'user_pass' => 'secret-password' ) ) ) );
+ $ids = array();
+ $this->assertIsString( $args );
+
+ for ( $i = 0; $i < 101; ++$i ) {
+ $wpdb->insert(
+ $table,
+ array(
+ 'hook' => 'woocommerce_send_queued_transactional_email',
+ 'status' => 'complete',
+ 'args' => $args,
+ )
+ );
+ $ids[] = $wpdb->insert_id;
+ }
+
+ $this->assertTrue( \wc_update_1131_redact_deferred_customer_passwords() );
+ $this->assertSame( $ids[99], get_option( 'woocommerce_update_1131_last_deferred_email_action_id' ) );
+
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- The table name uses the current site's trusted prefix.
+ $first = $wpdb->get_var( $wpdb->prepare( "SELECT args FROM {$table} WHERE action_id = %d", $ids[0] ) );
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- The table name uses the current site's trusted prefix.
+ $last = $wpdb->get_var( $wpdb->prepare( "SELECT args FROM {$table} WHERE action_id = %d", $ids[100] ) );
+ $this->assertArrayNotHasKey( 'user_pass', json_decode( $first, true )[1][1] );
+ $this->assertArrayHasKey( 'user_pass', json_decode( $last, true )[1][1] );
+
+ $this->assertFalse( \wc_update_1131_redact_deferred_customer_passwords() );
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- The table name uses the current site's trusted prefix.
+ $last = $wpdb->get_var( $wpdb->prepare( "SELECT args FROM {$table} WHERE action_id = %d", $ids[100] ) );
+ $this->assertArrayNotHasKey( 'user_pass', json_decode( $last, true )[1][1] );
+ $this->assertFalse( get_option( 'woocommerce_update_1131_last_deferred_email_action_id' ) );
+ }
+
/**
* @testdox Processing preserves null arguments.
*/