diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index a0215b4..abab495 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -188,6 +188,11 @@ const DRUPAL_KILOBYTE = 1024; /** + * The maximum number of characters in a module or theme name. + */ +const DRUPAL_EXTENSION_NAME_MAX_LENGTH = 50; + +/** * Special system language code (only applicable to UI language). * * Refers to the language used in Drupal and module/theme source code. Drupal diff --git a/core/includes/module.inc b/core/includes/module.inc index 5360a00..af8904b 100644 --- a/core/includes/module.inc +++ b/core/includes/module.inc @@ -8,6 +8,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\DependencyInjection\ContainerBuilder; +use Drupal\Core\Extension\ExtensionNameLengthException; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Yaml\Parser; @@ -260,6 +261,13 @@ function module_enable($module_list, $enable_dependencies = TRUE) { unset($module_list[$module]); continue; } + // Throw an exception if the module name is too long. + if (strlen($module) > DRUPAL_EXTENSION_NAME_MAX_LENGTH) { + throw new ExtensionNameLengthException(format_string('Module name %name is over the maximum allowed length of @max characters.', array( + '%name' => $module, + '@max' => DRUPAL_EXTENSION_NAME_MAX_LENGTH, + ))); + } $module_list[$module] = $module_data[$module]->sort; // Add dependencies to the list, with a placeholder weight. diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 0d0e739..2a9e40c 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -10,9 +10,10 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Config\Config; +use Drupal\Core\Extension\ExtensionNameLengthException; use Drupal\Core\Template\Attribute; -use Drupal\Core\Utility\ThemeRegistry; use Drupal\Core\Theme\ThemeSettings; +use Drupal\Core\Utility\ThemeRegistry; /** * @defgroup content_flags Content markers @@ -1560,6 +1561,14 @@ function theme_enable($theme_list) { $theme_config = config('system.theme'); $disabled_themes = config('system.theme.disabled'); foreach ($theme_list as $key) { + // Throw an exception if the theme name is too long. + if (strlen($key) > DRUPAL_EXTENSION_NAME_MAX_LENGTH) { + throw new ExtensionNameLengthException(format_string('Theme name %name is over the maximum allowed length of @max characters.', array( + '%name' => $key, + '@max' => DRUPAL_EXTENSION_NAME_MAX_LENGTH, + ))); + } + // The value is not used; the weight is ignored for themes currently. $theme_config->set("enabled.$key", 0); $disabled_themes->clear($key); diff --git a/core/modules/file/file.install b/core/modules/file/file.install index 3ca855e..2db5c47 100644 --- a/core/modules/file/file.install +++ b/core/modules/file/file.install @@ -112,7 +112,7 @@ function file_schema() { 'module' => array( 'description' => 'The name of the module that is using the file.', 'type' => 'varchar', - 'length' => 255, + 'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH, 'not null' => TRUE, 'default' => '', ), @@ -288,3 +288,19 @@ function file_update_8002() { } } } + +/** + * Convert the 'module' column in {file_usage} to the maximum shortname length. + */ +function file_update_8003() { + if (db_field_exists('file_usage', 'module')) { + $spec = array( + 'description' => 'The name of the module that is using the file.', + 'type' => 'varchar', + 'length' => SYSTEM_UPDATE_8056_NAME_MAX_LENGTH, + 'not null' => TRUE, + 'default' => '', + ); + db_change_field('file_usage', 'module', 'module', $spec); + } +} diff --git a/core/modules/menu_link/menu_link.install b/core/modules/menu_link/menu_link.install index 1d80679..a09be43 100644 --- a/core/modules/menu_link/menu_link.install +++ b/core/modules/menu_link/menu_link.install @@ -75,7 +75,7 @@ function menu_link_schema() { 'module' => array( 'description' => 'The name of the module that generated this link.', 'type' => 'varchar', - 'length' => 255, + 'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH, 'not null' => TRUE, 'default' => 'system', ), diff --git a/core/modules/node/node.install b/core/modules/node/node.install index b0735d3..9caf73e 100644 --- a/core/modules/node/node.install +++ b/core/modules/node/node.install @@ -320,7 +320,7 @@ function node_schema() { 'module' => array( 'description' => 'The module defining this node type.', 'type' => 'varchar', - 'length' => 255, + 'length' => SYSTEM_UPDATE_8056_NAME_MAX_LENGTH, 'not null' => TRUE, ), 'description' => array( @@ -752,6 +752,21 @@ function node_update_8015() { } /** + * Convert the 'module' column in {node_type} to the maximum shortname length. + */ +function node_update_8016() { + if (db_field_exists('node_type', 'module')) { + $spec = array( + 'description' => 'The module defining this node type.', + 'type' => 'varchar', + 'length' => SYSTEM_UPDATE_8056_NAME_MAX_LENGTH, + 'not null' => TRUE, + ); + db_change_field('node_type', 'module', 'module', $spec); + } +} + +/** * @} End of "addtogroup updates-7.x-to-8.x" * The next series of updates should start at 9000. */ diff --git a/core/modules/search/search.install b/core/modules/search/search.install index 40a569c..b4d879c 100644 --- a/core/modules/search/search.install +++ b/core/modules/search/search.install @@ -28,7 +28,7 @@ function search_schema() { ), 'type' => array( 'type' => 'varchar', - 'length' => 16, + 'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH, 'not null' => TRUE, 'description' => 'Type of item, e.g. node.', ), @@ -75,7 +75,7 @@ function search_schema() { ), 'type' => array( 'type' => 'varchar', - 'length' => 16, + 'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH, 'not null' => TRUE, 'description' => 'The {search_dataset}.type of the searchable item to which the word belongs.', ), @@ -132,7 +132,7 @@ function search_schema() { ), 'type' => array( 'type' => 'varchar', - 'length' => 16, + 'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH, 'not null' => TRUE, 'default' => '', 'description' => 'The {search_dataset}.type of the searchable item containing the link to the node.', diff --git a/core/modules/system/lib/Drupal/system/Tests/Module/ModuleEnable.php b/core/modules/system/lib/Drupal/system/Tests/Module/ModuleEnable.php index b2d08fc..97b4d03 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Module/ModuleEnable.php +++ b/core/modules/system/lib/Drupal/system/Tests/Module/ModuleEnable.php @@ -44,4 +44,19 @@ function testRequiredModuleSchemaVersions() { $this->assertTrue($version > 0, 'User module version is > 0.'); } + /** + * Tests that an exception is thrown when a module name is too long. + */ + function testModuleNameLength() { + $module_name = 'invalid_module_name_over_the_maximum_allowed_character_length'; + $message = format_string('Exception thrown when enabling module %name with a name length over the allowed maximum', array('%name' => $module_name)); + try { + module_enable(array($module_name)); + $this->fail($message); + } + catch (ExtensionNameLengthException $e) { + $this->pass($message); + } + } + } diff --git a/core/modules/system/system.install b/core/modules/system/system.install index 2a6f76c..39931df 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -1230,6 +1230,13 @@ function system_update_last_removed() { */ /** + * The maximum number of characters in a module name for system_update_8056(). + * + * @see DRUPAL_EXTENSION_NAME_MAX_LENGTH + */ +const SYSTEM_UPDATE_8056_NAME_MAX_LENGTH = 50; + +/** * Move from the Garland theme. */ function system_update_8001() { @@ -2142,6 +2149,23 @@ function system_update_8055() { } /** + * Change the length of the 'module' column to the maximum length. + */ +function system_update_8056() { + if (db_field_exists('menu_links', 'module')) { + $spec = array( + 'description' => 'The name of the module that generated this link.', + 'type' => 'varchar', + 'length' => SYSTEM_UPDATE_8056_NAME_MAX_LENGTH, + 'not null' => TRUE, + 'default' => 'system', + ); + db_change_field('menu_links', 'module', 'module', $spec); + } + +} + +/** * @} End of "defgroup updates-7.x-to-8.x". * The next series of updates should start at 9000. */ diff --git a/core/modules/system/tests/modules/invalid_module_name_over_the_maximum_allowed_character_length/invalid_module_name_over_the_maximum_allowed_character_length.info.yml b/core/modules/system/tests/modules/invalid_module_name_over_the_maximum_allowed_character_length/invalid_module_name_over_the_maximum_allowed_character_length.info.yml new file mode 100644 index 0000000..73f7f8b --- /dev/null +++ b/core/modules/system/tests/modules/invalid_module_name_over_the_maximum_allowed_character_length/invalid_module_name_over_the_maximum_allowed_character_length.info.yml @@ -0,0 +1,6 @@ +name: 'Module name length test' +description: 'Test module with a name over the maximum allowed characters.' +package: Testing +version: VERSION +core: 8.x +hidden: TRUE diff --git a/core/modules/system/tests/modules/invalid_module_name_over_the_maximum_allowed_character_length/invalid_module_name_over_the_maximum_allowed_character_length.module b/core/modules/system/tests/modules/invalid_module_name_over_the_maximum_allowed_character_length/invalid_module_name_over_the_maximum_allowed_character_length.module new file mode 100644 index 0000000..d0dc049 --- /dev/null +++ b/core/modules/system/tests/modules/invalid_module_name_over_the_maximum_allowed_character_length/invalid_module_name_over_the_maximum_allowed_character_length.module @@ -0,0 +1,8 @@ + array( 'type' => 'varchar', - 'length' => 255, + 'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH, 'not null' => TRUE, 'default' => '', 'description' => "User's default theme.", @@ -166,7 +166,7 @@ function user_schema() { ), 'module' => array( 'type' => 'varchar', - 'length' => 255, + 'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH, 'not null' => TRUE, 'default' => '', 'description' => "The module declaring the permission.", @@ -197,7 +197,7 @@ function user_schema() { 'module' => array( 'description' => 'The name of the module declaring the variable.', 'type' => 'varchar', - 'length' => 204, + 'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH, 'not null' => TRUE, 'default' => '', ), @@ -1051,5 +1051,43 @@ function user_update_8017() { } /** + * Use the maximum allowed module name length in module name database fields. + */ +function user_update_8018() { + if (db_field_exists('role_permission', 'module')) { + $spec = array( + 'type' => 'varchar', + 'length' => SYSTEM_UPDATE_8056_NAME_MAX_LENGTH, + 'not null' => TRUE, + 'default' => '', + 'description' => "The module declaring the permission.", + ); + db_change_field('role_permission', 'module', 'module', $spec); + } + + if (db_field_exists('users_data', 'module')) { + $spec = array( + 'description' => 'The name of the module declaring the variable.', + 'type' => 'varchar', + 'length' => SYSTEM_UPDATE_8056_NAME_MAX_LENGTH, + 'not null' => TRUE, + 'default' => '', + ); + db_change_field('users_data', 'module', 'module', $spec); + } + + if (db_field_exists('users', 'theme')) { + $spec = array( + 'type' => 'varchar', + 'length' => SYSTEM_UPDATE_8056_NAME_MAX_LENGTH, + 'not null' => TRUE, + 'default' => '', + 'description' => "User's default theme.", + ); + db_change_field('users', 'theme', 'theme', $spec); + } +} + +/** * @} End of "addtogroup updates-7.x-to-8.x". */