| 1 | use url::Url; |
| 2 | |
| 3 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 4 | pub enum HostRule { |
| 5 | Exact(&'static str), |
| 6 | Domain(&'static str), |
| 7 | } |
| 8 | |
| 9 | impl HostRule { |
| 10 | fn allows(self, host: &str) -> bool { |
| 11 | match self { |
| 12 | HostRule::Exact(name) => host == name, |
| 13 | HostRule::Domain(domain) => { |
| 14 | host == domain |
| 15 | || host |
| 16 | .strip_suffix(domain) |
| 17 | .is_some_and(|prefix| prefix.len() > 1 && prefix.ends_with('.')) |
| 18 | } |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | pub const MICROSOFT_HOSTS: [HostRule; 12] = [ |
| 24 | HostRule::Domain("microsoft.com"), |
| 25 | HostRule::Domain("cloud.microsoft"), |
| 26 | HostRule::Domain("microsoft365.com"), |
| 27 | HostRule::Domain("microsoftonline.com"), |
| 28 | HostRule::Domain("office.com"), |
| 29 | HostRule::Domain("office.net"), |
| 30 | HostRule::Domain("office365.com"), |
| 31 | HostRule::Domain("skype.com"), |
| 32 | HostRule::Domain("sharepoint.com"), |
| 33 | HostRule::Domain("onedrive.com"), |
| 34 | HostRule::Domain("live.com"), |
| 35 | HostRule::Domain("mcas.ms"), |
| 36 | ]; |
| 37 | |
| 38 | /// The parsed URL when `raw` is plain HTTPS to a host one of `rules` allows: no user info, no explicit port. |
| 39 | /// Callers send the returned URL, never `raw`, so the host that was checked is the host that is contacted. |
| 40 | pub fn trusted_https_url(raw: &str, rules: &[HostRule]) -> Option<Url> { |
| 41 | let url = Url::parse(raw).ok()?; |
| 42 | let host = url.host_str()?; |
| 43 | let plain = url.scheme() == "https" |
| 44 | && url.username().is_empty() |
| 45 | && url.password().is_none() |
| 46 | && url.port().is_none(); |
| 47 | (plain && rules.iter().any(|rule| rule.allows(host))).then_some(url) |
| 48 | } |
| 49 | |
| 50 | pub fn is_trusted_https_url(raw: &str, rules: &[HostRule]) -> bool { |
| 51 | trusted_https_url(raw, rules).is_some() |
| 52 | } |
| 53 | |
| 54 | pub fn may_carry_credentials(raw: &str) -> bool { |
| 55 | is_trusted_https_url(raw, &MICROSOFT_HOSTS) || is_websocket_to(raw, &MICROSOFT_HOSTS) |
| 56 | } |
| 57 | |
| 58 | fn is_websocket_to(raw: &str, rules: &[HostRule]) -> bool { |
| 59 | Url::parse(raw).ok().is_some_and(|url| { |
| 60 | url.scheme() == "wss" |
| 61 | && url.username().is_empty() |
| 62 | && url.password().is_none() |
| 63 | && url.port().is_none() |
| 64 | && url |
| 65 | .host_str() |
| 66 | .is_some_and(|host| rules.iter().any(|rule| rule.allows(host))) |
| 67 | }) |
| 68 | } |
| 69 | |
| 70 | #[cfg(test)] |
| 71 | mod tests { |
| 72 | use super::*; |
| 73 | |
| 74 | const AMS: [HostRule; 1] = [HostRule::Domain("asm.skype.com")]; |
| 75 | |
| 76 | #[test] |
| 77 | fn plain_https_to_an_allowed_host_passes() { |
| 78 | let url = trusted_https_url("https://eu-api.asm.skype.com/v1/objects/a", &AMS).unwrap(); |
| 79 | assert_eq!(url.host_str(), Some("eu-api.asm.skype.com")); |
| 80 | assert!(is_trusted_https_url("https://asm.skype.com/x", &AMS)); |
| 81 | assert!(is_trusted_https_url("https://EU.ASM.Skype.com/x", &AMS)); |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn separator_tricks_do_not_pass() { |
| 86 | for raw in [ |
| 87 | "https://evil.example\\.asm.skype.com/v1/objects/a", |
| 88 | "https://evil.example#.asm.skype.com/v1/objects/a", |
| 89 | "https://evil.example?.asm.skype.com/v1/objects/a", |
| 90 | "https://evil.example/.asm.skype.com/v1/objects/a", |
| 91 | "https://a.asm.skype.com:x@evil.example/", |
| 92 | "https://a.asm.skype.com@evil.example/", |
| 93 | "https://user@a.asm.skype.com/", |
| 94 | "https://a.asm.skype.com:8443/", |
| 95 | "https://evilasm.skype.com/", |
| 96 | "https://asm.skype.com.evil.example/", |
| 97 | "https://.asm.skype.com/", |
| 98 | "http://a.asm.skype.com/", |
| 99 | "//a.asm.skype.com/", |
| 100 | "javascript:alert(1)//a.asm.skype.com", |
| 101 | "not a url", |
| 102 | ] { |
| 103 | assert!(!is_trusted_https_url(raw, &AMS), "{raw}"); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn the_checked_host_is_the_host_that_gets_contacted() { |
| 109 | let url = trusted_https_url("https://a.asm.skype.com/x?y#z", &AMS).unwrap(); |
| 110 | assert_eq!(url.as_str(), "https://a.asm.skype.com/x?y#z"); |
| 111 | } |
| 112 | |
| 113 | #[test] |
| 114 | fn exact_rules_match_only_that_host() { |
| 115 | let rules = [HostRule::Exact("teams.microsoft.com")]; |
| 116 | assert!(is_trusted_https_url( |
| 117 | "https://teams.microsoft.com/x", |
| 118 | &rules |
| 119 | )); |
| 120 | assert!(!is_trusted_https_url( |
| 121 | "https://a.teams.microsoft.com/x", |
| 122 | &rules |
| 123 | )); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn credentials_go_only_to_microsoft_hosts() { |
| 128 | assert!(may_carry_credentials("https://graph.microsoft.com/v1.0/me")); |
| 129 | assert!(may_carry_credentials( |
| 130 | "https://teams.cloud.microsoft/api/mt/emea/beta/users" |
| 131 | )); |
| 132 | assert!(may_carry_credentials("https://contoso.sharepoint.com/x")); |
| 133 | assert!(may_carry_credentials( |
| 134 | "wss://go.trouter.teams.microsoft.com/v4" |
| 135 | )); |
| 136 | assert!(may_carry_credentials( |
| 137 | "https://teams.cloud.microsoft.mcas.ms/api/mt/emea/beta/users" |
| 138 | )); |
| 139 | assert!(!may_carry_credentials("https://evil.com\\.microsoft.com/")); |
| 140 | assert!(!may_carry_credentials("https://microsoft.com.evil.com/")); |
| 141 | assert!(!may_carry_credentials("https://evil.com/")); |
| 142 | assert!(!may_carry_credentials("http://graph.microsoft.com/v1.0/me")); |
| 143 | assert!(!may_carry_credentials("wss://evil.com/")); |
| 144 | } |
| 145 | } |