Skip to content

Commit 358342a

Browse files
swissspidyclaude
andcommitted
Reject malformed --where specifications instead of ignoring them
A specification without the three parts (or without a table) was skipped, which silently made the run unrestricted. Error out while parsing, before any table is processed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 9818c3d commit 358342a

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎features/search-replace-where.feature‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,39 @@ Feature: Test search-replace --where option
355355
Success: 1 replacement to be made.
356356
"""
357357

358+
@require-mysql
359+
Scenario: Malformed where specifications are rejected before any table is processed
360+
Given a WP install
361+
And I run `wp post create --post_title='Post foo' --porcelain`
362+
And save STDOUT as {POST_ID}
363+
364+
When I try `wp search-replace foo bar --where='wp_posts:post_title'`
365+
Then STDERR should be:
366+
"""
367+
Error: Invalid --where specification 'wp_posts:post_title'. Expected format: <table>[,table]:[column,...]:<SQL-condition>.
368+
"""
369+
And the return code should be 1
370+
371+
When I try `wp search-replace foo bar --where='posts::1=1;wp_postmeta:meta_value'`
372+
Then STDERR should be:
373+
"""
374+
Error: Invalid --where specification 'wp_postmeta:meta_value'. Expected format: <table>[,table]:[column,...]:<SQL-condition>.
375+
"""
376+
And the return code should be 1
377+
378+
When I try `wp search-replace foo bar --where=":post_title:1=1"`
379+
Then STDERR should be:
380+
"""
381+
Error: Invalid --where specification ':post_title:1=1': no table given.
382+
"""
383+
And the return code should be 1
384+
385+
When I run `wp post get {POST_ID} --field=title`
386+
Then STDOUT should be:
387+
"""
388+
Post foo
389+
"""
390+
358391
Scenario: Where clause cannot be combined with an export
359392
Given a WP install
360393

‎src/Search_Replace_Command.php‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,11 +1586,14 @@ private function develop_where_specs( $str_specs ) {
15861586
foreach ( $specs as $spec ) {
15871587
$parts = array_map( 'trim', explode( ':', $spec, 3 ) );
15881588
if ( count( $parts ) < 3 ) {
1589-
continue;
1589+
WP_CLI::error( "Invalid --where specification '{$spec}'. Expected format: <table>[,table]:[column,...]:<SQL-condition>." );
15901590
}
15911591
list( $tables, $cols, $conditions ) = $parts;
1592-
$tables = array_filter( array_map( 'trim', explode( ',', $tables ) ) );
1593-
$cols = array_filter( array_map( 'trim', explode( ',', $cols ) ) ) ?: [ '*' ];
1592+
$tables = array_filter( array_map( 'trim', explode( ',', $tables ) ), 'strlen' );
1593+
$cols = array_filter( array_map( 'trim', explode( ',', $cols ) ), 'strlen' ) ?: [ '*' ];
1594+
if ( ! $tables ) {
1595+
WP_CLI::error( "Invalid --where specification '{$spec}': no table given." );
1596+
}
15941597
foreach ( $tables as $table ) {
15951598
foreach ( $cols as $col ) {
15961599
$clauses[ empty( $wpdb->{$table} ) ? $table : $wpdb->{$table} ][ $col ][] = $conditions;

0 commit comments

Comments
 (0)