Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-18811: Permit spaces in table and column aliases. #8548

Merged
merged 1 commit into from
Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions CRM/Utils/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,15 @@ public static function mysqlColumnNameOrAlias($str) {
return FALSE;
}

// Ensure the string contains only valid characters:
// For column names: alphanumeric and underscores
// For aliases: backticks, alphanumeric hyphens and underscores.
if (!preg_match('/^((`[\w-]{1,64}`|[\w-]{1,64})\.)?(`[\w-]{1,64}`|[\w-]{1,64})$/i', $str)) {
// Ensure $str conforms to expected format. Not a complete expression of
// what MySQL permits; this should permit the formats CiviCRM generates.
//
// * Table name prefix is optional.
// * Table & column names & aliases:
// * Composed of alphanumeric chars, underscore and hyphens.
// * Maximum length of 64 chars.
// * Optionally surrounded by backticks, in which case spaces also OK.
if (!preg_match('/^((`[\w- ]{1,64}`|[\w-]{1,64})\.)?(`[\w- ]{1,64}`|[\w-]{1,64})$/i', $str)) {
return FALSE;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/phpunit/CRM/Utils/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

/**
* Class CRM_Utils_TypeTest
* @package CiviCRM
* @subpackage CRM_Utils_Type
* @group headless
*/
class CRM_Utils_TypeTest extends CiviUnitTestCase {
Expand Down Expand Up @@ -46,12 +48,23 @@ public function validateDataProvider() {
array('table.`Home-street_address`', 'MysqlColumnNameOrAlias', 'table.`Home-street_address`'),
array('`table-alias`.`Home-street_address`', 'MysqlColumnNameOrAlias', '`table-alias`.`Home-street_address`'),
array('`table-alias`.column', 'MysqlColumnNameOrAlias', '`table-alias`.column'),
// Spaces also permitted, only when enclosed in backticks.
array('`column alias`', 'MysqlColumnNameOrAlias', '`column alias`'),
array('`table alias`.column', 'MysqlColumnNameOrAlias', '`table alias`.column'),
array('`table alias`.`column alias`', 'MysqlColumnNameOrAlias', '`table alias`.`column alias`'),
array('table alias.column alias', 'MysqlColumnNameOrAlias', NULL),
array('table alias.column_alias', 'MysqlColumnNameOrAlias', NULL),
array('table_alias.column alias', 'MysqlColumnNameOrAlias', NULL),
// Functions are not permitted.
array('column_name, sleep(5)', 'MysqlColumnNameOrAlias', NULL),
// Length checking permits only 64 chars.
array(str_repeat('a', 64), 'MysqlColumnNameOrAlias', str_repeat('a', 64)),
array(str_repeat('a', 65), 'MysqlColumnNameOrAlias', NULL),
array(str_repeat('a', 64) . '.' . str_repeat('a', 64), 'MysqlColumnNameOrAlias', str_repeat('a', 64) . '.' . str_repeat('a', 64)),
array('`' . str_repeat('a', 64) . '`.`' . str_repeat('b', 64) . '`', 'MysqlColumnNameOrAlias', '`' . str_repeat('a', 64) . '`.`' . str_repeat('b', 64) . '`'),
array(str_repeat('a', 64) . '.' . str_repeat('a', 65), 'MysqlColumnNameOrAlias', NULL),
array(str_repeat('a', 65) . '.' . str_repeat('a', 64), 'MysqlColumnNameOrAlias', NULL),
// ORDER BY can be ASC or DESC, case not significant.
array('asc', 'MysqlOrderByDirection', 'asc'),
array('DESC', 'MysqlOrderByDirection', 'desc'),
array('DESCc', 'MysqlOrderByDirection', NULL),
Expand Down