Skip to content

Commit e8fc7ed

Browse files
committed
simple_httpclient: Strip auth headers on cross-origin redirects
When following a redirect to a different origin (scheme, host, or port), auth-related headers (Authorization and Cookie) should be stripped to avoid exposing them to the new host.
1 parent 96dc88c commit e8fc7ed

2 files changed

Lines changed: 97 additions & 2 deletions

File tree

‎tornado/simple_httpclient.py‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,42 @@ def finish(self) -> None:
631631
new_request.url = urllib.parse.urljoin(
632632
self.request.url, self.headers["Location"]
633633
)
634+
new_request.headers = self.request.headers.copy()
635+
parsed_orig_url = urllib.parse.urlsplit(original_request.url)
636+
parsed_new_url = urllib.parse.urlsplit(new_request.url)
637+
if (
638+
parsed_orig_url.scheme != parsed_new_url.scheme
639+
or parsed_orig_url.netloc != parsed_new_url.netloc
640+
):
641+
# Cross-origin redirect: strip auth headers.
642+
# Note that while there is no formal specification of headers that should be
643+
# stripped here, libcurl strips the Authorization and Cookie headers, so we
644+
# do the same.
645+
# Reference:
646+
# https://github.com/curl/curl/blob/01d8191b25a05e8fa91553a6c0d48acb99907d26/lib/http.c#L1827-L1828
647+
#
648+
# Note that checking for cross-origin redirects is a crude heuristic. It is both
649+
# too weak (e.g. cookies that have a path attribute may need to be stripped even on
650+
# same-origin redirects) and too strong (e.g. cookies may be kept on cross-host
651+
# redirects within the same domain). However, we cannot know the full details of
652+
# the cookie policy at this layer, so we use the same heuristic as libcurl.
653+
# Applications that need more control over behavior on redirects can set
654+
# follow_redirects=False and handle 3xx responses themselves.
655+
new_request.auth_username = None
656+
new_request.auth_password = None
657+
if "@" in parsed_new_url.netloc:
658+
if parsed_new_url.port is not None:
659+
new_netloc = f"{parsed_new_url.hostname}:{parsed_new_url.port}"
660+
else:
661+
assert parsed_new_url.hostname is not None
662+
new_netloc = parsed_new_url.hostname
663+
parsed_new_url = parsed_new_url._replace(netloc=new_netloc)
664+
new_request.url = urllib.parse.urlunsplit(parsed_new_url)
665+
for h in ["Authorization", "Cookie"]:
666+
try:
667+
del new_request.headers[h]
668+
except KeyError:
669+
pass
634670
assert self.request.max_redirects is not None
635671
new_request.max_redirects = self.request.max_redirects - 1
636672
del new_request.headers["Host"]
@@ -655,7 +691,7 @@ def finish(self) -> None:
655691
"Transfer-Encoding",
656692
]:
657693
try:
658-
del self.request.headers[h]
694+
del new_request.headers[h]
659695
except KeyError:
660696
pass
661697
new_request.original_request = original_request # type: ignore

‎tornado/test/httpclient_test.py‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import unicodedata
1414
import unittest
1515

16-
from tornado.escape import utf8, native_str, to_unicode
16+
from tornado.escape import utf8, native_str, to_unicode, json_encode, json_decode
1717
from tornado import gen
1818
from tornado.httpclient import (
1919
HTTPRequest,
@@ -156,6 +156,11 @@ def get(self):
156156
self.finish(self.request.headers["Foo"].encode("ISO8859-1"))
157157

158158

159+
class EchoHeadersHandler(RequestHandler):
160+
def get(self):
161+
self.write(json_encode(dict(self.request.headers.get_all())))
162+
163+
159164
# These tests end up getting run redundantly: once here with the default
160165
# HTTPClient implementation, and then again in each implementation's own
161166
# test suite.
@@ -181,10 +186,23 @@ def get_app(self):
181186
url("/set_header", SetHeaderHandler),
182187
url("/invalid_gzip", InvalidGzipHandler),
183188
url("/header-encoding", HeaderEncodingHandler),
189+
url("/echo_headers", EchoHeadersHandler),
184190
],
185191
gzip=True,
186192
)
187193

194+
def setUp(self):
195+
super().setUp()
196+
197+
# Add a second port (serving the same app) to the HTTP server, so we can test the effects
198+
# of redirects that span different origins.
199+
sock, port = bind_unused_port()
200+
self.http_server.add_socket(sock)
201+
self.__port2 = port
202+
203+
def get_url2(self, path: str) -> str:
204+
return f"{self.get_protocol()}://127.0.0.1:{self.__port2}{path}"
205+
188206
def test_patch_receives_payload(self):
189207
body = b"some patch data"
190208
response = self.fetch("/patch", method="PATCH", body=body)
@@ -759,6 +777,47 @@ def test_header_crlf(self):
759777
with self.assertRaises(ValueError):
760778
self.fetch("/hello", headers={header: "foo"})
761779

780+
def test_strip_headers_on_redirect(self):
781+
# Ensure that headers that should be stripped on cross-origin redirects
782+
# are stripped, even if the redirect is to a different port on localhost.
783+
test_cases: list[tuple[str, dict, str]] = [
784+
("manual auth header", dict(headers={"Authorization": "secret"}), ""),
785+
("credentials in URL", dict(), "me:secret"),
786+
("auth parameters", dict(auth_username="me", auth_password="secret"), ""),
787+
("manual cookie header", dict(headers={"Cookie": "secret"}), ""),
788+
]
789+
for name, kwargs, url_creds in test_cases:
790+
with self.subTest(name=name, origin="different"):
791+
url = self.get_url(
792+
"/redirect?url=%s&status=302" % self.get_url2("/echo_headers")
793+
)
794+
if url_creds:
795+
url = url.replace("http://", "http://%s@" % url_creds)
796+
response = self.fetch(**dict(path=url) | kwargs)
797+
response.rethrow()
798+
echoed_headers = json_decode(response.body)
799+
# Confirm that non-auth headers are getting through
800+
self.assertIn("User-Agent", echoed_headers)
801+
# Auth headers are stripped, however they were set.
802+
self.assertNotIn("Authorization", echoed_headers)
803+
self.assertNotIn("Cookie", echoed_headers)
804+
with self.subTest(name=name, origin="same"):
805+
url = self.get_url(
806+
"/redirect?url=%s&status=302" % self.get_url("/echo_headers")
807+
)
808+
if url_creds:
809+
url = url.replace("http://", "http://%s@" % url_creds)
810+
response = self.fetch(**dict(path=url) | kwargs)
811+
response.rethrow()
812+
echoed_headers = json_decode(response.body)
813+
# Confirm that non-auth headers are getting through
814+
self.assertIn("User-Agent", echoed_headers)
815+
# Auth headers are not stripped when the redirect is same-origin.
816+
# Each of our tests uses one of these headers, but not both.
817+
self.assertTrue(
818+
"Authorization" in echoed_headers or "Cookie" in echoed_headers
819+
)
820+
762821

763822
class RequestProxyTest(unittest.TestCase):
764823
def test_request_set(self):

0 commit comments

Comments
 (0)