Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit 0fdb054

Browse files
committed
Merge pull request #756
2 parents 6497509 + 14ceec2 commit 0fdb054

13 files changed

+279
-93
lines changed

collection.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ void php_mongo_collection_construct(zval *this, zval *parent, char *name_str, in
7474
char *ns;
7575

7676
/* check for empty and invalid collection names */
77-
if (
78-
name_len == 0 ||
79-
memchr(name_str, '\0', name_len) != 0
80-
) {
81-
zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "MongoDB::__construct(): invalid name %s", name_str);
77+
if (name_len == 0) {
78+
zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Collection name cannot be empty");
79+
return;
80+
}
81+
82+
if (memchr(name_str, '\0', name_len) != 0) {
83+
zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Collection name cannot contain null bytes: %s\\0...", name_str);
8284
return;
8385
}
8486

db.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,24 @@ static int php_mongo_command_supports_rp(zval *cmd)
112112

113113
int php_mongo_db_is_valid_dbname(char *dbname, int dbname_len TSRMLS_DC)
114114
{
115+
if (dbname_len == 0) {
116+
zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Database name cannot be empty");
117+
return 0;
118+
}
119+
115120
if (memchr(dbname, '\0', dbname_len) != NULL) {
116-
zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "MongoDB::__construct(): '\\0' not allowed in database names: %s\\0...", dbname);
121+
zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Database name cannot contain null bytes: %s\\0...", dbname);
122+
return 0;
117123
}
118124

119125
if (
120-
dbname_len == 0 ||
121126
memchr(dbname, ' ', dbname_len) != 0 || memchr(dbname, '.', dbname_len) != 0 || memchr(dbname, '\\', dbname_len) != 0 ||
122127
memchr(dbname, '/', dbname_len) != 0 || memchr(dbname, '$', dbname_len) != 0
123128
) {
124-
zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "MongoDB::__construct(): invalid name %s", dbname);
129+
zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Database name contains invalid characters: %s", dbname);
125130
return 0;
126131
}
132+
127133
return 1;
128134
}
129135

tests/generic/database-valid-name.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ foreach ($names as $name) {
1616
}
1717
?>
1818
--EXPECT--
19-
\: MongoDB::__construct(): invalid name \
20-
$: MongoDB::__construct(): invalid name $
21-
/: MongoDB::__construct(): invalid name /
22-
foo.bar: MongoDB::__construct(): invalid name foo.bar
19+
\: Database name contains invalid characters: \
20+
$: Database name contains invalid characters: $
21+
/: Database name contains invalid characters: /
22+
foo.bar: Database name contains invalid characters: foo.bar
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Mongo::selectCollection()
3+
--SKIPIF--
4+
<?php require_once "tests/utils/standalone.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/server.inc";
8+
9+
$host = MongoShellServer::getStandaloneInfo();
10+
$mc = new MongoClient($host);
11+
12+
$collection = $mc->selectCollection('db', 'collection');
13+
14+
echo get_class($collection), "\n";
15+
echo (string) $collection, "\n";
16+
?>
17+
--EXPECT--
18+
MongoCollection
19+
db.collection
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
--TEST--
2+
MongoClient::selectCollection() with invalid database name
3+
--SKIPIF--
4+
<?php require_once "tests/utils/standalone.inc" ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/server.inc";
8+
9+
$host = MongoShellServer::getStandaloneInfo();
10+
$mc = new MongoClient($host);
11+
12+
echo "Testing missing database name parameter\n";
13+
14+
$mc->selectCollection();
15+
16+
echo "\nTesting database name with invalid types\n";
17+
18+
$mc->selectCollection(array(), 'collection');
19+
$mc->selectCollection(new stdClass, 'collection');
20+
21+
echo "\nTesting empty database name\n";
22+
23+
try {
24+
$mc->selectCollection('', 'collection');
25+
} catch (Exception $e) {
26+
printf("exception class: %s\n", get_class($e));
27+
printf("exception message: %s\n", $e->getMessage());
28+
printf("exception code: %d\n", $e->getCode());
29+
}
30+
31+
echo "\nTesting database name with null bytes\n";
32+
33+
try {
34+
$mc->selectCollection("foo\0bar", 'collection');
35+
} catch (Exception $e) {
36+
printf("exception class: %s\n", get_class($e));
37+
printf("exception message: %s\n", $e->getMessage());
38+
printf("exception code: %d\n", $e->getCode());
39+
}
40+
41+
echo "\nTesting database name with invalid characters\n";
42+
43+
try {
44+
$mc->selectCollection("foo.bar", 'collection');
45+
} catch (Exception $e) {
46+
printf("exception class: %s\n", get_class($e));
47+
printf("exception message: %s\n", $e->getMessage());
48+
printf("exception code: %d\n", $e->getCode());
49+
}
50+
51+
?>
52+
--EXPECTF--
53+
Testing missing database name parameter
54+
55+
Warning: MongoClient::selectCollection() expects exactly 2 parameters, 0 given in %s on line %d
56+
57+
Testing database name with invalid types
58+
59+
Warning: MongoClient::selectCollection() expects parameter 1 to be string, array given in %s on line %d
60+
61+
Warning: MongoClient::selectCollection() expects parameter 1 to be string, object given in %s on line %d
62+
63+
Testing empty database name
64+
exception class: MongoException
65+
exception message: Database name cannot be empty
66+
exception code: 2
67+
68+
Testing database name with null bytes
69+
exception class: MongoException
70+
exception message: Database name cannot contain null bytes: foo\0...
71+
exception code: 2
72+
73+
Testing database name with invalid characters
74+
exception class: MongoException
75+
exception message: Database name contains invalid characters: foo.bar
76+
exception code: 2
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
--TEST--
2+
MongoClient::selectCollection() with invalid collection name
3+
--SKIPIF--
4+
<?php require_once "tests/utils/standalone.inc" ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/server.inc";
8+
9+
$host = MongoShellServer::getStandaloneInfo();
10+
$mc = new MongoClient($host);
11+
12+
echo "Testing missing collection name parameter\n";
13+
14+
$mc->selectCollection('db');
15+
16+
echo "\nTesting collection name with invalid types\n";
17+
18+
$mc->selectCollection('db', array());
19+
$mc->selectCollection('db', new stdClass);
20+
21+
echo "\nTesting empty collection name\n";
22+
23+
try {
24+
$mc->selectCollection('db', '');
25+
} catch (Exception $e) {
26+
printf("exception class: %s\n", get_class($e));
27+
printf("exception message: %s\n", $e->getMessage());
28+
printf("exception code: %d\n", $e->getCode());
29+
}
30+
31+
echo "\nTesting collection name with null bytes\n";
32+
33+
try {
34+
$mc->selectCollection('db', "foo\0bar");
35+
} catch (Exception $e) {
36+
printf("exception class: %s\n", get_class($e));
37+
printf("exception message: %s\n", $e->getMessage());
38+
printf("exception code: %d\n", $e->getCode());
39+
}
40+
41+
?>
42+
--EXPECTF--
43+
Testing missing collection name parameter
44+
45+
Warning: MongoClient::selectCollection() expects exactly 2 parameters, 1 given in %s on line %d
46+
47+
Testing collection name with invalid types
48+
49+
Warning: MongoClient::selectCollection() expects parameter 2 to be string, array given in %s on line %d
50+
51+
Warning: MongoClient::selectCollection() expects parameter 2 to be string, object given in %s on line %d
52+
53+
Testing empty collection name
54+
exception class: MongoException
55+
exception message: Collection name cannot be empty
56+
exception code: 2
57+
58+
Testing collection name with null bytes
59+
exception class: MongoException
60+
exception message: Collection name cannot contain null bytes: foo\0...
61+
exception code: 2

tests/generic/mongoclient-selectcollection_error.phpt

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Mongo::selectDB()
3+
--SKIPIF--
4+
<?php require_once "tests/utils/standalone.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/server.inc";
8+
9+
$host = MongoShellServer::getStandaloneInfo();
10+
$mc = new MongoClient($host);
11+
12+
$db = $mc->selectDB('db');
13+
14+
echo get_class($db), "\n";
15+
echo (string) $db, "\n";
16+
?>
17+
--EXPECT--
18+
MongoDB
19+
db

tests/generic/mongoclient-selectdb.phpt

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
--TEST--
2+
MongoClient::selectDB() with invalid database name
3+
--SKIPIF--
4+
<?php require_once "tests/utils/standalone.inc" ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/server.inc";
8+
9+
$host = MongoShellServer::getStandaloneInfo();
10+
$mc = new MongoClient($host);
11+
12+
echo "Testing missing database name parameter\n";
13+
14+
$mc->selectDB();
15+
16+
echo "\nTesting database name with invalid types\n";
17+
18+
$mc->selectDB(array());
19+
$mc->selectDB(new stdClass);
20+
21+
echo "\nTesting empty database name\n";
22+
23+
try {
24+
$mc->selectDB('');
25+
} catch (Exception $e) {
26+
printf("exception class: %s\n", get_class($e));
27+
printf("exception message: %s\n", $e->getMessage());
28+
printf("exception code: %d\n", $e->getCode());
29+
}
30+
31+
echo "\nTesting database name with null bytes\n";
32+
33+
try {
34+
$mc->selectDB("foo\0bar");
35+
} catch (Exception $e) {
36+
printf("exception class: %s\n", get_class($e));
37+
printf("exception message: %s\n", $e->getMessage());
38+
printf("exception code: %d\n", $e->getCode());
39+
}
40+
41+
echo "\nTesting database name with invalid characters\n";
42+
43+
try {
44+
$mc->selectDB("foo.bar");
45+
} catch (Exception $e) {
46+
printf("exception class: %s\n", get_class($e));
47+
printf("exception message: %s\n", $e->getMessage());
48+
printf("exception code: %d\n", $e->getCode());
49+
}
50+
51+
?>
52+
--EXPECTF--
53+
Testing missing database name parameter
54+
55+
Warning: MongoClient::selectDB() expects exactly 1 parameter, 0 given in %s on line %d
56+
57+
Testing database name with invalid types
58+
59+
Warning: MongoClient::selectDB() expects parameter 1 to be string, array given in %s on line %d
60+
61+
Warning: MongoClient::selectDB() expects parameter 1 to be string, object given in %s on line %d
62+
63+
Testing empty database name
64+
exception class: MongoException
65+
exception message: Database name cannot be empty
66+
exception code: 2
67+
68+
Testing database name with null bytes
69+
exception class: MongoException
70+
exception message: Database name cannot contain null bytes: foo\0...
71+
exception code: 2
72+
73+
Testing database name with invalid characters
74+
exception class: MongoException
75+
exception message: Database name contains invalid characters: foo.bar
76+
exception code: 2

0 commit comments

Comments
 (0)