mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 19:51:26 -05:00
Resolve Windows File URIs in evalFile
This commit is contained in:
@@ -1337,6 +1337,25 @@ impl Pathname {
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a drive URI from a File object's string form to a native path. Only whole,
|
||||
/// decoded name components are accepted: escapes may not introduce separators or traversal.
|
||||
pub(crate) fn windows_drive_uri(text: &str) -> Result<Option<String>, ()> {
|
||||
let bytes = text.as_bytes();
|
||||
if bytes.len() < 3 || bytes[0] != b'/' || !bytes[1].is_ascii_alphabetic() || bytes[2] != b'/' {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut names = Vec::new();
|
||||
for component in text[3..].split('/') {
|
||||
let name = decode(component);
|
||||
if name.is_empty() || name == "." || name == ".." || name.contains(['/', '\\', ':', '\0']) {
|
||||
return Err(());
|
||||
}
|
||||
names.push(name);
|
||||
}
|
||||
let root = Pathname { root: Root::Drive(bytes[1].to_ascii_uppercase() as char), names };
|
||||
Ok(Some(root.fs()))
|
||||
}
|
||||
|
||||
/// `s` with every character but letters, digits and `/-_.!~*'()` written as `%xx` escapes of its
|
||||
/// UTF-8 bytes (RFC 2396).
|
||||
fn encode(s: &str) -> String {
|
||||
@@ -2801,6 +2820,18 @@ mod tests {
|
||||
Pathname::parse(text, &base, &home, windows, true)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn windows_drive_uri_decodes_names_without_changing_components() {
|
||||
assert_eq!(super::windows_drive_uri("/c/a%20b/file.jsx"), Ok(Some(r"C:\a b\file.jsx".to_string())));
|
||||
assert_eq!(super::windows_drive_uri("/c/a%2520b/file.jsx"), Ok(Some(r"C:\a%20b\file.jsx".to_string())));
|
||||
for path in ["/c/a/%2e%2e/file.jsx", "/c/a%2fb/file.jsx", "/c/a%5cb/file.jsx", "/c/a%3ab/file.jsx", "/c//file.jsx"] {
|
||||
assert_eq!(super::windows_drive_uri(path), Err(()), "{path}");
|
||||
}
|
||||
assert_eq!(super::windows_drive_uri("//server/share/file.jsx"), Ok(None));
|
||||
assert_eq!(super::windows_drive_uri(r"C:\a\file.jsx"), Ok(None));
|
||||
assert_eq!(super::windows_drive_uri("a/file.jsx"), Ok(None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn windows_paths_take_both_separators_drives_and_uri_names() {
|
||||
let f = p(r"C:\art\one two.txt", r"C:\work", true);
|
||||
|
||||
@@ -526,7 +526,13 @@ impl Files {
|
||||
let current = self.frames.last().map(|f| f.shown.clone()).unwrap_or_else(|| Arc::from(""));
|
||||
let fail = |kind, message: String| Failure { kind, message, index: 0, file: current.clone(), line: 0, column: 0 };
|
||||
let dir = module_dir.or_else(|| self.frames.last().and_then(|f| f.dir.clone()));
|
||||
let (real, bytes) = self.open(path, dir.as_deref(), false).map_err(|(kind, message)| fail(kind, message))?;
|
||||
let native = if cfg!(windows) {
|
||||
crate::script::files::windows_drive_uri(path)
|
||||
.map_err(|()| fail(FailKind::Error, format!("script file read refused: {path}")))?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let (real, bytes) = self.open(native.as_deref().unwrap_or(path), dir.as_deref(), false).map_err(|(kind, message)| fail(kind, message))?;
|
||||
let text = self.admit(&real, bytes, path).map_err(|(kind, message)| fail(kind, message))?;
|
||||
let shown = display(&real);
|
||||
self.chain.push(real.clone());
|
||||
|
||||
@@ -301,6 +301,29 @@ fn files_are_checked_before_they_are_read() {
|
||||
assert!(s.execute("script.run", &path(&dir.join("folder.js").to_string_lossy())).is_err());
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn eval_file_accepts_file_uri_with_spaces_but_refuses_a_file_outside_the_script_folder() {
|
||||
let dir = fresh_folder("eval uri with spaces");
|
||||
let outside = fresh_folder("eval uri outside");
|
||||
std::fs::write(dir.join("more file.jsx"), "'inside';").unwrap();
|
||||
let other = outside.join("outside.jsx");
|
||||
std::fs::write(&other, "'outside';").unwrap();
|
||||
let main = dir.join("main.js");
|
||||
let source = format!(
|
||||
"vc.log($.evalFile(File($.fileName).parent + '/more%20file.jsx'));\n\
|
||||
try {{ $.evalFile(File({:?}).absoluteURI); }} catch (e) {{ vc.log(String(e)); }}",
|
||||
other.to_string_lossy()
|
||||
);
|
||||
std::fs::write(&main, source).unwrap();
|
||||
let mut session = two_docs();
|
||||
let result = session.execute("script.run", &json!({"path": main.to_string_lossy()})).unwrap();
|
||||
assert_eq!(result["log"][0], "inside");
|
||||
assert!(result["log"][1].as_str().unwrap().contains("script file read refused"));
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
let _ = std::fs::remove_dir_all(outside);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_js_script_reads_ecmascript_2015_and_a_jsx_script_does_not() {
|
||||
let dir = fresh_folder("es2015");
|
||||
|
||||
Reference in New Issue
Block a user