1use std::borrow::Cow;
11
12pub const REDACTED_USERINFO: &str = "***";
14
15pub fn redact_userinfo(url: &str) -> Cow<'_, str> {
20 let Some((scheme, rest)) = url.split_once("://") else { return Cow::Borrowed(url) };
21 let authority_end = rest.find(['/', '?', '#']).unwrap_or(rest.len());
22 let (authority, path) = rest.split_at(authority_end);
23 match authority.rsplit_once('@') {
24 Some((_, host)) => Cow::Owned(format!("{scheme}://{REDACTED_USERINFO}@{host}{path}")),
25 None => Cow::Borrowed(url),
26 }
27}
28
29pub fn host(url: &str) -> Option<String> {
31 let (host, _) = host_and_port(url)?;
32 Some(host)
33}
34
35pub fn origin(url: &str) -> Option<String> {
38 let scheme = scheme(url)?;
39 let (host, port) = host_and_port(url)?;
40 let port = port.unwrap_or(if scheme == "https" { 443 } else { 80 });
41 Some(format!("{scheme}://{host}:{port}"))
42}
43
44pub fn is_loopback_host(host: &str) -> bool {
46 let host = host.trim_start_matches('[').trim_end_matches(']');
47 host.eq_ignore_ascii_case("localhost") || host.parse::<std::net::IpAddr>().is_ok_and(|ip| ip.is_loopback())
48}
49
50pub fn is_cleartext_remote(url: &str) -> bool {
53 scheme(url).as_deref() == Some("http") && !host(url).is_some_and(|host| is_loopback_host(&host))
54}
55
56fn scheme(url: &str) -> Option<String> {
57 let (scheme, _) = url.split_once("://")?;
58 Some(scheme.to_ascii_lowercase())
59}
60
61fn host_and_port(url: &str) -> Option<(String, Option<u16>)> {
62 let (_, rest) = url.split_once("://")?;
63 let authority = rest.split(['/', '?', '#']).next().unwrap_or_default();
64 let host_port = authority.rsplit_once('@').map_or(authority, |(_, host)| host);
65 let (host, port) = if let Some(bracketed) = host_port.strip_prefix('[') {
66 let (inside, after) = bracketed.split_once(']')?;
67 (inside, after.strip_prefix(':'))
68 } else {
69 match host_port.rsplit_once(':') {
70 Some((host, port)) => (host, Some(port)),
71 None => (host_port, None),
72 }
73 };
74 if host.is_empty() {
75 return None;
76 }
77 let port = match port {
78 Some(text) => Some(text.parse::<u16>().ok()?),
79 None => None,
80 };
81 Some((host.to_ascii_lowercase(), port))
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87
88 #[test]
89 fn userinfo_is_redacted() {
90 assert_eq!(redact_userinfo("https://u:p@h.example/x?y=1"), "https://***@h.example/x?y=1");
91 assert_eq!(redact_userinfo("http://token@127.0.0.1:3000"), "http://***@127.0.0.1:3000");
92 assert_eq!(redact_userinfo("https://h.example/a@b"), "https://h.example/a@b");
93 assert_eq!(redact_userinfo("not a url"), "not a url");
94 }
95
96 #[test]
97 fn origins_compare_hosts_and_ports() {
98 assert_eq!(origin("https://A.example/x"), origin("https://a.example:443/y"));
99 assert_ne!(origin("https://a.example"), origin("https://b.example"));
100 assert_ne!(origin("http://a.example"), origin("https://a.example"));
101 assert_eq!(origin("http://u:p@[::1]:8080/x").as_deref(), Some("http://::1:8080"));
102 assert_eq!(origin("http://a.example:notaport"), None);
103 assert_eq!(host("https://user@Cloud.Example:9/x").as_deref(), Some("cloud.example"));
104 }
105
106 #[test]
107 fn cleartext_to_remote_hosts() {
108 assert!(is_cleartext_remote("http://langfuse.internal:3000"));
109 assert!(!is_cleartext_remote("http://localhost:3000"));
110 assert!(!is_cleartext_remote("http://127.0.0.1:3000"));
111 assert!(!is_cleartext_remote("http://[::1]:3000"));
112 assert!(!is_cleartext_remote("https://cloud.langfuse.com"));
113 assert!(!is_loopback_host("localhost.evil.example"));
114 }
115}