diff --git a/pbjson-types/src/wrappers.rs b/pbjson-types/src/wrappers.rs index 25e6c21..0c5fae0 100644 --- a/pbjson-types/src/wrappers.rs +++ b/pbjson-types/src/wrappers.rs @@ -32,7 +32,9 @@ macro_rules! ser_bytes_value { where S: serde::Serializer, { - let value = pbjson::private::base64::encode(&self.value); + use pbjson::private::base64::engine::Engine; + let value = + pbjson::private::base64::engine::general_purpose::STANDARD.encode(&self.value); value.serialize(ser) } } diff --git a/pbjson/Cargo.toml b/pbjson/Cargo.toml index 40557e5..5d36f44 100644 --- a/pbjson/Cargo.toml +++ b/pbjson/Cargo.toml @@ -12,7 +12,7 @@ repository = "https://github.com/influxdata/pbjson" [dependencies] serde = { version = "1.0", features = ["derive"] } -base64 = "0.13" +base64 = "0.21" [dev-dependencies] bytes = "1.0" diff --git a/pbjson/src/lib.rs b/pbjson/src/lib.rs index 0e17df0..ea7828b 100644 --- a/pbjson/src/lib.rs +++ b/pbjson/src/lib.rs @@ -22,6 +22,9 @@ pub mod private { /// Re-export base64 pub use base64; + use base64::engine::DecodePaddingMode; + use base64::engine::{GeneralPurpose, GeneralPurposeConfig}; + use base64::Engine; use serde::de::Visitor; use serde::Deserialize; use std::borrow::Cow; @@ -69,7 +72,15 @@ pub mod private { where E: serde::de::Error, { - let decoded = base64::decode_config(s, base64::STANDARD) + const INDIFFERENT_PAD: GeneralPurposeConfig = GeneralPurposeConfig::new() + .with_decode_padding_mode(DecodePaddingMode::Indifferent); + const STANDARD_INDIFFERENT_PAD: GeneralPurpose = + GeneralPurpose::new(&base64::alphabet::STANDARD, INDIFFERENT_PAD); + const URL_SAFE_INDIFFERENT_PAD: GeneralPurpose = + GeneralPurpose::new(&base64::alphabet::URL_SAFE, INDIFFERENT_PAD); + + let decoded = STANDARD_INDIFFERENT_PAD + .decode(s) .or_else(|e| match e { // Either standard or URL-safe base64 encoding are accepted // @@ -78,7 +89,7 @@ pub mod private { // Therefore if we error out on those characters, try again with // the URL-safe character set base64::DecodeError::InvalidByte(_, c) if c == b'-' || c == b'_' => { - base64::decode_config(s, base64::URL_SAFE) + URL_SAFE_INDIFFERENT_PAD.decode(s) } _ => Err(e), }) @@ -105,6 +116,7 @@ pub mod private { #[cfg(test)] mod tests { use super::*; + use base64::Engine; use bytes::Bytes; use rand::prelude::*; use serde::de::value::{BorrowedStrDeserializer, Error}; @@ -117,12 +129,12 @@ pub mod private { let raw: Vec<_> = std::iter::from_fn(|| Some(rng.gen())).take(len).collect(); for config in [ - base64::STANDARD, - base64::STANDARD_NO_PAD, - base64::URL_SAFE, - base64::URL_SAFE_NO_PAD, + base64::engine::general_purpose::STANDARD, + base64::engine::general_purpose::STANDARD_NO_PAD, + base64::engine::general_purpose::URL_SAFE, + base64::engine::general_purpose::URL_SAFE_NO_PAD, ] { - let encoded = base64::encode_config(&raw, config); + let encoded = config.encode(&raw); let deserializer = BorrowedStrDeserializer::<'_, Error>::new(&encoded); let a: Bytes = BytesDeserialize::deserialize(deserializer).unwrap().0;