1313import unicodedata
1414import 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
1717from tornado import gen
1818from 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
763822class RequestProxyTest (unittest .TestCase ):
764823 def test_request_set (self ):
0 commit comments