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

PHP-1431: Fix database name validation to include '$external', to allow execution of user admin commands #844

Merged
merged 1 commit into from
Apr 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions db.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ int php_mongo_db_is_valid_dbname(char *dbname, int dbname_len TSRMLS_DC)
return 0;
}

/* We allow the special case "$external" as database name (PHP-1431) */
if (strcmp("$external", dbname) == 0) {
return 1;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more concise to just move this special case above the preceding character checks. That would eliminate the diff to the character check if block and remove second conditional below (also keeping the exception for "$" characters consistent).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had it that way at first, but then decided against it. I can't remember why though... so I've changed it back.


if (
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
Expand Down
5 changes: 4 additions & 1 deletion tests/generic/database-valid-name.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ Database: valid name checks
<?php
require_once "tests/utils/server.inc";
$a = mongo_standalone();
$names = array("\\", "\$", "/", "foo.bar");
$names = array("\\", "\$", "/", "foo.bar", '$external', 'run$fores');
foreach ($names as $name) {
try {
$d = new MongoDB($a, $name);
echo $name, ": OK\n";
} catch (Exception $e) {
echo $name, ": ", $e->getMessage(), "\n";
}
Expand All @@ -20,3 +21,5 @@ foreach ($names as $name) {
$: Database name contains invalid characters: $
/: Database name contains invalid characters: /
foo.bar: Database name contains invalid characters: foo.bar
$external: OK
run$fores: Database name contains invalid characters: run$fores