-
Notifications
You must be signed in to change notification settings - Fork 455
CDRIVER-5580 treat commas in TOKEN_RESOURCE as a client error #1950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
value = bson_strdup (end_key + 1); | ||
|
||
if (strcmp (lkey, MONGOC_URI_AUTHMECHANISMPROPERTIES) == 0) { | ||
// Defer percent-decoding until after key-value pairs are split on unencoded ',' in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am concerned this reduces portability of the connection string among other drivers. Example:
bson_error_t error;
const char *uristr = "mongodb://u:p@host/?authMechanism=GSSAPI&authMechanismProperties=SERVICE_HOST%3ahost";
// ----------------------------------------------------------------------------- : percent encoded ^^^
mongoc_uri_t *const uri = mongoc_uri_new_with_error (uristr, &error);
// Before PR : parses as {'SERVICE_HOST': 'host'}
// After PR : Error: Unsupported 'GSSAPI' authentication mechanism property: 'SERVICE_HOST%3ahost'
mongoc_uri_destroy (uri);
I expect other drivers passing the spec test also percent decode before splitting. Example in PyMongo:
from pymongo.uri_parser import parse_uri
print(parse_uri("mongodb://u:p@host/?authMechanism=GSSAPI&authMechanismProperties=SERVICE_HOST%3ahost"))
# {'nodelist': [('localhost', 27017)], 'username': 'user', 'password': 'pass', 'database': None, 'collection': None, 'options': {'authMechanism': 'GSSAPI', 'authMechanismProperties': {'SERVICE_HOST': 'host'}}, 'fqdn': None}
Suggest doing option 1 (warn/error on comma) for portability.
Reverted initially suggested changes in favor of treating unexpected commas in As described above, the error is due to the invalid comma being treated as a KVP delimiter which produces an invalid mechanism property (e.g. "host2"). The URI documentation merged in #1941 is updated accordingly to avoid indicating support for percent-encoded commas. The comma spec test exception (expect: valid+warning, actual: invalid) is also relocated in the connection string spec test runner accordingly to match our stricter diagnostic behavior. |
Related to CDRIVER-5580. Followup to #1914.
Update: focus of the PR has changed to properly forbid unexpected commas in
TOKEN_RESOURCE
for better consistency with other Drivers; see below.This PR proposes supporting percent-encoded commas in KVPs ofauthMechanismProperties
as an extension to the Connection String specification, which is contrary to the intended behavior of CDRIVER-5580.The Connection String spec states:
Similarly, the Authentication spec states:
Accordingly, this Connection String spec test requires a percent-encoded comma to be parsed as valid and emit a warning:
A warning is expected by this spec test because the Connection String spec also states (however, note that this paragraph applies to query options, not to key-value pairs):
There is ambiguity regarding exactly how and why a warning should be emitted by this spec test due to the expected value of
authMechanismProperties
being unspecified by this spec test:TOKEN_RESOURCE
?Currently, mongoc favors Option 2 due to percent-decoding a connection string query option's value prior to further parsing according to the lowercased key (rough pseudocode):
Given Option 2, the properties' KVPs will be split on both the percent-encoded and unencoded commas. The resulting
"host2"
property key would not be a valid or supported property for theMONGODB-OIDC
authentication mechanism, which is now treated as a client error rather than a warning due to the changes in #1896:mongo-c-driver/src/libmongoc/src/mongoc/mongoc-uri.c
Line 1451 in ae43242
(Note the associated comment regarding the Authentication spec overriding the Connection String spec concerning error handling.)
Option 1 can be supported by deferring percent-decoding of the query option's value until after it is parsed as a
authMechanismProperties
and after the KVPs are correctly split only on unencoded commas inmongoc_uri_parse_auth_mechanism_properties
:mongo-c-driver/src/libmongoc/src/mongoc/mongoc-uri.c
Line 523 in 9e3bbd3
However, this means that percent-encoded commas would be parsed completely fine without any interference with regular URI connection string parsing or parsing of other KVPs in
authMechanismProperties
. This makes the warning expected by the spec test seemingly redundant: there is nothing to warn about, as the behavior is well-defined and consistent.In the spirit of supporting client errors for invalid properties (per the Authentication spec) as well as supporting a better user experience, this PR proposes supporting Option 1 without emitting a warning per the spec test requirements.
As documented in the associated comment in the new
mongoc_uri_parse_auth_mechanism_properties_kvp
function:This deferred percent-decoding behavior is limited only to the
authMechanismProperties
URI option. This could be extended to other KVP-style options (i.e.readPreferenceTags
), but given onlyTOKEN_RESOURCE
is expected to contain commas anyways, other options are left unchanged.Note the updated documentation for authentication mechanism properties added by #1941 already describe the behavior proposed in this PR, according to its PR description:
The discrepancy in behavior from spec test expectations is handled in
test-mongoc-connection-uri.c
by the spec test exception handler for"TOKEN_RESOURCE:mongodb://host1%2Chost2"
. Note: this test has not yet been synced with the repository due to theMONGODB-OIDC
authentication mechanism not yet being supported by the URI parser.Edit: fixed references to Option 1 and Option 2 which were mixed up.