From bcf7fdd7b1f20a0b72b5710d5747a92ccfa09f67 Mon Sep 17 00:00:00 2001 From: Cameron Pickett Date: Wed, 28 Aug 2024 11:18:04 -0700 Subject: [PATCH] Upgrade base64 to 0.21 (#123) Summary: Pull Request resolved: https://github.com/facebookresearch/Private-ID/pull/123 Reviewed By: zertosh Differential Revision: D61737689 fbshipit-source-id: dc98887dd6b346f11d8efe7ffa085311f6110c37 --- thrift/lib/rust/Cargo.toml | 2 +- thrift/lib/rust/any/src/deserialize.rs | 15 +++++++++++++-- thrift/lib/rust/any/src/thrift_any_type.rs | 15 ++++++++++++++- thrift/lib/rust/src/simplejson_protocol.rs | 19 ++++++++++++++----- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/thrift/lib/rust/Cargo.toml b/thrift/lib/rust/Cargo.toml index 1e9e619b662..3e85328b5b0 100644 --- a/thrift/lib/rust/Cargo.toml +++ b/thrift/lib/rust/Cargo.toml @@ -12,7 +12,7 @@ publish = false [dependencies] anyhow = "1.0.86" async-trait = "0.1.71" -base64 = "0.13" +base64 = "0.21" bufsize = "1.0.5" bytes = { version = "1.6.0", features = ["serde"] } futures = { version = "0.3.30", features = ["async-await", "compat"] } diff --git a/thrift/lib/rust/any/src/deserialize.rs b/thrift/lib/rust/any/src/deserialize.rs index 5ead3ef8819..58f2e4b240e 100644 --- a/thrift/lib/rust/any/src/deserialize.rs +++ b/thrift/lib/rust/any/src/deserialize.rs @@ -107,6 +107,11 @@ mod tests { use any::Any; use anyhow::anyhow; use anyhow::Result; + use base64::alphabet::STANDARD; + use base64::engine::general_purpose::GeneralPurpose; + use base64::engine::general_purpose::GeneralPurposeConfig; + use base64::engine::DecodePaddingMode; + use base64::Engine; use standard::StandardProtocol; use standard::TypeName; use standard::TypeUri; @@ -116,17 +121,23 @@ mod tests { use super::deserialize; use crate::AnyError; + // Bring back the pre 0.20 bevahiour and allow either padded or un-padded base64 strings at decode time. + const STANDARD_INDIFFERENT: GeneralPurpose = GeneralPurpose::new( + &STANDARD, + GeneralPurposeConfig::new().with_decode_padding_mode(DecodePaddingMode::Indifferent), + ); + fn make_test_any() -> Result { Ok(Any { r#type: Type { name: TypeName::structType(TypeUri::typeHashPrefixSha2_256( - base64::decode("e3AbYkUAP8FICiPtbGYI6w").unwrap(), + STANDARD_INDIFFERENT.decode("e3AbYkUAP8FICiPtbGYI6w").unwrap(), )), params: Vec::new(), ..Default::default() }, protocol: Protocol::standard(StandardProtocol::Compact), - data: base64::decode( + data: STANDARD_INDIFFERENT.decode( "GAozNjAwNjUwNzQ0GA5jb2d3aGVlbF90ZXN0cxwcHLwoENBU+E6zAHkpmP0Y3JboyaMAABkMABwVBAAYlgMZHBgNY29nd2hlZWxfdGVzdBkcGBIvbG9ncy90aHJpZnRfbW9ja3MYDHRocmlmdF9tb2NrczbAmgwAABwYjAJ7ImFsbF9zaWRlcyI6IFsiYSJdLCAicnBtX21hcCI6IHt9LCAiYnVuZGxlX3BhY2thZ2VfbWFwIjogeyJyY2VzZXJ2aWNlIjogImY4NDZjZjczNGM2YjQ3NDI5OTdiNDBjNTA2MWI1NmJhIiwgInR1cHBlcndhcmUuaW1hZ2Uud2ViZm91bmRhdGlvbi5jOS5yY2VzZXJ2aWNlIjogImZkNDZmN2VmMzQ0ZDYxNTIzMTdhODJlMzE4ZjZkMjk4In0sICJzZmlkIjogInJjZXNlcnZpY2Uvd3d3X2dlbmVyaWMiLCAiYnVuZGxlX2lkIjogODE4OCwgIm1pbm9yX2J1bmRsZV9pZCI6IDF9HBwSEgAcIRISHBIWAAAAHAAcJAIAHBglY29nd2hlZWxfcmNlc2VydmljZV90ZXN0X3Rlc3RfaGFybmVzcwAcAAAcGwAAABgAGwAAABIbABwAAA", )?, ..Default::default() diff --git a/thrift/lib/rust/any/src/thrift_any_type.rs b/thrift/lib/rust/any/src/thrift_any_type.rs index 4ee7f689d69..1d89beba757 100644 --- a/thrift/lib/rust/any/src/thrift_any_type.rs +++ b/thrift/lib/rust/any/src/thrift_any_type.rs @@ -109,6 +109,11 @@ mod tests { use any::Any; use anyhow::anyhow; use anyhow::Result; + use base64::alphabet::STANDARD; + use base64::engine::general_purpose::GeneralPurpose; + use base64::engine::general_purpose::GeneralPurposeConfig; + use base64::engine::DecodePaddingMode; + use base64::Engine; use standard::TypeName; use standard::TypeUri; use type_::Type; @@ -118,13 +123,21 @@ mod tests { use crate::AnyError; use crate::AnyTypeExpectationViolated; + // Bring back the pre 0.20 bevahiour and allow either padded or un-padded base64 strings at decode time. + const STANDARD_INDIFFERENT: GeneralPurpose = GeneralPurpose::new( + &STANDARD, + GeneralPurposeConfig::new().with_decode_padding_mode(DecodePaddingMode::Indifferent), + ); + #[test] fn test_ensure_thrift_any_type_0() -> Result<()> { use artifact_builder_override::ExperimentOverrideRequestContext; let val = Any { r#type: Type { name: TypeName::structType(TypeUri::typeHashPrefixSha2_256( - base64::decode("e3AbYkUAP8FICiPtbGYI6w").unwrap(), + STANDARD_INDIFFERENT + .decode("e3AbYkUAP8FICiPtbGYI6w") + .unwrap(), )), ..Default::default() }, diff --git a/thrift/lib/rust/src/simplejson_protocol.rs b/thrift/lib/rust/src/simplejson_protocol.rs index d71d6ccf1ad..c963764149c 100644 --- a/thrift/lib/rust/src/simplejson_protocol.rs +++ b/thrift/lib/rust/src/simplejson_protocol.rs @@ -20,6 +20,11 @@ use anyhow::anyhow; use anyhow::bail; use anyhow::Context; use anyhow::Result; +use base64::alphabet::STANDARD; +use base64::engine::general_purpose::GeneralPurpose; +use base64::engine::general_purpose::NO_PAD; +use base64::engine::DecodePaddingMode; +use base64::Engine; use bufsize::SizeCounter; use bytes::buf::Writer; use bytes::Buf; @@ -48,6 +53,12 @@ use crate::thrift_protocol::MessageType; use crate::thrift_protocol::ProtocolID; use crate::ttype::TType; +// Bring back the pre 0.20 bevahiour and allow either padded or un-padded base64 strings at decode time. +const STANDARD_NO_PAD_INDIFFERENT: GeneralPurpose = GeneralPurpose::new( + &STANDARD, + NO_PAD.with_decode_padding_mode(DecodePaddingMode::Indifferent), +); + #[phantom] #[derive(Copy, Clone)] pub struct SimpleJsonProtocol; @@ -349,10 +360,7 @@ impl ProtocolWriter for SimpleJsonProtocolSerializer { .begin_string(&mut self.buffer) .expect("Somehow failed to do \"io\" on a buffer"); CompactFormatter - .write_raw_fragment( - &mut self.buffer, - &base64::encode_config(value, base64::STANDARD_NO_PAD), - ) + .write_raw_fragment(&mut self.buffer, &STANDARD_NO_PAD_INDIFFERENT.encode(value)) .expect("Somehow failed to do \"io\" on a buffer"); CompactFormatter .end_string(&mut self.buffer) @@ -923,7 +931,8 @@ impl ProtocolReader for SimpleJsonProtocolDeserializer { None => bail!("Expected the string to continue"), } } - let bin = base64::decode_config(&ret, base64::STANDARD_NO_PAD) + let bin = STANDARD_NO_PAD_INDIFFERENT + .decode(&ret) .context("The `binary` data in JSON string does not contain valid base64")?; Ok(V::from_vec(bin)) }