Skip to content

Visual Studio Code, rebuilt in Rust

Rust
3.3 KB
Raw Blame
use super::{is_path_allowed_by_unc_host_allowlist, unc_access_error, unc_env, unc_host};

/// utilityProcess.ts joins the allowlist with backslashes, which a host name cannot hold.
#[test]
fn a_utility_process_gets_the_allowlist_or_the_disabled_checks() {
    let hosts = ["a".to_owned(), "b".to_owned()];
    assert_eq!(unc_env(false, &hosts), ("NODE_UNC_HOST_ALLOWLIST".to_owned(), r"a\b".to_owned()));
    assert_eq!(unc_env(true, &hosts), ("NODE_DISABLE_UNC_ACCESS_CHECKS".to_owned(), "1".to_owned()));
}

/// base/test/node/unc.test.ts `getUNCHost`.
#[test]
fn get_unc_host() {
    assert_eq!(unc_host("/"), None);
    assert_eq!(unc_host("/foo"), None);
    assert_eq!(unc_host("c:"), None);
    assert_eq!(unc_host(r"c:\"), None);
    assert_eq!(unc_host(r"c:\foo"), None);
    assert_eq!(unc_host(r"c:\foo\\server\path"), None);
    assert_eq!(unc_host(r"\"), None);
    assert_eq!(unc_host(r"\\"), None);
    assert_eq!(unc_host(r"\\localhost"), None);
    assert_eq!(unc_host(r"\\localhost\"), Some("localhost"));
    assert_eq!(unc_host(r"\\localhost\a"), Some("localhost"));
    assert_eq!(unc_host(r"\\."), None);
    assert_eq!(unc_host(r"\\?"), None);
    assert_eq!(unc_host(r"\\.\localhost"), Some("."));
    assert_eq!(unc_host(r"\\?\localhost"), Some("?"));
    assert_eq!(unc_host(r"\\.\UNC\localhost"), Some("."));
    assert_eq!(unc_host(r"\\?\UNC\localhost"), Some("?"));
    assert_eq!(unc_host(r"\\.\UNC\localhost\"), Some("localhost"));
    assert_eq!(unc_host(r"\\?\UNC\localhost\"), Some("localhost"));
    assert_eq!(unc_host(r"\\.\UNC\localhost\a"), Some("localhost"));
    assert_eq!(unc_host(r"\\?\UNC\localhost\a"), Some("localhost"));
}

/// base/test/node/unc.test.ts `isPathAllowedByUNCHostAllowlist`.
#[test]
fn is_path_allowed_by_unc_host_allowlist_matches_upstream() {
    let allowed_hosts = ["allowed-host".to_owned()];
    let cases = [
        (r"\\allowed-host\share\a.png", true),
        (r"\\ALLOWED-HOST\share\a.png", true),
        (r"\\?\UNC\allowed-host\share\a.png", true),
        (r"\\other-host\share\a.png", false),
        (r"\\?\UNC\other-host\share\a.png", false),
        (r"\\.\UNC\other-host\share\a.png", false),
        (r"\\other-host.png", false),
        ("//other-host/share/a.png", false),
        (r"\\?\c:\foo\a.png", false),
        (r"c:\foo\a.png", true),
        (r"c:\foo\\other-host\a.png", true),
        (r"\foo\a.png", true),
        ("/foo/a.png", true),
    ];
    for (path, allowed) in cases {
        assert_eq!(is_path_allowed_by_unc_host_allowlist(path, &allowed_hosts), allowed, "{path}");
    }
}

#[test]
fn the_access_error_names_the_host_and_takes_slashes_as_separators() {
    let allowed_hosts = ["allowed-host".to_owned()];
    assert_eq!(unc_access_error("//Allowed-Host/share/a.png", &allowed_hosts), None);
    assert_eq!(unc_access_error("//other-host/share/a.png", &allowed_hosts).as_deref(), Some("UNC host 'other-host' access is not allowed"));
    assert_eq!(unc_access_error(r"\\?\UNC\other-host\share", &allowed_hosts).as_deref(), Some("UNC host 'other-host' access is not allowed"));
    assert_eq!(unc_access_error(r"\\.\pipe\x", &allowed_hosts).as_deref(), Some("UNC host '.' access is not allowed"));
    assert_eq!(unc_access_error(r"c:\foo\a.png", &allowed_hosts), None);
    assert_eq!(unc_access_error("/foo/a.png", &allowed_hosts), None);
}