From 1eac9c5ec23d03feb164c4729ceeb0eda4ca0cda Mon Sep 17 00:00:00 2001 From: Deven Bansod Date: Wed, 19 Oct 2016 15:17:39 +0530 Subject: [PATCH 1/2] Proper initialization of $_message variable according to value of $result While calling getDisplay, $_message should be an instance of PMA\libraries\Message. Fix #12650 Also, spaces at the beginning of a table name are allowed if used inside backquotes. Signed-off-by: Deven Bansod --- libraries/Table.php | 18 +++++++++++++----- tbl_operations.php | 5 +++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/libraries/Table.php b/libraries/Table.php index 342e94cb4d..33cde1139b 100644 --- a/libraries/Table.php +++ b/libraries/Table.php @@ -1245,17 +1245,22 @@ class Table * checks if given name is a valid table name, * currently if not empty, trailing spaces, '.', '/' and '\' * - * @param string $table_name name to check + * @param string $table_name name to check + * @param boolean $is_backquoted whether this name is used inside backquotes or not * * @todo add check for valid chars in filename on current system/os * @see https://dev.mysql.com/doc/refman/5.0/en/legal-names.html * * @return boolean whether the string is valid or not */ - static function isValidName($table_name) + static function isValidName($table_name, $is_backquoted = false) { - if ($table_name !== trim($table_name)) { - // trailing spaces + if ($table_name !== rtrim($table_name)) { + // trailing spaces not allowed even in backquotes + return false; + } + if (! $is_backquoted && $table_name !== trim($table_name)) { + // spaces at the start or in between return false; } @@ -1310,7 +1315,10 @@ class Table return true; } - if (! Table::isValidName($new_name)) { + // Allow whitespaces (not trailing) in $new_name, + // since we are using $backquoted in getting the fullName of table + // below to be used in the query + if (! Table::isValidName($new_name, true)) { $this->errors[] = __('Invalid table name:') . ' ' . $new_table->getFullName(); return false; diff --git a/tbl_operations.php b/tbl_operations.php index 4f3b66760e..3c3754d7f4 100644 --- a/tbl_operations.php +++ b/tbl_operations.php @@ -226,7 +226,12 @@ if (isset($result) && empty($message_to_show)) { } exit; } + } else { + $_message = $result + ? PMA\libraries\Message::success($_message) + : PMA\libraries\Message::error($_message); } + if (! empty($warning_messages)) { $_message = new PMA\libraries\Message; $_message->addMessages($warning_messages); From 7d0e0b3163b713ecdbfa6977fd6c447f859f8f0f Mon Sep 17 00:00:00 2001 From: Deven Bansod Date: Wed, 19 Oct 2016 15:20:18 +0530 Subject: [PATCH 2/2] Add tests for validation of tablenames Signed-off-by: Deven Bansod --- test/classes/TableTest.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/classes/TableTest.php b/test/classes/TableTest.php index 508d167ce7..08b9a40d3a 100644 --- a/test/classes/TableTest.php +++ b/test/classes/TableTest.php @@ -385,11 +385,11 @@ class TableTest extends PMATestCase * * @dataProvider dataValidateName */ - public function testValidateName($name, $result) + public function testValidateName($name, $result, $is_backquoted=false) { $this->assertEquals( $result, - Table::isValidName($name) + Table::isValidName($name, $is_backquoted) ); } @@ -405,6 +405,10 @@ class TableTest extends PMATestCase array('te/st', false), array('te.st', false), array('te\\st', false), + array('te st', true), + array(' te st', true, true), + array('test ', false), + array('test ', false, true), ); }