ソースを参照

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)
nino-exe 2 日 前
親
コミット
bb0a2428fc
1 ファイル変更、13 行追加、0 行削除
  1. 13 0
      crates/victory-core/src/policy.rs

+ 13 - 0
crates/victory-core/src/policy.rs

@@ -92,6 +92,11 @@ fn resolve(path: &Path) -> Result<PathBuf, PolicyError> {
                     rest.push(name.to_os_string());
                     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 {
                         path: path.to_path_buf(),
@@ -156,4 +161,12 @@ mod tests {
         let tmp = tempfile::tempdir().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());
+    }
 }