Skip to content

Commit ef439eb

Browse files
authored
Prevent Response self-reference in redirect history (#7328)
1 parent cbce031 commit ef439eb

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

src/requests/sessions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,8 @@ def resolve_redirects(
179179
prepared_request = req.copy()
180180

181181
# Update history and keep track of redirects.
182-
# resp.history must ignore the original request in this loop
182+
resp.history = hist[:]
183183
hist.append(resp)
184-
resp.history = hist[1:]
185184

186185
try:
187186
resp.content # Consume socket so it can be released

tests/test_requests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@ def test_HTTP_302_ALLOW_REDIRECT_GET(self, httpbin):
217217
assert r.history[0].status_code == 302
218218
assert r.history[0].is_redirect
219219

220+
def test_redirect_history_no_self_reference(self, httpbin):
221+
r = requests.get(httpbin("redirect", "3"))
222+
assert r.status_code == 200
223+
assert len(r.history) == 3
224+
for i, resp in enumerate(r.history):
225+
assert resp not in resp.history
226+
assert resp.history == r.history[:i]
227+
220228
def test_HTTP_307_ALLOW_REDIRECT_POST(self, httpbin):
221229
r = requests.post(
222230
httpbin("redirect-to"),

0 commit comments

Comments
 (0)