Skip to content

Commit d9aa6b6

Browse files
committed
Refactor unittest logs
We already hide some most sensitive informatino before, and PEN test team loves it. This time we hide some extra information.
1 parent b149620 commit d9aa6b6

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

msal/token_cache.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,30 @@ def find(self, credential_type, target=None, query=None):
9999

100100
def add(self, event, now=None):
101101
# type: (dict) -> None
102-
# event typically contains: client_id, scope, token_endpoint,
103-
# resposne, params, data, grant_type
104-
for sensitive in ("password", "client_secret"):
105-
if sensitive in event.get("data", {}):
106-
# Hide them from accidental exposure in logging
107-
event["data"][sensitive] = "********"
108-
logger.debug("event=%s", json.dumps(
102+
"""Handle a token obtaining event, and add tokens into cache.
103+
104+
Known side effects: This function modifies the input event in place.
105+
"""
106+
def wipe(dictionary, sensitive_fields): # Masks sensitive info
107+
for sensitive in sensitive_fields:
108+
if sensitive in dictionary:
109+
dictionary[sensitive] = "********"
110+
wipe(event.get("data", {}),
111+
("password", "client_secret", "refresh_token", "assertion"))
112+
try:
113+
return self.__add(event, now=now)
114+
finally:
115+
wipe(event.get("response", {}), ("access_token", "refresh_token"))
116+
logger.debug("event=%s", json.dumps(
109117
# We examined and concluded that this log won't have Log Injection risk,
110118
# because the event payload is already in JSON so CR/LF will be escaped.
111-
event, indent=4, sort_keys=True,
112-
default=str, # A workaround when assertion is in bytes in Python 3
113-
))
119+
event, indent=4, sort_keys=True,
120+
default=str, # A workaround when assertion is in bytes in Python 3
121+
))
122+
123+
def __add(self, event, now=None):
124+
# event typically contains: client_id, scope, token_endpoint,
125+
# response, params, data, grant_type
114126
environment = realm = None
115127
if "token_endpoint" in event:
116128
_, environment, realm = canonicalize(event["token_endpoint"])

0 commit comments

Comments
 (0)