Skip to content

Commit dd75207

Browse files
committed
Prevent SQL injection through dump comments
Normalize comment line endings and prefix every physical line. Cover malicious table names containing newlines, carriage returns, backticks, and semicolons through an export/import regression.
1 parent 7ef82d4 commit dd75207

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎features/bootstrap/SQLiteFeatureContext.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
class SQLiteFeatureContext extends WPCLIFeatureContext implements Context {
1212

1313
private $db;
14+
private $comment_injection_table_name;
1415

1516
/**
1617
* @Given /^a SQL dump file named "([^"]*)" with content:$/
@@ -313,4 +314,32 @@ public function theSqliteDatabaseContainsATableWithABacktickInItsName() {
313314
'run'
314315
);
315316
}
317+
318+
/**
319+
* @Given /^the SQLite database contains a table with a comment injection name$/
320+
*/
321+
public function theSqliteDatabaseContainsATableWithACommentInjectionName() {
322+
$this->connectToDatabase();
323+
324+
$this->comment_injection_table_name = "safe`\nDROP TABLE IF EXISTS wp_users; --\rtable";
325+
$quoted_table_name = '"' . str_replace( '"', '""', $this->comment_injection_table_name ) . '"';
326+
327+
$this->db->exec( "CREATE TABLE $quoted_table_name (id INTEGER PRIMARY KEY, value TEXT)" );
328+
$this->db->exec( "INSERT INTO $quoted_table_name (id, value) VALUES (1, 'Test value')" );
329+
}
330+
331+
/**
332+
* @When /^I export the table with a comment injection name$/
333+
*/
334+
public function iExportTheTableWithACommentInjectionName() {
335+
$this->wpcli_tests_invoke_proc(
336+
$this->proc(
337+
sprintf(
338+
'wp sqlite --enable-ast-driver export test_export_comment.sql --tables=%s',
339+
escapeshellarg( $this->comment_injection_table_name )
340+
)
341+
),
342+
'run'
343+
);
344+
}
316345
}

‎features/sqlite-export.feature‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ Feature: WP-CLI SQLite Export Command
147147
INSERT INTO `test``table` VALUES (1,'Test value');
148148
"""
149149

150+
@require-sqlite
151+
Scenario: Export should keep multiline table names inside comments
152+
Given the SQLite database contains a table with a comment injection name
153+
When I export the table with a comment injection name
154+
And I run `wp sqlite --enable-ast-driver import test_export_comment.sql`
155+
Then STDOUT should contain:
156+
"""
157+
Success: Imported from 'test_export_comment.sql'.
158+
"""
159+
And the SQLite database should contain a table named "wp_users"
160+
150161
@require-sqlite
151162
Scenario: Export should preserve serialized settings containing a NUL byte
152163
Given the SQLite database contains a serialized settings option with a NUL byte separator

‎src/Export.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,11 @@ protected function escape_string( $value ) {
295295
* @return string
296296
*/
297297
protected function get_dump_comment( $comment ) {
298+
$comment = str_replace( array( "\r\n", "\r" ), "\n", $comment );
299+
298300
return implode(
299301
"\n",
300-
array( '--', sprintf( '-- %s', $comment ), '--' )
302+
array( '--', sprintf( '-- %s', str_replace( "\n", "\n-- ", $comment ) ), '--' )
301303
);
302304
}
303305

0 commit comments

Comments
 (0)