@@ -26,7 +26,6 @@ class RedirectHandler(BaseMiddleware):
2626 }
2727 STATUS_CODE_SEE_OTHER : int = 303
2828 LOCATION_HEADER : str = "Location"
29- AUTHORIZATION_HEADER : str = "Authorization"
3029
3130 def __init__ (self , options : RedirectHandlerOption = RedirectHandlerOption ()) -> None :
3231 super ().__init__ ()
@@ -125,15 +124,31 @@ def _build_redirect_request(
125124 """
126125 method = self ._redirect_method (request , response )
127126 url = self ._redirect_url (request , response , options )
128- headers = self ._redirect_headers (request , url , method )
129127 stream = self ._redirect_stream (request , method )
128+
129+ # Create the new request with the redirect URL and original headers
130130 new_request = httpx .Request (
131131 method = method ,
132132 url = url ,
133- headers = headers ,
133+ headers = request . headers . copy () ,
134134 stream = stream ,
135135 extensions = request .extensions ,
136136 )
137+
138+ # Scrub sensitive headers before following the redirect
139+ options .scrub_sensitive_headers (new_request , request .url )
140+
141+ # Update the Host header if not same origin
142+ if not self ._same_origin (url , request .url ):
143+ new_request .headers ["Host" ] = url .netloc .decode ("ascii" )
144+
145+ # Handle 303 See Other and other method changes
146+ if method != request .method and method == "GET" :
147+ # If we've switched to a 'GET' request, strip any headers which
148+ # are only relevant to the request body.
149+ new_request .headers .pop ("Content-Length" , None )
150+ new_request .headers .pop ("Transfer-Encoding" , None )
151+
137152 if hasattr (request , "context" ):
138153 new_request .context = request .context #type: ignore
139154 new_request .options = {} #type: ignore
@@ -201,35 +216,6 @@ def _redirect_url(
201216
202217 return url
203218
204- def _redirect_headers (
205- self , request : httpx .Request , url : httpx .URL , method : str
206- ) -> httpx .Headers :
207- """
208- Return the headers that should be used for the redirect request.
209- """
210- headers = httpx .Headers (request .headers )
211-
212- if not self ._same_origin (url , request .url ):
213- if not self .is_https_redirect (request .url , url ):
214- # Strip Authorization headers when responses are redirected
215- # away from the origin. (Except for direct HTTP to HTTPS redirects.)
216- headers .pop ("Authorization" , None )
217-
218- # Update the Host header.
219- headers ["Host" ] = url .netloc .decode ("ascii" )
220-
221- if method != request .method and method == "GET" :
222- # If we've switch to a 'GET' request, then strip any headers which
223- # are only relevant to the request body.
224- headers .pop ("Content-Length" , None )
225- headers .pop ("Transfer-Encoding" , None )
226-
227- # We should use the client cookie store to determine any cookie header,
228- # rather than whatever was on the original outgoing request.
229- headers .pop ("Cookie" , None )
230-
231- return headers
232-
233219 def _redirect_stream (
234220 self , request : httpx .Request , method : str
235221 ) -> typing .Optional [typing .Union [httpx .SyncByteStream , httpx .AsyncByteStream ]]:
@@ -246,17 +232,3 @@ def _same_origin(self, url: httpx.URL, other: httpx.URL) -> bool:
246232 Return 'True' if the given URLs share the same origin.
247233 """
248234 return (url .scheme == other .scheme and url .host == other .host )
249-
250- def port_or_default (self , url : httpx .URL ) -> typing .Optional [int ]:
251- if url .port is not None :
252- return url .port
253- return {"http" : 80 , "https" : 443 }.get (url .scheme )
254-
255- def is_https_redirect (self , url : httpx .URL , location : httpx .URL ) -> bool :
256- """
257- Return 'True' if 'location' is a HTTPS upgrade of 'url'
258- """
259- if url .host != location .host :
260- return False
261-
262- return (url .scheme == "http" and location .scheme == "https" )
0 commit comments