| 1 | use serde_json::{Value, json}; |
| 2 | use session::{App, IC3, Scope, Session}; |
| 3 | |
| 4 | use super::RealtimeConfig; |
| 5 | |
| 6 | pub const DEFAULT_TROUTER_HOST: &str = "go-eu.trouter.teams.microsoft.com"; |
| 7 | const DISCOVER_BODY: &str = include_str!("../../assets/discover_host.js"); |
| 8 | const TROUTER_DOMAIN: &str = ".trouter.teams.microsoft.com"; |
| 9 | |
| 10 | /// The bearer is sent to this host, so only Microsoft Trouter names pass. |
| 11 | pub fn is_trouter_host(host: &str) -> bool { |
| 12 | host.len() > TROUTER_DOMAIN.len() |
| 13 | && host.ends_with(TROUTER_DOMAIN) |
| 14 | && host.bytes().all(|byte| { |
| 15 | byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'.' || byte == b'-' |
| 16 | }) |
| 17 | && !host.starts_with('.') |
| 18 | && !host.contains("..") |
| 19 | } |
| 20 | |
| 21 | pub(super) fn ic3_scope() -> Scope { |
| 22 | Scope::new(IC3, "Teams.AccessAsUser.All") |
| 23 | } |
| 24 | |
| 25 | fn authz_scope() -> Scope { |
| 26 | Scope::new("https://api.spaces.skype.com", "authorization.readwrite") |
| 27 | } |
| 28 | |
| 29 | pub(super) async fn resolve(session: &Session, config: &RealtimeConfig) -> String { |
| 30 | if let Some(host) = config.host.as_deref().filter(|host| is_trouter_host(host)) { |
| 31 | return host.to_owned(); |
| 32 | } |
| 33 | if config.discover_host |
| 34 | && let Some(host) = discover(session).await |
| 35 | { |
| 36 | return host; |
| 37 | } |
| 38 | if is_trouter_host(&config.default_host) { |
| 39 | config.default_host.clone() |
| 40 | } else { |
| 41 | DEFAULT_TROUTER_HOST.to_owned() |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | async fn discover(session: &Session) -> Option<String> { |
| 46 | let answer = session |
| 47 | .run_with_token(App::Teams, &authz_scope(), DISCOVER_BODY, &json!({}), false) |
| 48 | .await |
| 49 | .ok()?; |
| 50 | host_from_answer(&answer) |
| 51 | } |
| 52 | |
| 53 | fn host_from_answer(answer: &Value) -> Option<String> { |
| 54 | answer |
| 55 | .get("host")? |
| 56 | .as_str() |
| 57 | .filter(|host| is_trouter_host(host)) |
| 58 | .map(str::to_owned) |
| 59 | } |
| 60 | |
| 61 | #[cfg(test)] |
| 62 | mod tests { |
| 63 | use super::*; |
| 64 | |
| 65 | #[test] |
| 66 | fn accepts_trouter_hosts() { |
| 67 | assert!(is_trouter_host("go-eu.trouter.teams.microsoft.com")); |
| 68 | assert!(is_trouter_host("go-eu2.trouter.teams.microsoft.com")); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn rejects_foreign_hosts() { |
| 73 | for host in [ |
| 74 | "trouter.teams.microsoft.com", |
| 75 | ".trouter.teams.microsoft.com", |
| 76 | "evil.com", |
| 77 | "go-eu.trouter.teams.microsoft.com.evil.com", |
| 78 | "go-eu.trouter.teams.microsoft.com:8443", |
| 79 | "a@go-eu.trouter.teams.microsoft.com", |
| 80 | "x/..trouter.teams.microsoft.com", |
| 81 | "Go-EU.trouter.teams.microsoft.com", |
| 82 | "", |
| 83 | ] { |
| 84 | assert!(!is_trouter_host(host), "{host}"); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn answer_without_valid_host_is_none() { |
| 90 | assert_eq!( |
| 91 | host_from_answer(&json!({"host": "go-eu.trouter.teams.microsoft.com"})).as_deref(), |
| 92 | Some("go-eu.trouter.teams.microsoft.com") |
| 93 | ); |
| 94 | assert_eq!(host_from_answer(&json!({"error": "authz_http_401"})), None); |
| 95 | assert_eq!(host_from_answer(&json!({"host": "evil.example"})), None); |
| 96 | } |
| 97 | } |