Terminal interface to find, download, and stream movies, TV shows, and live TV using local media players.
fix(logging): sanitize urls and paths for safe sharing
Samir Hossains72f374fd7524
diff --git a/src/cache.rs b/src/cache.rsindex a29c265..d3e71eb 100644--- a/src/cache.rs+++ b/src/cache.rs@@ -30,7 +30,10 @@ fn read_json_cache(path: &PathBuf, expiry_secs: u64) -> Option<serde_json::Valuefn write_json_cache(path: &PathBuf, data: &serde_json::Value) {let Ok(content) = serde_json::to_vec(data) else {- log::warn!("failed to serialize cache for {}", path.display());+ log::warn!(+ "failed to serialize cache for {}",+ crate::logging::sanitize_path(path)+ );return;};let stamp = std::time::SystemTime::now()@@ -39,13 +42,19 @@ fn write_json_cache(path: &PathBuf, data: &serde_json::Value) {.as_nanos();let temporary = path.with_extension(format!("tmp-{}-{stamp}", std::process::id()));if fs::write(&temporary, content).is_err() {- log::warn!("failed to write cache to {}", temporary.display());+ log::warn!(+ "failed to write cache to {}",+ crate::logging::sanitize_path(&temporary)+ );return;}if fs::rename(&temporary, path).is_err() {let _ = fs::remove_file(path);if fs::rename(&temporary, path).is_err() {- log::warn!("failed to commit cache to {}", path.display());+ log::warn!(+ "failed to commit cache to {}",+ crate::logging::sanitize_path(path)+ );let _ = fs::remove_file(temporary);}}@@ -318,13 +327,19 @@ pub fn set_namespaced_image_cache(namespace: &str, id: &str, bytes: &[u8]) {.as_nanos();let temporary = path.with_extension(format!("tmp-{}-{stamp}", std::process::id()));if std::fs::write(&temporary, bytes).is_err() {- log::warn!("failed to write image cache to {}", temporary.display());+ log::warn!(+ "failed to write image cache to {}",+ crate::logging::sanitize_path(&temporary)+ );return;}if std::fs::rename(&temporary, &path).is_err() {let _ = std::fs::remove_file(&path);if std::fs::rename(&temporary, &path).is_err() {- log::warn!("failed to commit image cache to {}", path.display());+ log::warn!(+ "failed to commit image cache to {}",+ crate::logging::sanitize_path(&path)+ );let _ = std::fs::remove_file(temporary);}}diff --git a/src/logging.rs b/src/logging.rsindex e179ac2..e2d3a70 100644--- a/src/logging.rs+++ b/src/logging.rs@@ -44,11 +44,11 @@ pub fn init() {"session started | moviebox-tui {} | {} | log file: {}",env!("CARGO_PKG_VERSION"),std::env::consts::OS,- log_file_path().display()+ display_path());eprintln!("[moviebox-tui] logging to {} (MOVIEBOX_LOG={})",- log_file_path().display(),+ display_path(),spec);}@@ -62,3 +62,46 @@ pub fn log_file_path() -> std::path::PathBuf {}).unwrap_or_else(std::env::temp_dir)}++pub fn display_path() -> String {+ sanitize_path(log_file_path())+}++pub fn sanitize_path(path: impl AsRef<std::path::Path>) -> String {+ let raw = path.as_ref().to_string_lossy().into_owned();+ dirs::home_dir()+ .and_then(|home| home.to_str().map(|home| home.to_string()))+ .map(|home| raw.replacen(&home, "~", 1))+ .unwrap_or(raw)+}++pub fn sanitize_url(raw: &str) -> String {+ match url::Url::parse(raw) {+ Ok(parsed) => {+ let host = parsed.host_str().unwrap_or("unknown");+ let scheme = parsed.scheme();+ if host.is_empty() {+ "[redacted]".to_string()+ } else {+ format!("{scheme}://{host}")+ }+ }+ Err(_) => {+ let lower = raw.to_ascii_lowercase();+ let host_start = lower.find("://").map(|index| index + 3);+ match host_start {+ Some(start) => {+ let rest = &raw[start..];+ let host_end = rest.find(['/', '?', '#']).unwrap_or(rest.len());+ let host = &rest[..host_end];+ if host.is_empty() {+ "[redacted]".to_string()+ } else {+ format!("https://{host}")+ }+ }+ None => "[redacted]".to_string(),+ }+ }+ }+}diff --git a/src/providers/bdix/dhakaflix/client.rs b/src/providers/bdix/dhakaflix/client.rsindex cd54484..ffd3125 100644--- a/src/providers/bdix/dhakaflix/client.rs+++ b/src/providers/bdix/dhakaflix/client.rs@@ -200,7 +200,10 @@ impl DhakaFlixClient {}}Err(error) => {- log::warn!("dhakaflix search request failed: {error} [{base_url}]");+ log::warn!(+ "dhakaflix search request failed: {error} [{}]",+ crate::logging::sanitize_url(base_url)+ );if let Ok(mut fails) = recent_fails.write() {fails.insert(base_url.to_string(), Instant::now());}diff --git a/src/providers/fourkhdhub/client.rs b/src/providers/fourkhdhub/client.rsindex 0d4fdfb..6ba0310 100644--- a/src/providers/fourkhdhub/client.rs+++ b/src/providers/fourkhdhub/client.rs@@ -123,7 +123,10 @@ impl FourKHdHubClient {}match self.preflight(&url, &merged).await {Ok(playable_url) => {- log::info!("4KHDHub mirror playable: {label} ({playable_url})");+ log::info!(+ "4KHDHub mirror playable: {label} ({})",+ crate::logging::sanitize_url(&playable_url)+ );return Ok(PlaybackSource {provider: ProviderKind::FourKHdHub,url: playable_url,@@ -133,7 +136,10 @@ impl FourKHdHubClient {});}Err(error) => {- log::warn!("4KHDHub mirror rejected ({label}): {error} [{url}]");+ log::warn!(+ "4KHDHub mirror rejected ({label}): {error} [{}]",+ crate::logging::sanitize_url(&url)+ );last_error = Some(error.to_string());}}diff --git a/src/providers/moviebox/client.rs b/src/providers/moviebox/client.rsindex b957f5e..74ec413 100644--- a/src/providers/moviebox/client.rs+++ b/src/providers/moviebox/client.rs@@ -190,7 +190,10 @@ impl MovieBoxClient {let status = resp.status().as_u16();if RETRY_STATUS_CODES.contains(&status) {- log::warn!("moviebox host {idx} returned retryable status {status}: {url}");+ log::warn!(+ "moviebox host {idx} returned retryable status {status}: {}",+ crate::logging::sanitize_url(&url)+ );continue;}@@ -199,19 +202,25 @@ impl MovieBoxClient {match self.parse_response(resp).await {Ok(val) => return Ok(val),Err(error) => {- log::warn!("moviebox host {idx} parse failed: {error} [{url}]");+ log::warn!(+ "moviebox host {idx} parse failed: {error} [{}]",+ crate::logging::sanitize_url(&url)+ );continue;}}}Err(error) => {- log::warn!("moviebox host {idx} request failed: {error} [{url}]");+ log::warn!(+ "moviebox host {idx} request failed: {error} [{}]",+ crate::logging::sanitize_url(&url)+ );continue;}}}- log::error!("moviebox: all hosts exhausted for {path_and_query}");+ log::error!("moviebox: all hosts exhausted for [redacted]");Err(ScraperError::HostsExhausted)}diff --git a/src/tui/app.rs b/src/tui/app.rsindex 7fdccc4..23944dd 100644--- a/src/tui/app.rs+++ b/src/tui/app.rs@@ -282,7 +282,7 @@ impl App {log::warn!("subtitle download failed for {:?} player, playing without subtitles (url was {})",kind,- url+ crate::logging::sanitize_url(&url));let _ = sender.send(Action::SetStatus("Subtitles unavailable; playing without subtitles.".to_string(),@@ -344,7 +344,11 @@ impl App {});}Err(error) => {- log::error!("failed to spawn player {:?} for {}: {error}", kind, link);+ log::error!(+ "failed to spawn player {:?} for {}: {error}",+ kind,+ crate::logging::sanitize_url(&link)+ );if let Some(path) = temporary_subtitle {let _ = tokio::fs::remove_file(path).await;}