Skip to content

Commit

Permalink
feat(strata-cli): warn user about password strength
Browse files Browse the repository at this point in the history
  • Loading branch information
storopoli committed Oct 9, 2024
1 parent 953d9f4 commit b3709c1
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
62 changes: 61 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/strata-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ sled = "0.34.7"
strata-bridge-tx-builder.workspace = true
terrors.workspace = true
tokio.workspace = true
zxcvbn = "3.1.0"

# sha2 fails to compile on windows with the "asm" feature
[target.'cfg(not(target_os = "windows"))'.dependencies]
Expand Down
12 changes: 12 additions & 0 deletions bin/strata-cli/src/cmd/change_pwd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use argh::FromArgs;
use console::Term;
use rand::thread_rng;
use zxcvbn::Score;

use crate::seed::{password::Password, EncryptedSeedPersister, Seed};

Expand All @@ -12,6 +13,17 @@ pub struct ChangePwdArgs {}
pub async fn change_pwd(_args: ChangePwdArgs, seed: Seed, persister: impl EncryptedSeedPersister) {
let term = Term::stdout();
let mut new_pw = Password::read(true).unwrap();
let entropy = new_pw.entropy();
let _ = term.write_line(format!("Password strength (Overall strength score from 0-4, where anything below 3 is too weak): {}", entropy.score()).as_str());
if entropy.score() <= Score::Two {
let _ = term.write_line(
entropy
.feedback()
.expect("No feedback")
.to_string()
.as_str(),
);
}

Check warning on line 26 in bin/strata-cli/src/cmd/change_pwd.rs

View check run for this annotation

Codecov / codecov/patch

bin/strata-cli/src/cmd/change_pwd.rs#L16-L26

Added lines #L16 - L26 were not covered by tests
let encrypted_seed = seed.encrypt(&mut new_pw, &mut thread_rng()).unwrap();
persister.save(&encrypted_seed).unwrap();
let _ = term.write_line("Password changed successfully");
Expand Down
12 changes: 12 additions & 0 deletions bin/strata-cli/src/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use password::{HashVersion, IncorrectPassword, Password};
use rand::{thread_rng, Rng, RngCore};
use sha2::{Digest, Sha256};
use terrors::OneOf;
use zxcvbn::Score;

use crate::constants::{AES_NONCE_LEN, AES_TAG_LEN, PW_SALT_LEN, SEED_LEN};

Expand Down Expand Up @@ -194,6 +195,17 @@ pub fn load_or_create(
};

let mut password = Password::read(true).map_err(OneOf::new)?;
let entropy = password.entropy();
let _ = term.write_line(format!("Password strength (Overall strength score from 0-4, where anything below 3 is too weak): {}", entropy.score()).as_str());
if entropy.score() <= Score::Two {
let _ = term.write_line(
entropy
.feedback()
.expect("No feedback")
.to_string()
.as_str(),
);
}

Check warning on line 208 in bin/strata-cli/src/seed.rs

View check run for this annotation

Codecov / codecov/patch

bin/strata-cli/src/seed.rs#L198-L208

Added lines #L198 - L208 were not covered by tests
let encrypted_seed = match seed.encrypt(&mut password, &mut thread_rng()) {
Ok(es) => es,
Err(e) => {
Expand Down
6 changes: 6 additions & 0 deletions bin/strata-cli/src/seed/password.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use argon2::{Algorithm, Argon2, Params, Version};
use dialoguer::Password as InputPassword;
use zxcvbn::{zxcvbn, Entropy};

use super::PW_SALT_LEN;

Expand Down Expand Up @@ -49,6 +50,11 @@ impl Password {
})
}

/// Returns the password entropy.
pub fn entropy(&self) -> Entropy {
zxcvbn(self.inner.as_str(), &[])
}

Check warning on line 56 in bin/strata-cli/src/seed/password.rs

View check run for this annotation

Codecov / codecov/patch

bin/strata-cli/src/seed/password.rs#L54-L56

Added lines #L54 - L56 were not covered by tests

pub fn seed_encryption_key(
&mut self,
salt: &[u8; PW_SALT_LEN],
Expand Down

0 comments on commit b3709c1

Please sign in to comment.