diff --git a/collection.c b/collection.c index 629e459b7..423fcc961 100644 --- a/collection.c +++ b/collection.c @@ -74,11 +74,13 @@ void php_mongo_collection_construct(zval *this, zval *parent, char *name_str, in char *ns; /* check for empty and invalid collection names */ - if ( - name_len == 0 || - memchr(name_str, '\0', name_len) != 0 - ) { - zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "MongoDB::__construct(): invalid name %s", name_str); + if (name_len == 0) { + zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Collection name cannot be empty"); + return; + } + + if (memchr(name_str, '\0', name_len) != 0) { + zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Collection name cannot contain null bytes: %s\\0...", name_str); return; } diff --git a/db.c b/db.c index bf5290303..d12ba2a49 100644 --- a/db.c +++ b/db.c @@ -112,18 +112,24 @@ static int php_mongo_command_supports_rp(zval *cmd) int php_mongo_db_is_valid_dbname(char *dbname, int dbname_len TSRMLS_DC) { + if (dbname_len == 0) { + zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Database name cannot be empty"); + return 0; + } + if (memchr(dbname, '\0', dbname_len) != NULL) { - zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "MongoDB::__construct(): '\\0' not allowed in database names: %s\\0...", dbname); + zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Database name cannot contain null bytes: %s\\0...", dbname); + return 0; } if ( - dbname_len == 0 || memchr(dbname, ' ', dbname_len) != 0 || memchr(dbname, '.', dbname_len) != 0 || memchr(dbname, '\\', dbname_len) != 0 || memchr(dbname, '/', dbname_len) != 0 || memchr(dbname, '$', dbname_len) != 0 ) { - zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "MongoDB::__construct(): invalid name %s", dbname); + zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Database name contains invalid characters: %s", dbname); return 0; } + return 1; } diff --git a/tests/generic/database-valid-name.phpt b/tests/generic/database-valid-name.phpt index b60bddb1b..9a9b699ba 100644 --- a/tests/generic/database-valid-name.phpt +++ b/tests/generic/database-valid-name.phpt @@ -16,7 +16,7 @@ foreach ($names as $name) { } ?> --EXPECT-- -\: MongoDB::__construct(): invalid name \ -$: MongoDB::__construct(): invalid name $ -/: MongoDB::__construct(): invalid name / -foo.bar: MongoDB::__construct(): invalid name foo.bar +\: Database name contains invalid characters: \ +$: Database name contains invalid characters: $ +/: Database name contains invalid characters: / +foo.bar: Database name contains invalid characters: foo.bar diff --git a/tests/generic/mongoclient-selectcollection-001.phpt b/tests/generic/mongoclient-selectcollection-001.phpt new file mode 100644 index 000000000..4c2e097f7 --- /dev/null +++ b/tests/generic/mongoclient-selectcollection-001.phpt @@ -0,0 +1,19 @@ +--TEST-- +Mongo::selectCollection() +--SKIPIF-- + +--FILE-- +selectCollection('db', 'collection'); + +echo get_class($collection), "\n"; +echo (string) $collection, "\n"; +?> +--EXPECT-- +MongoCollection +db.collection diff --git a/tests/generic/mongoclient-selectcollection_error-001.phpt b/tests/generic/mongoclient-selectcollection_error-001.phpt new file mode 100644 index 000000000..3ee03462c --- /dev/null +++ b/tests/generic/mongoclient-selectcollection_error-001.phpt @@ -0,0 +1,76 @@ +--TEST-- +MongoClient::selectCollection() with invalid database name +--SKIPIF-- + +--FILE-- +selectCollection(); + +echo "\nTesting database name with invalid types\n"; + +$mc->selectCollection(array(), 'collection'); +$mc->selectCollection(new stdClass, 'collection'); + +echo "\nTesting empty database name\n"; + +try { + $mc->selectCollection('', 'collection'); +} catch (Exception $e) { + printf("exception class: %s\n", get_class($e)); + printf("exception message: %s\n", $e->getMessage()); + printf("exception code: %d\n", $e->getCode()); +} + +echo "\nTesting database name with null bytes\n"; + +try { + $mc->selectCollection("foo\0bar", 'collection'); +} catch (Exception $e) { + printf("exception class: %s\n", get_class($e)); + printf("exception message: %s\n", $e->getMessage()); + printf("exception code: %d\n", $e->getCode()); +} + +echo "\nTesting database name with invalid characters\n"; + +try { + $mc->selectCollection("foo.bar", 'collection'); +} catch (Exception $e) { + printf("exception class: %s\n", get_class($e)); + printf("exception message: %s\n", $e->getMessage()); + printf("exception code: %d\n", $e->getCode()); +} + +?> +--EXPECTF-- +Testing missing database name parameter + +Warning: MongoClient::selectCollection() expects exactly 2 parameters, 0 given in %s on line %d + +Testing database name with invalid types + +Warning: MongoClient::selectCollection() expects parameter 1 to be string, array given in %s on line %d + +Warning: MongoClient::selectCollection() expects parameter 1 to be string, object given in %s on line %d + +Testing empty database name +exception class: MongoException +exception message: Database name cannot be empty +exception code: 2 + +Testing database name with null bytes +exception class: MongoException +exception message: Database name cannot contain null bytes: foo\0... +exception code: 2 + +Testing database name with invalid characters +exception class: MongoException +exception message: Database name contains invalid characters: foo.bar +exception code: 2 diff --git a/tests/generic/mongoclient-selectcollection_error-002.phpt b/tests/generic/mongoclient-selectcollection_error-002.phpt new file mode 100644 index 000000000..ce50373f5 --- /dev/null +++ b/tests/generic/mongoclient-selectcollection_error-002.phpt @@ -0,0 +1,61 @@ +--TEST-- +MongoClient::selectCollection() with invalid collection name +--SKIPIF-- + +--FILE-- +selectCollection('db'); + +echo "\nTesting collection name with invalid types\n"; + +$mc->selectCollection('db', array()); +$mc->selectCollection('db', new stdClass); + +echo "\nTesting empty collection name\n"; + +try { + $mc->selectCollection('db', ''); +} catch (Exception $e) { + printf("exception class: %s\n", get_class($e)); + printf("exception message: %s\n", $e->getMessage()); + printf("exception code: %d\n", $e->getCode()); +} + +echo "\nTesting collection name with null bytes\n"; + +try { + $mc->selectCollection('db', "foo\0bar"); +} catch (Exception $e) { + printf("exception class: %s\n", get_class($e)); + printf("exception message: %s\n", $e->getMessage()); + printf("exception code: %d\n", $e->getCode()); +} + +?> +--EXPECTF-- +Testing missing collection name parameter + +Warning: MongoClient::selectCollection() expects exactly 2 parameters, 1 given in %s on line %d + +Testing collection name with invalid types + +Warning: MongoClient::selectCollection() expects parameter 2 to be string, array given in %s on line %d + +Warning: MongoClient::selectCollection() expects parameter 2 to be string, object given in %s on line %d + +Testing empty collection name +exception class: MongoException +exception message: Collection name cannot be empty +exception code: 2 + +Testing collection name with null bytes +exception class: MongoException +exception message: Collection name cannot contain null bytes: foo\0... +exception code: 2 diff --git a/tests/generic/mongoclient-selectcollection_error.phpt b/tests/generic/mongoclient-selectcollection_error.phpt deleted file mode 100644 index 3d1ff1911..000000000 --- a/tests/generic/mongoclient-selectcollection_error.phpt +++ /dev/null @@ -1,24 +0,0 @@ ---TEST-- -MongoClient::selectCollection ---SKIPIF-- - -=')) echo "skip >= PHP 5.5 needed\n"; ?> ---INI-- -mongo.long_as_object=0 ---FILE-- -selectCollection('', ''); -} catch (MongoException $e) { - echo $e->getMessage(), "\n"; - echo $e->getCode(), "\n"; -} -?> ---EXPECT-- -MongoDB::__construct(): invalid name -2 diff --git a/tests/generic/mongoclient-selectdb-001.phpt b/tests/generic/mongoclient-selectdb-001.phpt new file mode 100644 index 000000000..a207d7d34 --- /dev/null +++ b/tests/generic/mongoclient-selectdb-001.phpt @@ -0,0 +1,19 @@ +--TEST-- +Mongo::selectDB() +--SKIPIF-- + +--FILE-- +selectDB('db'); + +echo get_class($db), "\n"; +echo (string) $db, "\n"; +?> +--EXPECT-- +MongoDB +db diff --git a/tests/generic/mongoclient-selectdb.phpt b/tests/generic/mongoclient-selectdb.phpt deleted file mode 100644 index 0fcc587fa..000000000 --- a/tests/generic/mongoclient-selectdb.phpt +++ /dev/null @@ -1,13 +0,0 @@ ---TEST-- -Mongo::selectDB ---SKIPIF-- - ---FILE-- -selectDB('test'); -echo is_object($db) ? '1' :'0' ; -?> ---EXPECT-- -1 diff --git a/tests/generic/mongoclient-selectdb_error-001.phpt b/tests/generic/mongoclient-selectdb_error-001.phpt new file mode 100644 index 000000000..974d7832e --- /dev/null +++ b/tests/generic/mongoclient-selectdb_error-001.phpt @@ -0,0 +1,76 @@ +--TEST-- +MongoClient::selectDB() with invalid database name +--SKIPIF-- + +--FILE-- +selectDB(); + +echo "\nTesting database name with invalid types\n"; + +$mc->selectDB(array()); +$mc->selectDB(new stdClass); + +echo "\nTesting empty database name\n"; + +try { + $mc->selectDB(''); +} catch (Exception $e) { + printf("exception class: %s\n", get_class($e)); + printf("exception message: %s\n", $e->getMessage()); + printf("exception code: %d\n", $e->getCode()); +} + +echo "\nTesting database name with null bytes\n"; + +try { + $mc->selectDB("foo\0bar"); +} catch (Exception $e) { + printf("exception class: %s\n", get_class($e)); + printf("exception message: %s\n", $e->getMessage()); + printf("exception code: %d\n", $e->getCode()); +} + +echo "\nTesting database name with invalid characters\n"; + +try { + $mc->selectDB("foo.bar"); +} catch (Exception $e) { + printf("exception class: %s\n", get_class($e)); + printf("exception message: %s\n", $e->getMessage()); + printf("exception code: %d\n", $e->getCode()); +} + +?> +--EXPECTF-- +Testing missing database name parameter + +Warning: MongoClient::selectDB() expects exactly 1 parameter, 0 given in %s on line %d + +Testing database name with invalid types + +Warning: MongoClient::selectDB() expects parameter 1 to be string, array given in %s on line %d + +Warning: MongoClient::selectDB() expects parameter 1 to be string, object given in %s on line %d + +Testing empty database name +exception class: MongoException +exception message: Database name cannot be empty +exception code: 2 + +Testing database name with null bytes +exception class: MongoException +exception message: Database name cannot contain null bytes: foo\0... +exception code: 2 + +Testing database name with invalid characters +exception class: MongoException +exception message: Database name contains invalid characters: foo.bar +exception code: 2 diff --git a/tests/generic/mongoclient-selectdb_error.phpt b/tests/generic/mongoclient-selectdb_error.phpt deleted file mode 100644 index 58d6e5eaf..000000000 --- a/tests/generic/mongoclient-selectdb_error.phpt +++ /dev/null @@ -1,34 +0,0 @@ ---TEST-- -MongoClient::selectDB ---SKIPIF-- - ---FILE-- -selectDB(); -echo is_object($db) ? '1' : '0', "\n"; - -$db = $m->selectDB(array('test')); -echo is_object($db) ? '1' : '0', "\n"; - -try { - $db = $m->selectDB(NULL); -} catch(Exception $e) { - echo $e->getMessage() . ".\n"; -} - -$db = $m->selectDB(new stdClass); -echo is_object($db) ? '1' : '0', "\n"; -?> ---EXPECTF-- -Warning: MongoClient::selectDB() expects exactly 1 parameter, 0 given in %smongoclient-selectdb_error.php on line %d -0 - -Warning: MongoClient::selectDB() expects parameter 1 to be string, array given in %smongoclient-selectdb_error.php on line %d -0 -MongoDB::__construct(): invalid name . - -Warning: MongoClient::selectDB() expects parameter 1 to be string, object given in %smongoclient-selectdb_error.php on line %d -0 - diff --git a/tests/standalone/bug00872-002.phpt b/tests/standalone/bug00872-002.phpt index 63a008012..9697f9696 100644 --- a/tests/standalone/bug00872-002.phpt +++ b/tests/standalone/bug00872-002.phpt @@ -32,10 +32,10 @@ try { var_dump($e->getMessage()); } ?> ---EXPECTF-- +--EXPECT-- int(2) -string(39) "MongoDB::__construct(): invalid name fo" +string(50) "Collection name cannot contain null bytes: fo\0..." int(2) -string(39) "MongoDB::__construct(): invalid name fo" +string(50) "Collection name cannot contain null bytes: fo\0..." int(2) -string(39) "MongoDB::__construct(): invalid name fo" +string(50) "Collection name cannot contain null bytes: fo\0..." diff --git a/tests/standalone/bug00872-003.phpt b/tests/standalone/bug00872-003.phpt index 736f6c7f6..df9d30c8b 100644 --- a/tests/standalone/bug00872-003.phpt +++ b/tests/standalone/bug00872-003.phpt @@ -9,7 +9,7 @@ require_once "tests/utils/server.inc"; $host = MongoShellServer::getStandaloneInfo(); $m = new MongoClient($host); -$faulty = 'f0o' . chr(0) . 'o'; +$faulty = 'fo' . chr(0) . 'o'; try { $d = $m->$faulty; @@ -18,8 +18,6 @@ try { var_dump($e->getMessage()); } -$faulty = 'f1o' . chr(0) . 'o'; - try { $c = $m->selectDb($faulty); } catch( MongoException $e ) { @@ -27,8 +25,8 @@ try { var_dump($e->getMessage()); } ?> ---EXPECTF-- +--EXPECT-- int(2) -string(68) "MongoDB::__construct(): '\0' not allowed in database names: f0o\0..." +string(48) "Database name cannot contain null bytes: fo\0..." int(2) -string(68) "MongoDB::__construct(): '\0' not allowed in database names: f1o\0..." +string(48) "Database name cannot contain null bytes: fo\0..."