omegaupload/web/src/decrypt.rs

250 lines
8.4 KiB
Rust
Raw Normal View History

2021-10-31 14:01:27 -07:00
// OmegaUpload Web Frontend
// Copyright (C) 2021 Edward Shen
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2021-10-26 18:19:14 -07:00
use std::io::Cursor;
2021-10-24 02:25:42 -07:00
use std::sync::Arc;
2021-10-23 10:10:55 -07:00
use gloo_console::log;
use js_sys::{Array, Uint8Array};
2021-10-31 00:57:52 -07:00
use omegaupload_common::crypto::{open_in_place, Error, Key};
2021-10-30 21:00:09 -07:00
use omegaupload_common::secrecy::{Secret, SecretVec};
2021-10-26 18:19:14 -07:00
use serde::Serialize;
2021-10-27 01:49:06 -07:00
use wasm_bindgen::JsCast;
use web_sys::{Blob, BlobPropertyBag};
2021-10-26 18:19:14 -07:00
#[derive(Clone, Serialize)]
pub struct ArchiveMeta {
name: String,
2021-10-31 12:34:26 -07:00
file_size: u64,
2021-10-26 18:19:14 -07:00
}
2021-10-23 10:10:55 -07:00
2021-10-24 11:40:19 -07:00
#[derive(Clone)]
pub enum DecryptedData {
String(Arc<String>),
Blob(Arc<Blob>),
Image(Arc<Blob>, usize),
2021-10-24 11:40:19 -07:00
Audio(Arc<Blob>),
Video(Arc<Blob>),
2021-10-26 18:19:14 -07:00
Archive(Arc<Blob>, Vec<ArchiveMeta>),
2021-10-24 11:40:19 -07:00
}
2021-10-23 10:10:55 -07:00
2021-10-24 20:54:49 -07:00
fn now() -> f64 {
web_sys::window()
.expect("should have a Window")
.performance()
.expect("should have a Performance")
.now()
}
2022-01-11 22:19:15 -08:00
pub struct MimeType(pub String);
2021-10-24 02:25:42 -07:00
pub fn decrypt(
2021-10-23 10:10:55 -07:00
mut container: Vec<u8>,
2021-10-31 00:57:52 -07:00
key: &Secret<Key>,
2021-10-30 21:00:09 -07:00
maybe_password: Option<SecretVec<u8>>,
name_hint: Option<&str>,
2022-01-11 22:19:15 -08:00
) -> Result<(DecryptedData, MimeType), Error> {
2021-10-31 00:57:52 -07:00
open_in_place(&mut container, key, maybe_password)?;
2021-10-23 10:10:55 -07:00
let mime_type = guess_mime_type(name_hint, &container);
2022-01-15 23:53:25 -08:00
log!("[rs] Mime type:", mime_type);
2021-10-27 01:49:06 -07:00
2022-01-15 23:53:25 -08:00
log!("[rs] Blob conversion started.");
2021-10-27 02:23:28 -07:00
let start = now();
let blob_chunks = Array::new_with_length(container.chunks(65536).len().try_into().unwrap());
for (i, chunk) in container.chunks(65536).enumerate() {
let array = Uint8Array::new_with_length(chunk.len().try_into().unwrap());
array.copy_from(chunk);
blob_chunks.set(i.try_into().unwrap(), array.dyn_into().unwrap());
}
let mut blob_props = BlobPropertyBag::new();
blob_props.type_(mime_type);
let blob = Arc::new(
Blob::new_with_u8_array_sequence_and_options(blob_chunks.dyn_ref().unwrap(), &blob_props)
2021-10-27 01:49:06 -07:00
.unwrap(),
2021-10-27 02:23:28 -07:00
);
2021-10-30 18:38:55 -07:00
2022-01-15 23:53:25 -08:00
log!(format!(
"[rs] Blob conversion completed in {}ms",
now() - start
));
2021-10-23 10:10:55 -07:00
2022-01-11 22:19:15 -08:00
let data = match container.content_type() {
ContentType::Text => DecryptedData::String(Arc::new(
// SAFETY: ContentType::Text is guaranteed to be valid UTF-8.
unsafe { String::from_utf8_unchecked(container) },
2022-01-11 22:19:15 -08:00
)),
ContentType::Image => DecryptedData::Image(blob, container.len()),
ContentType::Audio => DecryptedData::Audio(blob),
ContentType::Video => DecryptedData::Video(blob),
2022-01-15 21:38:33 -08:00
ContentType::ZipArchive => handle_zip_archive(blob, container),
ContentType::Gzip => handle_gzip(blob, container),
ContentType::Unknown => DecryptedData::Blob(blob),
};
2022-01-15 21:38:33 -08:00
Ok((data, MimeType(mime_type.to_owned())))
}
fn handle_zip_archive(blob: Arc<Blob>, container: Vec<u8>) -> DecryptedData {
let mut entries = vec![];
let cursor = Cursor::new(container);
if let Ok(mut zip) = zip::ZipArchive::new(cursor) {
for i in 0..zip.len() {
match zip.by_index(i) {
Ok(file) => entries.push(ArchiveMeta {
name: file.name().to_string(),
file_size: file.size(),
}),
Err(err) => match err {
zip::result::ZipError::UnsupportedArchive(s) => {
log!("Unsupported: ", s.to_string());
2022-01-15 19:47:14 -08:00
}
2022-01-15 21:38:33 -08:00
_ => {
2022-01-16 00:49:42 -08:00
log!(format!("Error: {err}"));
2022-01-15 21:38:33 -08:00
}
},
2022-01-15 19:47:14 -08:00
}
2022-01-15 21:34:52 -08:00
}
2022-01-15 21:38:33 -08:00
}
2022-01-11 22:19:15 -08:00
2022-01-15 21:38:33 -08:00
entries.sort_by(|a, b| a.name.cmp(&b.name));
DecryptedData::Archive(blob, entries)
}
fn handle_gzip(blob: Arc<Blob>, container: Vec<u8>) -> DecryptedData {
let mut entries = vec![];
let cursor = Cursor::new(container);
let gzip_dec = flate2::read::GzDecoder::new(cursor);
let mut archive = tar::Archive::new(gzip_dec);
if let Ok(files) = archive.entries() {
2022-01-15 21:40:36 -08:00
for file in files.flatten() {
let file_path = if let Ok(file_path) = file.path() {
file_path.display().to_string()
} else {
"<Invalid utf-8 path>".to_string()
};
entries.push(ArchiveMeta {
name: file_path,
file_size: file.size(),
});
2022-01-15 21:38:33 -08:00
}
}
2022-01-15 21:40:36 -08:00
if entries.is_empty() {
2022-01-15 21:38:33 -08:00
DecryptedData::Blob(blob)
2022-01-15 21:40:36 -08:00
} else {
DecryptedData::Archive(blob, entries)
2022-01-15 21:38:33 -08:00
}
}
fn guess_mime_type(name_hint: Option<&str>, data: &[u8]) -> &'static str {
if let Some(name) = name_hint {
let guesses = mime_guess::from_path(name);
if let Some(mime_type) = guesses.first_raw() {
// Found at least one, but generally speaking this crate only
// uses authoritative sources (RFCs), so generally speaking
// there's only one association, and multiple are due to legacy
// support. As a result, we can probably just get the first one.
log!("[rs] Mime type inferred from extension.");
return mime_type;
}
2022-01-16 19:40:14 -08:00
log!("[rs] No mime type found for extension, falling back to introspection.");
}
2022-01-16 19:40:14 -08:00
tree_magic_mini::from_u8(data)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
enum ContentType {
Text,
Image,
Audio,
Video,
ZipArchive,
2022-01-15 21:34:52 -08:00
Gzip,
Unknown,
}
trait ContentTypeExt {
fn mime_type(&self) -> &str;
fn content_type(&self) -> ContentType;
}
impl<T: AsRef<[u8]>> ContentTypeExt for T {
fn mime_type(&self) -> &str {
tree_magic_mini::from_u8(self.as_ref())
}
fn content_type(&self) -> ContentType {
let mime_type = self.mime_type();
2022-01-11 22:45:03 -08:00
// check image first; tree magic match_u8 matches SVGs as plain text
if mime_type.starts_with("image/")
2022-01-11 22:38:20 -08:00
// application/x-riff is WebP
|| mime_type == "application/x-riff"
{
ContentType::Image
} else if tree_magic_mini::match_u8("text/plain", self.as_ref()) {
if std::str::from_utf8(self.as_ref()).is_ok() {
ContentType::Text
} else {
ContentType::Unknown
}
} else if mime_type.starts_with("audio/") {
ContentType::Audio
} else if mime_type.starts_with("video/")
// application/x-matroska is mkv
|| mime_type == "application/x-matroska"
{
ContentType::Video
} else if mime_type == "application/zip" {
ContentType::ZipArchive
} else if mime_type == "application/gzip" {
2022-01-15 21:34:52 -08:00
ContentType::Gzip
} else {
ContentType::Unknown
2021-10-23 10:10:55 -07:00
}
}
}
#[cfg(test)]
mod content_type {
use super::*;
2021-10-27 17:45:36 -07:00
macro_rules! test_content_type {
($($name:ident, $path:literal, $type:expr),*) => {
$(
#[test]
fn $name() {
let data = include_bytes!(concat!("../../test/", $path));
assert_eq!(data.content_type(), $type);
}
)*
};
2021-10-23 10:10:55 -07:00
}
test_content_type!(license_is_text, "LICENSE.md", ContentType::Text);
test_content_type!(code_is_text, "code.rs", ContentType::Text);
test_content_type!(patch_is_text, "0000-test-patch.patch", ContentType::Text);
test_content_type!(png_is_image, "image.png", ContentType::Image);
test_content_type!(webp_is_image, "image.webp", ContentType::Image);
test_content_type!(svg_is_image, "image.svg", ContentType::Image);
test_content_type!(mp3_is_audio, "music.mp3", ContentType::Audio);
test_content_type!(mp4_is_video, "movie.mp4", ContentType::Video);
test_content_type!(mkv_is_video, "movie.mkv", ContentType::Video);
test_content_type!(zip_is_zip, "archive.zip", ContentType::ZipArchive);
2022-01-15 21:34:52 -08:00
test_content_type!(gzip_is_gzip, "image.png.gz", ContentType::Gzip);
test_content_type!(binary_is_unknown, "omegaupload", ContentType::Unknown);
2022-01-11 22:38:20 -08:00
test_content_type!(pgp_is_text, "text.pgp", ContentType::Text);
2021-10-23 10:10:55 -07:00
}