use crate::api::error::{ErrorStage, NetError};
use std::fmt;
const DEFAULT_MAX_REDIRECTS: usize = 10;
const MAX_REDIRECTS: usize = 128;
const MAX_ALLOWED_ORIGINS: usize = 128;
const MAX_ORIGIN_BYTES: usize = 2048;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum HttpTransportSecurity {
#[default]
PreventDowngrade,
HttpsOnly,
}
#[derive(Clone)]
pub struct RedirectPolicy {
enabled: bool,
allowed_origins: Vec<reqwest::Url>,
max_redirects: usize,
}
impl Default for RedirectPolicy {
fn default() -> Self {
Self::same_origin()
}
}
impl RedirectPolicy {
pub fn none() -> Self {
Self {
enabled: false,
allowed_origins: Vec::new(),
max_redirects: DEFAULT_MAX_REDIRECTS,
}
}
pub fn same_origin() -> Self {
Self {
enabled: true,
allowed_origins: Vec::new(),
max_redirects: DEFAULT_MAX_REDIRECTS,
}
}
pub fn allow_origins<I, S>(origins: I) -> Result<Self, NetError>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut policy = Self::same_origin();
for value in origins {
let parsed = parse_origin(value.as_ref())?;
if policy
.allowed_origins
.iter()
.any(|allowed| allowed.origin() == parsed.origin())
{
continue;
}
if policy.allowed_origins.len() >= MAX_ALLOWED_ORIGINS {
return Err(invalid_policy("http.redirect.allowed_origins"));
}
policy.allowed_origins.push(parsed);
}
Ok(policy)
}
pub fn with_max_redirects(mut self, max_redirects: usize) -> Result<Self, NetError> {
if !(1..=MAX_REDIRECTS).contains(&max_redirects) {
return Err(invalid_policy("http.redirect.max_redirects"));
}
self.max_redirects = max_redirects;
Ok(self)
}
pub fn is_enabled(&self) -> bool {
self.enabled
}
pub fn max_redirects(&self) -> usize {
self.max_redirects
}
pub(crate) fn check_redirect(
&self,
original_url: &reqwest::Url,
current_url: &reqwest::Url,
next_url: &reqwest::Url,
followed: usize,
security: HttpTransportSecurity,
) -> Result<(), HttpRedirectError> {
if !matches!(next_url.scheme(), "http" | "https")
|| next_url.host_str().is_none()
|| !next_url.username().is_empty()
|| next_url.password().is_some()
{
return Err(HttpRedirectError::InvalidLocation);
}
if next_url.scheme() != "https"
&& (security == HttpTransportSecurity::HttpsOnly || current_url.scheme() == "https")
{
return Err(HttpRedirectError::InsecureTarget);
}
if next_url.origin() != original_url.origin()
&& !self
.allowed_origins
.iter()
.any(|allowed| allowed.origin() == next_url.origin())
{
return Err(HttpRedirectError::OriginNotAllowed);
}
if followed >= self.max_redirects {
return Err(HttpRedirectError::TooManyRedirects);
}
Ok(())
}
}
impl fmt::Debug for RedirectPolicy {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("RedirectPolicy")
.field("enabled", &self.enabled)
.field("allowed_origin_count", &self.allowed_origins.len())
.field("max_redirects", &self.max_redirects)
.finish()
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum HttpRedirectError {
InsecureTarget,
OriginNotAllowed,
TooManyRedirects,
InvalidLocation,
}
impl fmt::Display for HttpRedirectError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(match self {
Self::InsecureTarget => "HTTP redirect violates transport security",
Self::OriginNotAllowed => "HTTP redirect origin is not allowed",
Self::TooManyRedirects => "HTTP redirect limit exceeded",
Self::InvalidLocation => "invalid HTTP redirect location",
})
}
}
impl std::error::Error for HttpRedirectError {}
fn parse_origin(value: &str) -> Result<reqwest::Url, NetError> {
let invalid = || invalid_policy("http.redirect.allowed_origins");
if value.len() > MAX_ORIGIN_BYTES
|| value.trim() != value
|| value.chars().any(char::is_control)
|| value.contains(['\\', '@', '?', '#'])
{
return Err(invalid());
}
let (_, authority_and_path) = value.split_once("://").ok_or_else(invalid)?;
if authority_and_path
.find('/')
.is_some_and(|index| &authority_and_path[index..] != "/")
{
return Err(invalid());
}
let parsed = reqwest::Url::parse(value).map_err(|_| invalid())?;
if !matches!(parsed.scheme(), "http" | "https")
|| parsed.host_str().is_none()
|| !parsed.username().is_empty()
|| parsed.password().is_some()
|| parsed.path() != "/"
|| parsed.query().is_some()
|| parsed.fragment().is_some()
{
return Err(invalid());
}
Ok(parsed)
}
fn invalid_policy(field: &'static str) -> NetError {
NetError::config(field, "invalid HTTP redirect policy").with_stage(ErrorStage::Configuration)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::api::error::ErrorKind;
use std::error::Error;
type TestResult = Result<(), Box<dyn Error + Send + Sync>>;
#[test]
fn normalized_origins_match_exactly_without_disclosing_hosts() -> TestResult {
let policy = RedirectPolicy::allow_origins([
"https://PRIVATE.example:443/",
"https://private.example",
"https://[::1]:8443",
])?;
let original = reqwest::Url::parse("https://api.example/start")?;
for target in [
"https://private.example/file?token=secret",
"https://[0:0:0:0:0:0:0:1]:8443/file",
"https://api.example/return",
] {
policy.check_redirect(
&original,
&original,
&reqwest::Url::parse(target)?,
0,
HttpTransportSecurity::PreventDowngrade,
)?;
}
for target in [
"https://sub.private.example",
"https://private.example:8443",
"https://private.example.attacker.example",
] {
assert_eq!(
policy.check_redirect(
&original,
&original,
&reqwest::Url::parse(target)?,
0,
HttpTransportSecurity::PreventDowngrade,
),
Err(HttpRedirectError::OriginNotAllowed)
);
}
let debug = format!("{policy:?}");
assert!(debug.contains("allowed_origin_count: 2"));
assert!(!debug.contains("private.example"));
assert!(!debug.contains("::1"));
Ok(())
}
#[test]
fn rejects_non_origin_inputs_without_echoing_them() {
for origin in [
"ftp://private.example",
"https://user:secret@private.example",
"https://@private.example",
"https://private.example/path",
"https://private.example/path/..",
"https://private.example?secret",
"https://private.example#secret",
"https:\\\\private.example",
"https:///private.example",
" https://private.example",
"https://private.example\n",
] {
let error = RedirectPolicy::allow_origins([origin])
.expect_err("an allowlist entry must be an origin");
assert_eq!(error.kind(), ErrorKind::InvalidConfig);
assert_eq!(error.context().stage, Some(ErrorStage::Configuration));
assert!(!format!("{error:?} {error}").contains("private.example"));
}
}
#[test]
fn limits_are_bounded_and_do_not_enable_a_disabled_policy() -> TestResult {
assert_eq!(RedirectPolicy::default().max_redirects(), 10);
assert!(RedirectPolicy::default().is_enabled());
assert!(!RedirectPolicy::none().with_max_redirects(1)?.is_enabled());
for limit in [0, 129, usize::MAX] {
assert!(RedirectPolicy::same_origin()
.with_max_redirects(limit)
.is_err());
}
let origins = (0..129).map(|index| format!("https://host{index}.example"));
assert!(RedirectPolicy::allow_origins(origins).is_err());
assert!(RedirectPolicy::allow_origins(["x".repeat(MAX_ORIGIN_BYTES + 1)]).is_err());
let policy = RedirectPolicy::same_origin().with_max_redirects(1)?;
let original = reqwest::Url::parse("https://api.example")?;
assert_eq!(
policy.check_redirect(
&original,
&original,
&original,
1,
HttpTransportSecurity::PreventDowngrade,
),
Err(HttpRedirectError::TooManyRedirects)
);
Ok(())
}
#[test]
fn allowlisted_hops_never_expand_the_original_authorization() -> TestResult {
let original = reqwest::Url::parse("https://api.example/start")?;
let current = reqwest::Url::parse("https://cdn.example/first")?;
let next = reqwest::Url::parse("https://cdn.example.evil/next")?;
let policy = RedirectPolicy::allow_origins(["https://cdn.example"])?;
assert_eq!(
policy.check_redirect(
&original,
¤t,
&next,
1,
HttpTransportSecurity::PreventDowngrade,
),
Err(HttpRedirectError::OriginNotAllowed)
);
policy.check_redirect(
&original,
¤t,
&original,
1,
HttpTransportSecurity::PreventDowngrade,
)?;
Ok(())
}
#[test]
fn transport_checks_each_hop_even_for_allowlisted_targets() -> TestResult {
let original = reqwest::Url::parse("http://api.example/start")?;
let secure = reqwest::Url::parse("https://api.example/secure")?;
let policy = RedirectPolicy::allow_origins(["https://api.example"])?;
policy.check_redirect(
&original,
&original,
&secure,
0,
HttpTransportSecurity::PreventDowngrade,
)?;
assert_eq!(
policy.check_redirect(
&original,
&secure,
&original,
1,
HttpTransportSecurity::PreventDowngrade,
),
Err(HttpRedirectError::InsecureTarget)
);
assert_eq!(
policy.check_redirect(
&original,
&original,
&original,
0,
HttpTransportSecurity::HttpsOnly,
),
Err(HttpRedirectError::InsecureTarget)
);
let failure =
NetError::protocol(HttpRedirectError::InsecureTarget).with_stage(ErrorStage::Receive);
assert_eq!(
failure
.source()
.and_then(|source| source.downcast_ref::<HttpRedirectError>()),
Some(&HttpRedirectError::InsecureTarget)
);
Ok(())
}
}