policy: resolve a .. in the not-yet-existing part of a path
`resolve` canonicalizes the longest existing prefix and re-appends the rest, but it walks up with `Path::file_name()`, which is None for a trailing `..`. A path such as `<tmp>/a/../b` (nothing exists yet) hit the `_` arm and returned PolicyError::Io, so a cache_dir or data_dir with a `..` in a directory that does not exist yet was rejected, and `normalize`'s `..` handling for the missing tail was never reached. A trailing `..` is now kept in `rest` so `normalize` resolves it lexically. Test: resolves_dot_dot_in_a_path_that_does_not_exist_yet (fails before, passes after). cargo test -p victory-core, cargo fmt --check and cargo clippy --tests are clean. (cherry picked from commit 52f0a099ede8aa4d5bde61a0c46963a77a4378ba)
This commit is contained in:
1 parent
12d53be52a
commit
7fed69565a
1 file changed
+13
@@ -92,6 +92,11 @@ fn resolve(path: &Path) -> Result<PathBuf, PolicyError> {
|
|||||||
rest.push(name.to_os_string());
|
rest.push(name.to_os_string());
|
||||||
existing = parent;
|
existing = parent;
|
||||||
}
|
}
|
||||||
|
// `file_name()` is None for a trailing `..`; keep it so `normalize` resolves it.
|
||||||
|
(Some(parent), None) if existing.ends_with("..") => {
|
||||||
|
rest.push("..".into());
|
||||||
|
existing = parent;
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return Err(PolicyError::Io {
|
return Err(PolicyError::Io {
|
||||||
path: path.to_path_buf(),
|
path: path.to_path_buf(),
|
||||||
@@ -156,4 +161,12 @@ mod tests {
|
|||||||
let tmp = tempfile::tempdir().unwrap();
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
ensure_outside_repo(&tmp.path().join("does").join("not").join("exist")).unwrap();
|
ensure_outside_repo(&tmp.path().join("does").join("not").join("exist")).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn resolves_dot_dot_in_a_path_that_does_not_exist_yet() {
|
||||||
|
let base = std::env::temp_dir().join("victory-policy-no-such");
|
||||||
|
let path = base.join("a").join("..").join("b");
|
||||||
|
let resolved = resolve(&path).expect("a `..` in the missing tail must resolve lexically");
|
||||||
|
assert_eq!(resolved, resolve(&base.join("b")).unwrap());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user