Skip to content

Commit c2add2f

Browse files
fix(backfill): preserve explicit redirect ports (#80)
Signed-off-by: Thomas Albrecht <241560317+thequantumfalcon@users.noreply.github.com>
1 parent 3127e1d commit c2add2f

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

examples/backfill_github.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ def redirect_request(self, req, fp, code, msg, headers, newurl):
6262
if (old_origin is None or new_origin is None
6363
or old_origin[0] != "https" or new_origin[0] != "https"
6464
or old_origin[1] is None or old_origin[1] != new_origin[1]
65-
or (old_origin[2] or 443) != (new_origin[2] or 443)):
65+
or (old_origin[2] if old_origin[2] is not None else 443)
66+
!= (new_origin[2] if new_origin[2] is not None else 443)):
6667
raise urllib.error.URLError("unsafe GitHub API redirect")
6768
return super().redirect_request(req, fp, code, msg, headers, newurl)
6869

tests/test_regressions_round17_backfill_layout.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,41 @@ def test_github_authorization_cannot_cross_a_redirect_boundary(
206206
request, None, 302, "Found", {}, destination)
207207

208208

209+
@pytest.mark.parametrize(
210+
("source", "destination", "allowed"),
211+
[
212+
("https://api.github.com/repos/o/r",
213+
"https://api.github.com:443/repos/o/r", True),
214+
("https://api.github.com:443/repos/o/r",
215+
"https://api.github.com/repos/o/r", True),
216+
("https://api.github.com/repos/o/r",
217+
"https://api.github.com:0/repos/o/r", False),
218+
("https://api.github.com:0/repos/o/r",
219+
"https://api.github.com/repos/o/r", False),
220+
("https://api.github.com:8443/repos/o/r",
221+
"https://api.github.com:8443/repositories/4242", True),
222+
("https://api.github.com:0/repos/o/r",
223+
"https://api.github.com:0/repositories/4242", True),
224+
],
225+
)
226+
def test_redirect_port_identity_is_not_truthiness(
227+
backfill, source, destination, allowed):
228+
request = urllib.request.Request(
229+
source, headers={"Authorization": "Bearer secret"})
230+
231+
if not allowed:
232+
with pytest.raises(
233+
urllib.error.URLError, match="unsafe GitHub API redirect"):
234+
backfill._SameOriginRedirectHandler().redirect_request(
235+
request, None, 302, "Found", {}, destination)
236+
return
237+
238+
redirected = backfill._SameOriginRedirectHandler().redirect_request(
239+
request, None, 302, "Found", {}, destination)
240+
assert redirected.full_url == destination
241+
assert redirected.get_header("Authorization") == "Bearer secret"
242+
243+
209244
def test_same_origin_https_redirect_remains_usable(backfill):
210245
request = urllib.request.Request(
211246
"https://api.github.com/repos/octo/demo",

0 commit comments

Comments
 (0)