Skip to content

Commit e70d019

Browse files
committed
requests: Do not leak header modifications when calling request()
The requests() function takes a headers dict argument (call-by-reference). This object is then modified in the function. For instance the host is added and authentication information. Such behavior is not expected. It is also problematic: - Modifications of the header dictionary will be visible on the caller site. - When reusing the same (supposedly read-only) headers object for differenct calls, the second call will apparently re-use wrong headers from the previous call and may fail. This patch should also fix micropython#839. Signed-off-by: Richard Weickelt <[email protected]>
1 parent e4cf095 commit e70d019

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

Diff for: python-ecosys/requests/requests/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def request(
4646
):
4747
if headers is None:
4848
headers = {}
49+
else:
50+
headers = dict(headers)
4951

5052
redirect = None # redirection url, None means no redirection
5153
chunked_data = data and getattr(data, "__next__", None) and not getattr(data, "__len__", None)

Diff for: python-ecosys/requests/test_requests.py

+7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ def test_get_custom_header():
7070
+ b"Connection: close\r\n\r\n"
7171
), format_message(response)
7272

73+
def test_do_not_modify_headers_argument():
74+
original_headers = {}
75+
headers = dict(original_headers)
76+
response = requests.request("GET", "http://example.com", headers=original_headers)
77+
78+
assert headers == original_headers
7379

7480
def test_post_json():
7581
response = requests.request("GET", "http://example.com", json="test")
@@ -148,6 +154,7 @@ def chunks():
148154
test_simple_get()
149155
test_get_auth()
150156
test_get_custom_header()
157+
test_do_not_modify_headers_argument()
151158
test_post_json()
152159
test_post_chunked_data()
153160
test_overwrite_get_headers()

0 commit comments

Comments
 (0)