| 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 | fn is_proxy(url: &Url) -> bool { |
| 22 | url.scheme() == "https" |
| 23 | && url.path() == PROXY_PATH |
| 24 | && url |
| 25 | .host_str() |
| 26 | .is_some_and(|host| host.ends_with(PROXY_HOST_SUFFIX)) |
| 27 | } |
| 28 | |
| 29 | fn direct_url(url: &Url) -> Option<String> { |
| 30 | let host = url.host_str()?; |
| 31 | let allowed = host == TEAMS_CDN_HOST |
| 32 | || host == GIPHY_HOST |
| 33 | || host |
| 34 | .strip_suffix(GIPHY_HOST) |
| 35 | .is_some_and(|prefix| prefix.ends_with('.')); |
| 36 | (url.scheme() == "https" && allowed).then(|| url.to_string()) |
| 37 | } |
| 38 | |
| 39 | #[cfg(test)] |
| 40 | mod tests { |
| 41 | use super::*; |
| 42 | |
| 43 | #[test] |
| 44 | fn giphy_and_teams_cdn_pass() { |
| 45 | let giphy = "https://media0.giphy.com/media/abc/giphy.gif"; |
| 46 | assert_eq!(external_image_url(giphy).as_deref(), Some(giphy)); |
| 47 | let sticker = "https://statics.teams.cdn.office.net/evergreen-assets/stickerassets/clippy-250x250/Clippy_HelloThere.gif?v=5"; |
| 48 | assert_eq!(external_image_url(sticker).as_deref(), Some(sticker)); |
| 49 | assert!(external_image_url("https://giphy.com/x.gif").is_some()); |
| 50 | } |
| 51 | |
| 52 | #[test] |
| 53 | fn proxy_urls_are_unwrapped() { |
| 54 | 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"; |
| 55 | assert_eq!( |
| 56 | external_image_url(wrapped).as_deref(), |
| 57 | Some("https://statics.teams.cdn.office.net/evergreen-assets/x.png?v=5") |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn proxy_of_a_foreign_host_is_rejected() { |
| 63 | let wrapped = "https://de-prod.asyncgw.teams.microsoft.com/urlp/v1/url/content?url=https%3a%2f%2fevil.example%2fx.png"; |
| 64 | assert_eq!(external_image_url(wrapped), None); |
| 65 | } |
| 66 | |
| 67 | #[test] |
| 68 | fn insecure_and_lookalike_hosts_are_rejected() { |
| 69 | assert_eq!(external_image_url("http://media0.giphy.com/a.gif"), None); |
| 70 | assert_eq!( |
| 71 | external_image_url("https://giphy.com.evil.example/a.gif"), |
| 72 | None |
| 73 | ); |
| 74 | assert_eq!(external_image_url("https://evilgiphy.com/a.gif"), None); |
| 75 | assert_eq!( |
| 76 | external_image_url( |
| 77 | "https://graph.microsoft.com/v1.0/chats/1/messages/2/hostedContents/3/$value" |
| 78 | ), |
| 79 | None |
| 80 | ); |
| 81 | assert_eq!(external_image_url("not a url"), None); |
| 82 | } |
| 83 | } |