diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index c7094af..91860af 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -186,6 +186,11 @@ const DRUPAL_KILOBYTE = 1024; /** + * The maximum number of characters in a module or theme name. + */ +const DRUPAL_MODULE_NAME_MAX_LENGTH = 40; + +/** * 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 85c0240..11e4c2f 100644 --- a/core/includes/module.inc +++ b/core/includes/module.inc @@ -260,6 +260,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_MODULE_NAME_MAX_LENGTH) { + throw new \Exception(format_string('Module name %name is over the maximum allowed length of @max characters.', array( + '%name' => $module, + '@max' => DRUPAL_MODULE_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 47c99d9..e7e0d9f 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1514,6 +1514,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_MODULE_NAME_MAX_LENGTH) { + throw new \Exception(format_string('Theme name %name is over the maximum allowed length of @max characters.', array( + '%name' => $key, + '@max' => DRUPAL_MODULE_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..8d9578b 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_MODULE_NAME_MAX_LENGTH, 'not null' => TRUE, 'default' => '', ), @@ -288,3 +288,17 @@ function file_update_8002() { } } } + +/** + * Convert the 'module' column in {file_usage} to the maximum shortname length. + */ +function file_update_8003() { + $spec = array( + 'description' => 'The name of the module that is using the file.', + 'type' => 'varchar', + 'length' => DRUPAL_MODULE_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..c711b2e 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_MODULE_NAME_MAX_LENGTH, 'not null' => TRUE, 'default' => 'system', ), @@ -214,3 +214,17 @@ function menu_link_schema() { return $schema; } + +/** + * Convert the 'module' column in {menu_links} to the maximum shortname length. + */ +function menu_link_update_8000() { + $spec = array( + 'description' => 'The name of the module that generated this link.', + 'type' => 'varchar', + 'length' => DRUPAL_MODULE_NAME_MAX_LENGTH, + 'not null' => TRUE, + 'default' => 'system', + ); + db_change_field('menu_links', 'module', 'module', $spec); +} diff --git a/core/modules/node/node.install b/core/modules/node/node.install index b0735d3..705ffe1 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' => DRUPAL_MODULE_NAME_MAX_LENGTH, 'not null' => TRUE, ), 'description' => array( @@ -752,6 +752,20 @@ function node_update_8015() { } /** + * Convert the 'module' column in {node_type} to the maximum shortname length. + */ +function node_update_8016() { + $spec = array( + 'description' => 'The module defining this node type.', + 'type' => 'varchar', + 'length' => DRUPAL_MODULE_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/system/lib/Drupal/system/Tests/Module/ModuleEnable.php b/core/modules/system/lib/Drupal/system/Tests/Module/ModuleEnable.php index b2d08fc..1b41d3e 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 (\Exception $e) { + $this->pass($message); + } + } + } 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 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 new file mode 100644 index 0000000..24c3a2a --- /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 @@ -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_MODULE_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_MODULE_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_MODULE_NAME_MAX_LENGTH, 'not null' => TRUE, 'default' => '', ), @@ -1051,5 +1051,47 @@ function user_update_8017() { } /** + * Convert the 'module' column in {role_permission} to the maximum shortname length. + */ +function user_update_8018() { + $spec = array( + 'type' => 'varchar', + 'length' => DRUPAL_MODULE_NAME_MAX_LENGTH, + 'not null' => TRUE, + 'default' => '', + 'description' => "The module declaring the permission.", + ); + db_change_field('role_permission', 'module', 'module', $spec); +} + +/** + * Convert the 'module' column in {users_data} to the maximum shortname length. + */ +function user_update_8019() { + $spec = array( + 'description' => 'The name of the module declaring the variable.', + 'type' => 'varchar', + 'length' => DRUPAL_MODULE_NAME_MAX_LENGTH, + 'not null' => TRUE, + 'default' => '', + ); + db_change_field('users_data', 'module', 'module', $spec); +} + +/** + * Convert the 'theme' column in {users_data} to the maximum shortname length. + */ +function user_update_8020() { + $spec = array( + 'type' => 'varchar', + 'length' => DRUPAL_MODULE_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". */