fix: get_filename() is now guaranteed to return a valid filename (#6537)

With iOS and Desktop copying the file to a to a temp file with the name
of `get_filename()`, it should be sanitized first.

The PR can be reviewed commit-by-commit or all at once.
This commit is contained in:
Hocuri
2025-02-13 12:26:23 +01:00
committed by GitHub
parent 81e9628ab7
commit 9b6ef5e54f
6 changed files with 124 additions and 50 deletions

View File

@@ -755,3 +755,31 @@ async fn test_delete_msgs_offline() -> Result<()> {
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_sanitize_filename_message() -> Result<()> {
let t = &TestContext::new().await;
let mut msg = Message::new(Viewtype::File);
// Even if some of these characters may be valid on one platform,
// they need to be removed in case a backup is transferred to another platform
// and the UI there tries to copy the blob to a file with the original name
// before passing it to an external program.
msg.set_file_from_bytes(t, "/\\:ee.tx*T ", b"hallo", None)?;
assert_eq!(msg.get_filename().unwrap(), "ee.txT");
let blob = msg.param.get_blob(Param::File, t).await?.unwrap();
assert_eq!(blob.suffix().unwrap(), "txt");
// The filename shouldn't be empty if there were only illegal characters:
msg.set_file_from_bytes(t, "/\\:.txt", b"hallo", None)?;
assert_eq!(msg.get_filename().unwrap(), "file.txt");
msg.set_file_from_bytes(t, "/\\:", b"hallo", None)?;
assert_eq!(msg.get_filename().unwrap(), "file");
msg.set_file_from_bytes(t, ".txt", b"hallo", None)?;
assert_eq!(msg.get_filename().unwrap(), "file.txt");
Ok(())
}