BlueGit

ict rusty-teams

Files

rusty-teams / crates / session / src / app.rs

app.rs

app.rs 7.7 KiB Download Raw
1use serde::Deserialize;
2
3/// Defender for Cloud Apps (Conditional Access App Control) proxies the app on `<host>.mcas.ms`.
4pub const MCAS_SUFFIX: &str = ".mcas.ms";
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum App {
8 Teams,
9}
10
11impl 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.
69pub 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
78impl 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
86const LOGIN_ORIGINS: [&str; 2] = [
87 "https://login.microsoftonline.com",
88 "https://login.live.com",
89];
90
91#[derive(Debug, Clone, Deserialize)]
92pub 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
101pub 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
107pub fn is_login_url(url: &str) -> bool {
108 LOGIN_ORIGINS.iter().any(|origin| url.starts_with(origin))
109}
110
111pub fn has_login_tab(targets: &[Target]) -> bool {
112 targets
113 .iter()
114 .any(|target| target.kind == "page" && is_login_url(&target.url))
115}
116
117#[cfg(test)]
118mod tests {
119 use super::*;
120
121 fn targets() -> Vec<Target> {
122 serde_json::from_str(
123 r#"[
124 {"type":"service_worker","url":"https://teams.cloud.microsoft/sw.js","webSocketDebuggerUrl":"ws://x/1"},
125 {"type":"page","url":"https://outlook.cloud.microsoft/mail/","webSocketDebuggerUrl":"ws://x/2"},
126 {"type":"page","url":"https://teams.cloud.microsoft/v2/","webSocketDebuggerUrl":"ws://x/3"},
127 {"type":"page","url":"about:blank"}
128 ]"#,
129 )
130 .unwrap()
131 }
132
133 #[test]
134 fn finds_page_tabs_per_app() {
135 let targets = targets();
136 assert_eq!(
137 find_app_tab(&targets, App::Teams)
138 .unwrap()
139 .websocket_url
140 .as_deref(),
141 Some("ws://x/3")
142 );
143 }
144
145 #[test]
146 fn ignores_non_page_targets_and_missing_tabs() {
147 let only_worker = &targets()[..1];
148 assert!(find_app_tab(only_worker, App::Teams).is_none());
149 }
150
151 #[test]
152 fn parked_tabs_are_found_by_origin_and_recognised() {
153 let parked: Vec<Target> = serde_json::from_str(
154 r#"[{"type":"page","url":"https://teams.cloud.microsoft/robots.txt","webSocketDebuggerUrl":"ws://x/9"},
155 {"type":"page","url":"https://outlook.cloud.microsoft/owa/favicon.ico","webSocketDebuggerUrl":"ws://x/8"}]"#,
156 )
157 .unwrap();
158 let teams = find_app_tab(&parked, App::Teams).unwrap();
159 assert!(App::Teams.is_park_url(&teams.url));
160 assert!(!App::Teams.is_park_url("https://teams.cloud.microsoft/v2/"));
161 }
162
163 #[test]
164 fn mcas_urls_go_through_the_proxy_and_both_park_urls_count() {
165 assert_eq!(
166 App::Teams.start_url(true),
167 "https://teams.cloud.microsoft.mcas.ms/"
168 );
169 assert_eq!(
170 App::Teams.park_url(true),
171 "https://teams.cloud.microsoft.mcas.ms/robots.txt"
172 );
173 assert_eq!(
174 App::Teams.start_url(false),
175 "https://teams.cloud.microsoft/"
176 );
177 assert_eq!(
178 App::Teams.start_url_like("https://teams.cloud.microsoft.mcas.ms/robots.txt"),
179 App::Teams.start_url(true)
180 );
181 assert_eq!(
182 App::Teams.start_url_like("https://teams.cloud.microsoft/robots.txt"),
183 App::Teams.start_url(false)
184 );
185 assert!(!is_mcas_url("http://teams.cloud.microsoft.mcas.ms/"));
186 assert!(!is_mcas_url("https://mcas.ms/"));
187 assert!(App::Teams.is_park_url("https://teams.cloud.microsoft.mcas.ms/robots.txt"));
188 assert!(App::Teams.is_park_url("https://teams.cloud.microsoft/robots.txt"));
189 assert!(!App::Teams.is_park_url("https://teams.cloud.microsoft.mcas.ms/v2/"));
190 }
191
192 #[test]
193 fn app_urls_need_an_exact_host() {
194 for url in [
195 "https://teams.cloud.microsoft/v2/",
196 "https://teams.microsoft.com/v2/",
197 "https://teams.cloud.microsoft.mcas.ms/",
198 "https://teams.cloud.microsoft.mcas.ms/robots.txt",
199 ] {
200 assert!(App::Teams.is_app_url(url), "{url}");
201 }
202 for url in [
203 "https://teams.cloud.microsoft.evil.example/",
204 "https://teams.cloud.microsoftevil.example/",
205 "https://evil.mcas.ms/",
206 "http://teams.cloud.microsoft/",
207 "https://login.microsoftonline.com/",
208 "about:blank",
209 ] {
210 assert!(!App::Teams.is_app_url(url), "{url}");
211 }
212 let look_alike: Vec<Target> = serde_json::from_str(
213 r#"[{"type":"page","url":"https://teams.cloud.microsoft.evil.example/","webSocketDebuggerUrl":"ws://x/1"}]"#,
214 )
215 .unwrap();
216 assert!(find_app_tab(&look_alike, App::Teams).is_none());
217 }
218
219 #[test]
220 fn detects_login_tab() {
221 let mut targets = targets();
222 assert!(!has_login_tab(&targets));
223 targets.push(Target {
224 kind: "page".into(),
225 url: "https://login.microsoftonline.com/common/oauth2".into(),
226 websocket_url: None,
227 });
228 assert!(has_login_tab(&targets));
229 }
230}