Skip to content

Commit 002673e

Browse files
swissspidyclaude
andcommitted
Harden --recurse-keys collision checks and stage object renames
- Detect destination-key collisions by PHP array-key storage identity instead of array_unique( SORT_REGULAR ), so distinct keys such as 8 and '08' no longer trigger the collision fallback. - Compare offsets case-insensitively for the Requests case-insensitive dictionary, and keep the original offsets for ArrayAccess implementations whose offset semantics are unknown. - Read and process all property values before removing or writing any property, so overlapping source/destination names cannot lose data. - Add Behat scenarios for all three cases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 8797224 commit 002673e

2 files changed

Lines changed: 157 additions & 7 deletions

File tree

‎features/search-replace.feature‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,3 +1831,86 @@ Feature: Do global search/replace
18311831
"""
18321832
HASREF
18331833
"""
1834+
1835+
Scenario: Key collision detection with --recurse-keys compares keys as PHP stores them
1836+
Given a WP install
1837+
And a recurse-keys-storage-identity.php file:
1838+
"""
1839+
<?php
1840+
$replacer = new \WP_CLI\SearchReplacer( 'old', 'new', true, false, '', '/', false, -1, true );
1841+
echo $replacer->run( serialize( array( 8 => 'v1', '08' => 'v2', 'old' => 'v3' ) ) ), "\n";
1842+
echo $replacer->run( serialize( (object) array( 8 => 'v1', '08' => 'v2', 'old' => 'v3' ) ) ), "\n";
1843+
"""
1844+
1845+
When I run `wp eval-file recurse-keys-storage-identity.php`
1846+
Then STDERR should be empty
1847+
And STDOUT should contain:
1848+
"""
1849+
a:3:{i:8;s:2:"v1";s:2:"08";s:2:"v2";s:3:"new";s:2:"v3";}
1850+
"""
1851+
And STDOUT should contain:
1852+
"""
1853+
s:3:"new";s:2:"v3";}
1854+
"""
1855+
And STDOUT should not contain:
1856+
"""
1857+
"old"
1858+
"""
1859+
1860+
Scenario: Overlapping property names are renamed without losing values with --recurse-keys
1861+
Given a WP install
1862+
And a recurse-keys-overlap.php file:
1863+
"""
1864+
<?php
1865+
$replacer = new \WP_CLI\SearchReplacer( 'old', 'newold', true, false, '', '/', false, -1, true );
1866+
$result = unserialize( $replacer->run( serialize( (object) array( 'old' => 'v1', 'newold' => 'v2' ) ) ) );
1867+
echo json_encode( $result );
1868+
"""
1869+
1870+
When I run `wp eval-file recurse-keys-overlap.php`
1871+
Then STDERR should be empty
1872+
And STDOUT should contain:
1873+
"""
1874+
"newold":"v1"
1875+
"""
1876+
And STDOUT should contain:
1877+
"""
1878+
"newnewold":"v2"
1879+
"""
1880+
1881+
# `CaseInsensitiveDictionary` is from Requests 2.0, which was added in 6.2.
1882+
@require-wp-6.2
1883+
Scenario: Case-insensitive dictionary offsets are not renamed onto an existing offset with --recurse-keys
1884+
Given a WP install
1885+
And a recurse-keys-case-insensitive.php file:
1886+
"""
1887+
<?php
1888+
WP_CLI::add_hook(
1889+
'search_replace_unserialize_options',
1890+
function () {
1891+
return [ 'allowed_classes' => [ 'WpOrg\Requests\Utility\CaseInsensitiveDictionary' ] ];
1892+
}
1893+
);
1894+
$replacer = new \WP_CLI\SearchReplacer( 'old', 'NEW', true, false, '', '/', false, -1, true );
1895+
$result = unserialize(
1896+
$replacer->run(
1897+
serialize( new \WpOrg\Requests\Utility\CaseInsensitiveDictionary( [ 'old' => 'v1', 'new' => 'v2' ] ) )
1898+
),
1899+
[ 'allowed_classes' => [ 'WpOrg\Requests\Utility\CaseInsensitiveDictionary' ] ]
1900+
);
1901+
echo json_encode( $result->getAll() );
1902+
"""
1903+
1904+
When I try `wp eval-file recurse-keys-case-insensitive.php`
1905+
Then STDERR should contain:
1906+
"""
1907+
two property names would map onto the same name
1908+
"""
1909+
And STDOUT should contain:
1910+
"""
1911+
"old":"v1"
1912+
"""
1913+
And STDOUT should contain:
1914+
"""
1915+
"new":"v2"
1916+
"""

‎src/WP_CLI/SearchReplacer.php‎

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ private function replace_array_keys( $data, $recursion_level, $visited_data ) {
308308
foreach ( array_keys( $data ) as $key ) {
309309
$renames[ $key ] = is_string( $key ) ? $this->replace_string( $key ) : $key;
310310
}
311-
if ( count( array_unique( $renames, SORT_REGULAR ) ) !== count( $renames ) ) {
311+
if ( $this->has_destination_collision( $renames, false ) ) {
312312
\WP_CLI::warning( 'Skipping key replacement for an array: two keys would map onto the same key and a value would be lost.' );
313313
$renames = array_combine( array_keys( $renames ), array_keys( $renames ) );
314314
}
@@ -324,9 +324,17 @@ private function replace_array_keys( $data, $recursion_level, $visited_data ) {
324324
/**
325325
* Recurse into an object, replacing string property names as well as values.
326326
*
327-
* When two property names would map onto the same name, the property
328-
* names are left unchanged (values are still replaced) and a warning is
329-
* emitted instead of silently discarding a value.
327+
* All values are read and processed before the object is modified, so a
328+
* renamed property can never overwrite a source property that has not
329+
* been read yet. When two property names would map onto the same name,
330+
* the property names are left unchanged (values are still replaced) and a
331+
* warning is emitted instead of silently discarding a value.
332+
*
333+
* ArrayAccess implementations may normalize offsets on write. Offsets are
334+
* only renamed for implementations whose offset semantics are known
335+
* (ArrayObject, ArrayIterator and the Requests case-insensitive
336+
* dictionary); for any other ArrayAccess implementation the original
337+
* offsets are kept.
330338
*
331339
* @param object $data The object to process.
332340
* @param bool $is_array_access Whether the object implements ArrayAccess.
@@ -339,20 +347,79 @@ private function replace_object_property_names( $data, $is_array_access, $recurs
339347
foreach ( $data as $key => $value ) {
340348
$renames[ $key ] = is_string( $key ) ? $this->replace_string( $key ) : $key;
341349
}
342-
if ( count( array_unique( $renames, SORT_REGULAR ) ) !== count( $renames ) ) {
350+
351+
$case_insensitive = false;
352+
$keep_offsets = false;
353+
if ( $is_array_access ) {
354+
if ( $data instanceof \WpOrg\Requests\Utility\CaseInsensitiveDictionary || is_a( $data, 'Requests_Utility_CaseInsensitiveDictionary' ) ) {
355+
$case_insensitive = true;
356+
} elseif ( ! $data instanceof \ArrayObject && ! $data instanceof \ArrayIterator ) {
357+
$keep_offsets = true;
358+
}
359+
}
360+
361+
$renamed = false;
362+
foreach ( $renames as $key => $new_key ) {
363+
if ( $new_key !== $key ) {
364+
$renamed = true;
365+
break;
366+
}
367+
}
368+
369+
if ( $renamed && $keep_offsets ) {
370+
\WP_CLI::warning(
371+
sprintf(
372+
'Skipping key replacement for an object of class "%s": its offset semantics are unknown.',
373+
get_class( $data )
374+
)
375+
);
376+
$renames = array_combine( array_keys( $renames ), array_keys( $renames ) );
377+
} elseif ( $renamed && $this->has_destination_collision( $renames, $case_insensitive ) ) {
343378
\WP_CLI::warning( 'Skipping key replacement for an object: two property names would map onto the same name and a value would be lost.' );
344379
$renames = array_combine( array_keys( $renames ), array_keys( $renames ) );
345380
}
381+
382+
// Stage all processed values before touching the object.
383+
$values = array();
384+
foreach ( $renames as $key => $new_key ) {
385+
$values[ $key ] = $this->run_recursively( $this->read_property( $data, $is_array_access, $key ), false, $recursion_level + 1, $visited_data );
386+
}
346387
foreach ( $renames as $key => $new_key ) {
347-
$value = $this->run_recursively( $this->read_property( $data, $is_array_access, $key ), false, $recursion_level + 1, $visited_data );
348388
if ( $new_key !== $key ) {
349389
$this->remove_property( $data, $is_array_access, $key );
350390
}
351-
$this->write_property( $data, $is_array_access, $new_key, $value );
391+
}
392+
foreach ( $renames as $key => $new_key ) {
393+
$this->write_property( $data, $is_array_access, $new_key, $values[ $key ] );
352394
}
353395
return $data;
354396
}
355397

398+
/**
399+
* Check whether two renamed keys would end up as the same stored key.
400+
*
401+
* Destination keys are compared by how PHP actually stores them as array
402+
* keys (so `8` and `'08'` stay distinct, while `'8'` and `8` collide), and
403+
* optionally case-insensitively for containers that normalize offsets.
404+
*
405+
* @param array $renames Map of original key to destination key.
406+
* @param bool $case_insensitive Whether destination keys are compared case-insensitively.
407+
* @return bool Whether at least two destination keys collide.
408+
*/
409+
private function has_destination_collision( $renames, $case_insensitive ) {
410+
$seen = array();
411+
foreach ( $renames as $new_key ) {
412+
if ( $case_insensitive && is_string( $new_key ) ) {
413+
$new_key = strtolower( $new_key );
414+
}
415+
if ( isset( $seen[ $new_key ] ) ) {
416+
return true;
417+
}
418+
$seen[ $new_key ] = true;
419+
}
420+
return false;
421+
}
422+
356423
/**
357424
* Read one property or offset from an object or ArrayAccess instance.
358425
*

0 commit comments

Comments
 (0)