use gpui_kit::App; use url::Url; const WEB_SCHEMES: [&str; 2] = ["https", "http"]; const PLAIN_SCHEMES: [&str; 5] = ["mailto", "tel", "sip", "callto", "msteams"]; const OFFICE_SCHEMES: [&str; 8] = [ "ms-word", "ms-excel", "ms-powerpoint", "ms-visio", "ms-access", "ms-project", "ms-publisher", "ms-infopath", ]; pub fn openable_url(raw: &str) -> Option { let url = Url::parse(raw.trim()).ok()?; let scheme = url.scheme(); let allowed = if WEB_SCHEMES.contains(&scheme) { url.host_str().is_some_and(|host| !host.is_empty()) } else if PLAIN_SCHEMES.contains(&scheme) { url.host().is_none() && !url.path().contains('\\') } else if scheme == "onenote" { web_document(url.path()).is_some() } else if OFFICE_SCHEMES.contains(&scheme) { url.path() .rsplit('|') .next() .and_then(web_document) .is_some() } else { false }; allowed.then(|| url.into()) } fn web_document(document: &str) -> Option { let document = Url::parse(document).ok()?; (document.scheme() == "https" && document.host_str().is_some()).then_some(document) } pub fn is_openable_url(raw: &str) -> bool { openable_url(raw).is_some() } pub fn open_url(cx: &mut App, raw: &str) { match openable_url(raw) { Some(url) => cx.open_url(&url), None => crate::diagnostics::record(format!("refused to open a link: {}", scheme_of(raw))), } } fn scheme_of(raw: &str) -> &str { raw.trim() .split_once(':') .map_or("no scheme", |(scheme, _)| scheme) } #[cfg(test)] mod tests { use super::*; #[test] fn web_mail_and_teams_links_open() { for url in [ "https://contoso.sharepoint.com/x.pdf", "http://intranet.example/x", "mailto:someone@example.com", "tel:+491234", "msteams:/l/chat/0/0", "onenote:https://contoso.sharepoint.com/n.one", "ms-word:ofe|u|https://contoso.sharepoint.com/a.docx", ] { assert!(is_openable_url(url), "{url}"); } } #[test] fn files_shares_and_local_handlers_do_not_open() { for url in [ "\\\\203.0.113.5\\s\\x.lnk", "file:///C:/Windows/System32/calc.exe", "file://203.0.113.5/s/x.lnk", "search-ms:query=x&crumb=location:\\\\203.0.113.5\\s", "javascript:alert(1)", "ms-msdt:/id x", "ms-word:ofe|u|\\\\203.0.113.5\\s\\a.docx", "ms-word:ofe|u|file:///C:/a.docx", "ms-word:ofe|u|http://203.0.113.5/a.docx", "mailto:\\\\203.0.113.5\\s", "onenote:\\\\203.0.113.5\\s\\n.one", "onenote://203.0.113.5/s/n.one", "onenote:file:///C:/n.one", "C:\\Windows\\System32\\calc.exe", "", ] { assert!(!is_openable_url(url), "{url}"); } } #[test] fn every_url_the_app_opens_goes_through_this_module() { let crates = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(".."); let mut offenders = Vec::new(); visit(&crates, &mut offenders); assert!( offenders.is_empty(), "use app_base::open_link::open_url instead of cx.open_url: {offenders:#?}" ); } fn visit(directory: &std::path::Path, offenders: &mut Vec) { let Ok(entries) = std::fs::read_dir(directory) else { return; }; for entry in entries.flatten() { let path = entry.path(); if path.is_dir() { if path.file_name().is_some_and(|name| name != "target") { visit(&path, offenders); } continue; } if path.extension().is_none_or(|extension| extension != "rs") || path.ends_with("app-base/src/open_link.rs") { continue; } let text = std::fs::read_to_string(&path).unwrap_or_default(); for (number, line) in text.lines().enumerate() { if line.contains("cx.open_url(") { offenders.push(format!("{}:{}", path.display(), number + 1)); } } } } }