| 1 | use serde::Deserialize; |
| 2 | |
| 3 | /// Defender for Cloud Apps (Conditional Access App Control) proxies the app on `<host>.mcas.ms`. |
| 4 | pub const MCAS_SUFFIX: &str = ".mcas.ms"; |
| 5 | |
| 6 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 7 | pub enum App { |
| 8 | Teams, |
| 9 | } |
| 10 | |
| 11 | impl App { |
| 12 | pub const ALL: [App; 1] = [App::Teams]; |
| 13 | |
| 14 | pub fn origins(self) -> &'static [&'static str] { |
| 15 | match self { |
| 16 | App::Teams => &[ |
| 17 | "https://teams.cloud.microsoft", |
| 18 | "https://teams.microsoft.com", |
| 19 | ], |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | /// With `mcas` the URL goes through the MCAS proxy, where the MSAL cache of a proxied |
| 24 | /// tenant lives. |
| 25 | pub fn start_url(self, mcas: bool) -> &'static str { |
| 26 | match (self, mcas) { |
| 27 | (App::Teams, false) => "https://teams.cloud.microsoft/", |
| 28 | (App::Teams, true) => "https://teams.cloud.microsoft.mcas.ms/", |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /// A static page on the app origin: keeps the MSAL cache reachable without running the app. |
| 33 | pub fn park_url(self, mcas: bool) -> &'static str { |
| 34 | match (self, mcas) { |
| 35 | (App::Teams, false) => "https://teams.cloud.microsoft/robots.txt", |
| 36 | (App::Teams, true) => "https://teams.cloud.microsoft.mcas.ms/robots.txt", |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// The start URL on the same origin as `tab_url`, so waking a parked tab keeps its cache. |
| 41 | pub fn start_url_like(self, tab_url: &str) -> &'static str { |
| 42 | self.start_url(is_mcas_url(tab_url)) |
| 43 | } |
| 44 | |
| 45 | pub fn is_park_url(self, url: &str) -> bool { |
| 46 | // both origins count, a tab parked before the switch must still be seen as parked |
| 47 | [self.park_url(false), self.park_url(true)] |
| 48 | .iter() |
| 49 | .any(|park| url.starts_with(park)) |
| 50 | } |
| 51 | |
| 52 | /// True when `url` is an https page on one of the app hosts, directly or through the |
| 53 | /// MCAS proxy. The host must match exactly, a look-alike host is not the app. |
| 54 | pub fn is_app_url(self, url: &str) -> bool { |
| 55 | let Ok(parsed) = url::Url::parse(url) else { |
| 56 | return false; |
| 57 | }; |
| 58 | let Some(host) = parsed.host_str().filter(|_| parsed.scheme() == "https") else { |
| 59 | return false; |
| 60 | }; |
| 61 | let host = host.strip_suffix(MCAS_SUFFIX).unwrap_or(host); |
| 62 | self.origins() |
| 63 | .iter() |
| 64 | .any(|origin| origin.strip_prefix("https://") == Some(host)) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// True when `url` is an https page on a host behind the MCAS proxy. |
| 69 | pub fn is_mcas_url(url: &str) -> bool { |
| 70 | url::Url::parse(url).is_ok_and(|parsed| { |
| 71 | parsed.scheme() == "https" |
| 72 | && parsed |
| 73 | .host_str() |
| 74 | .is_some_and(|host| host.len() > MCAS_SUFFIX.len() && host.ends_with(MCAS_SUFFIX)) |
| 75 | }) |
| 76 | } |
| 77 | |
| 78 | impl std::fmt::Display for App { |
| 79 | fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 80 | formatter.write_str(match self { |
| 81 | App::Teams => "teams", |
| 82 | }) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | const LOGIN_ORIGINS: [&str; 2] = [ |
| 87 | "https://login.microsoftonline.com", |
| 88 | "https://login.live.com", |
| 89 | ]; |
| 90 | |
| 91 | #[derive(Debug, Clone, Deserialize)] |
| 92 | pub struct Target { |
| 93 | #[serde(rename = "type")] |
| 94 | pub kind: String, |
| 95 | #[serde(default)] |
| 96 | pub url: String, |
| 97 | #[serde(default, rename = "webSocketDebuggerUrl")] |
| 98 | pub websocket_url: Option<String>, |
| 99 | } |
| 100 | |
| 101 | pub fn find_app_tab(targets: &[Target], app: App) -> Option<&Target> { |
| 102 | targets.iter().find(|target| { |
| 103 | target.kind == "page" && target.websocket_url.is_some() && app.is_app_url(&target.url) |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | pub fn is_login_url(url: &str) -> bool { |
| 108 | LOGIN_ORIGINS.iter().any(|origin| is_on_origin(url, origin)) |
| 109 | } |
| 110 | |
| 111 | pub(crate) fn is_on_origin(url: &str, origin: &str) -> bool { |
| 112 | url.strip_prefix(origin) |
| 113 | .is_some_and(|rest| rest.is_empty() || rest.starts_with(['/', '?', '#'])) |
| 114 | } |
| 115 | |
| 116 | pub fn has_login_tab(targets: &[Target]) -> bool { |
| 117 | targets |
| 118 | .iter() |
| 119 | .any(|target| target.kind == "page" && is_login_url(&target.url)) |
| 120 | } |
| 121 | |
| 122 | #[cfg(test)] |
| 123 | mod tests { |
| 124 | use super::*; |
| 125 | |
| 126 | fn targets() -> Vec<Target> { |
| 127 | serde_json::from_str( |
| 128 | r#"[ |
| 129 | {"type":"service_worker","url":"https://teams.cloud.microsoft/sw.js","webSocketDebuggerUrl":"ws://x/1"}, |
| 130 | {"type":"page","url":"https://outlook.cloud.microsoft/mail/","webSocketDebuggerUrl":"ws://x/2"}, |
| 131 | {"type":"page","url":"https://teams.cloud.microsoft/v2/","webSocketDebuggerUrl":"ws://x/3"}, |
| 132 | {"type":"page","url":"about:blank"} |
| 133 | ]"#, |
| 134 | ) |
| 135 | .unwrap() |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn finds_page_tabs_per_app() { |
| 140 | let targets = targets(); |
| 141 | assert_eq!( |
| 142 | find_app_tab(&targets, App::Teams) |
| 143 | .unwrap() |
| 144 | .websocket_url |
| 145 | .as_deref(), |
| 146 | Some("ws://x/3") |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | #[test] |
| 151 | fn ignores_non_page_targets_and_missing_tabs() { |
| 152 | let only_worker = &targets()[..1]; |
| 153 | assert!(find_app_tab(only_worker, App::Teams).is_none()); |
| 154 | } |
| 155 | |
| 156 | #[test] |
| 157 | fn parked_tabs_are_found_by_origin_and_recognised() { |
| 158 | let parked: Vec<Target> = serde_json::from_str( |
| 159 | r#"[{"type":"page","url":"https://teams.cloud.microsoft/robots.txt","webSocketDebuggerUrl":"ws://x/9"}, |
| 160 | {"type":"page","url":"https://outlook.cloud.microsoft/owa/favicon.ico","webSocketDebuggerUrl":"ws://x/8"}]"#, |
| 161 | ) |
| 162 | .unwrap(); |
| 163 | let teams = find_app_tab(&parked, App::Teams).unwrap(); |
| 164 | assert!(App::Teams.is_park_url(&teams.url)); |
| 165 | assert!(!App::Teams.is_park_url("https://teams.cloud.microsoft/v2/")); |
| 166 | } |
| 167 | |
| 168 | #[test] |
| 169 | fn mcas_urls_go_through_the_proxy_and_both_park_urls_count() { |
| 170 | assert_eq!( |
| 171 | App::Teams.start_url(true), |
| 172 | "https://teams.cloud.microsoft.mcas.ms/" |
| 173 | ); |
| 174 | assert_eq!( |
| 175 | App::Teams.park_url(true), |
| 176 | "https://teams.cloud.microsoft.mcas.ms/robots.txt" |
| 177 | ); |
| 178 | assert_eq!( |
| 179 | App::Teams.start_url(false), |
| 180 | "https://teams.cloud.microsoft/" |
| 181 | ); |
| 182 | assert_eq!( |
| 183 | App::Teams.start_url_like("https://teams.cloud.microsoft.mcas.ms/robots.txt"), |
| 184 | App::Teams.start_url(true) |
| 185 | ); |
| 186 | assert_eq!( |
| 187 | App::Teams.start_url_like("https://teams.cloud.microsoft/robots.txt"), |
| 188 | App::Teams.start_url(false) |
| 189 | ); |
| 190 | assert!(!is_mcas_url("http://teams.cloud.microsoft.mcas.ms/")); |
| 191 | assert!(!is_mcas_url("https://mcas.ms/")); |
| 192 | assert!(App::Teams.is_park_url("https://teams.cloud.microsoft.mcas.ms/robots.txt")); |
| 193 | assert!(App::Teams.is_park_url("https://teams.cloud.microsoft/robots.txt")); |
| 194 | assert!(!App::Teams.is_park_url("https://teams.cloud.microsoft.mcas.ms/v2/")); |
| 195 | } |
| 196 | |
| 197 | #[test] |
| 198 | fn a_lookalike_host_is_not_the_login_page() { |
| 199 | assert!(is_login_url("https://login.live.com/oauth")); |
| 200 | assert!(!is_login_url("https://login.live.com.evil.example/")); |
| 201 | } |
| 202 | |
| 203 | #[test] |
| 204 | fn app_urls_need_an_exact_host() { |
| 205 | for url in [ |
| 206 | "https://teams.cloud.microsoft/v2/", |
| 207 | "https://teams.microsoft.com/v2/", |
| 208 | "https://teams.cloud.microsoft.mcas.ms/", |
| 209 | "https://teams.cloud.microsoft.mcas.ms/robots.txt", |
| 210 | ] { |
| 211 | assert!(App::Teams.is_app_url(url), "{url}"); |
| 212 | } |
| 213 | for url in [ |
| 214 | "https://teams.cloud.microsoft.evil.example/", |
| 215 | "https://teams.cloud.microsoftevil.example/", |
| 216 | "https://evil.mcas.ms/", |
| 217 | "http://teams.cloud.microsoft/", |
| 218 | "https://login.microsoftonline.com/", |
| 219 | "about:blank", |
| 220 | ] { |
| 221 | assert!(!App::Teams.is_app_url(url), "{url}"); |
| 222 | } |
| 223 | let look_alike: Vec<Target> = serde_json::from_str( |
| 224 | r#"[{"type":"page","url":"https://teams.cloud.microsoft.evil.example/","webSocketDebuggerUrl":"ws://x/1"}]"#, |
| 225 | ) |
| 226 | .unwrap(); |
| 227 | assert!(find_app_tab(&look_alike, App::Teams).is_none()); |
| 228 | } |
| 229 | |
| 230 | #[test] |
| 231 | fn detects_login_tab() { |
| 232 | let mut targets = targets(); |
| 233 | assert!(!has_login_tab(&targets)); |
| 234 | targets.push(Target { |
| 235 | kind: "page".into(), |
| 236 | url: "https://login.microsoftonline.com/common/oauth2".into(), |
| 237 | websocket_url: None, |
| 238 | }); |
| 239 | assert!(has_login_tab(&targets)); |
| 240 | } |
| 241 | } |