Skip to content

Commit

Permalink
CSHARP-5106: Disallow comma character in authMechanismProperties conn… (
Browse files Browse the repository at this point in the history
  • Loading branch information
sanych-sun authored Jun 27, 2024
1 parent 6747c43 commit fc7df86
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 139 deletions.
8 changes: 4 additions & 4 deletions specifications/auth/tests/legacy/connection-string.json
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@
},
{
"description": "should throw an exception if username is specified for test (MONGODB-OIDC)",
"uri": "mongodb://principalName@localhost/?authMechanism=MONGODB-OIDC&ENVIRONMENT:test",
"uri": "mongodb://principalName@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:test",
"valid": false,
"credential": null
},
Expand Down Expand Up @@ -601,7 +601,7 @@
},
{
"description": "should handle a complicated url-encoded TOKEN_RESOURCE (MONGODB-OIDC)",
"uri": "mongodb://user@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:abc%2Cd%25ef%3Ag%26hi",
"uri": "mongodb://user@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:abcd%25ef%3Ag%26hi",
"valid": true,
"credential": {
"username": "user",
Expand All @@ -610,7 +610,7 @@
"mechanism": "MONGODB-OIDC",
"mechanism_properties": {
"ENVIRONMENT": "azure",
"TOKEN_RESOURCE": "abc,d%ef:g&hi"
"TOKEN_RESOURCE": "abcd%ef:g&hi"
}
}
},
Expand Down Expand Up @@ -669,4 +669,4 @@
"credential": null
}
]
}
}
6 changes: 3 additions & 3 deletions specifications/auth/tests/legacy/connection-string.yml
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ tests:
valid: false
credential:
- description: should throw an exception if username is specified for test (MONGODB-OIDC)
uri: mongodb://principalName@localhost/?authMechanism=MONGODB-OIDC&ENVIRONMENT:test
uri: mongodb://principalName@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:test
valid: false
credential:
- description: should throw an exception if specified environment is not supported (MONGODB-OIDC)
Expand Down Expand Up @@ -435,7 +435,7 @@ tests:
ENVIRONMENT: azure
TOKEN_RESOURCE: 'mongodb://test-cluster'
- description: should handle a complicated url-encoded TOKEN_RESOURCE (MONGODB-OIDC)
uri: mongodb://user@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:abc%2Cd%25ef%3Ag%26hi
uri: mongodb://user@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:abcd%25ef%3Ag%26hi
valid: true
credential:
username: user
Expand All @@ -444,7 +444,7 @@ tests:
mechanism: MONGODB-OIDC
mechanism_properties:
ENVIRONMENT: azure
TOKEN_RESOURCE: 'abc,d%ef:g&hi'
TOKEN_RESOURCE: 'abcd%ef:g&hi'
- description: should url-encode a TOKEN_RESOURCE (MONGODB-OIDC)
uri: mongodb://user@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:a$b
valid: true
Expand Down
55 changes: 55 additions & 0 deletions specifications/connection-string/tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Connection String Tests

The YAML and JSON files in this directory tree are platform-independent tests that drivers can use to prove their
conformance to the Connection String Spec.

As the spec is primarily concerned with parsing the parts of a URI, these tests do not focus on host and option
validation. Where necessary, the tests use options known to be (un)supported by drivers to assert behavior such as
issuing a warning on repeated option keys. As such these YAML tests are in no way a replacement for more thorough
testing. However, they can provide an initial verification of your implementation.

## Version

Files in the "specifications" repository have no version scheme. They are not tied to a MongoDB server version.

## Format

Each YAML file contains an object with a single `tests` key. This key is an array of test case objects, each of which
have the following keys:

- `description`: A string describing the test.
- `uri`: A string containing the URI to be parsed.
- `valid:` A boolean indicating if the URI should be considered valid.
- `warning:` A boolean indicating whether URI parsing should emit a warning (independent of whether or not the URI is
valid).
- `hosts`: An array of host objects, each of which have the following keys:
- `type`: A string denoting the type of host. Possible values are "ipv4", "ip_literal", "hostname", and "unix".
Asserting the type is *optional*.
- `host`: A string containing the parsed host.
- `port`: An integer containing the parsed port number.
- `auth`: An object containing the following keys:
- `username`: A string containing the parsed username. For auth mechanisms that do not utilize a password, this may be
the entire `userinfo` token (as discussed in [RFC 2396](https://www.ietf.org/rfc/rfc2396.txt)).
- `password`: A string containing the parsed password.
- `db`: A string containing the parsed authentication database. For legacy implementations that support namespaces
(databases and collections) this may be the full namespace eg: `<db>.<coll>`
- `options`: An object containing key/value pairs for each parsed query string option.

If a test case includes a null value for one of these keys (e.g. `auth: ~`, `port: ~`), no assertion is necessary. This
both simplifies parsing of the test files (keys should always exist) and allows flexibility for drivers that might
substitute default values *during* parsing (e.g. omitted `port` could be parsed as 27017).

The `valid` and `warning` fields are boolean in order to keep the tests flexible. We are not concerned with asserting
the format of specific error or warnings messages strings.

### Use as unit tests

Testing whether a URI is valid or not should simply be a matter of checking whether URI parsing (or MongoClient
construction) raises an error or exception. Testing for emitted warnings may require more legwork (e.g. configuring a
log handler and watching for output).

Not all drivers may be able to directly assert the hosts, auth credentials, and options. Doing so may require exposing
the driver's URI parsing component.

The file `valid-db-with-dotted-name.yml` is a special case for testing drivers that allow dotted namespaces, instead of
only database names, in the Auth Database portion of the URI.
73 changes: 0 additions & 73 deletions specifications/connection-string/tests/README.rst

This file was deleted.

2 changes: 0 additions & 2 deletions specifications/connection-string/tests/invalid-uris.yml
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,3 @@ tests:
hosts: ~
auth: ~
options: ~


19 changes: 19 additions & 0 deletions specifications/connection-string/tests/valid-options.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@
"options": {
"tls": true
}
},
{
"description": "Colon in a key value pair",
"uri": "mongodb://example.com/?authMechanism=MONGODB-OIDC&authMechanismProperties=TOKEN_RESOURCE:mongodb://test-cluster",
"valid": true,
"warning": false,
"hosts": [
{
"type": "hostname",
"host": "example.com",
"port": null
}
],
"auth": null,
"options": {
"authmechanismProperties": {
"TOKEN_RESOURCE": "mongodb://test-cluster"
}
}
}
]
}
14 changes: 14 additions & 0 deletions specifications/connection-string/tests/valid-options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,17 @@ tests:
auth: ~
options:
tls: true
-
description: Colon in a key value pair
uri: mongodb://example.com/?authMechanism=MONGODB-OIDC&authMechanismProperties=TOKEN_RESOURCE:mongodb://test-cluster
valid: true
warning: false
hosts:
-
type: hostname
host: example.com
port: ~
auth: ~
options:
authmechanismProperties:
TOKEN_RESOURCE: 'mongodb://test-cluster'
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@
"auth": null,
"options": null
},
{
"description": "Unix domain socket (mixed case)",
"uri": "mongodb://%2Ftmp%2FMongoDB-27017.sock",
"valid": true,
"warning": false,
"hosts": [
{
"type": "unix",
"host": "/tmp/MongoDB-27017.sock",
"port": null
}
],
"auth": null,
"options": null
},
{
"description": "Unix domain socket (absolute path with spaces in path)",
"uri": "mongodb://%2Ftmp%2F %2Fmongodb-27017.sock",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ tests:
port: ~
auth: ~
options: ~
-
description: "Unix domain socket (mixed case)"
uri: "mongodb://%2Ftmp%2FMongoDB-27017.sock"
valid: true
warning: false
hosts:
-
type: "unix"
host: "/tmp/MongoDB-27017.sock"
port: ~
auth: ~
options: ~
-
description: "Unix domain socket (absolute path with spaces in path)"
uri: "mongodb://%2Ftmp%2F %2Fmongodb-27017.sock"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@
"auth": null,
"options": null
},
{
"description": "Unix domain socket (mixed case)",
"uri": "mongodb://rel%2FMongoDB-27017.sock",
"valid": true,
"warning": false,
"hosts": [
{
"type": "unix",
"host": "rel/MongoDB-27017.sock",
"port": null
}
],
"auth": null,
"options": null
},
{
"description": "Unix domain socket (relative path with spaces)",
"uri": "mongodb://rel%2F %2Fmongodb-27017.sock",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ tests:
port: ~
auth: ~
options: ~
-
description: "Unix domain socket (mixed case)"
uri: "mongodb://rel%2FMongoDB-27017.sock"
valid: true
warning: false
hosts:
-
type: "unix"
host: "rel/MongoDB-27017.sock"
port: ~
auth: ~
options: ~
-
description: "Unix domain socket (relative path with spaces)"
uri: "mongodb://rel%2F %2Fmongodb-27017.sock"
Expand Down
15 changes: 15 additions & 0 deletions specifications/connection-string/tests/valid-warnings.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@
],
"auth": null,
"options": null
},
{
"description": "Comma in a key value pair causes a warning",
"uri": "mongodb://localhost?authMechanism=MONGODB-OIDC&authMechanismProperties=TOKEN_RESOURCE:mongodb://host1%2Chost2",
"valid": true,
"warning": true,
"hosts": [
{
"type": "hostname",
"host": "localhost",
"port": null
}
],
"auth": null,
"options": null
}
]
}
12 changes: 12 additions & 0 deletions specifications/connection-string/tests/valid-warnings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,15 @@ tests:
port: ~
auth: ~
options: ~
-
description: Comma in a key value pair causes a warning
uri: mongodb://localhost?authMechanism=MONGODB-OIDC&authMechanismProperties=TOKEN_RESOURCE:mongodb://host1%2Chost2
valid: true
warning: true
hosts:
-
type: "hostname"
host: "localhost"
port: ~
auth: ~
options: ~
Loading

0 comments on commit fc7df86

Please sign in to comment.