Skip to content

Commit 943a00e

Browse files
committed
PHPC-794: BulkWrite::update() should throw for invalid replace ops
1 parent 1fc30d4 commit 943a00e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/MongoDB/BulkWrite.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,16 @@ PHP_METHOD(BulkWrite, update)
242242
goto cleanup;
243243
}
244244
} else {
245+
if (!bson_validate(bupdate, BSON_VALIDATE_DOT_KEYS|BSON_VALIDATE_DOLLAR_KEYS, NULL)) {
246+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Replacement document may not contain \"$\" or \".\" in keys");
247+
goto cleanup;
248+
}
249+
250+
if (zoptions && php_array_existsc(zoptions, "multi") && php_array_fetchc_bool(zoptions, "multi")) {
251+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Replacement document conflicts with true \"multi\" option");
252+
goto cleanup;
253+
}
254+
245255
if (!mongoc_bulk_operation_replace_one_with_opts(intern->bulk, bquery, bupdate, boptions, &error)) {
246256
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
247257
goto cleanup;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
MongoDB\Driver\BulkWrite::update() with invalid options
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
$bulk = new MongoDB\Driver\BulkWrite;
9+
10+
echo throws(function() use ($bulk) {
11+
$bulk->update(['x' => 1], ['x.y' => 1]);
12+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n\n";
13+
14+
echo throws(function() use ($bulk) {
15+
$bulk->update(['x' => 1], ['y' => 1], ['multi' => true]);
16+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
17+
18+
?>
19+
===DONE===
20+
<?php exit(0); ?>
21+
--EXPECT--
22+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
23+
Replacement document may not contain "$" or "." in keys
24+
25+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
26+
Replacement document conflicts with true "multi" option
27+
===DONE===

0 commit comments

Comments
 (0)