diff --git a/crates/viport-hle/src/modules/media.rs b/crates/viport-hle/src/modules/media.rs index 2acf5e9..a7475df 100644 --- a/crates/viport-hle/src/modules/media.rs +++ b/crates/viport-hle/src/modules/media.rs @@ -51,13 +51,25 @@ fn local(url: &[u16]) -> Result, (String, String)> { b.len() >= 2 && b[0].is_ascii_alphabetic() && b[1] == b':' }; let host_of = |rest: &str| rest.split(['/', '\\']).next().unwrap_or("").to_string(); + // Two leading separators, of either kind: to the host, a share. + let share = |s: &str| { + let b = s.as_bytes(); + b.len() >= 2 && b[..2].iter().all(|c| b"/\\".contains(c)) + }; let path = if let Some(rest) = text.strip_prefix(r"\\?\") { if !drive(rest) { return Err(("unc".into(), host_of(rest.trim_start_matches("UNC\\")))); } text.clone() - } else if let Some(rest) = text.strip_prefix(r"\\").or_else(|| text.strip_prefix("//")) { - return Err(("unc".into(), host_of(rest))); + } else if let Some(rest) = text.strip_prefix(r"\??\") { + // The same prefix as the object manager spells it; the host's file + // calls take it too. + if !drive(rest) { + return Err(("unc".into(), host_of(rest.trim_start_matches("UNC\\")))); + } + return Ok(with_nul(url)); + } else if share(&text) { + return Err(("unc".into(), host_of(&text[2..]))); } else if drive(&text) { text.clone() } else if let Some((scheme, rest)) = text.split_once(':').filter(|(s, _)| { @@ -70,18 +82,24 @@ fn local(url: &[u16]) -> Result, (String, String)> { if scheme != "file" { return Err((scheme, authority.map(host_of).unwrap_or_default())); } - let path = match authority { + let (path, this_machine) = match authority { Some(rest) => { let host = host_of(rest); if !host.is_empty() && !host.eq_ignore_ascii_case("localhost") { return Err((scheme, host)); } - &rest[host.len()..] + (&rest[host.len()..], !host.is_empty()) } - None => rest, + None => (rest, false), }; + let whole = percent_decode(path); let path = percent_decode(path.trim_start_matches(['/', '\\'])).replace('/', "\\"); if !drive(&path) { + // With no host named, a path that starts with two separators + // names one: `file:////server/share` is `\\server\share`. + if !this_machine && share(&whole) { + return Err((scheme, host_of(whole.trim_start_matches(['/', '\\'])))); + } // A local file without a drive: the host resolves it as is. return Ok(with_nul(url)); } @@ -689,6 +707,86 @@ mod tests { assert!([a, b, c, d].iter().all(|out| out.is_null())); } + /// Other spellings of `\\server\share\a.mp4`, as paths and as `file:` + /// URLs without a host. + const SHARE_PATHS: [&str; 3] = [ + r"/\server\share\a.mp4", + r"\/server/share/a.mp4", + r"\??\UNC\server\share\a.mp4", + ]; + const SHARE_URLS: [&str; 5] = [ + "file:////server/share/a.mp4", + "file://///server/share/a.mp4", + r"file:\\server\share\a.mp4", + "file:/%5Cserver/share/a.mp4", + "file:///%5C%5Cserver/share/a.mp4", + ]; + + #[test] + fn shares_are_refused_however_they_are_spelled() { + let asked = |url: &str| local(&wide(url)).map(|v| text(&v)); + for path in SHARE_PATHS { + assert_eq!(asked(path), Err(("unc".into(), "server".into())), "{path}"); + } + for url in SHARE_URLS { + assert_eq!(asked(url), Err(("file".into(), "server".into())), "{url}"); + } + // Local files that look alike still pass: one leading separator, a + // drive after the slashes, a drive after the object prefix, and the + // path of a URL that names this machine. + for local_file in [ + r"\movies\a.mp4", + "file:///movies/a.mp4", + r"\??\C:\Windows\a.wav", + "file://localhost//server/share/a.mp4", + ] { + assert_eq!(asked(local_file), Ok(local_file.into()), "{local_file}"); + } + assert_eq!( + asked("file:////C:/Windows/a.wav"), + Ok(r"C:\Windows\a.wav".into()) + ); + } + + /// What the host makes of those spellings: the share, for each. + #[cfg(windows)] + #[test] + fn the_host_reads_those_spellings_as_shares() { + for path in &SHARE_PATHS[..2] { + assert_eq!( + std::path::absolute(path).unwrap().to_str(), + Some(r"\\server\share\a.mp4"), + "{path}" + ); + } + // The object prefix reaches the host's file calls as it is. + let windows = std::env::var("SystemRoot").unwrap(); + assert!(std::fs::metadata(format!(r"\??\{windows}")).is_ok()); + + let from_url = files::host!("shlwapi.dll", "PathCreateFromUrlW": + extern "system" fn(*const u16, *mut u16, *mut u32, u32) -> HRESULT); + let host_path = |url: &str| { + let url: Vec = url.encode_utf16().chain([0]).collect(); + let (mut path, mut n) = ([0u16; 260], 260); + assert_eq!(from_url(url.as_ptr(), path.as_mut_ptr(), &mut n, 0), S_OK); + String::from_utf16_lossy(&path[..n as usize]) + }; + for url in SHARE_URLS { + let path = host_path(url); + assert!( + path.starts_with(r"\\") && path.ends_with(r"server\share\a.mp4"), + "{url}: {path}" + ); + } + // And of the ones that pass: local paths. + assert_eq!( + host_path("file://localhost//server/share/a.mp4"), + r"\server\share\a.mp4" + ); + assert_eq!(host_path("file:////C:/Windows/a.wav"), r"C:\Windows\a.wav"); + assert_eq!(host_path("file:///movies/a.mp4"), r"\movies\a.mp4"); + } + #[test] fn interface_ids_are_the_resolvers() { assert_eq!(&IID_IUNKNOWN[8..], &[0xC0, 0, 0, 0, 0, 0, 0, 0x46]);