Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-7067] Remove unnecessary unwraps #682

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions crates/bitwarden-crypto/src/keys/master_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use super::utils::{derive_kdf_key, stretch_kdf_key};
use crate::{util, EncString, KeyDecryptable, Result, SymmetricCryptoKey, UserKey};
use crate::{util, CryptoError, EncString, KeyDecryptable, Result, SymmetricCryptoKey, UserKey};

#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
Expand Down Expand Up @@ -68,7 +68,10 @@ impl MasterKey {

EncString::encrypt_aes256_hmac(
user_key.to_vec().as_slice(),
stretched_key.mac_key.as_ref().unwrap(),
stretched_key
.mac_key
.as_ref()
.ok_or(CryptoError::InvalidMac)?,
&stretched_key.key,
)
}
Expand Down
7 changes: 5 additions & 2 deletions crates/bitwarden-crypto/src/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@
.to_pkcs8_der()
.map_err(|_| RsaError::CreatePrivateKey)?;

let protected =
EncString::encrypt_aes256_hmac(pkcs.as_bytes(), key.mac_key.as_ref().unwrap(), &key.key)?;
let protected = EncString::encrypt_aes256_hmac(
pkcs.as_bytes(),
key.mac_key.as_ref().ok_or(CryptoError::InvalidMac)?,
&key.key,
)?;

Check warning on line 43 in crates/bitwarden-crypto/src/rsa.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-crypto/src/rsa.rs#L43

Added line #L43 was not covered by tests

Ok(RsaKeyPair {
public: b64,
Expand Down
10 changes: 7 additions & 3 deletions crates/bitwarden/src/auth/login/api_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
JWTToken,
},
client::{LoginMethod, UserLoginMethod},
error::Result,
error::{Error, Result},
Client,
};

Expand Down Expand Up @@ -44,9 +44,13 @@
kdf,
}));

let user_key: EncString = r.key.as_deref().unwrap().parse().unwrap();
let private_key: EncString = r.private_key.as_deref().unwrap().parse().unwrap();
let user_key: EncString = r.key.as_deref().ok_or(Error::MissingFields)?.parse()?;
let private_key: EncString = r
.private_key
.as_deref()
.ok_or(Error::MissingFields)?
.parse()?;

Check warning on line 53 in crates/bitwarden/src/auth/login/api_key.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/login/api_key.rs#L47-L53

Added lines #L47 - L53 were not covered by tests
client.initialize_user_crypto(&input.password, user_key, private_key)?;
}

Expand Down
12 changes: 6 additions & 6 deletions crates/bitwarden/src/auth/login/auth_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
auth_request::new_auth_request,
},
client::{LoginMethod, UserLoginMethod},
error::Result,
error::{Error, Result},
mobile::crypto::{AuthRequestMethod, InitUserCryptoMethod, InitUserCryptoRequest},
Client,
};
Expand Down Expand Up @@ -50,7 +50,7 @@
fingerprint: auth.fingerprint,
email,
device_identifier,
auth_request_id: res.id.unwrap(),
auth_request_id: res.id.ok_or(Error::MissingFields)?,
access_code: auth.access_code,
private_key: auth.private_key,
})
Expand Down Expand Up @@ -103,11 +103,11 @@

let method = match res.master_password_hash {
Some(_) => AuthRequestMethod::MasterKey {
protected_master_key: res.key.unwrap().parse().unwrap(),
auth_request_key: r.key.unwrap().parse().unwrap(),
protected_master_key: res.key.ok_or(Error::MissingFields)?.parse()?,
auth_request_key: r.key.ok_or(Error::MissingFields)?.parse()?,

Check warning on line 107 in crates/bitwarden/src/auth/login/auth_request.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/login/auth_request.rs#L106-L107

Added lines #L106 - L107 were not covered by tests
},
None => AuthRequestMethod::UserKey {
protected_user_key: res.key.unwrap().parse().unwrap(),
protected_user_key: res.key.ok_or(Error::MissingFields)?.parse()?,

Check warning on line 110 in crates/bitwarden/src/auth/login/auth_request.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/login/auth_request.rs#L110

Added line #L110 was not covered by tests
},
};

Expand All @@ -116,7 +116,7 @@
.initialize_user_crypto(InitUserCryptoRequest {
kdf_params: kdf,
email: auth_req.email,
private_key: r.private_key.unwrap(),
private_key: r.private_key.ok_or(Error::MissingFields)?,

Check warning on line 119 in crates/bitwarden/src/auth/login/auth_request.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/login/auth_request.rs#L119

Added line #L119 was not covered by tests
method: InitUserCryptoMethod::AuthRequest {
request_private_key: auth_req.private_key,
method,
Expand Down
10 changes: 7 additions & 3 deletions crates/bitwarden/src/auth/login/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
) -> Result<PasswordLoginResponse> {
use bitwarden_crypto::{EncString, HashPurpose};

use crate::{auth::determine_password_hash, client::UserLoginMethod};
use crate::{auth::determine_password_hash, client::UserLoginMethod, error::Error};

Check warning on line 27 in crates/bitwarden/src/auth/login/password.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/login/password.rs#L27

Added line #L27 was not covered by tests

info!("password logging in");
debug!("{:#?}, {:#?}", client, input);
Expand All @@ -49,8 +49,12 @@
kdf: input.kdf.to_owned(),
}));

let user_key: EncString = r.key.as_deref().unwrap().parse().unwrap();
let private_key: EncString = r.private_key.as_deref().unwrap().parse().unwrap();
let user_key: EncString = r.key.as_deref().ok_or(Error::MissingFields)?.parse()?;
let private_key: EncString = r
.private_key
.as_deref()
.ok_or(Error::MissingFields)?
.parse()?;

Check warning on line 57 in crates/bitwarden/src/auth/login/password.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/auth/login/password.rs#L52-L57

Added lines #L52 - L57 were not covered by tests

client.initialize_user_crypto(&input.password, user_key, private_key)?;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bitwarden/src/mobile/vault/client_attachments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
decrypted_file_path: &Path,
encrypted_file_path: &Path,
) -> Result<Attachment> {
let data = std::fs::read(decrypted_file_path).unwrap();
let data = std::fs::read(decrypted_file_path)?;

Check warning on line 43 in crates/bitwarden/src/mobile/vault/client_attachments.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/mobile/vault/client_attachments.rs#L43

Added line #L43 was not covered by tests
let AttachmentEncryptResult {
attachment,
contents,
Expand Down Expand Up @@ -73,7 +73,7 @@
encrypted_file_path: &Path,
decrypted_file_path: &Path,
) -> Result<()> {
let data = std::fs::read(encrypted_file_path).unwrap();
let data = std::fs::read(encrypted_file_path)?;

Check warning on line 76 in crates/bitwarden/src/mobile/vault/client_attachments.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/mobile/vault/client_attachments.rs#L76

Added line #L76 was not covered by tests
let decrypted = self.decrypt_buffer(cipher, attachment, &data).await?;
std::fs::write(decrypted_file_path, decrypted)?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions crates/bitwarden/src/mobile/vault/client_sends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
encrypted_file_path: &Path,
decrypted_file_path: &Path,
) -> Result<()> {
let data = std::fs::read(encrypted_file_path).unwrap();
let data = std::fs::read(encrypted_file_path)?;

Check warning on line 39 in crates/bitwarden/src/mobile/vault/client_sends.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/mobile/vault/client_sends.rs#L39

Added line #L39 was not covered by tests
let decrypted = self.decrypt_buffer(send, &data).await?;
std::fs::write(decrypted_file_path, decrypted)?;
Ok(())
Expand Down Expand Up @@ -65,7 +65,7 @@
decrypted_file_path: &Path,
encrypted_file_path: &Path,
) -> Result<()> {
let data = std::fs::read(decrypted_file_path).unwrap();
let data = std::fs::read(decrypted_file_path)?;

Check warning on line 68 in crates/bitwarden/src/mobile/vault/client_sends.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/mobile/vault/client_sends.rs#L68

Added line #L68 was not covered by tests
let encrypted = self.encrypt_buffer(send, &data).await?;
std::fs::write(encrypted_file_path, encrypted)?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden/src/vault/cipher/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@
.ok()
.flatten(),
discoverable: value.discoverable.ok_or(Error::MissingFields)?.parse()?,
creation_date: value.creation_date.parse().unwrap(),
creation_date: value.creation_date.parse()?,

Check warning on line 241 in crates/bitwarden/src/vault/cipher/login.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden/src/vault/cipher/login.rs#L241

Added line #L241 was not covered by tests
})
}
}
Expand Down
8 changes: 2 additions & 6 deletions crates/bw/src/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,13 @@
let email = text_prompt_when_none("Email", email)?;
let device_identifier = text_prompt_when_none("Device Identifier", device_identifier)?;

let auth = client
.auth()
.login_device(email, device_identifier)
.await
.unwrap();
let auth = client.auth().login_device(email, device_identifier).await?;

Check warning on line 126 in crates/bw/src/auth/login.rs

View check run for this annotation

Codecov / codecov/patch

crates/bw/src/auth/login.rs#L126

Added line #L126 was not covered by tests

println!("Fingerprint: {}", auth.fingerprint);

Text::new("Press enter once approved").prompt()?;

client.auth().login_device_complete(auth).await.unwrap();
client.auth().login_device_complete(auth).await?;

Check warning on line 132 in crates/bw/src/auth/login.rs

View check run for this annotation

Codecov / codecov/patch

crates/bw/src/auth/login.rs#L132

Added line #L132 was not covered by tests

Ok(())
}
Loading