Skip to content

Commit

Permalink
Upgrade base64 to 0.21 (#123)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: facebookresearch/Private-ID#123

Reviewed By: zertosh

Differential Revision: D61737689

fbshipit-source-id: dc98887dd6b346f11d8efe7ffa085311f6110c37
  • Loading branch information
capickett authored and facebook-github-bot committed Aug 28, 2024
1 parent 30886b0 commit bcf7fdd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
2 changes: 1 addition & 1 deletion thrift/lib/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
15 changes: 13 additions & 2 deletions thrift/lib/rust/any/src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Any> {
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()
Expand Down
15 changes: 14 additions & 1 deletion thrift/lib/rust/any/src/thrift_any_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
},
Expand Down
19 changes: 14 additions & 5 deletions thrift/lib/rust/src/simplejson_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<F = Bytes>;
Expand Down Expand Up @@ -349,10 +360,7 @@ impl<B: BufMutExt> ProtocolWriter for SimpleJsonProtocolSerializer<B> {
.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)
Expand Down Expand Up @@ -923,7 +931,8 @@ impl<B: Buf> ProtocolReader for SimpleJsonProtocolDeserializer<B> {
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))
}
Expand Down

0 comments on commit bcf7fdd

Please sign in to comment.