From 98c411cf2f908fe2f8b7cff424edaf9d7cffbc7f Mon Sep 17 00:00:00 2001
From: Volkor <me@volkor.me>
Date: Tue, 26 Sep 2023 19:12:12 +1000
Subject: [PATCH] fix: hopefully fix up mimetype checking bug by using tokio

---
 src/handlers/mod_tests.rs | 47 +++++++++++++++++++++++++++++++++++++++
 src/handlers/upload.rs    | 21 ++++++++---------
 2 files changed, 58 insertions(+), 10 deletions(-)

diff --git a/src/handlers/mod_tests.rs b/src/handlers/mod_tests.rs
index f2f57bf..4e571ca 100644
--- a/src/handlers/mod_tests.rs
+++ b/src/handlers/mod_tests.rs
@@ -118,6 +118,53 @@ mod tests {
         assert!(result.is_ok());
         assert_eq!(result.unwrap(), "application/x-empty");
 
+        // Test case 3: small png file
+        // This should test for a bug that lets really small files not count as the correct
+        // filetype
+        let small_png_data: &[u8] = &[
+            // a random screenshot lol
+			0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
+			0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00, 0x00, 0x20,
+			0x08, 0x06, 0x00, 0x00, 0x00, 0x82, 0x71, 0x21, 0x79, 0x00, 0x00, 0x00,
+			0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00,
+			0x00, 0x04, 0x67, 0x41, 0x4d, 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc,
+			0x61, 0x05, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00,
+			0x0e, 0xc3, 0x00, 0x00, 0x0e, 0xc3, 0x01, 0xc7, 0x6f, 0xa8, 0x64, 0x00,
+			0x00, 0x01, 0x23, 0x49, 0x44, 0x41, 0x54, 0x58, 0x47, 0xed, 0x97, 0xb1,
+			0xad, 0x84, 0x30, 0x0c, 0x86, 0x33, 0x00, 0x4b, 0xb0, 0x03, 0x2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, 0xcc, 0x2c, 0x8e, 0xe3,
+			0x18, 0x74, 0x0a, 0x30, 0x9f, 0xa2, 0x53, 0x54, 0x74, 0x6a, 0xa0, 0xc5,
+			0x87, 0x88, 0x4e, 0x6e, 0x4e, 0x4f, 0xe1, 0x7c, 0x6a, 0xa5, 0xf1, 0xb7,
+			0xe6, 0xe9, 0x10, 0x85, 0x21, 0x7e, 0x36, 0x8a, 0xd1, 0x59, 0x59, 0x53,
+			0x98, 0x98, 0x74, 0x8f, 0xa2, 0x5c, 0x50, 0xf4, 0x79, 0x9e, 0x83, 0x2e,
+			0xeb, 0xd4, 0x8c, 0x74, 0xb7, 0xd4, 0x7d, 0x34, 0xc6, 0x27, 0xa7, 0x95,
+			0x70, 0x39, 0xe3, 0x40, 0x31, 0x0c, 0xc9, 0x9a, 0xee, 0x11, 0x93, 0xa7,
+			0xc5, 0x7c, 0xac, 0xc7, 0xe6, 0x65, 0x6f, 0x0e, 0xc9, 0x8f, 0x69, 0x9a,
+			0xa6, 0xa8, 0x95, 0xa9, 0xfc, 0x0b, 0x8b, 0x4a, 0xde, 0xf6, 0xdf, 0xf9,
+			0xf2, 0xac, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42,
+		];
+		let result = detect_mime_type(small_png_data);
+	        println!("Test case 3: small_png: {:?}", result);
+	        assert!(result.is_ok());
+	        assert_eq!(
+	            result.unwrap(),
+	            "image/pngf"
+	        );
+
         // Test case 4: putty.exe
         let file_data: &[u8] = &[
             // Putty.exe trimmed at 2048
diff --git a/src/handlers/upload.rs b/src/handlers/upload.rs
index c7ee6d9..27432ad 100644
--- a/src/handlers/upload.rs
+++ b/src/handlers/upload.rs
@@ -1,11 +1,10 @@
 use chrono::{TimeZone, Utc};
 use salvo::{handler, hyper::header::HOST, prelude::StatusCode, writer::Text, Request, Response};
 use std::{
-    fs::{self, File},
-    io::Read,
     path::{Path, PathBuf},
     time::SystemTime,
 };
+use tokio::{fs::{self, File}, io::AsyncReadExt};
 use tracing::{debug, error, info, trace};
 
 use super::guess_ip;
@@ -52,14 +51,16 @@ pub async fn upload(req: &mut Request, res: &mut Response) {
             engine::generate_filename(length, file.name().unwrap_or("file").to_string()).await;
 
         // Get the temporary file
-        let mime_file = File::open(file.path()).expect("Error opening file");
-        let mut buffer = Vec::with_capacity(2048);
+        trace!("opening file for mimetype check: {:?}", file.path());
+        let mime_file = File::open(file.path()).await.expect("Failed to open file");
+        let mut buffer = Vec::with_capacity(512);
 
-        // Load up 2048 bytes (There wasn't a decent answer on how much or little I should read)
-        mime_file
-            .take(2048)
-            .read_to_end(&mut buffer)
-            .expect("couldn't read mimetype buffer");
+        // Load up 512 bytes (This breaks with a few filetypes, but meh. https://en.wikipedia.org/wiki/List_of_file_signatures)
+        let n = mime_file.take(512).read_to_end(&mut buffer).await.expect("Error reading file");
+
+        // mime_file
+        //     .take(512)
+        //     .read_to_end(&mut buffer);
 
         trace!("read bytes for mime parsing: {:x?}", buffer);
 
@@ -130,7 +131,7 @@ pub async fn upload(req: &mut Request, res: &mut Response) {
                 // file.path is the path to the temp file.
                 // file.size seems to be always 'None', so we'll ask the filesystem.
                 let path = Path::new(file.path());
-                let filesize = fs::metadata(path).unwrap().len() as i32;
+                let filesize = fs::metadata(path).await.unwrap().len() as i32;
                 debug!("upload(filesize): {:?}", filesize);
                 let expiry =
                     engine::calculate_expiry(sqlconn, &filename.to_str().unwrap(), filesize).await;
-- 
GitLab