use url::Url; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum HostRule { Exact(&'static str), Domain(&'static str), } impl HostRule { fn allows(self, host: &str) -> bool { match self { HostRule::Exact(name) => host == name, HostRule::Domain(domain) => { host == domain || host .strip_suffix(domain) .is_some_and(|prefix| prefix.len() > 1 && prefix.ends_with('.')) } } } } pub const MICROSOFT_HOSTS: [HostRule; 12] = [ HostRule::Domain("microsoft.com"), HostRule::Domain("cloud.microsoft"), HostRule::Domain("microsoft365.com"), HostRule::Domain("microsoftonline.com"), HostRule::Domain("office.com"), HostRule::Domain("office.net"), HostRule::Domain("office365.com"), HostRule::Domain("skype.com"), HostRule::Domain("sharepoint.com"), HostRule::Domain("onedrive.com"), HostRule::Domain("live.com"), HostRule::Domain("mcas.ms"), ]; /// The parsed URL when `raw` is plain HTTPS to a host one of `rules` allows: no user info, no explicit port. /// Callers send the returned URL, never `raw`, so the host that was checked is the host that is contacted. pub fn trusted_https_url(raw: &str, rules: &[HostRule]) -> Option { let url = Url::parse(raw).ok()?; let host = url.host_str()?; let plain = url.scheme() == "https" && url.username().is_empty() && url.password().is_none() && url.port().is_none(); (plain && rules.iter().any(|rule| rule.allows(host))).then_some(url) } pub fn is_trusted_https_url(raw: &str, rules: &[HostRule]) -> bool { trusted_https_url(raw, rules).is_some() } pub fn may_carry_credentials(raw: &str) -> bool { is_trusted_https_url(raw, &MICROSOFT_HOSTS) || is_websocket_to(raw, &MICROSOFT_HOSTS) } fn is_websocket_to(raw: &str, rules: &[HostRule]) -> bool { Url::parse(raw).ok().is_some_and(|url| { url.scheme() == "wss" && url.username().is_empty() && url.password().is_none() && url.port().is_none() && url .host_str() .is_some_and(|host| rules.iter().any(|rule| rule.allows(host))) }) } #[cfg(test)] mod tests { use super::*; const AMS: [HostRule; 1] = [HostRule::Domain("asm.skype.com")]; #[test] fn plain_https_to_an_allowed_host_passes() { let url = trusted_https_url("https://eu-api.asm.skype.com/v1/objects/a", &AMS).unwrap(); assert_eq!(url.host_str(), Some("eu-api.asm.skype.com")); assert!(is_trusted_https_url("https://asm.skype.com/x", &AMS)); assert!(is_trusted_https_url("https://EU.ASM.Skype.com/x", &AMS)); } #[test] fn separator_tricks_do_not_pass() { for raw in [ "https://evil.example\\.asm.skype.com/v1/objects/a", "https://evil.example#.asm.skype.com/v1/objects/a", "https://evil.example?.asm.skype.com/v1/objects/a", "https://evil.example/.asm.skype.com/v1/objects/a", "https://a.asm.skype.com:x@evil.example/", "https://a.asm.skype.com@evil.example/", "https://user@a.asm.skype.com/", "https://a.asm.skype.com:8443/", "https://evilasm.skype.com/", "https://asm.skype.com.evil.example/", "https://.asm.skype.com/", "http://a.asm.skype.com/", "//a.asm.skype.com/", "javascript:alert(1)//a.asm.skype.com", "not a url", ] { assert!(!is_trusted_https_url(raw, &AMS), "{raw}"); } } #[test] fn the_checked_host_is_the_host_that_gets_contacted() { let url = trusted_https_url("https://a.asm.skype.com/x?y#z", &AMS).unwrap(); assert_eq!(url.as_str(), "https://a.asm.skype.com/x?y#z"); } #[test] fn exact_rules_match_only_that_host() { let rules = [HostRule::Exact("teams.microsoft.com")]; assert!(is_trusted_https_url( "https://teams.microsoft.com/x", &rules )); assert!(!is_trusted_https_url( "https://a.teams.microsoft.com/x", &rules )); } #[test] fn credentials_go_only_to_microsoft_hosts() { assert!(may_carry_credentials("https://graph.microsoft.com/v1.0/me")); assert!(may_carry_credentials( "https://teams.cloud.microsoft/api/mt/emea/beta/users" )); assert!(may_carry_credentials("https://contoso.sharepoint.com/x")); assert!(may_carry_credentials( "wss://go.trouter.teams.microsoft.com/v4" )); assert!(may_carry_credentials( "https://teams.cloud.microsoft.mcas.ms/api/mt/emea/beta/users" )); assert!(!may_carry_credentials("https://evil.com\\.microsoft.com/")); assert!(!may_carry_credentials("https://microsoft.com.evil.com/")); assert!(!may_carry_credentials("https://evil.com/")); assert!(!may_carry_credentials("http://graph.microsoft.com/v1.0/me")); assert!(!may_carry_credentials("wss://evil.com/")); } }