Skip to content

Commit 44b0968

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 44b0968

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-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

+9
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ def chunks():
145145
), format_message(response)
146146

147147

148+
def test_do_not_modify_headers_argument():
149+
original_headers = {}
150+
headers = dict(original_headers)
151+
requests.request("GET", "https://example.com", headers=original_headers)
152+
153+
assert headers == original_headers
154+
155+
148156
test_simple_get()
149157
test_get_auth()
150158
test_get_custom_header()
@@ -153,3 +161,4 @@ def chunks():
153161
test_overwrite_get_headers()
154162
test_overwrite_post_json_headers()
155163
test_overwrite_post_chunked_data_headers()
164+
test_do_not_modify_headers_argument()

0 commit comments

Comments
 (0)