Skip to content

Commit 325e30a

Browse files
committed
extend filtering to cover tuple and dict arguments for logging
1 parent eea5951 commit 325e30a

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

Diff for: realtime/_async/client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ async def connect(self) -> None:
125125

126126
while retries < self.max_retries:
127127
try:
128-
self.ws_connection = await websockets.connect(self.url)
128+
self.ws_connection = await websockets.connect(self.url, logger=logger)
129129
if self.ws_connection.open:
130130
logger.info("Connection was successful")
131131
return await self._on_connect()

Diff for: realtime/logging_util.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import logging
23
import re
34

@@ -8,17 +9,35 @@
89
redact = r"(eyJh[-_\w]*\.)([-_\w]*)\."
910

1011

12+
def gred(g):
13+
"""Redact the payload of the JWT, keeping the header and signature"""
14+
return f"{g.group(1)}REDACTED." if len(g.groups()) > 1 else g
15+
16+
1117
class TokenMaskingFilter(logging.Filter):
1218
"""Mask access_tokens in logs"""
1319

1420
def filter(self, record):
1521
record.msg = self.sanitize_line(record.msg)
22+
record.args = self.sanitize_args(record.args)
1623
return True
1724

1825
@staticmethod
19-
def sanitize_line(line):
20-
def gred(g):
21-
"""Redact the payload of the JWT, keeping the header and signature"""
22-
return f"{g.group(1)}REDACTED." if len(g.groups()) > 1 else g
26+
def sanitize_args(d):
27+
if isinstance(d, dict):
28+
d = d.copy() # so we don't overwrite anything
29+
for k, v in d.items():
30+
d[k] = self.sanitize_line(v)
31+
elif isinstance(d, tuple):
32+
# need a deepcopy of tuple turned to a list, as to not change the original values
33+
# otherwise we end up changing the items at the original memory location of the passed in tuple
34+
y = copy.deepcopy(list(d))
35+
for x, value in enumerate(y):
36+
if isinstance(value, str):
37+
y[x] = re.sub(redact, gred, value)
38+
return tuple(y) # convert the list back to a tuple
39+
return d
2340

41+
@staticmethod
42+
def sanitize_line(line):
2443
return re.sub(redact, gred, line)

0 commit comments

Comments
 (0)