Skip to content

Commit 7fed42c

Browse files
authored
db export: Avoid exec() in option probe (#326)
1 parent bfaf245 commit 7fed42c

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎features/db-export.feature‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,16 @@ Feature: Export a WordPress database
147147

148148
When I try `wp db export --no-defaults --debug`
149149
Then STDERR should match #Debug \(db\): Running initial shell command: /usr/bin/env (mysqldump|mariadb-dump) --no-defaults#
150+
151+
@skip-sqlite
152+
@skip-windows
153+
Scenario: Export database when PHP exec() is disabled
154+
Given a WP install
155+
156+
When I try `{INVOKE_WP_CLI_WITH_PHP_ARGS--ddisable_functions=exec} db export wp_cli_test.sql --porcelain`
157+
Then the return code should be 0
158+
And STDOUT should contain:
159+
"""
160+
wp_cli_test.sql
161+
"""
162+
And the wp_cli_test.sql file should exist

‎src/DB_Command.php‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use WP_CLI\Formatter;
4+
use WP_CLI\Process;
45
use WP_CLI\Utils;
56
use cli\table\Column;
67

@@ -747,7 +748,7 @@ public function export( $args, $assoc_args ) {
747748

748749
$mysqldump_binary = Utils\force_env_on_nix_systems( Utils\get_sql_dump_command() );
749750

750-
$support_column_statistics = exec( $mysqldump_binary . ' --help | grep "column-statistics"' );
751+
$support_column_statistics = $this->command_supports_option( $mysqldump_binary, 'column-statistics' );
751752

752753
/*
753754
* In case that `--default-character-set` is not given and `DB_CHARSET` is `utf8`,
@@ -859,6 +860,19 @@ private function get_posts_table_charset( $assoc_args ) {
859860
return $stdout;
860861
}
861862

863+
/**
864+
* Check whether a shell command advertises support for a specific option in `--help`.
865+
*
866+
* @param string $command Base shell command to inspect.
867+
* @param string $option Option name to look for.
868+
* @return bool Whether the option is listed in help output.
869+
*/
870+
private function command_supports_option( $command, $option ) {
871+
$result = Process::create( "{$command} --help" )->run();
872+
873+
return false !== strpos( $result->stdout . $result->stderr, $option );
874+
}
875+
862876
/**
863877
* Imports a database from a file or from STDIN.
864878
*

0 commit comments

Comments
 (0)