Skip to content

Commit 19e4a9e

Browse files
authored
db import: Improve SQLite dot-command detection regex (#347)
1 parent f2e97a3 commit 19e4a9e

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎features/db-import.feature‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,16 @@ Feature: Import a WordPress database
256256
CREATE TABLE wp_cli_sqlite_meta (id int NOT NULL);
257257
.shell touch side_effect_sqlite.txt
258258
"""
259+
And a malicious_sqlite_space.sql file:
260+
"""
261+
CREATE TABLE wp_cli_sqlite_meta (id int NOT NULL);
262+
. shell touch side_effect_sqlite_space.txt
263+
"""
264+
And a malicious_sqlite_quotes.sql file:
265+
"""
266+
CREATE TABLE wp_cli_sqlite_meta (id int NOT NULL);
267+
."shell" touch side_effect_sqlite_quotes.txt
268+
"""
259269

260270
When I try `wp db import malicious_sqlite.sql`
261271
Then STDERR should contain:
@@ -264,6 +274,43 @@ Feature: Import a WordPress database
264274
"""
265275
And the side_effect_sqlite.txt file should not exist
266276

277+
When I try `wp db import malicious_sqlite_space.sql`
278+
Then STDERR should contain:
279+
"""
280+
SQLite dot-commands are not allowed in import files.
281+
"""
282+
And the side_effect_sqlite_space.txt file should not exist
283+
284+
When I try `wp db import malicious_sqlite_quotes.sql`
285+
Then STDERR should contain:
286+
"""
287+
SQLite dot-commands are not allowed in import files.
288+
"""
289+
And the side_effect_sqlite_quotes.txt file should not exist
290+
291+
@require-sqlite
292+
Scenario: `wp db import` allows multiline SQL containing numeric literals starting with a dot
293+
Given a WP install
294+
And a numeric_literal.sql file:
295+
"""
296+
CREATE TABLE wp_cli_prices (value REAL);
297+
INSERT INTO wp_cli_prices (value) VALUES (
298+
.5
299+
);
300+
"""
301+
302+
When I run `wp db import numeric_literal.sql`
303+
Then STDOUT should contain:
304+
"""
305+
Success: Imported from 'numeric_literal.sql'.
306+
"""
307+
308+
When I run `wp db query 'SELECT value FROM wp_cli_prices;' --skip-column-names`
309+
Then STDOUT should be:
310+
"""
311+
0.5
312+
"""
313+
267314
# SQLite does not use the MySQL client and has no concept of SQL modes.
268315
@require-mysql-or-mariadb
269316
Scenario: `wp db import` adapts the SQL mode via --init-command by default

‎src/DB_Command_SQLite.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ protected function sqlite_import( $file, $assoc_args ) {
467467
$contents = (string) file_get_contents( $file );
468468
}
469469

470-
if ( preg_match( '/^\s*\.[a-zA-Z]+/m', $contents ) ) {
470+
if ( preg_match( '/^[ \t]*\.(?![0-9])/m', $contents ) ) {
471471
WP_CLI::error( 'SQLite dot-commands are not allowed in import files.' );
472472
}
473473

0 commit comments

Comments
 (0)