@@ -6,11 +6,18 @@ class RequestLogger:
66 def __init__ (self , file = sys .stdout ):
77 self ._file = file
88
9- def obfuscate (self , value ):
10- if value .startswith ('ApiKey SU-' ):
11- return value .split (':' )[0 ] + '*' * 10
12- else :
13- return '*' * 20
9+ def obfuscate (self , key , value ):
10+ if key in ('authorization' , 'authentication' ):
11+ if value .startswith ('ApiKey ' ):
12+ return value .split (':' )[0 ] + '*' * 10
13+ else :
14+ return '*' * 20
15+ if key in ('cookie' , 'set-cookie' ):
16+ if 'api_key="' in value :
17+ start_idx = value .index ('api_key="' ) + len ('api_key="' )
18+ end_idx = value .index ('"' , start_idx )
19+ return f'{ value [0 :start_idx + 2 ]} ******{ value [end_idx - 2 :]} '
20+ return value
1421
1522 def log_request (self , method , url , kwargs ):
1623 other_args = {k : v for k , v in kwargs .items () if k not in ('headers' , 'json' , 'params' )}
@@ -26,8 +33,8 @@ def log_request(self, method, url, kwargs):
2633
2734 if 'headers' in kwargs :
2835 for k , v in kwargs ['headers' ].items ():
29- if k == 'Authorization' :
30- v = self .obfuscate (v )
36+ if k . lower () in ( 'authorization' , 'authentication' , 'cookie' ) :
37+ v = self .obfuscate (k . lower (), v )
3138 lines .append (f'{ k } : { v } ' )
3239
3340 if 'json' in kwargs :
@@ -45,6 +52,8 @@ def log_response(self, response):
4552 ]
4653
4754 for k , v in response .headers .items ():
55+ if k .lower () == 'set-cookie' :
56+ v = self .obfuscate (k .lower (), v )
4857 lines .append (f'{ k } : { v } ' )
4958
5059 if response .headers .get ('Content-Type' , None ) == 'application/json' :
0 commit comments