From af4970ec1f32c43c9170f400e40678e1ce8e24bf Mon Sep 17 00:00:00 2001 From: Arturo Marquez Date: Mon, 27 Jun 2022 23:10:58 -0500 Subject: [PATCH] Bump `base64ct` crate `1.5.0` -> `1.5.1` This new release checks for invalid symbols in non-padded inputs for decoding. Therefore, we can get rid of the logic implemented in `https://gitlab.torproject.org/tpo/core/arti/-/merge_requests/600` --- Cargo.lock | 4 ++-- crates/tor-netdoc/Cargo.toml | 2 +- crates/tor-netdoc/src/types/misc.rs | 24 ++++++------------------ 3 files changed, 9 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0ea23f03b..d6a73ff40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -510,9 +510,9 @@ checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" [[package]] name = "base64ct" -version = "1.5.0" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dea908e7347a8c64e378c17e30ef880ad73e3b4498346b055c2c00ea342f3179" +checksum = "3bdca834647821e0b13d9539a8634eb62d3501b6b6c2cec1722786ee6671b851" [[package]] name = "bitflags" diff --git a/crates/tor-netdoc/Cargo.toml b/crates/tor-netdoc/Cargo.toml index 7de53690f..7501c32ce 100644 --- a/crates/tor-netdoc/Cargo.toml +++ b/crates/tor-netdoc/Cargo.toml @@ -45,7 +45,7 @@ experimental-api = [] dangerous-expose-struct-fields = ["visible", "visibility"] [dependencies] -base64ct = { version = "1.5.0", features = ["alloc"] } +base64ct = { version = "1.5.1", features = ["alloc"] } bitflags = "1" derive_more = "0.99" digest = "0.10.0" diff --git a/crates/tor-netdoc/src/types/misc.rs b/crates/tor-netdoc/src/types/misc.rs index 6b3890e77..72cfe785b 100644 --- a/crates/tor-netdoc/src/types/misc.rs +++ b/crates/tor-netdoc/src/types/misc.rs @@ -36,7 +36,7 @@ pub(crate) trait FromBytes: Sized { /// Types for decoding base64-encoded values. mod b64impl { use crate::{Error, ParseErrorKind as EK, Pos, Result}; - use base64ct::{Base64, Encoding}; + use base64ct::{Base64, Base64Unpadded, Encoding}; use std::ops::RangeBounds; /// A byte array, encoded in base64 with optional padding. @@ -45,22 +45,10 @@ mod b64impl { impl std::str::FromStr for B64 { type Err = Error; fn from_str(s: &str) -> Result { - // The `base64ct` crate only rejects invalid - // characters when the input is padded. Therefore, - // the input must be padded fist. For more info on - // this, take a look at this issue: - // `https://github.com/RustCrypto/utils/issues/576` - let mut string = s.to_string(); - // Determine padding length - let offset = 4 - s.len() % 4; - match offset { - 4 => (), - _ => { - // Add pad to input - string.push_str("=".repeat(offset).as_str()); - } - } - let v = Base64::decode_vec(&string); + let v: core::result::Result, base64ct::Error> = match s.len() % 4 { + 0 => Base64::decode_vec(s), + _ => Base64Unpadded::decode_vec(s), + }; let v = v.map_err(|_| { EK::BadArgument .with_msg("Invalid base64") @@ -533,8 +521,8 @@ mod test { assert!("B".parse::().is_err()); assert!("B=".parse::().is_err()); assert!("B==".parse::().is_err()); + assert!("Bg=".parse::().is_err()); assert_eq!("Bg".parse::()?.as_bytes(), b"\x06"); - assert_eq!("Bg=".parse::()?.as_bytes(), b"\x06"); assert_eq!("Bg==".parse::()?.as_bytes(), b"\x06"); assert_eq!("BCg".parse::()?.as_bytes(), b"\x04\x28"); assert_eq!("BCg=".parse::()?.as_bytes(), b"\x04\x28");