From a9fbfc2a8eb5b6f2a2345e7c79291de1c804ffdb Mon Sep 17 00:00:00 2001 From: Renato Caldas Date: Sat, 28 Jun 2025 08:19:00 +0100 Subject: [PATCH] Follow jmap redirections to same host in cli (#1735) Fixes #1734 --- crates/cli/src/main.rs | 3 ++- crates/cli/src/modules/mod.rs | 13 ++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 8351437d..d3d33a8f 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -17,7 +17,7 @@ use jmap_client::client::Credentials; use modules::{ UnwrapResult, cli::{Cli, Client, Commands}, - is_localhost, + host, is_localhost, }; use reqwest::{Method, StatusCode, header::AUTHORIZATION}; use serde::{Deserialize, Serialize, de::DeserializeOwned}; @@ -191,6 +191,7 @@ impl Client { jmap_client::client::Client::new() .credentials(self.credentials) .accept_invalid_certs(is_localhost(&self.url)) + .follow_redirects([host(&self.url).expect("Invalid host").to_owned()]) .timeout(Duration::from_secs(self.timeout.unwrap_or(60))) .connect(&self.url) .await diff --git a/crates/cli/src/modules/mod.rs b/crates/cli/src/modules/mod.rs index f2039bcd..e0a390ee 100644 --- a/crates/cli/src/modules/mod.rs +++ b/crates/cli/src/modules/mod.rs @@ -235,13 +235,16 @@ pub async fn name_to_id(client: &Client, name: &str) -> String { } } -pub fn is_localhost(url: &str) -> bool { +pub fn host(url: &str) -> Option<&str> { url.split_once("://") .map(|(_, url)| url.split_once('/').map_or(url, |(host, _)| host)) - .is_some_and(|host| { - let host = host.rsplit_once(':').map_or(host, |(host, _)| host); - host == "localhost" || host == "127.0.0.1" || host == "[::1]" - }) +} + +pub fn is_localhost(url: &str) -> bool { + host(url).is_some_and(|host| { + let host = host.rsplit_once(':').map_or(host, |(host, _)| host); + host == "localhost" || host == "127.0.0.1" || host == "[::1]" + }) } pub trait OAuthResponse {