Skip to content

Commit aabd7e0

Browse files
Copilotswissspidy
andcommitted
Fix command injection vulnerabilities in sqlite_create and sqlite_reset
- Use Utils\esc_cmd to properly escape database path arguments - Add is_sqlite3_available() helper to check for sqlite3 binary - Add preflight check with clear error message if sqlite3 not found - Addresses security concerns about unescaped shell commands Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 41709cd commit aabd7e0

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

src/DB_Command_SQLite.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@
1111
*/
1212
trait DB_Command_SQLite {
1313

14+
/**
15+
* Check if sqlite3 CLI binary is available.
16+
*
17+
* @return bool True if sqlite3 is available, false otherwise.
18+
*/
19+
protected function is_sqlite3_available() {
20+
static $available = null;
21+
22+
if ( null === $available ) {
23+
$result = \WP_CLI\Process::create( 'which sqlite3', null, null )->run();
24+
$available = 0 === $result->return_code;
25+
}
26+
27+
return $available;
28+
}
29+
1430
/**
1531
* Check if SQLite is being used.
1632
*
@@ -103,7 +119,13 @@ protected function sqlite_create() {
103119
WP_CLI::error( 'Database already exists.' );
104120
}
105121

106-
$command = "sqlite3 $db_path \"\"";
122+
// Check if sqlite3 binary is available.
123+
if ( ! $this->is_sqlite3_available() ) {
124+
WP_CLI::error( 'The sqlite3 CLI binary is required but not found. Please install SQLite3.' );
125+
}
126+
127+
// Use Utils\esc_cmd to properly escape the command and arguments.
128+
$command = Utils\esc_cmd( 'sqlite3 %s %s', $db_path, '' );
107129

108130
WP_CLI::debug( "Running shell command: {$command}", 'db' );
109131

@@ -162,7 +184,13 @@ protected function sqlite_reset() {
162184
}
163185
}
164186

165-
$command = "sqlite3 $db_path \"\"";
187+
// Check if sqlite3 binary is available.
188+
if ( ! $this->is_sqlite3_available() ) {
189+
WP_CLI::error( 'The sqlite3 CLI binary is required but not found. Please install SQLite3.' );
190+
}
191+
192+
// Use Utils\esc_cmd to properly escape the command and arguments.
193+
$command = Utils\esc_cmd( 'sqlite3 %s %s', $db_path, '' );
166194

167195
WP_CLI::debug( "Running shell command: {$command}", 'db' );
168196

0 commit comments

Comments
 (0)