| 1 | use url::Url; |
| 2 | |
| 3 | const PROXY_PATH: &str = "/urlp/v1/url/content"; |
| 4 | const PROXY_HOST_SUFFIX: &str = ".asyncgw.teams.microsoft.com"; |
| 5 | const GIPHY_HOST: &str = "giphy.com"; |
| 6 | const TEAMS_CDN_HOST: &str = "statics.teams.cdn.office.net"; |
| 7 | |
| 8 | /// The direct URL of a GIF or sticker image, `None` when `src` is not one of the known public hosts. |
| 9 | pub fn external_image_url(src: &str) -> Option<String> { |
| 10 | let url = Url::parse(src).ok()?; |
| 11 | if is_proxy(&url) { |
| 12 | let target = url |
| 13 | .query_pairs() |
| 14 | .find(|(name, _)| name == "url") |
| 15 | .map(|(_, value)| value.into_owned())?; |
| 16 | return direct_url(&Url::parse(&target).ok()?); |
| 17 | } |
| 18 | direct_url(&url) |
| 19 | } |
| 20 | |
| 21 | pub(crate) fn is_giphy_url(url: &str) -> bool { |
| 22 | Url::parse(url) |
| 23 | .ok() |
| 24 | .and_then(|url| url.host_str().map(is_giphy_host)) |
| 25 | .unwrap_or(false) |
| 26 | } |
| 27 | |
| 28 | fn is_giphy_host(host: &str) -> bool { |
| 29 | host == GIPHY_HOST |
| 30 | || host |
| 31 | .strip_suffix(GIPHY_HOST) |
| 32 | .is_some_and(|prefix| prefix.ends_with('.')) |
| 33 | } |
| 34 | |
| 35 | fn is_proxy(url: &Url) -> bool { |
| 36 | url.scheme() == "https" |
| 37 | && url.path() == PROXY_PATH |
| 38 | && url |
| 39 | .host_str() |
| 40 | .is_some_and(|host| host.ends_with(PROXY_HOST_SUFFIX)) |
| 41 | } |
| 42 | |
| 43 | fn direct_url(url: &Url) -> Option<String> { |
| 44 | let host = url.host_str()?; |
| 45 | let allowed = host == TEAMS_CDN_HOST || is_giphy_host(host); |
| 46 | (url.scheme() == "https" && allowed).then(|| url.to_string()) |
| 47 | } |
| 48 | |
| 49 | #[cfg(test)] |
| 50 | mod tests { |
| 51 | use super::*; |
| 52 | |
| 53 | #[test] |
| 54 | fn giphy_and_teams_cdn_pass() { |
| 55 | let giphy = "https://media0.giphy.com/media/abc/giphy.gif"; |
| 56 | assert_eq!(external_image_url(giphy).as_deref(), Some(giphy)); |
| 57 | let sticker = "https://statics.teams.cdn.office.net/evergreen-assets/stickerassets/clippy-250x250/Clippy_HelloThere.gif?v=5"; |
| 58 | assert_eq!(external_image_url(sticker).as_deref(), Some(sticker)); |
| 59 | assert!(external_image_url("https://giphy.com/x.gif").is_some()); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn proxy_urls_are_unwrapped() { |
| 64 | let wrapped = "https://de-prod.asyncgw.teams.microsoft.com/urlp/v1/url/content?url=https%3a%2f%2fstatics.teams.cdn.office.net%2fevergreen-assets%2fx.png%3fv%3d5"; |
| 65 | assert_eq!( |
| 66 | external_image_url(wrapped).as_deref(), |
| 67 | Some("https://statics.teams.cdn.office.net/evergreen-assets/x.png?v=5") |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn proxy_of_a_foreign_host_is_rejected() { |
| 73 | let wrapped = "https://de-prod.asyncgw.teams.microsoft.com/urlp/v1/url/content?url=https%3a%2f%2fevil.example%2fx.png"; |
| 74 | assert_eq!(external_image_url(wrapped), None); |
| 75 | } |
| 76 | |
| 77 | #[test] |
| 78 | fn insecure_and_lookalike_hosts_are_rejected() { |
| 79 | assert_eq!(external_image_url("http://media0.giphy.com/a.gif"), None); |
| 80 | assert_eq!( |
| 81 | external_image_url("https://giphy.com.evil.example/a.gif"), |
| 82 | None |
| 83 | ); |
| 84 | assert_eq!(external_image_url("https://evilgiphy.com/a.gif"), None); |
| 85 | assert_eq!( |
| 86 | external_image_url( |
| 87 | "https://graph.microsoft.com/v1.0/chats/1/messages/2/hostedContents/3/$value" |
| 88 | ), |
| 89 | None |
| 90 | ); |
| 91 | assert_eq!(external_image_url("not a url"), None); |
| 92 | } |
| 93 | } |