Skip to content

Commit 216ddb8

Browse files
authored
Merge pull request #53 from cloudblue/LITE-25143_improve_logging
LITE-21143 obfuscate api_key in cookies
2 parents 5a0f4dd + 54a3aad commit 216ddb8

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

connect/client/logger.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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':

tests/client/test_logger.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ def test_log_request():
3636
rl.log_request(
3737
'get',
3838
PATH1,
39-
{'headers': {'Auth': 'None', 'Cookie': 'XXX', 'Authorization': 'SecretToken'}},
39+
{'headers': {
40+
'Auth': 'None',
41+
'Cookie': '_ga=wathever; api_key="test@example.com:abcdefg"; _gid=whatever',
42+
'Authorization': 'SecretToken',
43+
}},
4044
)
4145
assert ios.getvalue() == LOG_REQUEST_HEADER + 'GET ' + PATH1 + ' \n' + """Auth: None
42-
Cookie: XXX
46+
Cookie: _ga=wathever; api_key="te******fg"; _gid=whatever
4347
Authorization: ********************
4448
4549
"""
@@ -100,12 +104,19 @@ def test_log_response(mocker):
100104
mocker.patch('requests.models.Response.json', return_value=json)
101105
rsp = Response()
102106
rsp.raw = HTTPResponse()
103-
rsp.headers = {'Content-Type': 'application/json'}
107+
rsp.headers = {
108+
'Content-Type': 'application/json',
109+
'Set-Cookie': (
110+
'api_key="test@example.com:abcdefg"; '
111+
'expires=Wed, 19 Oct 2022 06:56:08 GMT; HttpOnly;'
112+
),
113+
}
104114
rsp.status_code = 200
105115
rsp.raw.reason = 'OK'
106116
rl.log_response(rsp)
107117
assert ios.getvalue() == LOG_RESPONSE_HEADER + """200 OK
108118
Content-Type: application/json
119+
Set-Cookie: api_key="te******fg"; expires=Wed, 19 Oct 2022 06:56:08 GMT; HttpOnly;
109120
{
110121
"id": "XX-1234",
111122
"name": "XXX"

0 commit comments

Comments
 (0)