sk-desktop 0.3.0

Skathi's desktop integration: argv, shell, single instance, custom protocols and the tray
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//! The shell: open a URL or file with its handler, reveal a file, move a file to the trash.
//!
//! Validation is pure, runs on the frame thread and refuses synchronously; the OS call runs on one `sk-shell`
//! thread (a COM STA) that also does the `stat` in front of each call, so the frame thread never touches a
//! user path. With `SK_SHELL=record` nothing reaches the OS: each call is validated and stat'ed, then
//! settles successfully and logs `[shell] <op> <target> (recorded)`.

use std::path::{Path, PathBuf};
use std::sync::mpsc::{channel, Sender};
use std::sync::{Mutex, OnceLock};

use crate::Fail;

#[cfg(target_os = "windows")]
mod win;
#[cfg(target_os = "windows")]
use win as imp;

#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
use linux as imp;

#[cfg(not(any(target_os = "windows", target_os = "linux")))]
mod stub;
#[cfg(not(any(target_os = "windows", target_os = "linux")))]
use stub as imp;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Op {
    OpenExternal(String),
    OpenPath(PathBuf),
    ShowItem(PathBuf),
    Trash(PathBuf),
}

impl Op {
    fn verb(&self) -> &'static str {
        match self {
            Op::OpenExternal(_) => "openExternal",
            Op::OpenPath(_) => "openPath",
            Op::ShowItem(_) => "showItemInFolder",
            Op::Trash(_) => "trashItem",
        }
    }
    fn target(&self) -> String {
        match self {
            Op::OpenExternal(u) => u.clone(),
            Op::OpenPath(p) | Op::ShowItem(p) | Op::Trash(p) => p.display().to_string(),
        }
    }
}

/// Schemes CONTENT (`<a href>`, `window.open`) may hand to the OS. `file:`, custom protocols and
/// `ms-settings:` never pass; `appShell.openExternal` is the escape hatch for a URL the app trusts.
pub const CONTENT_SCHEMES: &[&str] = &["http", "https", "mailto"];

/// `https://x` → `Some("https")`, lowercased. A drive letter (`C:\x`) parses as a one-letter scheme.
pub fn scheme_of(url: &str) -> Option<String> {
    let colon = url.find(':')?;
    let s = &url[..colon];
    let mut chars = s.chars();
    let first = chars.next()?;
    if !first.is_ascii_alphabetic() {
        return None;
    }
    if !chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.')) {
        return None;
    }
    Some(s.to_ascii_lowercase())
}

pub fn validate_external(url: &str, content: bool) -> Result<String, Fail> {
    if url.is_empty() {
        return Err(Fail::type_error("openExternal: the URL is empty"));
    }
    if url.chars().any(|c| (c as u32) < 0x20 || c == '\u{7f}') {
        return Err(Fail::type_error(
            "openExternal: the URL contains a control character",
        ));
    }
    let Some(scheme) = scheme_of(url) else {
        return Err(Fail::type_error(format!(
            "openExternal: {url:?} is not an absolute URL"
        )));
    };
    if scheme.len() == 1 {
        return Err(Fail::type_error(format!(
            "openExternal: {url:?} is a file path — use appShell.openPath()"
        )));
    }
    if content {
        if !CONTENT_SCHEMES.contains(&scheme.as_str()) {
            return Err(Fail::new(
                "NotAllowedError",
                format!(
                    "a {scheme}: link was refused: content links only open http, https and mailto"
                ),
            )
            .code("ESCHEME"));
        }
    } else if scheme == "file" {
        return Err(Fail::type_error(format!(
            "openExternal: {url:?} is a file: URL — use appShell.openPath() for files"
        )));
    }
    Ok(url.to_string())
}

/// An absolute path, or why not. `\\?\` and UNC are fine; a relative path is a TypeError, not resolved against a cwd.
pub fn validate_path(path: &str, what: &str) -> Result<PathBuf, Fail> {
    if path.is_empty() {
        return Err(Fail::type_error(format!("{what}: the path is empty")));
    }
    if path.contains('\0') {
        return Err(Fail::type_error(format!("{what}: the path contains a NUL")));
    }
    let p = PathBuf::from(path);
    if !p.is_absolute() {
        return Err(Fail::type_error(format!(
            "{what}: {path:?} is not an absolute path (appStorage.getUserFolder() names the usual places)"
        )));
    }
    if alternate_stream(&p) {
        return Err(Fail::type_error(format!(
            "{what}: {path:?} names an alternate data stream (a `:` after the drive)"
        )));
    }
    Ok(p)
}

fn alternate_stream(p: &Path) -> bool {
    let s = p.to_string_lossy();
    let rest = s
        .strip_prefix(r"\\?\UNC\")
        .or_else(|| s.strip_prefix(r"\\?\"))
        .or_else(|| s.strip_prefix(r"\\.\"))
        .unwrap_or(&s);
    let rest = if rest.len() >= 2
        && rest.as_bytes()[1] == b':'
        && rest.as_bytes()[0].is_ascii_alphabetic()
    {
        &rest[2..]
    } else {
        rest
    };
    rest.contains(':')
}

/// Extensions `openPath` refuses without `{ allowExecutable: true }`: everything Windows will run, install,
/// or hand to a script host when "opened". Keeps a save-then-open flow from launching `report.pdf.exe`.
const EXECUTABLE_EXTS: &[&str] = &[
    "exe",
    "com",
    "bat",
    "cmd",
    "scr",
    "pif",
    "cpl",
    "msc",
    "msi",
    "msp",
    "mst",
    "lnk",
    "url",
    "scf",
    "hta",
    "ps1",
    "psm1",
    "psd1",
    "ps1xml",
    "ps2",
    "ps2xml",
    "psc1",
    "psc2",
    "vb",
    "vbs",
    "vbe",
    "js",
    "jse",
    "ws",
    "wsf",
    "wsc",
    "wsh",
    "reg",
    "inf",
    "ins",
    "isp",
    "jar",
    "chm",
    "hlp",
    "gadget",
    "application",
    "appref-ms",
    "settingcontent-ms",
    "msix",
    "msixbundle",
    "appx",
    "appxbundle",
    "appinstaller",
    "diagcab",
    "library-ms",
    "search-ms",
    "searchconnector-ms",
    "xbap",
    "msh",
    "msh1",
    "msh2",
    "mshxml",
    "msh1xml",
    "msh2xml",
    "sct",
    "shb",
    "xll",
    "website",
];

/// Would the shell RUN this rather than open it? The extension is read as Windows reads it: trailing dots
/// and spaces stripped, case-insensitive; `PATHEXT` only ever ADDS refusals.
pub fn is_executable(path: &Path, pathext: Option<&str>) -> bool {
    let name = path
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_default();
    let name = name.trim_end_matches(['.', ' ']);
    let Some(dot) = name.rfind('.') else {
        return false;
    };
    let ext = name[dot + 1..].to_ascii_lowercase();
    if ext.is_empty() {
        return false;
    }
    if EXECUTABLE_EXTS.contains(&ext.as_str()) {
        return true;
    }
    pathext.is_some_and(|list| {
        list.split(';')
            .map(|e| e.trim().trim_start_matches('.').to_ascii_lowercase())
            .any(|e| !e.is_empty() && e == ext)
    })
}

pub fn fail_from_io(e: &std::io::Error, what: &str, target: &str) -> Fail {
    use std::io::ErrorKind as K;
    let os = e.raw_os_error();
    let f = match e.kind() {
        K::NotFound => Fail::new(
            "NotFoundError",
            format!(
                "{what}: {target} — a requested file or directory could not be found at the time an operation was processed."
            ),
        )
        .code("ENOENT"),
        K::PermissionDenied => Fail::new("NotAllowedError", format!("{what}: {target} — access is denied")).code("EACCES"),
        _ => Fail::new("InvalidStateError", format!("{what}: {target} — {e}")).code("EIO"),
    };
    match os {
        Some(code) => f.os(code),
        None => f,
    }
}

pub fn start_open_external(url: &str, content: bool) -> Result<u32, Fail> {
    Ok(submit(Op::OpenExternal(validate_external(url, content)?)))
}

pub fn start_open_path(path: &str, allow_executable: bool) -> Result<u32, Fail> {
    let p = validate_path(path, "openPath")?;
    if !allow_executable && is_executable(&p, std::env::var("PATHEXT").ok().as_deref()) {
        return Err(Fail::new(
            "NotAllowedError",
            format!(
                "openPath refused {path:?}: it is a program or a script, and opening it would run it — \
                 pass {{ allowExecutable: true }} if that is intended"
            ),
        )
        .code("EEXECUTABLE"));
    }
    Ok(submit(Op::OpenPath(p)))
}

pub fn start_show_item(path: &str) -> Result<u32, Fail> {
    Ok(submit(Op::ShowItem(validate_path(
        path,
        "showItemInFolder",
    )?)))
}

pub fn start_trash(path: &str) -> Result<u32, Fail> {
    Ok(submit(Op::Trash(validate_path(path, "trashItem")?)))
}

struct Job {
    token: u32,
    op: Op,
}

pub fn recording() -> bool {
    static R: OnceLock<bool> = OnceLock::new();
    *R.get_or_init(|| std::env::var("SK_SHELL").is_ok_and(|v| v.eq_ignore_ascii_case("record")))
}

fn submit(op: Op) -> u32 {
    let token = crate::next_token();
    static TX: OnceLock<Mutex<Sender<Job>>> = OnceLock::new();
    let tx = TX.get_or_init(|| {
        let (tx, rx) = channel::<Job>();
        std::thread::Builder::new()
            .name("sk-shell".into())
            .spawn(move || {
                imp::thread_init();
                for job in rx {
                    let result = run(&job.op);
                    crate::settle(job.token, result.map(|()| "{}".to_string()));
                }
            })
            .expect("spawning the sk-shell thread");
        Mutex::new(tx)
    });
    let sent = tx
        .lock()
        .map(|tx| {
            tx.send(Job {
                token,
                op: op.clone(),
            })
            .is_ok()
        })
        .unwrap_or(false);
    if !sent {
        crate::settle(
            token,
            Err(Fail::new("InvalidStateError", "the shell thread is gone").code("EIO")),
        );
    }
    token
}

fn run(op: &Op) -> Result<(), Fail> {
    match op {
        Op::OpenExternal(_) => {}
        Op::OpenPath(p) | Op::ShowItem(p) | Op::Trash(p) => {
            if let Err(e) = std::fs::symlink_metadata(p) {
                return Err(fail_from_io(&e, op.verb(), &p.display().to_string()));
            }
        }
    }
    if recording() {
        crate::log_later(
            "shell",
            format!(
                "[shell] {} {} (recorded — SK_SHELL=record)",
                op.verb(),
                op.target()
            ),
        );
        return Ok(());
    }
    match op {
        Op::OpenExternal(url) => imp::open_external(url),
        Op::OpenPath(p) => imp::open_path(p),
        Op::ShowItem(p) => imp::show_item(p),
        Op::Trash(p) => imp::trash(p),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn schemes_parse_per_rfc_3986() {
        assert_eq!(scheme_of("https://x").as_deref(), Some("https"));
        assert_eq!(scheme_of("MAILTO:a@b").as_deref(), Some("mailto"));
        assert_eq!(scheme_of("web+app:x").as_deref(), Some("web+app"));
        assert_eq!(
            scheme_of("ms-settings:privacy").as_deref(),
            Some("ms-settings")
        );
        assert_eq!(scheme_of("1x:y"), None);
        assert_eq!(scheme_of("/relative"), None);
        assert_eq!(scheme_of("no colon"), None);
        assert_eq!(scheme_of(":x"), None);
        assert_eq!(scheme_of("a b:c"), None);
    }

    #[test]
    fn content_links_open_only_http_https_and_mailto() {
        assert!(validate_external("https://example.com/a?b", true).is_ok());
        assert!(validate_external("http://example.com", true).is_ok());
        assert!(validate_external("mailto:a@example.com", true).is_ok());
        for refused in [
            "file:///C:/Windows/System32/calc.exe",
            "myapp://x",
            "ms-settings:privacy",
            "javascript:alert(1)",
            "search-ms:query=x",
        ] {
            let e = validate_external(refused, true).unwrap_err();
            assert_eq!(e.name, "NotAllowedError", "{refused}");
            assert_eq!(e.code, Some("ESCHEME"));
        }
    }

    #[test]
    fn tier_two_takes_any_scheme_but_file() {
        assert!(validate_external("myapp://x", false).is_ok());
        assert!(validate_external("ms-settings:privacy", false).is_ok());
        assert_eq!(
            validate_external("file:///C:/x.txt", false)
                .unwrap_err()
                .name,
            "TypeError"
        );
        assert_eq!(
            validate_external("FILE:///C:/x.txt", false)
                .unwrap_err()
                .name,
            "TypeError"
        );
        assert_eq!(
            validate_external(r"C:\x.txt", false).unwrap_err().name,
            "TypeError"
        );
        assert_eq!(
            validate_external("relative/x", false).unwrap_err().name,
            "TypeError"
        );
        assert_eq!(validate_external("", false).unwrap_err().name, "TypeError");
        assert_eq!(
            validate_external("https://x/\na", false).unwrap_err().name,
            "TypeError"
        );
    }

    #[cfg(windows)]
    #[test]
    fn paths_must_be_absolute_and_name_no_stream() {
        assert!(validate_path(r"C:\Users\x\a.txt", "t").is_ok());
        assert!(validate_path(r"\\server\share\a.txt", "t").is_ok());
        assert!(validate_path(r"\\?\C:\very\long\a.txt", "t").is_ok());
        assert_eq!(validate_path("a.txt", "t").unwrap_err().name, "TypeError");
        assert_eq!(
            validate_path(r"..\a.txt", "t").unwrap_err().name,
            "TypeError"
        );
        assert_eq!(validate_path("", "t").unwrap_err().name, "TypeError");
        assert_eq!(
            validate_path(r"C:\x\report.txt:evil.exe", "t")
                .unwrap_err()
                .name,
            "TypeError"
        );
        assert_eq!(
            validate_path(r"\\?\C:\x\a.txt:s", "t").unwrap_err().name,
            "TypeError"
        );
    }

    #[cfg(unix)]
    #[test]
    fn paths_must_be_absolute() {
        assert!(validate_path("/home/x/a.txt", "t").is_ok());
        assert_eq!(validate_path("a.txt", "t").unwrap_err().name, "TypeError");
        assert_eq!(
            validate_path("../a.txt", "t").unwrap_err().name,
            "TypeError"
        );
        assert_eq!(validate_path("", "t").unwrap_err().name, "TypeError");
    }

    #[test]
    fn the_executable_guard_reads_extensions_the_way_windows_does() {
        let exe = |p: &str| is_executable(Path::new(p), None);
        for yes in [
            r"C:\a\setup.exe",
            r"C:\a\SETUP.EXE",
            r"C:\a\report.pdf.exe",
            r"C:\a\x.bat",
            r"C:\a\x.cmd",
            r"C:\a\x.lnk",
            r"C:\a\x.url",
            r"C:\a\x.ps1",
            r"C:\a\x.vbs",
            r"C:\a\x.js",
            r"C:\a\x.hta",
            r"C:\a\x.msi",
            r"C:\a\x.reg",
            r"C:\a\x.cpl",
            r"C:\a\x.scr",
            r"C:\a\x.settingcontent-ms",
            r"C:\a\x.appref-ms",
            r"C:\a\x.msix",
            r"C:\a\evil.exe.",
            r"C:\a\evil.exe ",
            r"C:\a\evil.exe. . ",
        ] {
            assert!(exe(yes), "{yes} must be refused");
        }
        for no in [
            r"C:\a\report.pdf",
            r"C:\a\notes.txt",
            r"C:\a\photo.PNG",
            r"C:\a\folder",
            r"C:\a\.gitignore",
            r"C:\a\archive.tar.gz",
            r"C:\a\exe",
        ] {
            assert!(!exe(no), "{no} must be allowed");
        }
        assert!(is_executable(
            Path::new(r"C:\a\x.foo"),
            Some(".COM;.EXE;.FOO")
        ));
        assert!(!is_executable(
            Path::new(r"C:\a\x.txt"),
            Some(".COM;.EXE;.FOO")
        ));
    }

    #[test]
    fn a_refused_call_answers_synchronously_without_a_token() {
        assert_eq!(
            start_open_external("file:///x", false).unwrap_err().name,
            "TypeError"
        );
        let setup = if cfg!(windows) {
            r"C:\x\setup.exe"
        } else {
            "/x/setup.exe"
        };
        let f = start_open_path(setup, false).unwrap_err();
        assert_eq!((f.name, f.code), ("NotAllowedError", Some("EEXECUTABLE")));
    }
}