From 9bc0824be6ed603e2a340d8b38174f1cbf40b3ff Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Wed, 29 Dec 2021 18:00:34 +0100 Subject: [PATCH] allow accessing zip-archives with absolute paths --- src/w30.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/w30.rs b/src/w30.rs index cf1101788..0093e23fd 100644 --- a/src/w30.rs +++ b/src/w30.rs @@ -239,6 +239,14 @@ impl Message { let archive = dc_open_file_std(context, archive)?; let mut archive = zip::ZipArchive::new(archive)?; + // ignore first slash. + // this way, files can be accessed absolutely (`/index.html`) as well as relatively (`index.html`) + let name = if name.starts_with('/') { + name.split_at(1).1 + } else { + name + }; + let mut file = archive.by_name(name)?; let mut buf = Vec::new(); @@ -695,6 +703,22 @@ mod tests { Ok(()) } + #[async_std::test] + async fn test_get_blob_from_archive_with_absolute_paths() -> Result<()> { + let t = TestContext::new_alice().await; + let chat_id = create_group_chat(&t, ProtectionStatus::Unprotected, "foo").await?; + let instance = send_w30_instance(&t, chat_id).await?; + + let buf = instance.get_blob_from_archive(&t, "/index.html").await?; + assert!(String::from_utf8_lossy(&buf).contains("document.write")); + + assert!(instance + .get_blob_from_archive(&t, "/not-there") + .await + .is_err()); + Ok(()) + } + #[async_std::test] async fn test_get_blob_from_archive_subdirs() -> Result<()> { let t = TestContext::new_alice().await;