Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 151 additions & 10 deletions inc/media_rename/attachment_db_renamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -180,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 '^[aiO]:[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
Expand Down Expand Up @@ -411,13 +418,18 @@ 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 );
$unserialized = $this->read_serialized( $value );
if ( null === $unserialized ) {
return $value;
}

// If unserialize successful, process the data
if ( $unserialized !== false ) {
$replaced = $this->replace_in_data( $unserialized, $old_url, $new_url );
return serialize( $replaced );
// 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->has_text_after_string( trim( $value ) ) ) {
return $this->replace_image_urls( $value, $old_url, $new_url );
}

return serialize( $this->replace_in_data( $unserialized, $old_url, $new_url, 0 ) );
}

return $this->replace_image_urls( $value, $old_url, $new_url );
Expand Down Expand Up @@ -527,33 +539,160 @@ 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, without references.
*
* @return bool True when the data holds such an object.
*/
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 ) ) {
return true;
}
}
}

return false;
}

/**
* Recursively replace URLs in data structure
*
* @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 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.
* @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 = $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 ? serialize( $this->replace_in_data( $inner, $old_url, $new_url, $depth + 1 ) ) : $value;
}
}

return $this->replace_image_urls( $value, $old_url, $new_url );
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
Comment thread
pirate-bot marked this conversation as resolved.
Resolved
}

/**
* Read a serialized value when the URL walk can rewrite it safely.
*
* 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 mixed|null The data, or null when the value is left as stored.
*/
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;
}

/**
* 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.
*
* 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.
*
* @return bool True when the data serializes to the stored text.
*/
private function reads_back( $data, string $stored ): bool {
$unpadded = preg_replace( '/(^|[;{}])([saOCE]):0+(?=\d)/', '$1$2:', $stored );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note The unpadding regex also changes text inside string payloads. A nested padded value whose string holds text like ;s:003:"x"; then fails both comparisons. Line 619 applies the plain replacement, and the inner value stops unserializing. A sandbox run showed this with a hand-built value. Does any plugin or importer store padded lengths? If none does, this is safe. If one does, remove zeros only at token positions.

$precision = ini_get( 'serialize_precision' );

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;
}

/**
* Check if a string is serialized
*
Expand Down Expand Up @@ -594,6 +733,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 );
Expand Down
Loading
Loading