From c4b22638b1a8a00f156c3d5bb94694f5af68967d Mon Sep 17 00:00:00 2001 From: Jessie Date: Sat, 30 Sep 2023 00:28:44 -0400 Subject: [PATCH] fix host spoofing for redirects --- src/request.rs | 6 ++---- src/response.rs | 4 ++++ src/unit.rs | 7 +++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/request.rs b/src/request.rs index 9f5abb3..9081b05 100644 --- a/src/request.rs +++ b/src/request.rs @@ -148,18 +148,16 @@ impl Request { let request_fn = |req: Request| { let reader = payload.into_read(); - let mut unit = Unit::new( + let unit = Unit::new( &req.agent, &req.method, &url, req.headers, &reader, deadline, + req.spoofed_host ); - // forward the spoofed host address - unit.spoofed_host = req.spoofed_host; - unit::connect(unit, true, reader).map_err(|e| e.url(url.clone())) }; diff --git a/src/response.rs b/src/response.rs index 2f56cc9..6af4dd7 100644 --- a/src/response.rs +++ b/src/response.rs @@ -767,6 +767,7 @@ impl FromStr for Response { vec![], &request_reader, None, + None ); Self::do_from_stream(stream, unit) } @@ -1151,6 +1152,7 @@ mod tests { vec![], &request_reader, None, + None, ); let resp = Response::do_from_stream(s.into(), unit).unwrap(); assert_eq!(resp.status(), 200); @@ -1215,6 +1217,7 @@ mod tests { vec![], &Payload::Empty.into_read(), None, + None, ), ) .unwrap(); @@ -1247,6 +1250,7 @@ mod tests { vec![], &Payload::Empty.into_read(), None, + None, ), ) .unwrap(); diff --git a/src/unit.rs b/src/unit.rs index ae538e4..5b60c00 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -46,8 +46,8 @@ impl Unit { mut headers: Vec
, body: &SizedReader, deadline: Option, + spoofed_host: Option>, ) -> Self { - // let (is_transfer_encoding_set, mut is_chunked) = get_header(&headers, "transfer-encoding") // if the user has set an encoding header, obey that. @@ -110,7 +110,7 @@ impl Unit { is_chunked, headers, deadline, - spoofed_host: None, + spoofed_host, } } @@ -227,6 +227,8 @@ pub(crate) fn connect( && (!h.is_name("authorization") || keep_auth_header) }); + + // recreate the unit to get a new hostname and cookies for the new host. unit = Unit::new( &unit.agent, @@ -235,6 +237,7 @@ pub(crate) fn connect( headers, &body, unit.deadline, + unit.spoofed_host ); }; resp.history = history;