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

Commit 22a0ee9

Browse files
committed
PHP-1431: Fix database name validation to include '$external', to allow execution of user admin commands
1 parent 722aa73 commit 22a0ee9

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

db.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,22 @@ int php_mongo_db_is_valid_dbname(char *dbname, int dbname_len TSRMLS_DC)
131131

132132
if (
133133
memchr(dbname, ' ', dbname_len) != 0 || memchr(dbname, '.', dbname_len) != 0 || memchr(dbname, '\\', dbname_len) != 0 ||
134-
memchr(dbname, '/', dbname_len) != 0 || memchr(dbname, '$', dbname_len) != 0
134+
memchr(dbname, '/', dbname_len) != 0
135135
) {
136136
zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Database name contains invalid characters: %s", dbname);
137137
return 0;
138138
}
139139

140+
/* We allow the special case "$external" as database name (PHP-1431) */
141+
if (strcmp("$external", dbname) == 0) {
142+
return 1;
143+
}
144+
145+
if (memchr(dbname, '$', dbname_len) != 0) {
146+
zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Database name contains invalid character '$': %s", dbname);
147+
return 0;
148+
}
149+
140150
return 1;
141151
}
142152

tests/generic/database-valid-name.phpt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ Database: valid name checks
66
<?php
77
require_once "tests/utils/server.inc";
88
$a = mongo_standalone();
9-
$names = array("\\", "\$", "/", "foo.bar");
9+
$names = array("\\", "\$", "/", "foo.bar", '$external', 'run$fores');
1010
foreach ($names as $name) {
1111
try {
1212
$d = new MongoDB($a, $name);
13+
echo $name, ": OK\n";
1314
} catch (Exception $e) {
1415
echo $name, ": ", $e->getMessage(), "\n";
1516
}
1617
}
1718
?>
1819
--EXPECT--
1920
\: Database name contains invalid characters: \
20-
$: Database name contains invalid characters: $
21+
$: Database name contains invalid character '$': $
2122
/: Database name contains invalid characters: /
2223
foo.bar: Database name contains invalid characters: foo.bar
24+
$external: OK
25+
run$fores: Database name contains invalid character '$': run$fores

0 commit comments

Comments
 (0)