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

Sanitize totp base32 and use no pad #492

Merged
merged 1 commit into from
Jan 10, 2024
Merged
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
46 changes: 25 additions & 21 deletions crates/bitwarden/src/vault/totp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashMap, str::FromStr};

use crate::error::{Error, Result};
use chrono::{DateTime, Utc};
use data_encoding::BASE32;
use data_encoding::BASE32_NOPAD;
use hmac::{Hmac, Mac};
use reqwest::Url;
use schemars::JsonSchema;
Expand All @@ -12,6 +12,7 @@ type HmacSha1 = Hmac<sha1::Sha1>;
type HmacSha256 = Hmac<sha2::Sha256>;
type HmacSha512 = Hmac<sha2::Sha512>;

const BASE32_CHARS: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
const STEAM_CHARS: &str = "23456789BCDFGHJKMNPQRTVWXY";

const DEFAULT_ALGORITHM: Algorithm = Algorithm::Sha1;
Expand Down Expand Up @@ -128,9 +129,16 @@ impl FromStr for Totp {
/// - Steam URI
fn from_str(key: &str) -> Result<Self> {
fn decode_secret(secret: &str) -> Result<Vec<u8>> {
BASE32
.decode(secret.to_uppercase().as_bytes())
.map_err(|_| "Unable to decode secret".into())
// Sanitize the secret to only contain allowed characters
let secret = secret
.to_uppercase()
.chars()
.filter(|c| BASE32_CHARS.contains(*c))
.collect::<String>();

BASE32_NOPAD
.decode(secret.as_bytes())
.map_err(|e| e.to_string().into())
}

let params = if key.starts_with("otpauth://") {
Expand Down Expand Up @@ -218,30 +226,26 @@ mod tests {

#[test]
fn test_generate_totp() {
let key = "WQIQ25BRKZYCJVYP".to_string();
let time = Some(
DateTime::parse_from_rfc3339("2023-01-01T00:00:00.000Z")
.unwrap()
.with_timezone(&Utc),
);
let response = generate_totp(key, time).unwrap();

assert_eq!(response.code, "194506".to_string());
assert_eq!(response.period, 30);
}
let cases = vec![
("WQIQ25BRKZYCJVYP", "194506"), // valid base32
("wqiq25brkzycjvyp", "194506"), // lowercase
("PIUDISEQYA", "829846"), // non padded
("PIUDISEQYA======", "829846"), // padded
("PIUD1IS!EQYA=", "829846"), // sanitized
];

#[test]
fn test_lowercase_secret() {
let key = "wqiq25brkzycjvyp".to_string();
let time = Some(
DateTime::parse_from_rfc3339("2023-01-01T00:00:00.000Z")
.unwrap()
.with_timezone(&Utc),
);
let response = generate_totp(key, time).unwrap();

assert_eq!(response.code, "194506".to_string());
assert_eq!(response.period, 30);
for (key, expected_code) in cases {
let response = generate_totp(key.to_string(), time).unwrap();

assert_eq!(response.code, expected_code, "wrong code for key: {key}");
assert_eq!(response.period, 30);
}
}

#[test]
Expand Down