Skip to content

Commit b36e72f

Browse files
authored
db import: Reject dot-commands in SQLite dump files (#340)
1 parent 154723e commit b36e72f

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎features/db-import.feature‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,22 @@ Feature: Import a WordPress database
248248
🍣
249249
"""
250250

251+
@require-sqlite
252+
Scenario: `wp db import` rejects dot-commands in SQLite dump files
253+
Given a WP install
254+
And a malicious_sqlite.sql file:
255+
"""
256+
CREATE TABLE wp_cli_sqlite_meta (id int NOT NULL);
257+
.shell touch side_effect_sqlite.txt
258+
"""
259+
260+
When I try `wp db import malicious_sqlite.sql`
261+
Then STDERR should contain:
262+
"""
263+
SQLite dot-commands are not allowed in import files.
264+
"""
265+
And the side_effect_sqlite.txt file should not exist
266+
251267
# SQLite does not use the MySQL client and has no concept of SQL modes.
252268
@require-mysql-or-mariadb
253269
Scenario: `wp db import` adapts the SQL mode via --init-command by default

‎src/DB_Command_SQLite.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ protected function sqlite_import( $file, $assoc_args ) {
454454
$contents = (string) file_get_contents( $file );
455455
}
456456

457+
if ( preg_match( '/^\s*\.[a-zA-Z]+/m', $contents ) ) {
458+
WP_CLI::error( 'SQLite dot-commands are not allowed in import files.' );
459+
}
460+
457461
// Ignore errors about unique constraints and existing indexes.
458462
$contents = str_replace( 'INSERT INTO', 'INSERT OR IGNORE INTO', $contents );
459463
$contents = preg_replace( '/\bCREATE TABLE (?!IF NOT EXISTS\b)/i', 'CREATE TABLE IF NOT EXISTS ', $contents );

0 commit comments

Comments
 (0)