From 6cfade8f5a0e8d3052fa132cb72b2d54ba8c9e15 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Mon, 19 Mar 2012 18:44:33 +0100 Subject: [PATCH] MDL-32112 validate field name restrictions in sql_generator --- lib/ddl/simpletest/testddl.php | 46 ++++++++++++++++++++++++++++++++++ lib/xmldb/xmldb_field.php | 11 +++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/lib/ddl/simpletest/testddl.php b/lib/ddl/simpletest/testddl.php index 5d452a0396e..77e0567d2ed 100644 --- a/lib/ddl/simpletest/testddl.php +++ b/lib/ddl/simpletest/testddl.php @@ -15,6 +15,7 @@ require_once($CFG->libdir . '/adminlib.php'); class ddl_test extends UnitTestCase { private $tables = array(); private $records= array(); + /** @var moodle_database */ private $tdb; public static $includecoverage = array('lib/ddl'); public static $excludecoverage = array('lib/ddl/simpletest'); @@ -310,6 +311,51 @@ class ddl_test extends UnitTestCase { $this->assertTrue($e instanceof ddl_exception); } + // weird column names - the largest allowed + $table = new xmldb_table('test_table3'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('abcdef____0123456789_______xyz', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2'); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->setComment("This is a test'n drop table. You can drop it safely"); + + $this->tables[$table->getName()] = $table; + + $dbman->create_table($table); + $this->assertTrue($dbman->table_exists($table)); + $dbman->drop_table($table); + + // Too long field name - max 30 + $table = new xmldb_table('test_table4'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('abcdeabcdeabcdeabcdeabcdeabcdez', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2'); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->setComment("This is a test'n drop table. You can drop it safely"); + + $this->tables[$table->getName()] = $table; + + try { + $dbman->create_table($table); + $this->fail('Exception expected'); + } catch (Exception $e) { + $this->assertIdentical(get_class($e), 'coding_exception'); + } + + // Invalid field name + $table = new xmldb_table('test_table4'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('abCD', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2'); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->setComment("This is a test'n drop table. You can drop it safely"); + + $this->tables[$table->getName()] = $table; + + try { + $dbman->create_table($table); + $this->fail('Exception expected'); + } catch (Exception $e) { + $this->assertIdentical(get_class($e), 'coding_exception'); + } + } /** diff --git a/lib/xmldb/xmldb_field.php b/lib/xmldb/xmldb_field.php index a47f87e6514..05b3e7ab5a3 100644 --- a/lib/xmldb/xmldb_field.php +++ b/lib/xmldb/xmldb_field.php @@ -701,7 +701,16 @@ class xmldb_field extends xmldb_object { */ function validateDefinition(xmldb_table $xmldb_table=null) { if (!$xmldb_table) { - return 'Invalid xmldb_field->validateDefinition() call, $xmldb_table si required.'; + return 'Invalid xmldb_field->validateDefinition() call, $xmldb_table is required.'; + } + + $name = $this->getName(); + if (strlen($name) > 30) { + return 'Invalid field name in table {'.$xmldb_table->getName().'}: field "'.$this->getName().'" name is too long.' + .' Limit is 30 chars.'; + } + if (!preg_match('/^[a-z][a-z0-9_]*$/', $name)) { + return 'Invalid field name in table {'.$xmldb_table->getName().'}: field "'.$this->getName().'" name includes invalid characters.'; } switch ($this->getType()) {