BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-base / src / open_link.rs

open_link.rs

open_link.rs 4.2 KiB Download Raw
1use gpui_kit::App;
2use url::Url;
3
4const WEB_SCHEMES: [&str; 2] = ["https", "http"];
5const PLAIN_SCHEMES: [&str; 5] = ["mailto", "tel", "sip", "callto", "msteams"];
6const OFFICE_SCHEMES: [&str; 8] = [
7 "ms-word",
8 "ms-excel",
9 "ms-powerpoint",
10 "ms-visio",
11 "ms-access",
12 "ms-project",
13 "ms-publisher",
14 "ms-infopath",
15];
16
17pub fn openable_url(raw: &str) -> Option<String> {
18 let url = Url::parse(raw.trim()).ok()?;
19 let scheme = url.scheme();
20 let allowed = if WEB_SCHEMES.contains(&scheme) {
21 url.host_str().is_some_and(|host| !host.is_empty())
22 } else if PLAIN_SCHEMES.contains(&scheme) {
23 url.host().is_none() && !url.path().contains('\\')
24 } else if scheme == "onenote" {
25 web_document(url.path()).is_some()
26 } else if OFFICE_SCHEMES.contains(&scheme) {
27 url.path()
28 .rsplit('|')
29 .next()
30 .and_then(web_document)
31 .is_some()
32 } else {
33 false
34 };
35 allowed.then(|| url.into())
36}
37
38fn web_document(document: &str) -> Option<Url> {
39 let document = Url::parse(document).ok()?;
40 (document.scheme() == "https" && document.host_str().is_some()).then_some(document)
41}
42
43pub fn is_openable_url(raw: &str) -> bool {
44 openable_url(raw).is_some()
45}
46
47pub fn open_url(cx: &mut App, raw: &str) {
48 match openable_url(raw) {
49 Some(url) => cx.open_url(&url),
50 None => crate::diagnostics::record(format!("refused to open a link: {}", scheme_of(raw))),
51 }
52}
53
54fn scheme_of(raw: &str) -> &str {
55 raw.trim()
56 .split_once(':')
57 .map_or("no scheme", |(scheme, _)| scheme)
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn web_mail_and_teams_links_open() {
66 for url in [
67 "https://contoso.sharepoint.com/x.pdf",
68 "http://intranet.example/x",
69 "mailto:someone@example.com",
70 "tel:+491234",
71 "msteams:/l/chat/0/0",
72 "onenote:https://contoso.sharepoint.com/n.one",
73 "ms-word:ofe|u|https://contoso.sharepoint.com/a.docx",
74 ] {
75 assert!(is_openable_url(url), "{url}");
76 }
77 }
78
79 #[test]
80 fn files_shares_and_local_handlers_do_not_open() {
81 for url in [
82 "\\\\203.0.113.5\\s\\x.lnk",
83 "file:///C:/Windows/System32/calc.exe",
84 "file://203.0.113.5/s/x.lnk",
85 "search-ms:query=x&crumb=location:\\\\203.0.113.5\\s",
86 "javascript:alert(1)",
87 "ms-msdt:/id x",
88 "ms-word:ofe|u|\\\\203.0.113.5\\s\\a.docx",
89 "ms-word:ofe|u|file:///C:/a.docx",
90 "ms-word:ofe|u|http://203.0.113.5/a.docx",
91 "mailto:\\\\203.0.113.5\\s",
92 "onenote:\\\\203.0.113.5\\s\\n.one",
93 "onenote://203.0.113.5/s/n.one",
94 "onenote:file:///C:/n.one",
95 "C:\\Windows\\System32\\calc.exe",
96 "",
97 ] {
98 assert!(!is_openable_url(url), "{url}");
99 }
100 }
101
102 #[test]
103 fn every_url_the_app_opens_goes_through_this_module() {
104 let crates = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("..");
105 let mut offenders = Vec::new();
106 visit(&crates, &mut offenders);
107 assert!(
108 offenders.is_empty(),
109 "use app_base::open_link::open_url instead of cx.open_url: {offenders:#?}"
110 );
111 }
112
113 fn visit(directory: &std::path::Path, offenders: &mut Vec<String>) {
114 let Ok(entries) = std::fs::read_dir(directory) else {
115 return;
116 };
117 for entry in entries.flatten() {
118 let path = entry.path();
119 if path.is_dir() {
120 if path.file_name().is_some_and(|name| name != "target") {
121 visit(&path, offenders);
122 }
123 continue;
124 }
125 if path.extension().is_none_or(|extension| extension != "rs")
126 || path.ends_with("app-base/src/open_link.rs")
127 {
128 continue;
129 }
130 let text = std::fs::read_to_string(&path).unwrap_or_default();
131 for (number, line) in text.lines().enumerate() {
132 if line.contains("cx.open_url(") {
133 offenders.push(format!("{}:{}", path.display(), number + 1));
134 }
135 }
136 }
137 }
138}