From cd077d3c364b5d7e4efb1a0d0fb20f88b5f39b33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 11 Nov 2026 18:50:21 +0200 Subject: [PATCH 1/9] fix: keep stored objects unchanged when replacing renamed attachment URLs Serialized values are read with only stdClass allowed, and values that hold any other object are left as stored. --- inc/media_rename/attachment_db_renamer.php | 38 ++++++++- tests/media_rename/test-db-renamer.php | 96 ++++++++++++++++++++++ 2 files changed, 133 insertions(+), 1 deletion(-) diff --git a/inc/media_rename/attachment_db_renamer.php b/inc/media_rename/attachment_db_renamer.php index 5d5d9aba..13a30e9d 100644 --- a/inc/media_rename/attachment_db_renamer.php +++ b/inc/media_rename/attachment_db_renamer.php @@ -411,10 +411,17 @@ private function php_handle_column( $table, $column, $primary_keys, $old_url, $n private function replace_urls_in_value( $value, $old_url, $new_url ) { // Check if the value is serialized if ( $this->is_serialized( $value ) ) { - $unserialized = @unserialize( $value ); + // Any table can hold the URL, including values visitors write, so stored data must not create + // objects whose class runs code. + $unserialized = @unserialize( $value, [ 'allowed_classes' => [ 'stdClass' ] ] ); // If unserialize successful, process the data if ( $unserialized !== false ) { + // Such values are left as stored: an object read without its class could not be written + // back unchanged, and an enum cannot change. + if ( $this->holds_other_objects( $unserialized ) ) { + return $value; + } $replaced = $this->replace_in_data( $unserialized, $old_url, $new_url ); return serialize( $replaced ); } @@ -527,6 +534,35 @@ function ( $matches ) use ( $new_base, $new_domain, $new_dir, $new_ext ) { return $content; } + /** + * Check whether unserialized data holds an object other than stdClass, such as an incomplete object or an enum. + * + * @param mixed $data The unserialized data. + * @param array $seen Ids of the stdClass objects already checked, so self-references end. + * + * @return bool True when the data holds such an object. + */ + private function holds_other_objects( $data, array &$seen = [] ): bool { + if ( is_object( $data ) ) { + if ( ! $data instanceof stdClass ) { + return true; + } + if ( isset( $seen[ spl_object_id( $data ) ] ) ) { + return false; + } + $seen[ spl_object_id( $data ) ] = true; + } + if ( is_array( $data ) || is_object( $data ) ) { + foreach ( $data as $value ) { + if ( $this->holds_other_objects( $value, $seen ) ) { + return true; + } + } + } + + return false; + } + /** * Recursively replace URLs in data structure * diff --git a/tests/media_rename/test-db-renamer.php b/tests/media_rename/test-db-renamer.php index 34776908..a6c54484 100644 --- a/tests/media_rename/test-db-renamer.php +++ b/tests/media_rename/test-db-renamer.php @@ -445,6 +445,79 @@ public function test_replace_updates_serialized_user_meta(): void { $this->assertSame( [ 'url' => $new_url ], get_user_meta( $user_id, 'optml_test_avatar', true ) ); } + public function test_replace_keeps_stored_objects_of_other_classes_unchanged(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/object-photo.jpg'; + $stored = 'O:' . strlen( Optml_Test_Renamer_Stored_Object::class ) . ':"' . Optml_Test_Renamer_Stored_Object::class . '":1:{s:3:"url";s:' . strlen( $old_url ) . ':"' . $old_url . '";}'; + $comment_id = self::factory()->comment->create( + [ + 'comment_post_ID' => self::$post_id, + 'comment_content' => $stored, + ] + ); + Optml_Test_Renamer_Stored_Object::$woken = false; + + self::$replacer->replace( $old_url, 'http://om-wp.test/wp-content/uploads/2026/10/object-renamed.jpg' ); + clean_comment_cache( $comment_id ); + + $this->assertFalse( Optml_Test_Renamer_Stored_Object::$woken, 'The stored object must not be created.' ); + $this->assertSame( $stored, get_comment( $comment_id )->comment_content ); + } + + public function test_replace_keeps_values_with_custom_serialized_objects_unchanged(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/custom-photo.jpg'; + // An array with the URL next to an object in the Serializable format, whose payload only its class can read. + $stored = 'a:2:{s:5:"image";s:' . strlen( $old_url ) . ':"' . $old_url . '";s:6:"widget";C:21:"Optml_Test_Not_Loaded":7:{payload}}'; + $comment_id = self::factory()->comment->create( + [ + 'comment_post_ID' => self::$post_id, + 'comment_content' => $stored, + ] + ); + + self::$replacer->replace( $old_url, 'http://om-wp.test/wp-content/uploads/2026/10/custom-renamed.jpg' ); + clean_comment_cache( $comment_id ); + + $this->assertSame( $stored, get_comment( $comment_id )->comment_content ); + } + + public function test_replace_updates_serialized_std_class(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/std-photo.jpg'; + $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/std-renamed.jpg'; + update_post_meta( self::$post_id, 'optml_test_object', (object) [ 'image' => $old_url ] ); + + self::$replacer->replace( $old_url, $new_url ); + wp_cache_flush(); + + $this->assertEquals( (object) [ 'image' => $new_url ], get_post_meta( self::$post_id, 'optml_test_object', true ) ); + } + + public function test_replace_finishes_on_a_std_class_that_refers_to_itself(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/cycle-photo.jpg'; + $value = (object) [ "\0*\0self" => $old_url ]; + $value->self = $value; + self::factory()->comment->create( + [ + 'comment_post_ID' => self::$post_id, + 'comment_content' => serialize( $value ), + ] + ); + + $count = self::$replacer->replace( $old_url, 'http://om-wp.test/wp-content/uploads/2026/10/cycle-renamed.jpg' ); + + $this->assertGreaterThan( 0, $count ); + } + + public function test_replace_updates_serialized_text_that_looks_like_an_object(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/text-photo.jpg'; + $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/text-renamed.jpg'; + update_post_meta( self::$post_id, 'optml_test_notes', [ 'image' => $old_url, 'note' => 'O:4:"Test":0:{}' ] ); + + self::$replacer->replace( $old_url, $new_url ); + wp_cache_flush(); + + $this->assertSame( [ 'image' => $new_url, 'note' => 'O:4:"Test":0:{}' ], get_post_meta( self::$post_id, 'optml_test_notes', true ) ); + } + public function test_replace_updates_size_variants_in_serialized_meta(): void { update_post_meta( self::$post_id, 'optml_test_thumbnail', [ 'image' => 'http://om-wp.test/wp-content/uploads/2026/10/sized-photo-300x200.jpg' ] ); @@ -574,3 +647,26 @@ public function prepare( $query, ...$args ) { return parent::prepare( str_replace( '%i', '%%i', $query ), ...$args ); } } + +/** + * A class whose code runs when a stored value creates it. + */ +class Optml_Test_Renamer_Stored_Object { + /** + * Whether a stored value created an instance. + * + * @var bool + */ + public static $woken = false; + + /** + * URL the stored value carries. + * + * @var string + */ + public $url = ''; + + public function __wakeup() { + self::$woken = true; + } +} From 6b3e021763d2a4c43939f3b50886102c604fb8eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 11 Nov 2026 20:51:47 +0200 Subject: [PATCH 2/9] fix: leave serialized values the renamer cannot read safely as stored Values with references, values that do not unserialize, and Serializable objects stored on their own are no longer rewritten. --- inc/media_rename/attachment_db_renamer.php | 43 +++++++------ tests/media_rename/test-db-renamer.php | 71 ++++++++++++++++++++-- 2 files changed, 86 insertions(+), 28 deletions(-) diff --git a/inc/media_rename/attachment_db_renamer.php b/inc/media_rename/attachment_db_renamer.php index 13a30e9d..89f11614 100644 --- a/inc/media_rename/attachment_db_renamer.php +++ b/inc/media_rename/attachment_db_renamer.php @@ -180,7 +180,7 @@ private function process_column( $table, $column, $primary_keys, $old_url, $new_ // Check for serialized data $has_serialized = $wpdb->get_var( 'SELECT COUNT(' . esc_sql( $this->quote_identifier( $column ) ) . ') FROM ' . esc_sql( $this->quote_identifier( $table ) ) . - ' WHERE ' . esc_sql( $this->quote_identifier( $column ) ) . " REGEXP '^[aiO]:[1-9]' LIMIT 1" + ' WHERE ' . esc_sql( $this->quote_identifier( $column ) ) . " REGEXP '^[aisOCE]:[0-9]' LIMIT 1" ); // Process with PHP if serialized data is found @@ -411,20 +411,24 @@ private function php_handle_column( $table, $column, $primary_keys, $old_url, $n private function replace_urls_in_value( $value, $old_url, $new_url ) { // Check if the value is serialized if ( $this->is_serialized( $value ) ) { + // A value with references (r: or R:) can refer to itself, which the URL walk cannot end. + if ( preg_match( '/[;{][rR]:\d+;/', $value ) ) { + return $value; + } + // Any table can hold the URL, including values visitors write, so stored data must not create // objects whose class runs code. $unserialized = @unserialize( $value, [ 'allowed_classes' => [ 'stdClass' ] ] ); - // If unserialize successful, process the data - if ( $unserialized !== false ) { - // Such values are left as stored: an object read without its class could not be written - // back unchanged, and an enum cannot change. - if ( $this->holds_other_objects( $unserialized ) ) { - return $value; - } - $replaced = $this->replace_in_data( $unserialized, $old_url, $new_url ); - return serialize( $replaced ); + // A serialized value is left as stored when it cannot be read, such as one naming an enum that is + // not loaded, or when it holds an object other than stdClass: an object read without its class + // could not be written back unchanged, and an enum cannot change. A raw text replacement would + // break its declared lengths. + if ( false === $unserialized || $this->holds_other_objects( $unserialized ) ) { + return $value; } + + return serialize( $this->replace_in_data( $unserialized, $old_url, $new_url ) ); } return $this->replace_image_urls( $value, $old_url, $new_url ); @@ -537,24 +541,17 @@ function ( $matches ) use ( $new_base, $new_domain, $new_dir, $new_ext ) { /** * Check whether unserialized data holds an object other than stdClass, such as an incomplete object or an enum. * - * @param mixed $data The unserialized data. - * @param array $seen Ids of the stdClass objects already checked, so self-references end. + * @param mixed $data The unserialized data, without references. * * @return bool True when the data holds such an object. */ - private function holds_other_objects( $data, array &$seen = [] ): bool { - if ( is_object( $data ) ) { - if ( ! $data instanceof stdClass ) { - return true; - } - if ( isset( $seen[ spl_object_id( $data ) ] ) ) { - return false; - } - $seen[ spl_object_id( $data ) ] = true; + private function holds_other_objects( $data ): bool { + if ( is_object( $data ) && ! $data instanceof stdClass ) { + return true; } if ( is_array( $data ) || is_object( $data ) ) { foreach ( $data as $value ) { - if ( $this->holds_other_objects( $value, $seen ) ) { + if ( $this->holds_other_objects( $value ) ) { return true; } } @@ -630,6 +627,8 @@ private function is_serialized( $data ) { // Fall through case 'a': case 'O': + case 'C': + case 'E': case 'i': case 'd': return (bool) preg_match( "/^{$token}:[0-9]+:/", $data ); diff --git a/tests/media_rename/test-db-renamer.php b/tests/media_rename/test-db-renamer.php index a6c54484..aaac24e1 100644 --- a/tests/media_rename/test-db-renamer.php +++ b/tests/media_rename/test-db-renamer.php @@ -491,20 +491,79 @@ public function test_replace_updates_serialized_std_class(): void { $this->assertEquals( (object) [ 'image' => $new_url ], get_post_meta( self::$post_id, 'optml_test_object', true ) ); } - public function test_replace_finishes_on_a_std_class_that_refers_to_itself(): void { - $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/cycle-photo.jpg'; - $value = (object) [ "\0*\0self" => $old_url ]; + public function test_replace_keeps_a_std_class_that_refers_to_itself_unchanged(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/cycle-photo.jpg'; + $value = (object) [ 'image' => $old_url ]; $value->self = $value; + $stored = serialize( $value ); + $comment_id = self::factory()->comment->create( + [ + 'comment_post_ID' => self::$post_id, + 'comment_content' => $stored, + ] + ); + + self::$replacer->replace( $old_url, 'http://om-wp.test/wp-content/uploads/2026/10/cycle-renamed.jpg' ); + clean_comment_cache( $comment_id ); + + $this->assertSame( $stored, get_comment( $comment_id )->comment_content ); + } + + public function test_replace_keeps_a_custom_serialized_object_unchanged(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/custom-top-photo.jpg'; + $payload = '{' . $old_url . '}'; + $stored = 'C:21:"Optml_Test_Not_Loaded":' . ( strlen( $payload ) - 2 ) . ':' . $payload; + $comment_id = self::factory()->comment->create( + [ + 'comment_post_ID' => self::$post_id, + 'comment_content' => $stored, + ] + ); + + self::$replacer->replace( $old_url, 'http://om-wp.test/wp-content/uploads/2026/10/custom-top-renamed.jpg' ); + clean_comment_cache( $comment_id ); + + $this->assertSame( $stored, get_comment( $comment_id )->comment_content ); + } + + public function test_replace_keeps_a_custom_serialized_object_next_to_other_serialized_rows_unchanged(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/custom-mixed-photo.jpg'; + $payload = '{' . $old_url . '}'; + $stored = 'C:21:"Optml_Test_Not_Loaded":' . ( strlen( $payload ) - 2 ) . ':' . $payload; + // Another row in the column that starts like an array, so the column is read row by row in PHP. self::factory()->comment->create( [ 'comment_post_ID' => self::$post_id, - 'comment_content' => serialize( $value ), + 'comment_content' => serialize( [ 'image' => $old_url ] ), + ] + ); + $comment_id = self::factory()->comment->create( + [ + 'comment_post_ID' => self::$post_id, + 'comment_content' => $stored, ] ); - $count = self::$replacer->replace( $old_url, 'http://om-wp.test/wp-content/uploads/2026/10/cycle-renamed.jpg' ); + self::$replacer->replace( $old_url, 'http://om-wp.test/wp-content/uploads/2026/10/custom-mixed-renamed.jpg' ); + clean_comment_cache( $comment_id ); - $this->assertGreaterThan( 0, $count ); + $this->assertSame( $stored, get_comment( $comment_id )->comment_content ); + } + + public function test_replace_keeps_values_with_an_enum_that_is_not_loaded_unchanged(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/enum-photo.jpg'; + $stored = 'a:2:{s:5:"image";s:' . strlen( $old_url ) . ':"' . $old_url . '";s:4:"kind";E:20:"Optml_Test_No_Enum:A";}'; + $comment_id = self::factory()->comment->create( + [ + 'comment_post_ID' => self::$post_id, + 'comment_content' => $stored, + ] + ); + + self::$replacer->replace( $old_url, 'http://om-wp.test/wp-content/uploads/2026/10/enum-renamed.jpg' ); + clean_comment_cache( $comment_id ); + + $this->assertSame( $stored, get_comment( $comment_id )->comment_content ); } public function test_replace_updates_serialized_text_that_looks_like_an_object(): void { From eb2d2625a97125d0d1b5d6810789fbd6158d699e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Wed, 11 Nov 2026 23:19:15 +0200 Subject: [PATCH 3/9] fix: leave plain text that starts like a serialized string to the text replacement Only the serialized forms the PHP path must protect, arrays, objects, custom serialized objects and enums, now send a column to it. --- inc/media_rename/attachment_db_renamer.php | 2 +- tests/media_rename/test-db-renamer.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/inc/media_rename/attachment_db_renamer.php b/inc/media_rename/attachment_db_renamer.php index 89f11614..58ab80c9 100644 --- a/inc/media_rename/attachment_db_renamer.php +++ b/inc/media_rename/attachment_db_renamer.php @@ -180,7 +180,7 @@ private function process_column( $table, $column, $primary_keys, $old_url, $new_ // Check for serialized data $has_serialized = $wpdb->get_var( 'SELECT COUNT(' . esc_sql( $this->quote_identifier( $column ) ) . ') FROM ' . esc_sql( $this->quote_identifier( $table ) ) . - ' WHERE ' . esc_sql( $this->quote_identifier( $column ) ) . " REGEXP '^[aisOCE]:[0-9]' LIMIT 1" + ' WHERE ' . esc_sql( $this->quote_identifier( $column ) ) . " REGEXP '^[aiOCE]:[1-9]' LIMIT 1" ); // Process with PHP if serialized data is found diff --git a/tests/media_rename/test-db-renamer.php b/tests/media_rename/test-db-renamer.php index aaac24e1..efb5e6f9 100644 --- a/tests/media_rename/test-db-renamer.php +++ b/tests/media_rename/test-db-renamer.php @@ -577,6 +577,22 @@ public function test_replace_updates_serialized_text_that_looks_like_an_object() $this->assertSame( [ 'image' => $new_url, 'note' => 'O:4:"Test":0:{}' ], get_post_meta( self::$post_id, 'optml_test_notes', true ) ); } + public function test_replace_updates_text_that_starts_like_a_serialized_string(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/string-photo.jpg'; + $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/string-renamed.jpg'; + $comment_id = self::factory()->comment->create( + [ + 'comment_post_ID' => self::$post_id, + 'comment_content' => 's:3:"foo"; see "' . $old_url . '";', + ] + ); + + self::$replacer->replace( $old_url, $new_url ); + clean_comment_cache( $comment_id ); + + $this->assertSame( 's:3:"foo"; see "' . $new_url . '";', get_comment( $comment_id )->comment_content ); + } + public function test_replace_updates_size_variants_in_serialized_meta(): void { update_post_meta( self::$post_id, 'optml_test_thumbnail', [ 'image' => 'http://om-wp.test/wp-content/uploads/2026/10/sized-photo-300x200.jpg' ] ); From 8a5a836ad3e904d0ae612052c655b750b22e36c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Thu, 12 Nov 2026 00:20:48 +0200 Subject: [PATCH 4/9] test: drop the plain-text probe test, which passes on the base --- tests/media_rename/test-db-renamer.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/tests/media_rename/test-db-renamer.php b/tests/media_rename/test-db-renamer.php index efb5e6f9..aaac24e1 100644 --- a/tests/media_rename/test-db-renamer.php +++ b/tests/media_rename/test-db-renamer.php @@ -577,22 +577,6 @@ public function test_replace_updates_serialized_text_that_looks_like_an_object() $this->assertSame( [ 'image' => $new_url, 'note' => 'O:4:"Test":0:{}' ], get_post_meta( self::$post_id, 'optml_test_notes', true ) ); } - public function test_replace_updates_text_that_starts_like_a_serialized_string(): void { - $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/string-photo.jpg'; - $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/string-renamed.jpg'; - $comment_id = self::factory()->comment->create( - [ - 'comment_post_ID' => self::$post_id, - 'comment_content' => 's:3:"foo"; see "' . $old_url . '";', - ] - ); - - self::$replacer->replace( $old_url, $new_url ); - clean_comment_cache( $comment_id ); - - $this->assertSame( 's:3:"foo"; see "' . $new_url . '";', get_comment( $comment_id )->comment_content ); - } - public function test_replace_updates_size_variants_in_serialized_meta(): void { update_post_meta( self::$post_id, 'optml_test_thumbnail', [ 'image' => 'http://om-wp.test/wp-content/uploads/2026/10/sized-photo-300x200.jpg' ] ); From 6861701e964c213b89452ee8d80786e0ca45c10a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Thu, 12 Nov 2026 09:17:46 +0200 Subject: [PATCH 5/9] fix: rewrite serialized values stored in strings of serialized data --- inc/media_rename/attachment_db_renamer.php | 47 +++++++++++++++++++--- tests/media_rename/test-db-renamer.php | 40 ++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/inc/media_rename/attachment_db_renamer.php b/inc/media_rename/attachment_db_renamer.php index 58ab80c9..1b874f6b 100644 --- a/inc/media_rename/attachment_db_renamer.php +++ b/inc/media_rename/attachment_db_renamer.php @@ -33,6 +33,13 @@ class Optml_Attachment_Db_Renamer { */ private $handle_image_sizes = false; + /** + * How many serialized values nested in strings are rewritten as data. + * + * Each level reads a copy of the string, so the cap bounds memory. + */ + private const MAX_NESTED_DEPTH = 3; + /** * Constructor */ @@ -405,10 +412,11 @@ private function php_handle_column( $table, $column, $primary_keys, $old_url, $n * @param string $value The value to process. * @param string $old_url Old URL. * @param string $new_url New URL. + * @param int $depth How many serialized strings hold this value. * * @return string The processed value */ - private function replace_urls_in_value( $value, $old_url, $new_url ) { + private function replace_urls_in_value( $value, $old_url, $new_url, int $depth = 0 ) { // Check if the value is serialized if ( $this->is_serialized( $value ) ) { // A value with references (r: or R:) can refer to itself, which the URL walk cannot end. @@ -428,7 +436,7 @@ private function replace_urls_in_value( $value, $old_url, $new_url ) { return $value; } - return serialize( $this->replace_in_data( $unserialized, $old_url, $new_url ) ); + return serialize( $this->replace_in_data( $unserialized, $old_url, $new_url, $depth ) ); } return $this->replace_image_urls( $value, $old_url, $new_url ); @@ -566,27 +574,54 @@ private function holds_other_objects( $data ): bool { * @param mixed $data The data to process. * @param string $old_url Old URL. * @param string $new_url New URL. + * @param int $depth How many serialized strings hold this data. * * @return mixed The processed data */ - private function replace_in_data( $data, $old_url, $new_url ) { + private function replace_in_data( $data, $old_url, $new_url, int $depth ) { if ( is_array( $data ) ) { // Process arrays recursively foreach ( $data as $key => $value ) { - $data[ $key ] = $this->replace_in_data( $value, $old_url, $new_url ); + $data[ $key ] = $this->replace_in_data( $value, $old_url, $new_url, $depth ); } } elseif ( is_object( $data ) ) { // Process objects recursively foreach ( $data as $key => $value ) { - $data->$key = $this->replace_in_data( $value, $old_url, $new_url ); + $data->$key = $this->replace_in_data( $value, $old_url, $new_url, $depth ); } } elseif ( is_string( $data ) ) { - $data = $this->replace_image_urls( $data, $old_url, $new_url ); + $data = $this->replace_in_string( $data, $old_url, $new_url, $depth ); } return $data; } + /** + * Replace URLs in a string read from serialized data. + * + * Plugins can store a serialized value in a string of another one. That value is rewritten as data, so its + * declared lengths stay right, but only when it reads back to exactly the same text. Other text that only + * starts like serialized data gets the plain replacement. + * + * @param string $value The string. + * @param string $old_url Old URL. + * @param string $new_url New URL. + * @param int $depth How many serialized strings hold the data this string is in. + * + * @return string The processed string. + */ + private function replace_in_string( string $value, string $old_url, string $new_url, int $depth ): string { + if ( $this->is_serialized( $value ) ) { + $inner = @unserialize( $value, [ 'allowed_classes' => [ 'stdClass' ] ] ); + if ( false !== $inner && serialize( $inner ) === $value ) { + // Past the cap the value is kept as stored, because a text replacement would break its lengths. + return $depth < self::MAX_NESTED_DEPTH ? $this->replace_urls_in_value( $value, $old_url, $new_url, $depth + 1 ) : $value; + } + } + + return $this->replace_image_urls( $value, $old_url, $new_url ); + } + /** * Check if a string is serialized * diff --git a/tests/media_rename/test-db-renamer.php b/tests/media_rename/test-db-renamer.php index aaac24e1..55e3b61f 100644 --- a/tests/media_rename/test-db-renamer.php +++ b/tests/media_rename/test-db-renamer.php @@ -577,6 +577,46 @@ public function test_replace_updates_serialized_text_that_looks_like_an_object() $this->assertSame( [ 'image' => $new_url, 'note' => 'O:4:"Test":0:{}' ], get_post_meta( self::$post_id, 'optml_test_notes', true ) ); } + public function test_replace_updates_a_serialized_string_inside_a_serialized_option(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/nested-photo.jpg'; + $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/nested-renamed-photo.jpg'; + update_option( 'optml_test_nested', [ 'data' => serialize( [ 'url' => $old_url ] ) ] ); + + self::$replacer->replace( $old_url, $new_url ); + wp_cache_flush(); + + $this->assertSame( [ 'url' => $new_url ], unserialize( get_option( 'optml_test_nested' )['data'] ) ); + } + + public function test_replace_keeps_serialized_strings_past_the_nesting_cap_as_stored(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/deep-photo.jpg'; + $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/deep-renamed-photo.jpg'; + $deepest = serialize( [ 'url' => $old_url ] ); + $value = $deepest; + for ( $level = 0; $level < 3; $level++ ) { + $value = serialize( [ 'data' => $value ] ); + } + update_option( 'optml_test_deep', [ 'data' => $value, 'url' => $old_url ] ); + + self::$replacer->replace( $old_url, $new_url ); + wp_cache_flush(); + + $stored = get_option( 'optml_test_deep' ); + $this->assertSame( $new_url, $stored['url'] ); + $this->assertSame( serialize( [ 'data' => serialize( [ 'data' => $deepest ] ) ] ), unserialize( $stored['data'] )['data'] ); + } + + public function test_replace_updates_nested_text_that_starts_like_a_serialized_string(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/note-photo.jpg'; + $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/note-renamed-photo.jpg'; + update_option( 'optml_test_note', [ 'note' => 's:3:"foo"; see "' . $old_url . '";' ] ); + + self::$replacer->replace( $old_url, $new_url ); + wp_cache_flush(); + + $this->assertSame( [ 'note' => 's:3:"foo"; see "' . $new_url . '";' ], get_option( 'optml_test_note' ) ); + } + public function test_replace_updates_size_variants_in_serialized_meta(): void { update_post_meta( self::$post_id, 'optml_test_thumbnail', [ 'image' => 'http://om-wp.test/wp-content/uploads/2026/10/sized-photo-300x200.jpg' ] ); From 42abe22c8343af5e77c5e3f1bd2d1dec75149a68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Thu, 12 Nov 2026 09:30:32 +0200 Subject: [PATCH 6/9] test: pin the nesting cap and the class restriction of nested values --- tests/media_rename/test-db-renamer.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/tests/media_rename/test-db-renamer.php b/tests/media_rename/test-db-renamer.php index 55e3b61f..db65c27f 100644 --- a/tests/media_rename/test-db-renamer.php +++ b/tests/media_rename/test-db-renamer.php @@ -594,16 +594,33 @@ public function test_replace_keeps_serialized_strings_past_the_nesting_cap_as_st $deepest = serialize( [ 'url' => $old_url ] ); $value = $deepest; for ( $level = 0; $level < 3; $level++ ) { - $value = serialize( [ 'data' => $value ] ); + $value = serialize( [ 'data' => $value, 'url' => $old_url ] ); } update_option( 'optml_test_deep', [ 'data' => $value, 'url' => $old_url ] ); self::$replacer->replace( $old_url, $new_url ); wp_cache_flush(); + // Three nested levels are rewritten, and the fourth stays as stored. $stored = get_option( 'optml_test_deep' ); - $this->assertSame( $new_url, $stored['url'] ); - $this->assertSame( serialize( [ 'data' => serialize( [ 'data' => $deepest ] ) ] ), unserialize( $stored['data'] )['data'] ); + for ( $level = 0; $level < 3; $level++ ) { + $stored = unserialize( $stored['data'] ); + $this->assertSame( $new_url, $stored['url'] ); + } + $this->assertSame( $deepest, $stored['data'] ); + } + + public function test_replace_does_not_create_objects_nested_in_strings(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/nested-object-photo.jpg'; + $object = 'O:' . strlen( Optml_Test_Renamer_Stored_Object::class ) . ':"' . Optml_Test_Renamer_Stored_Object::class . '":1:{s:3:"url";s:' . strlen( $old_url ) . ':"' . $old_url . '";}'; + update_option( 'optml_test_nested_object', [ 'data' => $object, 'url' => $old_url ] ); + Optml_Test_Renamer_Stored_Object::$woken = false; + + self::$replacer->replace( $old_url, 'http://om-wp.test/wp-content/uploads/2026/10/nested-object-renamed.jpg' ); + wp_cache_flush(); + + $this->assertFalse( Optml_Test_Renamer_Stored_Object::$woken, 'The nested object must not be created.' ); + $this->assertSame( $object, get_option( 'optml_test_nested_object' )['data'] ); } public function test_replace_updates_nested_text_that_starts_like_a_serialized_string(): void { From d71c6e8d39503c81a570605793dbdc9a1664cb04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Thu, 12 Nov 2026 11:35:36 +0200 Subject: [PATCH 7/9] fix: keep nested values that cannot be read safely, and handle serialized strings in any column --- inc/media_rename/attachment_db_renamer.php | 42 ++++++++++-- tests/media_rename/test-db-renamer.php | 74 +++++++++++++++++++++- 2 files changed, 110 insertions(+), 6 deletions(-) diff --git a/inc/media_rename/attachment_db_renamer.php b/inc/media_rename/attachment_db_renamer.php index 1b874f6b..815f0441 100644 --- a/inc/media_rename/attachment_db_renamer.php +++ b/inc/media_rename/attachment_db_renamer.php @@ -187,7 +187,7 @@ private function process_column( $table, $column, $primary_keys, $old_url, $new_ // Check for serialized data $has_serialized = $wpdb->get_var( 'SELECT COUNT(' . esc_sql( $this->quote_identifier( $column ) ) . ') FROM ' . esc_sql( $this->quote_identifier( $table ) ) . - ' WHERE ' . esc_sql( $this->quote_identifier( $column ) ) . " REGEXP '^[aiOCE]:[1-9]' LIMIT 1" + ' WHERE ' . esc_sql( $this->quote_identifier( $column ) ) . " REGEXP '^[aiOCEs]:[1-9]' LIMIT 1" ); // Process with PHP if serialized data is found @@ -436,6 +436,12 @@ private function replace_urls_in_value( $value, $old_url, $new_url, int $depth = return $value; } + // Plain text can start like a serialized string, such as `s:3:"foo"; see …`, and keeps its text after + // the first value. + if ( is_string( $unserialized ) && ! $this->reads_back( $unserialized, trim( $value ) ) ) { + return $this->replace_image_urls( $value, $old_url, $new_url ); + } + return serialize( $this->replace_in_data( $unserialized, $old_url, $new_url, $depth ) ); } @@ -600,8 +606,9 @@ private function replace_in_data( $data, $old_url, $new_url, int $depth ) { * Replace URLs in a string read from serialized data. * * Plugins can store a serialized value in a string of another one. That value is rewritten as data, so its - * declared lengths stay right, but only when it reads back to exactly the same text. Other text that only - * starts like serialized data gets the plain replacement. + * declared lengths stay right, but only when it reads back to the same text. A serialized value that cannot + * be read safely stays as stored, as at the top level. Other text that only starts like serialized data gets + * the plain replacement. * * @param string $value The string. * @param string $old_url Old URL. @@ -613,7 +620,10 @@ private function replace_in_data( $data, $old_url, $new_url, int $depth ) { private function replace_in_string( string $value, string $old_url, string $new_url, int $depth ): string { if ( $this->is_serialized( $value ) ) { $inner = @unserialize( $value, [ 'allowed_classes' => [ 'stdClass' ] ] ); - if ( false !== $inner && serialize( $inner ) === $value ) { + if ( false === $inner || $this->holds_other_objects( $inner ) ) { + return $value; + } + if ( $this->reads_back( $inner, $value ) ) { // Past the cap the value is kept as stored, because a text replacement would break its lengths. return $depth < self::MAX_NESTED_DEPTH ? $this->replace_urls_in_value( $value, $old_url, $new_url, $depth + 1 ) : $value; } @@ -622,6 +632,30 @@ private function replace_in_string( string $value, string $old_url, string $new_ return $this->replace_image_urls( $value, $old_url, $new_url ); } + /** + * Check whether unserialized data serializes back to the stored text. + * + * PHP 7.0 and earlier wrote floats with 17 digits, such as `d:0.10000000000000001;`, so that precision is + * tried as well. + * + * @param mixed $data The unserialized data. + * @param string $stored The stored text. + * + * @return bool True when the data serializes to the stored text. + */ + private function reads_back( $data, string $stored ): bool { + if ( serialize( $data ) === $stored ) { + return true; + } + + $precision = ini_get( 'serialize_precision' ); + ini_set( 'serialize_precision', '17' ); + $same = serialize( $data ) === $stored; + ini_set( 'serialize_precision', $precision ); + + return $same; + } + /** * Check if a string is serialized * diff --git a/tests/media_rename/test-db-renamer.php b/tests/media_rename/test-db-renamer.php index db65c27f..ba7c4eec 100644 --- a/tests/media_rename/test-db-renamer.php +++ b/tests/media_rename/test-db-renamer.php @@ -626,12 +626,82 @@ public function test_replace_does_not_create_objects_nested_in_strings(): void { public function test_replace_updates_nested_text_that_starts_like_a_serialized_string(): void { $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/note-photo.jpg'; $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/note-renamed-photo.jpg'; - update_option( 'optml_test_note', [ 'note' => 's:3:"foo"; see "' . $old_url . '";' ] ); + update_option( + 'optml_test_note', + [ + 'note' => 's:3:"foo"; see "' . $old_url . '";', + 'data' => serialize( [ 'url' => $old_url ] ), + ] + ); + + self::$replacer->replace( $old_url, $new_url ); + wp_cache_flush(); + + $stored = get_option( 'optml_test_note' ); + $this->assertSame( 's:3:"foo"; see "' . $new_url . '";', $stored['note'] ); + $this->assertSame( [ 'url' => $new_url ], unserialize( $stored['data'] ) ); + } + + public function test_replace_keeps_nested_values_it_cannot_read_safely_as_stored(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/nested-unsafe-photo.jpg'; + $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/nested-unsafe-renamed.jpg'; + $url = 's:3:"url";s:' . strlen( $old_url ) . ':"' . $old_url . '";'; + $custom = 'a:2:{' . $url . 's:6:"widget";C:21:"Optml_Test_Not_Loaded":7:{payload}}'; + $enum = 'a:2:{' . $url . 's:4:"kind";E:20:"Optml_Test_No_Enum:A";}'; + update_option( + 'optml_test_nested_unsafe', + [ + 'custom' => $custom, + 'enum' => $enum, + 'url' => $old_url, + ] + ); + + self::$replacer->replace( $old_url, $new_url ); + wp_cache_flush(); + + $this->assertSame( + [ + 'custom' => $custom, + 'enum' => $enum, + 'url' => $new_url, + ], + get_option( 'optml_test_nested_unsafe' ) + ); + } + + public function test_replace_updates_nested_values_with_old_float_precision(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/float-photo.jpg'; + $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/float-renamed-photo.jpg'; + update_option( 'optml_test_float', [ 'data' => 'a:2:{s:3:"url";s:' . strlen( $old_url ) . ':"' . $old_url . '";s:5:"ratio";d:0.10000000000000001;}' ] ); self::$replacer->replace( $old_url, $new_url ); wp_cache_flush(); - $this->assertSame( [ 'note' => 's:3:"foo"; see "' . $new_url . '";' ], get_option( 'optml_test_note' ) ); + $this->assertSame( [ 'url' => $new_url, 'ratio' => 0.1 ], unserialize( get_option( 'optml_test_float' )['data'] ) ); + } + + public function test_replace_updates_columns_whose_only_serialized_values_are_strings(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/string-photo.jpg'; + $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/string-renamed-photo.jpg'; + $string = self::factory()->comment->create( + [ + 'comment_post_ID' => self::$post_id, + 'comment_content' => serialize( 'see ' . $old_url ), + ] + ); + $text = self::factory()->comment->create( + [ + 'comment_post_ID' => self::$post_id, + 'comment_content' => 's:3:"foo"; see "' . $old_url . '";', + ] + ); + + self::$replacer->replace( $old_url, $new_url ); + clean_comment_cache( [ $string, $text ] ); + + $this->assertSame( 'see ' . $new_url, unserialize( get_comment( $string )->comment_content ) ); + $this->assertSame( 's:3:"foo"; see "' . $new_url . '";', get_comment( $text )->comment_content ); } public function test_replace_updates_size_variants_in_serialized_meta(): void { From 73544e2bee3bf0e737ac50bf1cb8222d58daa491 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Thu, 12 Nov 2026 11:53:17 +0200 Subject: [PATCH 8/9] fix: check nested values for references, and keep padded string lengths valid --- inc/media_rename/attachment_db_renamer.php | 36 ++++++++++++++++++++-- tests/media_rename/test-db-renamer.php | 30 +++++++++++++++++- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/inc/media_rename/attachment_db_renamer.php b/inc/media_rename/attachment_db_renamer.php index 815f0441..667500c6 100644 --- a/inc/media_rename/attachment_db_renamer.php +++ b/inc/media_rename/attachment_db_renamer.php @@ -419,8 +419,7 @@ private function php_handle_column( $table, $column, $primary_keys, $old_url, $n private function replace_urls_in_value( $value, $old_url, $new_url, int $depth = 0 ) { // Check if the value is serialized if ( $this->is_serialized( $value ) ) { - // A value with references (r: or R:) can refer to itself, which the URL walk cannot end. - if ( preg_match( '/[;{][rR]:\d+;/', $value ) ) { + if ( $this->has_references( $value ) ) { return $value; } @@ -438,7 +437,7 @@ private function replace_urls_in_value( $value, $old_url, $new_url, int $depth = // Plain text can start like a serialized string, such as `s:3:"foo"; see …`, and keeps its text after // the first value. - if ( is_string( $unserialized ) && ! $this->reads_back( $unserialized, trim( $value ) ) ) { + if ( is_string( $unserialized ) && $this->has_text_after_string( trim( $value ) ) ) { return $this->replace_image_urls( $value, $old_url, $new_url ); } @@ -619,6 +618,9 @@ private function replace_in_data( $data, $old_url, $new_url, int $depth ) { */ private function replace_in_string( string $value, string $old_url, string $new_url, int $depth ): string { if ( $this->is_serialized( $value ) ) { + if ( $this->has_references( $value ) ) { + return $value; + } $inner = @unserialize( $value, [ 'allowed_classes' => [ 'stdClass' ] ] ); if ( false === $inner || $this->holds_other_objects( $inner ) ) { return $value; @@ -632,6 +634,34 @@ private function replace_in_string( string $value, string $old_url, string $new_ return $this->replace_image_urls( $value, $old_url, $new_url ); } + /** + * Check whether a serialized value has references (r: or R:). + * + * A reference can point back at its own value, which the URL walk and the object check cannot end. + * + * @param string $value The serialized value. + * + * @return bool True when the value has references. + */ + private function has_references( string $value ): bool { + return (bool) preg_match( '/[;{][rR]:\d+;/', $value ); + } + + /** + * Check whether text follows the first value of a serialized string, which unserialize() ignores. + * + * Lengths can have leading zeros, such as `s:027:"…";`, which serialize() does not write. + * + * @param string $value A serialized string that unserializes. + * + * @return bool True when text follows the string. + */ + private function has_text_after_string( string $value ): bool { + preg_match( '/^s:(\d+):"/', $value, $matches ); + + return strlen( $value ) > strlen( $matches[0] ) + (int) $matches[1] + 2; + } + /** * Check whether unserialized data serializes back to the stored text. * diff --git a/tests/media_rename/test-db-renamer.php b/tests/media_rename/test-db-renamer.php index ba7c4eec..c9b15128 100644 --- a/tests/media_rename/test-db-renamer.php +++ b/tests/media_rename/test-db-renamer.php @@ -670,14 +670,35 @@ public function test_replace_keeps_nested_values_it_cannot_read_safely_as_stored ); } + public function test_replace_keeps_nested_values_with_references_as_stored(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/nested-reference-photo.jpg'; + $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/nested-reference-renamed.jpg'; + $inner = 'O:8:"stdClass":2:{s:3:"url";s:' . strlen( $old_url ) . ':"' . $old_url . '";s:4:"self";r:1;}'; + // The escaped string format hides the reference from the check on the outer value. + $escaped = str_replace( ';', '\\3b', $inner ); + $comment_id = self::factory()->comment->create( + [ + 'comment_post_ID' => self::$post_id, + 'comment_content' => 'a:2:{s:3:"url";s:' . strlen( $old_url ) . ':"' . $old_url . '";s:4:"data";S:' . strlen( $inner ) . ':"' . $escaped . '";}', + ] + ); + + self::$replacer->replace( $old_url, $new_url ); + clean_comment_cache( $comment_id ); + + $this->assertSame( [ 'url' => $new_url, 'data' => $inner ], unserialize( get_comment( $comment_id )->comment_content ) ); + } + public function test_replace_updates_nested_values_with_old_float_precision(): void { $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/float-photo.jpg'; $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/float-renamed-photo.jpg'; update_option( 'optml_test_float', [ 'data' => 'a:2:{s:3:"url";s:' . strlen( $old_url ) . ':"' . $old_url . '";s:5:"ratio";d:0.10000000000000001;}' ] ); + $precision = ini_get( 'serialize_precision' ); self::$replacer->replace( $old_url, $new_url ); wp_cache_flush(); + $this->assertSame( $precision, ini_get( 'serialize_precision' ) ); $this->assertSame( [ 'url' => $new_url, 'ratio' => 0.1 ], unserialize( get_option( 'optml_test_float' )['data'] ) ); } @@ -696,11 +717,18 @@ public function test_replace_updates_columns_whose_only_serialized_values_are_st 'comment_content' => 's:3:"foo"; see "' . $old_url . '";', ] ); + $padded = self::factory()->comment->create( + [ + 'comment_post_ID' => self::$post_id, + 'comment_content' => 's:0' . strlen( $old_url ) . ':"' . $old_url . '";', + ] + ); self::$replacer->replace( $old_url, $new_url ); - clean_comment_cache( [ $string, $text ] ); + clean_comment_cache( [ $string, $text, $padded ] ); $this->assertSame( 'see ' . $new_url, unserialize( get_comment( $string )->comment_content ) ); + $this->assertSame( $new_url, unserialize( get_comment( $padded )->comment_content ) ); $this->assertSame( 's:3:"foo"; see "' . $new_url . '";', get_comment( $text )->comment_content ); } From fa309fb7ec5101b154bcddcc0a2976386ee10932 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytis=20Bulkevi=C4=8Dius?= Date: Thu, 12 Nov 2026 12:32:15 +0200 Subject: [PATCH 9/9] fix: read padded lengths and both float precisions, and share the read guard --- inc/media_rename/attachment_db_renamer.php | 83 ++++++++++++---------- tests/media_rename/test-db-renamer.php | 54 +++++++++++--- 2 files changed, 91 insertions(+), 46 deletions(-) diff --git a/inc/media_rename/attachment_db_renamer.php b/inc/media_rename/attachment_db_renamer.php index 667500c6..9e69d335 100644 --- a/inc/media_rename/attachment_db_renamer.php +++ b/inc/media_rename/attachment_db_renamer.php @@ -187,7 +187,7 @@ private function process_column( $table, $column, $primary_keys, $old_url, $new_ // Check for serialized data $has_serialized = $wpdb->get_var( 'SELECT COUNT(' . esc_sql( $this->quote_identifier( $column ) ) . ') FROM ' . esc_sql( $this->quote_identifier( $table ) ) . - ' WHERE ' . esc_sql( $this->quote_identifier( $column ) ) . " REGEXP '^[aiOCEs]:[1-9]' LIMIT 1" + ' WHERE ' . esc_sql( $this->quote_identifier( $column ) ) . " REGEXP '^[aiOCEs]:[0-9]' LIMIT 1" ); // Process with PHP if serialized data is found @@ -412,26 +412,14 @@ private function php_handle_column( $table, $column, $primary_keys, $old_url, $n * @param string $value The value to process. * @param string $old_url Old URL. * @param string $new_url New URL. - * @param int $depth How many serialized strings hold this value. * * @return string The processed value */ - private function replace_urls_in_value( $value, $old_url, $new_url, int $depth = 0 ) { + private function replace_urls_in_value( $value, $old_url, $new_url ) { // Check if the value is serialized if ( $this->is_serialized( $value ) ) { - if ( $this->has_references( $value ) ) { - return $value; - } - - // Any table can hold the URL, including values visitors write, so stored data must not create - // objects whose class runs code. - $unserialized = @unserialize( $value, [ 'allowed_classes' => [ 'stdClass' ] ] ); - - // A serialized value is left as stored when it cannot be read, such as one naming an enum that is - // not loaded, or when it holds an object other than stdClass: an object read without its class - // could not be written back unchanged, and an enum cannot change. A raw text replacement would - // break its declared lengths. - if ( false === $unserialized || $this->holds_other_objects( $unserialized ) ) { + $unserialized = $this->read_serialized( $value ); + if ( null === $unserialized ) { return $value; } @@ -441,7 +429,7 @@ private function replace_urls_in_value( $value, $old_url, $new_url, int $depth = return $this->replace_image_urls( $value, $old_url, $new_url ); } - return serialize( $this->replace_in_data( $unserialized, $old_url, $new_url, $depth ) ); + return serialize( $this->replace_in_data( $unserialized, $old_url, $new_url, 0 ) ); } return $this->replace_image_urls( $value, $old_url, $new_url ); @@ -618,16 +606,13 @@ private function replace_in_data( $data, $old_url, $new_url, int $depth ) { */ private function replace_in_string( string $value, string $old_url, string $new_url, int $depth ): string { if ( $this->is_serialized( $value ) ) { - if ( $this->has_references( $value ) ) { - return $value; - } - $inner = @unserialize( $value, [ 'allowed_classes' => [ 'stdClass' ] ] ); - if ( false === $inner || $this->holds_other_objects( $inner ) ) { + $inner = $this->read_serialized( $value ); + if ( null === $inner ) { return $value; } if ( $this->reads_back( $inner, $value ) ) { // Past the cap the value is kept as stored, because a text replacement would break its lengths. - return $depth < self::MAX_NESTED_DEPTH ? $this->replace_urls_in_value( $value, $old_url, $new_url, $depth + 1 ) : $value; + return $depth < self::MAX_NESTED_DEPTH ? serialize( $this->replace_in_data( $inner, $old_url, $new_url, $depth + 1 ) ) : $value; } } @@ -635,16 +620,32 @@ private function replace_in_string( string $value, string $old_url, string $new_ } /** - * Check whether a serialized value has references (r: or R:). + * Read a serialized value when the URL walk can rewrite it safely. * - * A reference can point back at its own value, which the URL walk and the object check cannot end. + * The value is left as stored when this returns null: + * - It has references (r: or R:), which can point back at their own value, so the walk cannot end. + * - It cannot be read, such as one naming an enum that is not loaded. + * - It holds an object other than stdClass. Such an object read without its class could not be written back + * unchanged, and an enum cannot change. + * A raw text replacement would break its declared lengths. * * @param string $value The serialized value. * - * @return bool True when the value has references. + * @return mixed|null The data, or null when the value is left as stored. */ - private function has_references( string $value ): bool { - return (bool) preg_match( '/[;{][rR]:\d+;/', $value ); + private function read_serialized( string $value ) { + if ( preg_match( '/[;{][rR]:\d+;/', $value ) ) { + return null; + } + + // Any table can hold the URL, including values visitors write, so stored data must not create objects + // whose class runs code. + $data = @unserialize( $value, [ 'allowed_classes' => [ 'stdClass' ] ] ); + if ( false === $data || $this->holds_other_objects( $data ) ) { + return null; + } + + return $data; } /** @@ -665,8 +666,9 @@ private function has_text_after_string( string $value ): bool { /** * Check whether unserialized data serializes back to the stored text. * - * PHP 7.0 and earlier wrote floats with 17 digits, such as `d:0.10000000000000001;`, so that precision is - * tried as well. + * Floats were written with 17 digits, such as `d:0.10000000000000001;`, by PHP 7.0 and earlier or with that + * setting, and in the shortest form since, so both precisions are tried. Lengths can have leading zeros, + * such as `s:062:"…";`, which serialize() does not write. * * @param mixed $data The unserialized data. * @param string $stored The stored text. @@ -674,16 +676,21 @@ private function has_text_after_string( string $value ): bool { * @return bool True when the data serializes to the stored text. */ private function reads_back( $data, string $stored ): bool { - if ( serialize( $data ) === $stored ) { - return true; - } - + $unpadded = preg_replace( '/(^|[;{}])([saOCE]):0+(?=\d)/', '$1$2:', $stored ); $precision = ini_get( 'serialize_precision' ); - ini_set( 'serialize_precision', '17' ); - $same = serialize( $data ) === $stored; - ini_set( 'serialize_precision', $precision ); - return $same; + try { + foreach ( [ $precision, '17', '-1' ] as $try ) { + ini_set( 'serialize_precision', $try ); + if ( in_array( serialize( $data ), [ $stored, $unpadded ], true ) ) { + return true; + } + } + } finally { + ini_set( 'serialize_precision', $precision ); + } + + return false; } /** diff --git a/tests/media_rename/test-db-renamer.php b/tests/media_rename/test-db-renamer.php index c9b15128..c3233b56 100644 --- a/tests/media_rename/test-db-renamer.php +++ b/tests/media_rename/test-db-renamer.php @@ -670,6 +670,51 @@ public function test_replace_keeps_nested_values_it_cannot_read_safely_as_stored ); } + public function test_replace_updates_columns_whose_only_serialized_values_have_padded_lengths(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/padded-photo.jpg'; + $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/padded-renamed-photo.jpg'; + $comment_id = self::factory()->comment->create( + [ + 'comment_post_ID' => self::$post_id, + 'comment_content' => 's:0' . strlen( $old_url ) . ':"' . $old_url . '";', + ] + ); + + self::$replacer->replace( $old_url, $new_url ); + clean_comment_cache( $comment_id ); + + $this->assertSame( $new_url, unserialize( get_comment( $comment_id )->comment_content ) ); + } + + public function test_replace_updates_nested_values_with_padded_lengths(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/nested-padded-photo.jpg'; + $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/nested-padded-renamed.jpg'; + update_option( 'optml_test_nested_padded', [ 'data' => 'a:1:{s:3:"url";s:0' . strlen( $old_url ) . ':"' . $old_url . '";}' ] ); + + self::$replacer->replace( $old_url, $new_url ); + wp_cache_flush(); + + $this->assertSame( [ 'url' => $new_url ], unserialize( get_option( 'optml_test_nested_padded' )['data'] ) ); + } + + public function test_replace_updates_nested_floats_when_the_site_uses_precision_17(): void { + $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/precision-photo.jpg'; + $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/precision-renamed-photo.jpg'; + $precision = ini_get( 'serialize_precision' ); + update_option( 'optml_test_precision', [ 'data' => 'a:2:{s:3:"url";s:' . strlen( $old_url ) . ':"' . $old_url . '";s:5:"ratio";d:0.1;}' ] ); + + ini_set( 'serialize_precision', '17' ); + try { + self::$replacer->replace( $old_url, $new_url ); + $this->assertSame( '17', ini_get( 'serialize_precision' ) ); + } finally { + ini_set( 'serialize_precision', $precision ); + } + wp_cache_flush(); + + $this->assertSame( [ 'url' => $new_url, 'ratio' => 0.1 ], unserialize( get_option( 'optml_test_precision' )['data'] ) ); + } + public function test_replace_keeps_nested_values_with_references_as_stored(): void { $old_url = 'http://om-wp.test/wp-content/uploads/2026/10/nested-reference-photo.jpg'; $new_url = 'http://om-wp.test/wp-content/uploads/2026/10/nested-reference-renamed.jpg'; @@ -717,18 +762,11 @@ public function test_replace_updates_columns_whose_only_serialized_values_are_st 'comment_content' => 's:3:"foo"; see "' . $old_url . '";', ] ); - $padded = self::factory()->comment->create( - [ - 'comment_post_ID' => self::$post_id, - 'comment_content' => 's:0' . strlen( $old_url ) . ':"' . $old_url . '";', - ] - ); self::$replacer->replace( $old_url, $new_url ); - clean_comment_cache( [ $string, $text, $padded ] ); + clean_comment_cache( [ $string, $text ] ); $this->assertSame( 'see ' . $new_url, unserialize( get_comment( $string )->comment_content ) ); - $this->assertSame( $new_url, unserialize( get_comment( $padded )->comment_content ) ); $this->assertSame( 's:3:"foo"; see "' . $new_url . '";', get_comment( $text )->comment_content ); }