Skip to content

Commit e31a981

Browse files
committed
PYTHON-2718 Test redaction of security sensitive command monitoring events (#637)
(cherry picked from commit 59dc6d8)
1 parent 8c81beb commit e31a981

16 files changed

+585
-10
lines changed

pymongo/monitoring.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,15 @@ def register(listener):
498498
"updateuser", "copydbgetnonce", "copydbsaslstart", "copydb"])
499499

500500

501+
# The "hello" command is also deemed sensitive when attempting speculative
502+
# authentication.
503+
def _is_speculative_authenticate(command_name, doc):
504+
if (command_name.lower() in ('hello', 'ismaster') and
505+
'speculativeAuthenticate' in doc):
506+
return True
507+
return False
508+
509+
501510
class _CommandEvent(object):
502511
"""Base class for command events."""
503512

@@ -564,7 +573,9 @@ def __init__(self, command, database_name, request_id, connection_id,
564573
super(CommandStartedEvent, self).__init__(
565574
command_name, request_id, connection_id, operation_id,
566575
service_id=service_id)
567-
if command_name.lower() in _SENSITIVE_COMMANDS:
576+
cmd_name, cmd_doc = command_name.lower(), command[command_name]
577+
if (cmd_name in _SENSITIVE_COMMANDS or
578+
_is_speculative_authenticate(cmd_name, command)):
568579
self.__cmd = {}
569580
else:
570581
self.__cmd = command
@@ -610,7 +621,9 @@ def __init__(self, duration, reply, command_name,
610621
command_name, request_id, connection_id, operation_id,
611622
service_id=service_id)
612623
self.__duration_micros = _to_micros(duration)
613-
if command_name.lower() in _SENSITIVE_COMMANDS:
624+
cmd_name = command_name.lower()
625+
if (cmd_name in _SENSITIVE_COMMANDS or
626+
_is_speculative_authenticate(cmd_name, reply)):
614627
self.__reply = {}
615628
else:
616629
self.__reply = reply

0 commit comments

Comments
 (0)