Skip to content

Commit cad3d78

Browse files
committed
fix: make sure the signature file itself is not added to the list of files to verify
1 parent 75f73b7 commit cad3d78

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

src/cli/signing.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ fn get_paths_for(format: Option<FileType>, file_path: &Path) -> anyhow::Result<V
1919
let handler = crate::core::handlers::handler_for(format, file_path, Scope::Signing);
2020
// get the paths to sign or verify
2121
if let Ok(handler) = handler {
22-
handler.paths_to_sign(file_path)
22+
handler.paths_to_sign(file_path).map(|paths| {
23+
paths
24+
.iter()
25+
.map(|path| path.canonicalize().unwrap())
26+
.collect()
27+
})
2328
} else {
24-
Ok(vec![file_path.to_path_buf()])
29+
Ok(vec![file_path.canonicalize()?.to_path_buf()])
2530
}
2631
}
2732

@@ -59,9 +64,12 @@ fn get_paths_of_interest(
5964

6065
fn signature_path(file_path: &Path, signature_path: Option<PathBuf>) -> PathBuf {
6166
if let Some(path) = signature_path {
62-
path
67+
path.canonicalize().unwrap()
6368
} else if file_path.is_file() {
64-
file_path.with_extension("signature")
69+
file_path
70+
.with_extension("signature")
71+
.canonicalize()
72+
.unwrap()
6573
} else {
6674
file_path.join("tensor-man.signature")
6775
}
@@ -112,6 +120,8 @@ pub(crate) fn verify(args: VerifyArgs) -> anyhow::Result<()> {
112120
let mut manifest = Manifest::from_public_key_path(&base_path, &args.key_path)?;
113121
// get the paths to verify
114122
let mut paths_to_verify = get_paths_of_interest(args.format, &args.file_path)?;
123+
// remove the signature file from the list
124+
paths_to_verify.retain(|p| p != &signature_path);
115125

116126
// this will compute the checksums and verify the signature
117127
manifest.verify(&mut paths_to_verify, &signature)?;
@@ -131,11 +141,12 @@ mod tests {
131141
fn test_get_paths_single_file() -> anyhow::Result<()> {
132142
let temp_dir = TempDir::new()?;
133143
let file_path = temp_dir.path().join("model.safetensors");
144+
134145
File::create(&file_path)?;
135146

136147
let paths = get_paths_of_interest(None, &file_path)?;
137148
assert_eq!(paths.len(), 1);
138-
assert_eq!(paths[0], file_path);
149+
assert_eq!(paths[0], file_path.canonicalize()?);
139150

140151
Ok(())
141152
}
@@ -258,4 +269,33 @@ mod tests {
258269

259270
Ok(())
260271
}
272+
273+
#[test]
274+
fn test_paths_are_canonicalized() -> anyhow::Result<()> {
275+
let temp_dir = TempDir::new()?;
276+
let file_path = temp_dir.path().join("test.txt");
277+
File::create(&file_path)?;
278+
279+
// Get paths using absolute path first to verify it works
280+
let paths = get_paths_of_interest(None, &file_path)?;
281+
assert_eq!(paths.len(), 1);
282+
let canonical_path = file_path.canonicalize()?;
283+
assert_eq!(&paths[0], &canonical_path);
284+
285+
// Change into temp dir to test relative path
286+
std::env::set_current_dir(temp_dir.path())?;
287+
288+
// Get paths using relative path
289+
let relative_path = PathBuf::from("test.txt");
290+
let paths = get_paths_of_interest(None, &relative_path)?;
291+
292+
assert_eq!(paths.len(), 1);
293+
let returned_path = &paths[0];
294+
295+
// Verify the returned path is absolute and canonicalized
296+
assert!(returned_path.is_absolute());
297+
assert_eq!(returned_path, &canonical_path);
298+
299+
Ok(())
300+
}
261301
}

0 commit comments

Comments
 (0)