Skip to content

Commit

Permalink
fix: auth leakage with basic authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Nestor Acuna-Blanco committed Jun 27, 2024
1 parent 8a42d5c commit 023d3ed
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/RequestsLibrary/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from RequestsLibrary.utils import is_file_descriptor

LOG_CHAR_LIMIT = 10000
AUTHORIZATION = 'Authorization'


def log_response(response):
Expand All @@ -24,11 +25,14 @@ def log_request(response):
else:
original_request = request
redirected = ""
safe_headers = dict(original_request.headers)
if AUTHORIZATION in safe_headers:
safe_headers[AUTHORIZATION] = '*****'
logger.info(
"%s Request : " % original_request.method.upper()
+ "url=%s %s\n " % (original_request.url, redirected)
+ "path_url=%s \n " % original_request.path_url
+ "headers=%s \n " % original_request.headers
+ "headers=%s \n " % safe_headers
+ "body=%s \n " % format_data_to_log_string(original_request.body)
)

Expand Down
41 changes: 41 additions & 0 deletions utests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,47 @@ def test_log_request(mocked_logger):
"body=%s \n " % request.body)


@mock.patch('RequestsLibrary.log.logger')
def test_log_request_with_headers(mocked_logger):
headers = {'User-Agent': 'python-requests/2.31.0',
'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*',
'Connection': 'keep-alive'}
request = Request(method='get', url='http://mock.rulezz', headers=headers)
request = request.prepare()
response = mock.MagicMock()
response.history = []
response.request = request
log_request(response)
assert mocked_logger.info.call_args[0][0] == ("%s Request : " % request.method +
"url=%s \n " % request.url +
"path_url=%s \n " % request.path_url +
"headers=%s \n " % request.headers +
"body=%s \n " % request.body)


@mock.patch('RequestsLibrary.log.logger')
def test_log_request_with_headers_auth(mocked_logger):
headers = {'User-Agent': 'python-requests/2.31.0',
'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*',
'Connection': 'keep-alive',
'Authorization': 'some_token'}
safe_headers = dict(headers)
safe_headers['Authorization'] = '*****'
request = Request(method='get', url='http://mock.rulezz', headers=headers)
request = request.prepare()
response = mock.MagicMock()
response.history = []
response.request = request
log_request(response)
assert mocked_logger.info.call_args[0][0] == ("%s Request : " % request.method +
"url=%s \n " % request.url +
"path_url=%s \n " % request.path_url +
"headers=%s \n " % safe_headers +
"body=%s \n " % request.body)


@mock.patch('RequestsLibrary.log.logger')
def test_log_request_with_redirect(mocked_logger):
request = Request(method='get', url='http://mock.rulezz/redirected')
Expand Down

0 comments on commit 023d3ed

Please sign in to comment.