From d1cb726996d6fa4e8e4aa4c40b0feabbd829d0fb Mon Sep 17 00:00:00 2001 From: BlockListed <44610569+BlockListed@users.noreply.github.com> Date: Sat, 9 Sep 2023 19:21:32 +0200 Subject: [PATCH] fix bug when extracing host from domain --- src/auth.rs | 28 +++++++++++++++++++--------- src/config.rs | 1 + 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/auth.rs b/src/auth.rs index 400d2226..b86c3925 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -365,7 +365,7 @@ use crate::db::{ DbConn, }; -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct HostInfo { pub base_url: String, pub origin: String, @@ -393,7 +393,8 @@ impl<'r> FromRequest<'r> for HostInfo { // Get host // TODO: UPDATE THIS SECTION - if CONFIG.domain_set() { + let host_info = if CONFIG.domain_set() { + log::debug!("Using configured host info"); let host: Cow<'_, str> = if let Some(host) = headers.get_one("X-Forwarded-Host") { host.into() } else if let Some(host) = headers.get_one("Host") { @@ -403,15 +404,20 @@ impl<'r> FromRequest<'r> for HostInfo { }; let host_info = get_host_info(host.as_ref()) - .unwrap_or_else(|| get_host_info(&get_main_host()).expect("Main domain doesn't have entry!")); + .unwrap_or_else(|| { + log::debug!("Falling back to default domain, because {host} was not in domains."); + get_host_info(&get_main_host()).expect("Main domain doesn't have entry!") + }); - return Outcome::Success(host_info); + host_info } else if let Some(referer) = headers.get_one("Referer") { - return Outcome::Success(HostInfo { + log::debug!("Using referer host info"); + HostInfo { base_url: referer.to_string(), origin: extract_url_origin(referer), - }); + } } else { + log::debug!("Guessing host info with headers"); // Try to guess from the headers use std::env; @@ -433,11 +439,15 @@ impl<'r> FromRequest<'r> for HostInfo { let base_url_origin = format!("{protocol}://{host}"); - return Outcome::Success(HostInfo { + HostInfo { base_url: base_url_origin.clone(), origin: base_url_origin, - }); - } + } + }; + + log::debug!("Using host_info: {:?}", host_info); + + Outcome::Success(host_info) } } diff --git a/src/config.rs b/src/config.rs index 5c4a9616..97eb4b9a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1058,6 +1058,7 @@ pub fn extract_url_host(url: &str) -> String { }; if let Some(port) = u.port().map(|p| p.to_string()) { + host.push(':'); host.push_str(&port); }