Merge pull request #10483 from dotCooCoo/fix/windows-evalfile-uri

Scripts: $.evalFile reads the URI paths File.parent gives, on Windows too
This commit is contained in:
.CooCoo authored and GitHub committed 2026-12-02 02:02:31 -08:00
commit d753c48d2e
6 files changed
+85 -8

No files matched your search

+1 -1
View File
@@ -1352,7 +1352,7 @@ fn encode(s: &str) -> String {
}
/// `s` with its `%xx` escapes read as UTF-8 bytes; `s` as it is when they do not make UTF-8.
fn decode(s: &str) -> String {
pub(crate) fn decode(s: &str) -> String {
let b = s.as_bytes();
let mut out = Vec::with_capacity(b.len());
let mut i = 0;
+45 -4
View File
@@ -359,6 +359,32 @@ fn has_parent(p: &str) -> bool {
p.split(['/', '\\']).any(|name| name == "..")
}
/// The paths `value` can name in a `File`'s URI notation, as `File.parent` and `File.fullName` give
/// them, other than `value` as written: on a platform that reads drive letters (`windows`), `/c/…`
/// is the drive path `C:/…`, and on every platform `%20` and the other escapes are decoded. Only a
/// path that starts with one `/` has them; `$.evalFile` tries them after `value` as written
/// misses. A `//host/share` path, and a form with a `..` name, is left out.
pub fn uri_forms(value: &str, windows: bool) -> Vec<String> {
if !value.starts_with('/') || value.starts_with("//") {
return Vec::new();
}
let drive = value.strip_prefix('/').filter(|_| windows && !value.starts_with("//")).and_then(|rest| {
let (first, after) = rest.split_once('/').unwrap_or((rest, ""));
let letter = first.as_bytes();
(letter.len() == 1 && letter[0].is_ascii_alphabetic()).then(|| format!("{}:/{after}", letter[0].to_ascii_uppercase() as char))
});
let mut forms = Vec::new();
for form in drive.iter().map(String::as_str).chain(drive.is_none().then_some(value)) {
let decoded = crate::script::files::decode(form);
for f in [decoded.as_str(), form] {
if f != value && !has_parent(f) && !(windows && f.replace('\\', "/").starts_with("//")) && !forms.iter().any(|g| g == f) {
forms.push(f.to_string());
}
}
}
forms
}
/// A real path as people read it.
fn display(path: &Path) -> Arc<str> {
Arc::from(crate::cmd::findfiles::plain(path.to_path_buf()).to_string_lossy().as_ref())
@@ -575,13 +601,18 @@ impl Files {
let refused = || (FailKind::Error, format!("script file read refused: {value}"));
// A script given as text has no folder, and so no file it may read.
let Some(dir) = dir.filter(|_| !has_parent(value)) else { return Err(refused()) };
// Each candidate, and whether the scope's refusal passes it over.
let mut candidates: Vec<(Option<PathBuf>, bool)> = vec![(Some(dir.join(path)), false)];
// Each candidate, and whether the scope's refusal passes it over. The path as written comes
// first; for `$.evalFile` only, when it misses, fails or is refused, the forms of a path in
// URI notation are tried, and when none of them is read either, the error of the path as written stands.
let forms = if search { Vec::new() } else { uri_forms(value, cfg!(windows)) };
let mut literal_error = None;
let mut candidates: Vec<(Option<PathBuf>, bool)> = vec![(Some(dir.join(path)), !forms.is_empty())];
candidates.extend(forms.into_iter().map(|f| (Some(dir.join(f)), true)));
if search && !path.is_absolute() {
candidates.extend(self.include_path.iter().map(|p| ((!has_parent(p)).then(|| dir.join(p).join(path)), false)));
candidates.extend(self.jsinclude.iter().filter(|p| p.is_absolute()).map(|p| (Some(p.join(path)), true)));
}
for (candidate, passed_over_when_refused) in candidates {
for (i, (candidate, passed_over_when_refused)) in candidates.into_iter().enumerate() {
let Some(candidate) = candidate else { return Err(refused()) };
self.probes += 1;
if self.probes > MAX_PROBES {
@@ -590,12 +621,22 @@ impl Files {
match self.scope.read(&candidate, crate::script::MAX_SOURCE_BYTES) {
Ok(found) => return Ok(found),
Err(ScriptRead::Missing) => {}
Err(ScriptRead::Refused) if passed_over_when_refused => {}
Err(ScriptRead::Refused) if passed_over_when_refused => {
if i == 0 {
literal_error = Some(refused());
}
}
Err(ScriptRead::Refused) => return Err(refused()),
Err(ScriptRead::TooLarge) => return Err((FailKind::Limit, format!("script file is too large: {value}"))),
Err(ScriptRead::Failed(e)) if i == 0 && passed_over_when_refused => {
literal_error = Some((FailKind::Error, format!("cannot read script file {value}: {e}")));
}
Err(ScriptRead::Failed(e)) => return Err((FailKind::Error, format!("cannot read script file {value}: {e}"))),
}
}
if let Some(error) = literal_error {
return Err(error);
}
Err((FailKind::Error, format!("include file not found: {value}")))
}