Skip to content

PYTHON-4463 Disallow comma character in authMechanismProperties connection string value #1646

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

Merged
merged 7 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pymongo/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,11 @@ def validate_auth_mechanism_properties(option: str, value: Any) -> dict[str, Uni
return props

value = validate_string(option, value)
value = unquote_plus(value)
for opt in value.split(","):
key, _, val = opt.partition(":")
if not val:
raise ValueError("Malformed auth mechanism properties")
if key not in _MECHANISM_PROPS:
# Try not to leak the token.
if "AWS_SESSION_TOKEN" in key:
Expand All @@ -471,7 +474,7 @@ def validate_auth_mechanism_properties(option: str, value: Any) -> dict[str, Uni
if key == "CANONICALIZE_HOST_NAME":
props[key] = validate_boolean_or_string(key, val)
else:
props[key] = unquote_plus(val)
props[key] = val

return props

Expand Down
13 changes: 4 additions & 9 deletions pymongo/uri_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,16 +494,11 @@ def parse_uri(
collection = None
options = _CaseInsensitiveDictionary()

host_part, _, path_part = scheme_free.partition("/")
if not host_part:
host_part = path_part
path_part = ""

if path_part:
dbase, _, opts = path_part.partition("?")
host_plus_db_part, _, opts = scheme_free.partition("?")
if "/" in host_plus_db_part:
host_part, _, dbase = host_plus_db_part.partition("/")
else:
# There was no slash in scheme_free, check for a sole "?".
host_part, _, opts = host_part.partition("?")
host_part = host_plus_db_part

if dbase:
dbase = unquote_plus(dbase)
Expand Down
4 changes: 2 additions & 2 deletions test/auth/legacy/connection-string.json
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,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 @@ -568,7 +568,7 @@
"mechanism": "MONGODB-OIDC",
"mechanism_properties": {
"ENVIRONMENT": "azure",
"TOKEN_RESOURCE": "abc,d%ef:g&hi"
"TOKEN_RESOURCE": "abcd%ef:g&hi"
}
}
},
Expand Down
19 changes: 19 additions & 0 deletions test/connection_string/test/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?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"
}
}
Comment on lines +40 to +58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did a new a colon test get added?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we now need an XFAIL test with a comma in it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This came up because TOKEN_RESOURCE is the first auth mech property that will commonly have a colon in it, and that part of the spec was ambiguous. I added a test for the case with a comma.

}
]
}
15 changes: 15 additions & 0 deletions test/connection_string/test/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://example.com?authMechanismProperties=TOKEN_RESOURCE:mongodb://host1%2Chost2",
"valid": true,
"warning": true,
"hosts": [
{
"type": "hostname",
"host": "example.com",
"port": null
}
],
"auth": null,
"options": null
}
]
}
8 changes: 4 additions & 4 deletions test/test_uri_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,9 @@ def test_normalize_options(self):
res = {"tls": True, "appname": "myapp"}
self.assertEqual(res, parse_uri(uri)["options"])

def test_unquote_after_parsing(self):
quoted_val = "val%21%40%23%24%25%5E%26%2A%28%29_%2B%2C%3A+etc"
unquoted_val = "val!@#$%^&*()_+,: etc"
def test_unquote_during_parsing(self):
quoted_val = "val%21%40%23%24%25%5E%26%2A%28%29_%2B%3A+etc"
unquoted_val = "val!@#$%^&*()_+: etc"
uri = (
"mongodb://user:password@localhost/?authMechanism=MONGODB-AWS"
"&authMechanismProperties=AWS_SESSION_TOKEN:" + quoted_val
Expand Down Expand Up @@ -511,7 +511,7 @@ def test_redact_AWS_SESSION_TOKEN(self):
)
with self.assertRaisesRegex(
ValueError,
"auth mechanism properties must be key:value pairs like AWS_SESSION_TOKEN:<token>",
"Malformed auth mechanism properties",
):
parse_uri(uri)

Expand Down