Skip to content

Commit 75b2b1f

Browse files
committed
PYTHON-1327 - No credentials in repr(client)
1 parent 031559e commit 75b2b1f

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

pymongo/mongo_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,8 @@ def option_repr(option, value):
998998
options.extend(
999999
option_repr(key, self.__options._options[key])
10001000
for key in self.__options._options
1001-
if key not in set(self._constructor_args))
1001+
if key not in set(self._constructor_args)
1002+
and key != 'username' and key != 'password')
10021003
return ', '.join(options)
10031004

10041005
def __repr__(self):

test/test_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,16 @@ def test_username_and_password(self):
597597
self.client.admin.add_user("ad min", "pa/ss", roles=["root"])
598598
self.addCleanup(self.client.admin.remove_user, "ad min")
599599

600-
rs_or_single_client(username="ad min", password="pa/ss").server_info()
600+
c = rs_or_single_client(username="ad min", password="pa/ss")
601+
602+
# Username and password aren't in strings that will likely be logged.
603+
self.assertNotIn("ad min", repr(c))
604+
self.assertNotIn("ad min", str(c))
605+
self.assertNotIn("pa/ss", repr(c))
606+
self.assertNotIn("pa/ss", str(c))
607+
608+
# Auth succeeds.
609+
c.server_info()
601610

602611
with self.assertRaises(OperationFailure):
603612
rs_or_single_client(username="ad min", password="foo").server_info()

0 commit comments

Comments
 (0)