Skip to content

Commit

Permalink
fix: credentials leakage in request headers
Browse files Browse the repository at this point in the history
The headers request contains the authentication, and it gets
printed in the robot log report.
  • Loading branch information
nestoracunablanco committed Jan 7, 2024
1 parent 533c31c commit 3123af4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
requests
robotframework
requests_ntlm
pytest
14 changes: 6 additions & 8 deletions src/RequestsLibrary/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@ def log_response(response):
"body=%s \n " % format_data_to_log_string(response.text))


def log_request(response):
def log_request(response, log_headers=False):
request = response.request
if response.history:
original_request = response.history[0].request
redirected = '(redirected) '
else:
original_request = request
redirected = ''
response_history = response.history
original_request = response_history[0].request if response_history else request
redirected = '(redirected) ' if response_history else ''
headers = original_request.headers if log_headers else '{}'
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 " % headers +
"body=%s \n " % format_data_to_log_string(original_request.body))


Expand Down
34 changes: 22 additions & 12 deletions utests/test_log.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import json
import os
import pytest

from requests import Request

from requests import Request, PreparedRequest
from RequestsLibrary.log import format_data_to_log_string, log_request, log_response
from unittest.mock import patch
from utests import SCRIPT_DIR
from utests import mock


__MOCKED_HEADERS = "mocked_headers"


def test_format_with_data_and_headers_none():
data_str = format_data_to_log_string(None)
assert data_str is None
Expand Down Expand Up @@ -45,19 +49,25 @@ def test_format_with_file_descriptor():
assert data_str == repr(f)


@mock.patch('RequestsLibrary.log.logger')
def test_log_request(mocked_logger):
request = Request(method='get', url='http://mock.rulezz')
request = request.prepare()
@pytest.mark.parametrize("log_headers, expected_headers", [(False, '{}'), (True, __MOCKED_HEADERS)])
def test_log_request_with_headers(log_headers: bool, expected_headers: str):
with patch('RequestsLibrary.log.logger') as mocked_logger:
request = Request(method='get', url='http://mock.rulezz').prepare()
response = __mock_log_request_response(request)
log_request(response, log_headers)
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 " % expected_headers +
"body=%s \n " % request.body)


def __mock_log_request_response(request: PreparedRequest):
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)
response.request.headers = __MOCKED_HEADERS
return response


@mock.patch('RequestsLibrary.log.logger')
Expand Down

0 comments on commit 3123af4

Please sign in to comment.