Skip to content

Commit fc7df86

Browse files
authored
CSHARP-5106: Disallow comma character in authMechanismProperties conn… (#1354)
1 parent 6747c43 commit fc7df86

File tree

15 files changed

+222
-139
lines changed

15 files changed

+222
-139
lines changed

specifications/auth/tests/legacy/connection-string.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@
517517
},
518518
{
519519
"description": "should throw an exception if username is specified for test (MONGODB-OIDC)",
520-
"uri": "mongodb://principalName@localhost/?authMechanism=MONGODB-OIDC&ENVIRONMENT:test",
520+
"uri": "mongodb://principalName@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:test",
521521
"valid": false,
522522
"credential": null
523523
},
@@ -601,7 +601,7 @@
601601
},
602602
{
603603
"description": "should handle a complicated url-encoded TOKEN_RESOURCE (MONGODB-OIDC)",
604-
"uri": "mongodb://user@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:abc%2Cd%25ef%3Ag%26hi",
604+
"uri": "mongodb://user@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:abcd%25ef%3Ag%26hi",
605605
"valid": true,
606606
"credential": {
607607
"username": "user",
@@ -610,7 +610,7 @@
610610
"mechanism": "MONGODB-OIDC",
611611
"mechanism_properties": {
612612
"ENVIRONMENT": "azure",
613-
"TOKEN_RESOURCE": "abc,d%ef:g&hi"
613+
"TOKEN_RESOURCE": "abcd%ef:g&hi"
614614
}
615615
}
616616
},
@@ -669,4 +669,4 @@
669669
"credential": null
670670
}
671671
]
672-
}
672+
}

specifications/auth/tests/legacy/connection-string.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ tests:
375375
valid: false
376376
credential:
377377
- description: should throw an exception if username is specified for test (MONGODB-OIDC)
378-
uri: mongodb://principalName@localhost/?authMechanism=MONGODB-OIDC&ENVIRONMENT:test
378+
uri: mongodb://principalName@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:test
379379
valid: false
380380
credential:
381381
- description: should throw an exception if specified environment is not supported (MONGODB-OIDC)
@@ -435,7 +435,7 @@ tests:
435435
ENVIRONMENT: azure
436436
TOKEN_RESOURCE: 'mongodb://test-cluster'
437437
- description: should handle a complicated url-encoded TOKEN_RESOURCE (MONGODB-OIDC)
438-
uri: mongodb://user@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:abc%2Cd%25ef%3Ag%26hi
438+
uri: mongodb://user@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:abcd%25ef%3Ag%26hi
439439
valid: true
440440
credential:
441441
username: user
@@ -444,7 +444,7 @@ tests:
444444
mechanism: MONGODB-OIDC
445445
mechanism_properties:
446446
ENVIRONMENT: azure
447-
TOKEN_RESOURCE: 'abc,d%ef:g&hi'
447+
TOKEN_RESOURCE: 'abcd%ef:g&hi'
448448
- description: should url-encode a TOKEN_RESOURCE (MONGODB-OIDC)
449449
uri: mongodb://user@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:a$b
450450
valid: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Connection String Tests
2+
3+
The YAML and JSON files in this directory tree are platform-independent tests that drivers can use to prove their
4+
conformance to the Connection String Spec.
5+
6+
As the spec is primarily concerned with parsing the parts of a URI, these tests do not focus on host and option
7+
validation. Where necessary, the tests use options known to be (un)supported by drivers to assert behavior such as
8+
issuing a warning on repeated option keys. As such these YAML tests are in no way a replacement for more thorough
9+
testing. However, they can provide an initial verification of your implementation.
10+
11+
## Version
12+
13+
Files in the "specifications" repository have no version scheme. They are not tied to a MongoDB server version.
14+
15+
## Format
16+
17+
Each YAML file contains an object with a single `tests` key. This key is an array of test case objects, each of which
18+
have the following keys:
19+
20+
- `description`: A string describing the test.
21+
- `uri`: A string containing the URI to be parsed.
22+
- `valid:` A boolean indicating if the URI should be considered valid.
23+
- `warning:` A boolean indicating whether URI parsing should emit a warning (independent of whether or not the URI is
24+
valid).
25+
- `hosts`: An array of host objects, each of which have the following keys:
26+
- `type`: A string denoting the type of host. Possible values are "ipv4", "ip_literal", "hostname", and "unix".
27+
Asserting the type is *optional*.
28+
- `host`: A string containing the parsed host.
29+
- `port`: An integer containing the parsed port number.
30+
- `auth`: An object containing the following keys:
31+
- `username`: A string containing the parsed username. For auth mechanisms that do not utilize a password, this may be
32+
the entire `userinfo` token (as discussed in [RFC 2396](https://www.ietf.org/rfc/rfc2396.txt)).
33+
- `password`: A string containing the parsed password.
34+
- `db`: A string containing the parsed authentication database. For legacy implementations that support namespaces
35+
(databases and collections) this may be the full namespace eg: `<db>.<coll>`
36+
- `options`: An object containing key/value pairs for each parsed query string option.
37+
38+
If a test case includes a null value for one of these keys (e.g. `auth: ~`, `port: ~`), no assertion is necessary. This
39+
both simplifies parsing of the test files (keys should always exist) and allows flexibility for drivers that might
40+
substitute default values *during* parsing (e.g. omitted `port` could be parsed as 27017).
41+
42+
The `valid` and `warning` fields are boolean in order to keep the tests flexible. We are not concerned with asserting
43+
the format of specific error or warnings messages strings.
44+
45+
### Use as unit tests
46+
47+
Testing whether a URI is valid or not should simply be a matter of checking whether URI parsing (or MongoClient
48+
construction) raises an error or exception. Testing for emitted warnings may require more legwork (e.g. configuring a
49+
log handler and watching for output).
50+
51+
Not all drivers may be able to directly assert the hosts, auth credentials, and options. Doing so may require exposing
52+
the driver's URI parsing component.
53+
54+
The file `valid-db-with-dotted-name.yml` is a special case for testing drivers that allow dotted namespaces, instead of
55+
only database names, in the Auth Database portion of the URI.

specifications/connection-string/tests/README.rst

-73
This file was deleted.

specifications/connection-string/tests/invalid-uris.yml

-2
Original file line numberDiff line numberDiff line change
@@ -249,5 +249,3 @@ tests:
249249
hosts: ~
250250
auth: ~
251251
options: ~
252-
253-

specifications/connection-string/tests/valid-options.json

+19
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@
3737
"options": {
3838
"tls": true
3939
}
40+
},
41+
{
42+
"description": "Colon in a key value pair",
43+
"uri": "mongodb://example.com/?authMechanism=MONGODB-OIDC&authMechanismProperties=TOKEN_RESOURCE:mongodb://test-cluster",
44+
"valid": true,
45+
"warning": false,
46+
"hosts": [
47+
{
48+
"type": "hostname",
49+
"host": "example.com",
50+
"port": null
51+
}
52+
],
53+
"auth": null,
54+
"options": {
55+
"authmechanismProperties": {
56+
"TOKEN_RESOURCE": "mongodb://test-cluster"
57+
}
58+
}
4059
}
4160
]
4261
}

specifications/connection-string/tests/valid-options.yml

+14
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,17 @@ tests:
2828
auth: ~
2929
options:
3030
tls: true
31+
-
32+
description: Colon in a key value pair
33+
uri: mongodb://example.com/?authMechanism=MONGODB-OIDC&authMechanismProperties=TOKEN_RESOURCE:mongodb://test-cluster
34+
valid: true
35+
warning: false
36+
hosts:
37+
-
38+
type: hostname
39+
host: example.com
40+
port: ~
41+
auth: ~
42+
options:
43+
authmechanismProperties:
44+
TOKEN_RESOURCE: 'mongodb://test-cluster'

specifications/connection-string/tests/valid-unix_socket-absolute.json

+15
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@
3030
"auth": null,
3131
"options": null
3232
},
33+
{
34+
"description": "Unix domain socket (mixed case)",
35+
"uri": "mongodb://%2Ftmp%2FMongoDB-27017.sock",
36+
"valid": true,
37+
"warning": false,
38+
"hosts": [
39+
{
40+
"type": "unix",
41+
"host": "/tmp/MongoDB-27017.sock",
42+
"port": null
43+
}
44+
],
45+
"auth": null,
46+
"options": null
47+
},
3348
{
3449
"description": "Unix domain socket (absolute path with spaces in path)",
3550
"uri": "mongodb://%2Ftmp%2F %2Fmongodb-27017.sock",

specifications/connection-string/tests/valid-unix_socket-absolute.yml

+12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ tests:
2323
port: ~
2424
auth: ~
2525
options: ~
26+
-
27+
description: "Unix domain socket (mixed case)"
28+
uri: "mongodb://%2Ftmp%2FMongoDB-27017.sock"
29+
valid: true
30+
warning: false
31+
hosts:
32+
-
33+
type: "unix"
34+
host: "/tmp/MongoDB-27017.sock"
35+
port: ~
36+
auth: ~
37+
options: ~
2638
-
2739
description: "Unix domain socket (absolute path with spaces in path)"
2840
uri: "mongodb://%2Ftmp%2F %2Fmongodb-27017.sock"

specifications/connection-string/tests/valid-unix_socket-relative.json

+15
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@
3030
"auth": null,
3131
"options": null
3232
},
33+
{
34+
"description": "Unix domain socket (mixed case)",
35+
"uri": "mongodb://rel%2FMongoDB-27017.sock",
36+
"valid": true,
37+
"warning": false,
38+
"hosts": [
39+
{
40+
"type": "unix",
41+
"host": "rel/MongoDB-27017.sock",
42+
"port": null
43+
}
44+
],
45+
"auth": null,
46+
"options": null
47+
},
3348
{
3449
"description": "Unix domain socket (relative path with spaces)",
3550
"uri": "mongodb://rel%2F %2Fmongodb-27017.sock",

specifications/connection-string/tests/valid-unix_socket-relative.yml

+12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ tests:
2323
port: ~
2424
auth: ~
2525
options: ~
26+
-
27+
description: "Unix domain socket (mixed case)"
28+
uri: "mongodb://rel%2FMongoDB-27017.sock"
29+
valid: true
30+
warning: false
31+
hosts:
32+
-
33+
type: "unix"
34+
host: "rel/MongoDB-27017.sock"
35+
port: ~
36+
auth: ~
37+
options: ~
2638
-
2739
description: "Unix domain socket (relative path with spaces)"
2840
uri: "mongodb://rel%2F %2Fmongodb-27017.sock"

specifications/connection-string/tests/valid-warnings.json

+15
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@
9393
],
9494
"auth": null,
9595
"options": null
96+
},
97+
{
98+
"description": "Comma in a key value pair causes a warning",
99+
"uri": "mongodb://localhost?authMechanism=MONGODB-OIDC&authMechanismProperties=TOKEN_RESOURCE:mongodb://host1%2Chost2",
100+
"valid": true,
101+
"warning": true,
102+
"hosts": [
103+
{
104+
"type": "hostname",
105+
"host": "localhost",
106+
"port": null
107+
}
108+
],
109+
"auth": null,
110+
"options": null
96111
}
97112
]
98113
}

specifications/connection-string/tests/valid-warnings.yml

+12
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,15 @@ tests:
7373
port: ~
7474
auth: ~
7575
options: ~
76+
-
77+
description: Comma in a key value pair causes a warning
78+
uri: mongodb://localhost?authMechanism=MONGODB-OIDC&authMechanismProperties=TOKEN_RESOURCE:mongodb://host1%2Chost2
79+
valid: true
80+
warning: true
81+
hosts:
82+
-
83+
type: "hostname"
84+
host: "localhost"
85+
port: ~
86+
auth: ~
87+
options: ~

0 commit comments

Comments
 (0)