use anyhow::{Context, Error, Result};
use reqwest::Url;
use crate::fetch::request::RequestHeaders;
use crate::security::validate_url_safe;
fn is_loopback_url(url_str: &str) -> bool {
url::Url::parse(url_str)
.ok()
.and_then(|u| {
u.host().map(|h| match h {
url::Host::Ipv4(ip) => ip.is_loopback(),
url::Host::Ipv6(ip) => ip.is_loopback(),
url::Host::Domain(_) => false,
})
})
.unwrap_or(false)
}
fn validate_redirect_target(
new_url_str: &str,
new_scheme: &str,
chain_started_https: bool,
allow_loopback: bool,
) -> Result<()> {
if chain_started_https && new_scheme == "http" {
return Err(anyhow::anyhow!(
"Blocked HTTPS->HTTP downgrade redirect to {new_url_str}"
));
}
let skip_validation = allow_loopback && is_loopback_url(new_url_str);
if !skip_validation {
validate_url_safe(new_url_str)
.with_context(|| format!("Unsafe redirect target rejected: {new_url_str}"))?;
}
Ok(())
}
fn is_redirect_status(status_code: u16) -> bool {
matches!(status_code, 301 | 302 | 303 | 307 | 308)
}
pub async fn resolve_redirect_chain(
start_url: &str,
max_hops: usize,
client: &reqwest::Client,
allow_loopback: bool,
) -> Result<(String, Vec<(String, u16)>, Option<reqwest::Response>), Error> {
if max_hops == 0 {
return Err(anyhow::anyhow!("max_hops must be > 0"));
}
let skip_initial_validation = allow_loopback && is_loopback_url(start_url);
if !skip_initial_validation {
validate_url_safe(start_url)
.with_context(|| format!("Unsafe initial URL rejected: {start_url}"))?;
}
let chain_started_https = Url::parse(start_url)
.map(|u| u.scheme() == "https")
.unwrap_or(false);
let mut chain: Vec<(String, u16)> = Vec::with_capacity(max_hops + 1);
let mut current = start_url.to_string();
let mut last_fetched_url = start_url.to_string(); let mut final_response: Option<reqwest::Response> = None;
for hop_num in 0..max_hops {
last_fetched_url.clone_from(¤t);
let resp = RequestHeaders::apply_to_request_builder(client.get(¤t))
.send()
.await?;
let status = resp.status();
let status_code = status.as_u16();
chain.push((current.clone(), status_code));
if is_redirect_status(status_code) {
if let Some(loc) = resp.headers().get(reqwest::header::LOCATION) {
let loc_str = match loc.to_str() {
Ok(s) => s,
Err(e) => {
log::warn!(
"Location header for {current} contains invalid UTF-8: {e}. Skipping redirect."
);
break;
}
};
let new_url = Url::parse(loc_str)
.or_else(|_| Url::parse(¤t).and_then(|base| base.join(loc_str)))?;
let new_url_str = new_url.to_string();
if let Err(e) = validate_redirect_target(
&new_url_str,
new_url.scheme(),
chain_started_https,
allow_loopback,
) {
log::warn!(
"Blocked unsafe redirect from {current} to {new_url_str}: {e}. Stopping redirect chain."
);
break;
}
if hop_num == max_hops - 1 {
log::warn!(
"Redirect chain for {start_url} exceeded max_hops ({max_hops}). Stopping at {last_fetched_url} (which returned redirect to {new_url})."
);
break;
}
current = new_url.to_string();
continue;
}
log::warn!("Redirect status {status_code} for {current} but no Location header");
break;
}
final_response = Some(resp);
break;
}
if chain.last().map(|(url, _)| url) != Some(&last_fetched_url) {
chain.push((last_fetched_url.clone(), 0));
}
let final_url = last_fetched_url;
Ok((final_url, chain, final_response))
}
#[cfg(test)]
mod tests {
use super::*;
use httptest::{matchers::*, responders::*, Expectation, Server};
#[tokio::test]
async fn test_resolve_redirect_chain_no_redirects() {
let server = Server::run();
server.expect(
Expectation::matching(request::method_path("GET", "/"))
.respond_with(status_code(200).body("OK")),
);
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let url = server.url("/").to_string();
let (final_url, chain, _) = resolve_redirect_chain(&url, 10, &client, true)
.await
.unwrap();
assert_eq!(final_url, url);
assert_eq!(chain.len(), 1);
assert_eq!(chain[0].0, url);
}
#[tokio::test]
async fn test_resolve_redirect_chain_single_redirect() {
let server = Server::run();
let final_url = server.url("/final").to_string();
server.expect(
Expectation::matching(request::method_path("GET", "/")).respond_with(
status_code(302)
.insert_header("Location", final_url.as_str())
.body("Redirect"),
),
);
server.expect(
Expectation::matching(request::method_path("GET", "/final"))
.respond_with(status_code(200).body("OK")),
);
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let start_url = server.url("/").to_string();
let (result_final, chain, _) = resolve_redirect_chain(&start_url, 10, &client, true)
.await
.unwrap();
assert_eq!(result_final, final_url);
assert_eq!(chain.len(), 2);
assert_eq!(chain[0].0, start_url);
assert_eq!(chain[1].0, final_url);
}
#[tokio::test]
async fn test_resolve_redirect_chain_max_hops() {
let server = Server::run();
let url1 = server.url("/1").to_string();
let url2 = server.url("/2").to_string();
server.expect(
Expectation::matching(request::method_path("GET", "/")).respond_with(
status_code(302)
.insert_header("Location", url1.as_str())
.body("Redirect"),
),
);
server.expect(
Expectation::matching(request::method_path("GET", "/1")).respond_with(
status_code(302)
.insert_header("Location", url2.as_str())
.body("Redirect"),
),
);
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let start_url = server.url("/").to_string();
let (result_final, chain, _) = resolve_redirect_chain(&start_url, 2, &client, true)
.await
.unwrap();
assert_eq!(chain.len(), 2);
assert_eq!(chain[0].0, start_url);
assert_eq!(chain[1].0, url1);
assert_eq!(result_final, url1);
}
#[tokio::test]
async fn test_resolve_redirect_chain_max_hops_zero() {
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let result = resolve_redirect_chain("https://example.com", 0, &client, true).await;
assert!(result.is_err());
assert!(result
.unwrap_err()
.to_string()
.contains("max_hops must be > 0"));
}
#[tokio::test]
async fn test_resolve_redirect_chain_relative_location() {
let server = Server::run();
let final_url = server.url("/final").to_string();
server.expect(
Expectation::matching(request::method_path("GET", "/start")).respond_with(
status_code(302)
.insert_header("Location", "/final") .body("Redirect"),
),
);
server.expect(
Expectation::matching(request::method_path("GET", "/final"))
.respond_with(status_code(200).body("OK")),
);
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let start_url = server.url("/start").to_string();
let (result_final, chain, _) = resolve_redirect_chain(&start_url, 10, &client, true)
.await
.unwrap();
assert_eq!(result_final, final_url);
assert_eq!(chain.len(), 2);
assert_eq!(chain[0].0, start_url);
assert_eq!(chain[1].0, final_url);
}
#[tokio::test]
async fn test_resolve_redirect_chain_redirect_without_location() {
let server = Server::run();
server.expect(
Expectation::matching(request::method_path("GET", "/"))
.respond_with(status_code(302).body("Redirect but no Location")),
);
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let start_url = server.url("/").to_string();
let (result_final, chain, _) = resolve_redirect_chain(&start_url, 10, &client, true)
.await
.unwrap();
assert_eq!(chain.len(), 1);
assert_eq!(chain[0].0, start_url);
assert_eq!(result_final, start_url);
}
#[tokio::test]
async fn test_resolve_redirect_chain_different_redirect_codes() {
let server = Server::run();
let final_url = server.url("/final").to_string();
server.expect(
Expectation::matching(request::method_path("GET", "/301")).respond_with(
status_code(301)
.insert_header("Location", final_url.as_str())
.body("Moved"),
),
);
server.expect(
Expectation::matching(request::method_path("GET", "/final"))
.respond_with(status_code(200).body("OK")),
);
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let start_url = server.url("/301").to_string();
let (result_final, chain, _) = resolve_redirect_chain(&start_url, 10, &client, true)
.await
.unwrap();
assert_eq!(result_final, final_url);
assert_eq!(chain.len(), 2);
}
#[tokio::test]
async fn test_resolve_redirect_chain_invalid_utf8_location() {
let server = Server::run();
server.expect(
Expectation::matching(request::method_path("GET", "/"))
.respond_with(status_code(302).body("Redirect")),
);
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let start_url = server.url("/").to_string();
let (result_final, chain, _) = resolve_redirect_chain(&start_url, 10, &client, true)
.await
.unwrap();
assert_eq!(chain.len(), 1);
assert_eq!(result_final, start_url);
}
#[tokio::test]
async fn test_resolve_redirect_chain_rejects_unsafe_initial_url() {
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let result = resolve_redirect_chain("http://169.254.169.254", 10, &client, true).await;
assert!(
result.is_err(),
"Initial link-local URL must be rejected before any request"
);
let err = result.unwrap_err().to_string();
assert!(
err.contains("169.254") || err.contains("Unsafe") || err.contains("private"),
"Error should mention unsafe/private URL: {}",
err
);
}
#[tokio::test]
async fn test_resolve_redirect_chain_network_error() {
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.timeout(std::time::Duration::from_millis(100))
.build()
.unwrap();
let invalid_url = "http://192.0.2.0:9999/invalid"; let result = resolve_redirect_chain(invalid_url, 10, &client, true).await;
assert!(result.is_err(), "Network error should return error");
}
#[tokio::test]
async fn test_resolve_redirect_chain_all_redirect_codes() {
let codes = [301, 302, 303, 307, 308];
for code in codes {
let server = Server::run();
let final_url = server.url("/final").to_string();
server.expect(
Expectation::matching(request::method_path("GET", "/start")).respond_with(
status_code(code)
.insert_header("Location", final_url.as_str())
.body("Redirect"),
),
);
server.expect(
Expectation::matching(request::method_path("GET", "/final"))
.respond_with(status_code(200).body("OK")),
);
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let start_url = server.url("/start").to_string();
let (result_final, chain, _) = resolve_redirect_chain(&start_url, 10, &client, true)
.await
.unwrap();
assert_eq!(
result_final, final_url,
"Redirect code {} should work",
code
);
assert_eq!(
chain.len(),
2,
"Redirect code {} should have 2 URLs in chain",
code
);
}
}
#[tokio::test]
async fn test_resolve_redirect_chain_relative_location_edge_cases() {
let server = Server::run();
let base_url = server.url("/base/path").to_string();
let final_url = server.url("/final").to_string();
server.expect(
Expectation::matching(request::method_path("GET", "/base/path")).respond_with(
status_code(302)
.insert_header("Location", "../final") .body("Redirect"),
),
);
server.expect(
Expectation::matching(request::method_path("GET", "/final"))
.respond_with(status_code(200).body("OK")),
);
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let (result_final, chain, _) = resolve_redirect_chain(&base_url, 10, &client, true)
.await
.unwrap();
assert_eq!(result_final, final_url);
assert_eq!(chain.len(), 2);
}
#[tokio::test]
async fn test_resolve_redirect_chain_blocks_private_ip() {
let server = Server::run();
let start_url = server.url("/").to_string();
server.expect(
Expectation::matching(request::method_path("GET", "/")).respond_with(
status_code(302)
.insert_header("Location", "http://10.0.0.1:8080")
.body("Redirect to private IP"),
),
);
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let (result_final, chain, _) = resolve_redirect_chain(&start_url, 10, &client, true)
.await
.unwrap();
assert_eq!(result_final, start_url);
assert_eq!(chain.len(), 1);
assert_eq!(chain[0].0, start_url);
}
#[tokio::test]
async fn test_resolve_redirect_chain_blocks_localhost() {
let server = Server::run();
let start_url = server.url("/").to_string();
server.expect(
Expectation::matching(request::method_path("GET", "/")).respond_with(
status_code(302)
.insert_header("Location", "http://localhost:8080")
.body("Redirect to localhost"),
),
);
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let (result_final, chain, _) = resolve_redirect_chain(&start_url, 10, &client, true)
.await
.unwrap();
assert_eq!(result_final, start_url);
assert_eq!(chain.len(), 1);
}
#[tokio::test]
async fn test_resolve_redirect_chain_blocks_unsafe_scheme() {
let server = Server::run();
let start_url = server.url("/").to_string();
server.expect(
Expectation::matching(request::method_path("GET", "/")).respond_with(
status_code(302)
.insert_header("Location", "file:///etc/passwd")
.body("Redirect to file://"),
),
);
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let (result_final, chain, _) = resolve_redirect_chain(&start_url, 10, &client, true)
.await
.unwrap();
assert_eq!(result_final, start_url);
assert_eq!(chain.len(), 1);
}
#[tokio::test]
async fn test_resolve_redirect_chain_allows_public_urls() {
let server = Server::run();
let start_url = server.url("/").to_string();
let final_url = server.url("/final").to_string();
server.expect(
Expectation::matching(request::method_path("GET", "/")).respond_with(
status_code(302)
.insert_header("Location", final_url.as_str())
.body("Redirect"),
),
);
server.expect(
Expectation::matching(request::method_path("GET", "/final"))
.respond_with(status_code(200).body("OK")),
);
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let (result_final, chain, _) = resolve_redirect_chain(&start_url, 10, &client, true)
.await
.unwrap();
assert_eq!(result_final, final_url);
assert_eq!(chain.len(), 2);
}
#[tokio::test]
async fn test_resolve_redirect_chain_circular_redirect() {
let server = Server::run();
let url_a = server.url("/a").to_string();
let url_b = server.url("/b").to_string();
server.expect(
Expectation::matching(request::method_path("GET", "/a"))
.times(3) .respond_with(
status_code(302)
.insert_header("Location", url_b.as_str())
.body("Redirect to B"),
),
);
server.expect(
Expectation::matching(request::method_path("GET", "/b"))
.times(2) .respond_with(
status_code(302)
.insert_header("Location", url_a.as_str())
.body("Redirect to A"),
),
);
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let (result_final, chain, _) = resolve_redirect_chain(&url_a, 5, &client, true)
.await
.unwrap();
assert_eq!(chain.len(), 5);
assert_eq!(result_final, url_a);
}
#[tokio::test]
async fn test_resolve_redirect_chain_max_hops_one() {
let server = Server::run();
let start_url = server.url("/").to_string();
let redirect_url = server.url("/redirect").to_string();
server.expect(
Expectation::matching(request::method_path("GET", "/")).respond_with(
status_code(302)
.insert_header("Location", redirect_url.as_str())
.body("Redirect"),
),
);
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
let (result_final, chain, _) = resolve_redirect_chain(&start_url, 1, &client, true)
.await
.unwrap();
assert_eq!(chain.len(), 1);
assert_eq!(result_final, start_url);
assert!(!chain.iter().any(|(url, _)| url == &redirect_url));
}
#[test]
fn test_validate_redirect_target_blocks_https_to_http_downgrade() {
let err = validate_redirect_target("http://example.com/login", "http", true, false)
.expect_err("https chain must reject http hop");
let msg = err.to_string();
assert!(
msg.contains("downgrade"),
"expected downgrade message, got: {msg}"
);
}
#[test]
fn test_validate_redirect_target_blocks_https_to_http_same_host_downgrade() {
validate_redirect_target("http://example.com/", "http", true, false)
.expect_err("same-host https->http downgrade must be rejected");
}
#[test]
fn test_validate_redirect_target_allows_https_to_https() {
validate_redirect_target("https://example.com/next", "https", true, false)
.expect("https->https hop must be allowed");
}
#[test]
fn test_validate_redirect_target_allows_http_to_http_when_chain_started_http() {
validate_redirect_target("http://example.com/next", "http", false, false)
.expect("http chain may continue on http");
}
#[test]
fn test_validate_redirect_target_blocks_private_ip_target() {
for url in [
"http://10.0.0.1/",
"http://192.168.1.1/",
"http://169.254.169.254/", "http://172.16.0.1/",
] {
let err = match validate_redirect_target(url, "http", false, false) {
Ok(()) => panic!("private IP {url} must be rejected"),
Err(e) => e,
};
assert!(
!err.to_string().contains("downgrade"),
"expected SSRF rejection for {url}, got downgrade message: {err}"
);
}
}
#[test]
fn test_validate_redirect_target_blocks_unsafe_scheme() {
validate_redirect_target("file:///etc/passwd", "file", false, false)
.expect_err("file:// must be rejected by validate_url_safe");
}
}