Skip to content

Commit

Permalink
Stripping white spaces out of secrets (#119)
Browse files Browse the repository at this point in the history
* 1.58

* 1.58
  • Loading branch information
gr211 authored Jan 16, 2022
1 parent 053ea97 commit c097de4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
matrix:
rust:
- 1.57.0 # MSRV
- 1.58.0 # MSRV

steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
rust:
- 1.57.0 # MSRV
- 1.58.0 # MSRV

steps:
- name: Checkout
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ release : src
gresource:
glib-compile-resources data/uk.co.grumlimited.authenticator-rs.xml

test:
cargo test

run: gresource
cargo run

Expand Down
30 changes: 28 additions & 2 deletions src/ui/edit_account_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use glib::Sender;
use gtk::prelude::*;
use gtk::{Builder, EntryIconPosition, StateFlags};
use log::{debug, warn};
use regex::Regex;
use rqrr::PreparedImage;
use rusqlite::Connection;

Expand Down Expand Up @@ -87,8 +88,9 @@ impl EditAccountWindow {
style_context.set_state(StateFlags::INCONSISTENT);
result = Err(ValidationError::FieldError("secret".to_owned()));
} else {
match Account::generate_time_based_password(secret_value.as_str()) {
Ok(_) => {}
let stripped = Self::strip_secret(&secret_value);
match Account::generate_time_based_password(stripped.as_str()) {
Ok(_) => buffer.set_text(&stripped),
Err(_) => {
let style_context = input_secret_frame.style_context();
style_context.set_state(StateFlags::INCONSISTENT);
Expand Down Expand Up @@ -302,4 +304,28 @@ impl EditAccountWindow {
.and_then(|account_id| Keyring::upsert(name.as_str(), account_id, secret.as_str()))
.unwrap();
}

/**
* Strips spaces out of string.
*/
fn strip_secret(secret: &str) -> String {
let re = Regex::new(r"\s").unwrap();
re.replace_all(secret, "").as_ref().to_owned()
}
}

#[cfg(test)]
mod tests {
use crate::ui::EditAccountWindow;

#[test]
fn should_strip_non_alphanum() {
assert_eq!("abcd", EditAccountWindow::strip_secret("a b c d"));
assert_eq!("b", EditAccountWindow::strip_secret(" b"));
assert_eq!("c", EditAccountWindow::strip_secret("c "));
assert_eq!(
"kfai5qjfvbz7u6uu3iqd4n2iajdvtzvg",
EditAccountWindow::strip_secret("kfai 5qjf vbz7 u6uu 3iqd 4n2i ajdv tzvg")
);
}
}

0 comments on commit c097de4

Please sign in to comment.