Skip to content

Commit 8e7e300

Browse files
Copilotswissspidy
andauthored
Fix: allow stdClass by default, use custom class in hook test scenario
Agent-Logs-Url: https://github.com/wp-cli/search-replace-command/sessions/599c39d4-de3e-4bc3-a371-0e93ec6c6a78 Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 21f1537 commit 8e7e300

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

features/search-replace.feature

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,19 +1207,22 @@ Feature: Do global search/replace
12071207
Scenario: The search_replace_unserialize_options hook allows overriding allowed_classes for unserialize
12081208

12091209
Given a WP install
1210-
And I run `wp option add cereal_isation 'O:8:"stdClass":1:{s:3:"foo";s:13:"cereal_marker";}'`
1210+
And I run `wp db query "INSERT INTO wp_options (option_name,option_value) VALUES ('cereal_isation','O:7:\"MyClass\":1:{s:3:\"foo\";s:13:\"cereal_marker\";}')"`
12111211
And a hook.php file:
12121212
"""
12131213
<?php
1214+
class MyClass {
1215+
public $foo = '';
1216+
}
12141217
WP_CLI::add_hook( 'search_replace_unserialize_options', function() {
1215-
return [ 'allowed_classes' => [ 'stdClass' ] ];
1218+
return [ 'allowed_classes' => [ 'stdClass', 'MyClass' ] ];
12161219
} );
12171220
"""
12181221

12191222
When I try `wp search-replace cereal_marker cereal_replaced`
12201223
Then STDERR should contain:
12211224
"""
1222-
Warning: Skipping an uninitialized class "stdClass", replacements might not be complete.
1225+
Warning: Skipping an uninitialized class "MyClass", replacements might not be complete.
12231226
"""
12241227
And STDOUT should contain:
12251228
"""

src/WP_CLI/SearchReplacer.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,14 @@ public function __construct( $from, $to, $recurse_objects = false, $regex = fals
103103
/**
104104
* Filter the options passed to unserialize() during search-replace.
105105
*
106-
* Defaults to `[ 'allowed_classes' => false ]` to prevent instantiation
107-
* of arbitrary classes as a hardening measure. Use this hook to allow
108-
* specific classes when needed.
106+
* Defaults to `[ 'allowed_classes' => [ 'stdClass' ] ]` to allow the
107+
* built-in stdClass (used extensively by WordPress, e.g. theme mods)
108+
* while blocking arbitrary user-defined class instantiation. Use this
109+
* hook to allow additional classes when needed.
109110
*
110111
* @param array<string, mixed> $options Options array for unserialize().
111112
*/
112-
$this->unserialize_options = \WP_CLI::do_hook( 'search_replace_unserialize_options', [ 'allowed_classes' => false ] );
113+
$this->unserialize_options = \WP_CLI::do_hook( 'search_replace_unserialize_options', [ 'allowed_classes' => [ 'stdClass' ] ] );
113114
}
114115

115116
/**

0 commit comments

Comments
 (0)