Skip to content

Commit aaf1660

Browse files
authored
PHPC-990: Strict type validation for boolean URI options (#1713)
* Note that mongoc_uri_option_is_int32 includes int64 options * Test invalid types for more int32 and string URI options
1 parent 9a2a162 commit aaf1660

File tree

3 files changed

+329
-13
lines changed

3 files changed

+329
-13
lines changed

UPGRADE-2.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ UPGRADE FROM 1.x to 2.0
3838
`allowPartialResults` instead), `maxScan`, `modifiers` (use alternative
3939
top-level options instead), `oplogReplay`, and `snapshot`. Support for
4040
negative `limit` values has been removed (use `singleBatch` instead).
41+
* The `MongoDB\Driver\Manager` constructor now throws if the URI options array
42+
includes a non-boolean value for an option expecting a boolean. This behavior
43+
is now consistent with validation for other option types.

src/phongo_client.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,23 @@ static bool php_phongo_apply_options_to_uri(mongoc_uri_t* uri, bson_t* options)
143143
}
144144

145145
if (mongoc_uri_option_is_bool(key)) {
146-
/* The option's type is not validated because bson_iter_as_bool() is
147-
* used to cast the value to a boolean. Validation may be introduced
148-
* in PHPC-990. */
149-
if (!mongoc_uri_set_option_as_bool(uri, key, bson_iter_as_bool(&iter))) {
146+
if (!BSON_ITER_HOLDS_BOOL(&iter)) {
147+
PHONGO_URI_INVALID_TYPE(iter, "boolean");
148+
return false;
149+
}
150+
151+
if (!mongoc_uri_set_option_as_bool(uri, key, bson_iter_bool(&iter))) {
150152
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse \"%s\" URI option", key);
151153
return false;
152154
}
153155

154156
continue;
155157
}
156158

159+
/* Note: mongoc_uri_option_is_int32 also accepts int64 options, but
160+
* BSON_ITER_HOLDS_INT32 would reject a 64-bit value. This is not a
161+
* problem as MONGOC_URI_WTIMEOUTMS is the only 64-bit option and it is
162+
* handled explicitly in php_phongo_apply_wc_options_to_uri. */
157163
if (mongoc_uri_option_is_int32(key)) {
158164
if (!BSON_ITER_HOLDS_INT32(&iter)) {
159165
PHONGO_URI_INVALID_TYPE(iter, "32-bit integer");

0 commit comments

Comments
 (0)