mirror of
https://github.com/chatmail/core.git
synced 2026-04-24 08:56:29 +03:00
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:
@@ -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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user