Skip to content

Commit d190398

Browse files
committed
PHPC-990: Strict type validation for boolean URI options
1 parent 9a2a162 commit d190398

File tree

2 files changed

+229
-11
lines changed

2 files changed

+229
-11
lines changed

src/phongo_client.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,12 @@ 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
}

tests/manager/manager-ctor_error-003.phpt

Lines changed: 223 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,47 @@ MongoDB\Driver\Manager::__construct(): invalid types in URI options arrays
55

66
require_once __DIR__ . '/../utils/basic.inc';
77

8-
/* Note: generic boolean options (e.g. "ssl") are not tested because the driver
9-
* uses bson_iter_as_bool() to cast the value to a boolean for assignment.
10-
*
11-
* Read concern, read preference, and write concern options are tested in their
12-
* respective test files. */
8+
/* Note: read concern, read preference, and write concern options are tested in
9+
* their respective test files. */
1310

14-
echo "Testing 32-bit integer options:\n";
11+
echo "Testing boolean options:\n";
12+
13+
$booleanOptions = [
14+
'canonicalizeHostname',
15+
'directConnection',
16+
'journal',
17+
'loadBalanced',
18+
'retryReads',
19+
'retryWrites',
20+
'safe',
21+
'serverSelectionTryOnce',
22+
'ssl',
23+
'tls',
24+
'tlsAllowInvalidCertificates',
25+
'tlsAllowInvalidHostnames',
26+
'tlsDisableCertificateRevocationCheck',
27+
'tlsDisableOCSPEndpointCheck',
28+
'tlsInsecure',
29+
];
30+
31+
$invalidBooleanValues = [
32+
1.0,
33+
42,
34+
'string',
35+
new MongoDB\BSON\ObjectId,
36+
[ 1, 2, 3 ],
37+
['x' => 1],
38+
];
39+
40+
foreach ($booleanOptions as $option) {
41+
foreach ($invalidBooleanValues as $value) {
42+
echo throws(function() use ($option, $value) {
43+
create_test_manager(null, [$option => $value]);
44+
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";
45+
}
46+
}
47+
48+
echo "\nTesting 32-bit integer options:\n";
1549

1650
$integerOptions = [
1751
'connectTimeoutMS',
@@ -82,13 +116,195 @@ $invalidDocumentValues = [
82116
foreach ($invalidDocumentValues as $value) {
83117
echo throws(function() use ($value) {
84118
create_test_manager(null, ['authMechanismProperties' => $value]);
85-
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
119+
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";
86120
}
87121

88122
?>
89123
===DONE===
90124
<?php exit(0); ?>
91125
--EXPECT--
126+
Testing boolean options:
127+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
128+
Expected boolean for "canonicalizeHostname" URI option, double given
129+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
130+
Expected boolean for "canonicalizeHostname" URI option, 32-bit integer given
131+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
132+
Expected boolean for "canonicalizeHostname" URI option, string given
133+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
134+
Expected boolean for "canonicalizeHostname" URI option, ObjectId given
135+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
136+
Expected boolean for "canonicalizeHostname" URI option, array given
137+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
138+
Expected boolean for "canonicalizeHostname" URI option, document given
139+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
140+
Expected boolean for "directConnection" URI option, double given
141+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
142+
Expected boolean for "directConnection" URI option, 32-bit integer given
143+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
144+
Expected boolean for "directConnection" URI option, string given
145+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
146+
Expected boolean for "directConnection" URI option, ObjectId given
147+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
148+
Expected boolean for "directConnection" URI option, array given
149+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
150+
Expected boolean for "directConnection" URI option, document given
151+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
152+
Expected boolean for "journal" URI option, double given
153+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
154+
Expected boolean for "journal" URI option, 32-bit integer given
155+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
156+
Expected boolean for "journal" URI option, string given
157+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
158+
Expected boolean for "journal" URI option, ObjectId given
159+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
160+
Expected boolean for "journal" URI option, array given
161+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
162+
Expected boolean for "journal" URI option, document given
163+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
164+
Expected boolean for "loadBalanced" URI option, double given
165+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
166+
Expected boolean for "loadBalanced" URI option, 32-bit integer given
167+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
168+
Expected boolean for "loadBalanced" URI option, string given
169+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
170+
Expected boolean for "loadBalanced" URI option, ObjectId given
171+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
172+
Expected boolean for "loadBalanced" URI option, array given
173+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
174+
Expected boolean for "loadBalanced" URI option, document given
175+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
176+
Expected boolean for "retryReads" URI option, double given
177+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
178+
Expected boolean for "retryReads" URI option, 32-bit integer given
179+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
180+
Expected boolean for "retryReads" URI option, string given
181+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
182+
Expected boolean for "retryReads" URI option, ObjectId given
183+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
184+
Expected boolean for "retryReads" URI option, array given
185+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
186+
Expected boolean for "retryReads" URI option, document given
187+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
188+
Expected boolean for "retryWrites" URI option, double given
189+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
190+
Expected boolean for "retryWrites" URI option, 32-bit integer given
191+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
192+
Expected boolean for "retryWrites" URI option, string given
193+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
194+
Expected boolean for "retryWrites" URI option, ObjectId given
195+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
196+
Expected boolean for "retryWrites" URI option, array given
197+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
198+
Expected boolean for "retryWrites" URI option, document given
199+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
200+
Expected boolean for "safe" URI option, double given
201+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
202+
Expected boolean for "safe" URI option, 32-bit integer given
203+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
204+
Expected boolean for "safe" URI option, string given
205+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
206+
Expected boolean for "safe" URI option, ObjectId given
207+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
208+
Expected boolean for "safe" URI option, array given
209+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
210+
Expected boolean for "safe" URI option, document given
211+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
212+
Expected boolean for "serverSelectionTryOnce" URI option, double given
213+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
214+
Expected boolean for "serverSelectionTryOnce" URI option, 32-bit integer given
215+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
216+
Expected boolean for "serverSelectionTryOnce" URI option, string given
217+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
218+
Expected boolean for "serverSelectionTryOnce" URI option, ObjectId given
219+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
220+
Expected boolean for "serverSelectionTryOnce" URI option, array given
221+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
222+
Expected boolean for "serverSelectionTryOnce" URI option, document given
223+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
224+
Expected boolean for "ssl" URI option, double given
225+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
226+
Expected boolean for "ssl" URI option, 32-bit integer given
227+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
228+
Expected boolean for "ssl" URI option, string given
229+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
230+
Expected boolean for "ssl" URI option, ObjectId given
231+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
232+
Expected boolean for "ssl" URI option, array given
233+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
234+
Expected boolean for "ssl" URI option, document given
235+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
236+
Expected boolean for "tls" URI option, double given
237+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
238+
Expected boolean for "tls" URI option, 32-bit integer given
239+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
240+
Expected boolean for "tls" URI option, string given
241+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
242+
Expected boolean for "tls" URI option, ObjectId given
243+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
244+
Expected boolean for "tls" URI option, array given
245+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
246+
Expected boolean for "tls" URI option, document given
247+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
248+
Expected boolean for "tlsAllowInvalidCertificates" URI option, double given
249+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
250+
Expected boolean for "tlsAllowInvalidCertificates" URI option, 32-bit integer given
251+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
252+
Expected boolean for "tlsAllowInvalidCertificates" URI option, string given
253+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
254+
Expected boolean for "tlsAllowInvalidCertificates" URI option, ObjectId given
255+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
256+
Expected boolean for "tlsAllowInvalidCertificates" URI option, array given
257+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
258+
Expected boolean for "tlsAllowInvalidCertificates" URI option, document given
259+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
260+
Expected boolean for "tlsAllowInvalidHostnames" URI option, double given
261+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
262+
Expected boolean for "tlsAllowInvalidHostnames" URI option, 32-bit integer given
263+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
264+
Expected boolean for "tlsAllowInvalidHostnames" URI option, string given
265+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
266+
Expected boolean for "tlsAllowInvalidHostnames" URI option, ObjectId given
267+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
268+
Expected boolean for "tlsAllowInvalidHostnames" URI option, array given
269+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
270+
Expected boolean for "tlsAllowInvalidHostnames" URI option, document given
271+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
272+
Expected boolean for "tlsDisableCertificateRevocationCheck" URI option, double given
273+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
274+
Expected boolean for "tlsDisableCertificateRevocationCheck" URI option, 32-bit integer given
275+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
276+
Expected boolean for "tlsDisableCertificateRevocationCheck" URI option, string given
277+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
278+
Expected boolean for "tlsDisableCertificateRevocationCheck" URI option, ObjectId given
279+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
280+
Expected boolean for "tlsDisableCertificateRevocationCheck" URI option, array given
281+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
282+
Expected boolean for "tlsDisableCertificateRevocationCheck" URI option, document given
283+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
284+
Expected boolean for "tlsDisableOCSPEndpointCheck" URI option, double given
285+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
286+
Expected boolean for "tlsDisableOCSPEndpointCheck" URI option, 32-bit integer given
287+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
288+
Expected boolean for "tlsDisableOCSPEndpointCheck" URI option, string given
289+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
290+
Expected boolean for "tlsDisableOCSPEndpointCheck" URI option, ObjectId given
291+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
292+
Expected boolean for "tlsDisableOCSPEndpointCheck" URI option, array given
293+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
294+
Expected boolean for "tlsDisableOCSPEndpointCheck" URI option, document given
295+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
296+
Expected boolean for "tlsInsecure" URI option, double given
297+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
298+
Expected boolean for "tlsInsecure" URI option, 32-bit integer given
299+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
300+
Expected boolean for "tlsInsecure" URI option, string given
301+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
302+
Expected boolean for "tlsInsecure" URI option, ObjectId given
303+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
304+
Expected boolean for "tlsInsecure" URI option, array given
305+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
306+
Expected boolean for "tlsInsecure" URI option, document given
307+
92308
Testing 32-bit integer options:
93309
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
94310
Expected 32-bit integer for "connectTimeoutMS" URI option, boolean given

0 commit comments

Comments
 (0)