Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion pbjson-types/src/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pbjson/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 19 additions & 7 deletions pbjson/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
//
Expand All @@ -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),
})
Expand All @@ -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};
Expand All @@ -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;
Expand Down