From e9e99956ad8f0a1f44cf1430aa02e9e65b041c1e Mon Sep 17 00:00:00 2001 From: Geometrically <18202329+Geometrically@users.noreply.github.com> Date: Tue, 23 Apr 2024 15:03:03 -0700 Subject: [PATCH] Remove unsafe unwraps causing crashes (#1135) * Remove unsafe unwraps causing crashes * run fmt * bump version --- Cargo.lock | 4 +-- theseus/Cargo.toml | 2 +- theseus/src/state/metadata.rs | 5 ++-- theseus/src/state/minecraft_auth.rs | 18 ++++++++++--- theseus/src/state/projects.rs | 38 ++++++++++++++------------- theseus/src/state/tags.rs | 3 +-- theseus_gui/package.json | 2 +- theseus_gui/src-tauri/Cargo.toml | 2 +- theseus_gui/src-tauri/tauri.conf.json | 2 +- 9 files changed, 44 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 170559eb4..beb4423be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5074,7 +5074,7 @@ dependencies = [ [[package]] name = "theseus" -version = "0.7.0" +version = "0.7.1" dependencies = [ "async-recursion", "async-tungstenite", @@ -5126,7 +5126,7 @@ dependencies = [ [[package]] name = "theseus_gui" -version = "0.7.0" +version = "0.7.1" dependencies = [ "chrono", "cocoa 0.25.0", diff --git a/theseus/Cargo.toml b/theseus/Cargo.toml index 4b17872b2..839a28052 100644 --- a/theseus/Cargo.toml +++ b/theseus/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "theseus" -version = "0.7.0" +version = "0.7.1" authors = ["Jai A "] edition = "2018" diff --git a/theseus/src/state/metadata.rs b/theseus/src/state/metadata.rs index 7d67e3097..e50bb2f2e 100644 --- a/theseus/src/state/metadata.rs +++ b/theseus/src/state/metadata.rs @@ -146,7 +146,7 @@ impl Metadata { .join("metadata.json.bak"); if metadata_path.exists() { - std::fs::copy(&metadata_path, &metadata_backup_path).unwrap(); + std::fs::copy(&metadata_path, &metadata_backup_path)?; } write( @@ -154,8 +154,7 @@ impl Metadata { &serde_json::to_vec(&metadata_fetch)?, &state.io_semaphore, ) - .await - .unwrap(); + .await?; let mut old_metadata = state.metadata.write().await; *old_metadata = metadata_fetch; diff --git a/theseus/src/state/minecraft_auth.rs b/theseus/src/state/minecraft_auth.rs index f3224094a..ce41b98f4 100644 --- a/theseus/src/state/minecraft_auth.rs +++ b/theseus/src/state/minecraft_auth.rs @@ -34,6 +34,8 @@ pub enum MinecraftAuthStep { #[derive(thiserror::Error, Debug)] pub enum MinecraftAuthenticationError { + #[error("Error reading public key during generation")] + ReadingPublicKey, #[error("Failed to serialize private key to PEM: {0}")] PEMSerialize(#[from] p256::pkcs8::Error), #[error("Failed to serialize body to JSON during step {step:?}: {source}")] @@ -63,6 +65,8 @@ pub enum MinecraftAuthenticationError { #[source] source: std::io::Error, }, + #[error("Error reading XBOX Session ID header")] + NoSessionId, #[error("Error reading user hash")] NoUserHash, } @@ -415,7 +419,7 @@ async fn sisu_authenticate( let session_id = headers .get("X-SessionId") .and_then(|x| x.to_str().ok()) - .unwrap() + .ok_or_else(|| MinecraftAuthenticationError::NoSessionId)? .to_string(); Ok((session_id, res)) @@ -760,8 +764,16 @@ fn generate_key() -> Result { Ok(DeviceTokenKey { id, key: signing_key, - x: BASE64_URL_SAFE_NO_PAD.encode(encoded_point.x().unwrap()), - y: BASE64_URL_SAFE_NO_PAD.encode(encoded_point.y().unwrap()), + x: BASE64_URL_SAFE_NO_PAD.encode( + encoded_point.x().ok_or_else(|| { + MinecraftAuthenticationError::ReadingPublicKey + })?, + ), + y: BASE64_URL_SAFE_NO_PAD.encode( + encoded_point.y().ok_or_else(|| { + MinecraftAuthenticationError::ReadingPublicKey + })?, + ), }) } diff --git a/theseus/src/state/projects.rs b/theseus/src/state/projects.rs index 7d572bd94..888b244af 100644 --- a/theseus/src/state/projects.rs +++ b/theseus/src/state/projects.rs @@ -232,24 +232,26 @@ async fn read_icon_from_file( zip_file_reader.file().entries().iter().position(|f| { f.filename().as_str().unwrap_or_default() == icon_path }); - let mut bytes = Vec::new(); - if zip_file_reader - .reader_with_entry(zip_index_option.unwrap()) - .await? - .read_to_end_checked(&mut bytes) - .await - .is_ok() - { - let bytes = bytes::Bytes::from(bytes); - let path = write_cached_icon( - &icon_path, - cache_dir, - bytes, - io_semaphore, - ) - .await?; - - return Ok(Some(path)); + if let Some(zip_index) = zip_index_option { + let mut bytes = Vec::new(); + if zip_file_reader + .reader_with_entry(zip_index) + .await? + .read_to_end_checked(&mut bytes) + .await + .is_ok() + { + let bytes = bytes::Bytes::from(bytes); + let path = write_cached_icon( + &icon_path, + cache_dir, + bytes, + io_semaphore, + ) + .await?; + + return Ok(Some(path)); + } } } } diff --git a/theseus/src/state/tags.rs b/theseus/src/state/tags.rs index c30d7a002..4db64e4d7 100644 --- a/theseus/src/state/tags.rs +++ b/theseus/src/state/tags.rs @@ -101,8 +101,7 @@ impl Tags { &serde_json::to_vec(&tags_fetch)?, &state.io_semaphore, ) - .await - .unwrap(); + .await?; let mut old_tags = state.tags.write().await; *old_tags = tags_fetch; diff --git a/theseus_gui/package.json b/theseus_gui/package.json index abbaebabc..e30a42134 100644 --- a/theseus_gui/package.json +++ b/theseus_gui/package.json @@ -1,7 +1,7 @@ { "name": "theseus_gui", "private": true, - "version": "0.7.0", + "version": "0.7.1", "type": "module", "scripts": { "dev": "vite", diff --git a/theseus_gui/src-tauri/Cargo.toml b/theseus_gui/src-tauri/Cargo.toml index bb3d82b89..f5480a728 100644 --- a/theseus_gui/src-tauri/Cargo.toml +++ b/theseus_gui/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "theseus_gui" -version = "0.7.0" +version = "0.7.1" description = "A Tauri App" authors = ["you"] license = "" diff --git a/theseus_gui/src-tauri/tauri.conf.json b/theseus_gui/src-tauri/tauri.conf.json index 551ea7ca9..1c65d2a3b 100644 --- a/theseus_gui/src-tauri/tauri.conf.json +++ b/theseus_gui/src-tauri/tauri.conf.json @@ -8,7 +8,7 @@ }, "package": { "productName": "Modrinth App", - "version": "0.7.0" + "version": "0.7.1" }, "tauri": { "allowlist": {