fix(profile-store): refresh native Unix body filenames

This commit is contained in:
Tunglies
2026-12-04 01:48:32 +08:00
parent 1172ff24ff
commit 59762520cc
3 changed files with 11 additions and 5 deletions
+5 -1
View File
@@ -433,7 +433,11 @@ fn read_decision(path: &Path) -> Result<Decision> {
} }
fn validate_filename(file: &str) -> Result<()> { fn validate_filename(file: &str) -> Result<()> {
if file.is_empty() || matches!(file, "." | "..") || file.contains(['/', '\\', ':', '\0']) { if file.is_empty()
|| matches!(file, "." | "..")
|| file.contains(['/', '\\', '\0'])
|| (cfg!(windows) && file.contains(':'))
{
return Err(StoreError::InvalidFilename); return Err(StoreError::InvalidFilename);
} }
#[cfg(windows)] #[cfg(windows)]
@@ -30,18 +30,19 @@ fn refresh_creates_missing_body_for_legacy_index() -> Result<(), clash_verge_pro
#[cfg(unix)] #[cfg(unix)]
#[test] #[test]
fn refresh_allows_an_unrelated_unix_filename() -> Result<(), clash_verge_profile_store::StoreError> { fn refresh_preserves_native_unix_filenames() -> Result<(), clash_verge_profile_store::StoreError> {
let dir = tempfile::tempdir()?; let dir = tempfile::tempdir()?;
let root = dir.path(); let root = dir.path();
fs::create_dir(root.join("profiles"))?; fs::create_dir(root.join("profiles"))?;
fs::write(root.join("profiles/r.yaml"), b"old")?; fs::write(root.join("profiles/remote:old.yaml"), b"old")?;
fs::write(root.join("profiles/office:old.yaml"), b"local")?; fs::write(root.join("profiles/office:old.yaml"), b"local")?;
let old = "items:\n- {uid: r, type: remote, file: r.yaml, updated: 1}\n- {uid: l, type: local, file: 'office:old.yaml'}\n"; let old = "items:\n- {uid: r, type: remote, file: 'remote:old.yaml', updated: 1}\n- {uid: l, type: local, file: 'office:old.yaml'}\n";
fs::write(root.join("profiles.yaml"), old)?; fs::write(root.join("profiles.yaml"), old)?;
let candidate = serde_yaml_ng::from_str(&old.replace("updated: 1", "updated: 2"))?; let candidate = serde_yaml_ng::from_str(&old.replace("updated: 1", "updated: 2"))?;
let outcome = Session::open(root)?.commit_refresh("r", &candidate, b"new"); let outcome = Session::open(root)?.commit_refresh("r", &candidate, b"new");
assert!(matches!(outcome, CommitOutcome::Committed), "{outcome:?}"); assert!(matches!(outcome, CommitOutcome::Committed), "{outcome:?}");
assert_eq!(fs::read(root.join("profiles/remote:old.yaml"))?, b"new");
assert_eq!(fs::read(root.join("profiles/office:old.yaml"))?, b"local"); assert_eq!(fs::read(root.join("profiles/office:old.yaml"))?, b"local");
Ok(()) Ok(())
} }
+2 -1
View File
@@ -159,7 +159,8 @@ async fn commit_recoverable_refresh(
})?; })?;
let item = candidate.get_item(&uid)?; let item = candidate.get_item(&uid)?;
if let Some(file) = item.file.as_deref() if let Some(file) = item.file.as_deref()
&& !file.contains(['/', '\\', ':', '\0']) && !file.contains(['/', '\\', '\0'])
&& (!cfg!(windows) || !file.contains(':'))
&& !matches!(file, "" | "." | "..") && !matches!(file, "" | "." | "..")
{ {
let path = root.join("profiles").join(file); let path = root.join("profiles").join(file);