From 1b6240286fec68041863d2a88b7559a50e25b48f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 20 Sep 2023 12:28:57 +0200 Subject: [PATCH] common: Allow padding when decoding the `Base64` type from a string --- crates/ruma-common/CHANGELOG.md | 1 + crates/ruma-common/src/serde/base64.rs | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index d8bdc871..28030f0f 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -17,6 +17,7 @@ Breaking changes: Improvements: +- Allow padding when decoding the `Base64` type from a string - Add convenience methods for `push::Ruleset`: - To update the server-default push rules - To remove a user-defined push rule diff --git a/crates/ruma-common/src/serde/base64.rs b/crates/ruma-common/src/serde/base64.rs index 83bf376a..a7e2e78b 100644 --- a/crates/ruma-common/src/serde/base64.rs +++ b/crates/ruma-common/src/serde/base64.rs @@ -3,7 +3,7 @@ use std::{fmt, marker::PhantomData}; use base64::{ - engine::{general_purpose, GeneralPurpose, GeneralPurposeConfig}, + engine::{general_purpose, DecodePaddingMode, GeneralPurpose, GeneralPurposeConfig}, Engine, }; use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; @@ -56,9 +56,10 @@ impl Base64Config for UrlSafe { } impl Base64 { - // See https://github.com/matrix-org/matrix-spec/issues/838 - const CONFIG: GeneralPurposeConfig = - general_purpose::NO_PAD.with_decode_allow_trailing_bits(true); + const CONFIG: GeneralPurposeConfig = general_purpose::NO_PAD + // See https://github.com/matrix-org/matrix-spec/issues/838 + .with_decode_allow_trailing_bits(true) + .with_decode_padding_mode(DecodePaddingMode::Indifferent); const ENGINE: GeneralPurpose = GeneralPurpose::new(&C::CONF.0, Self::CONFIG); } @@ -155,6 +156,10 @@ mod tests { fn slightly_malformed_base64() { const INPUT: &str = "3UmJnEIzUr2xWyaUnJg5fXwRybwG5FVC6Gq\ MHverEUn0ztuIsvVxX89JXX2pvdTsOBbLQx+4TVL02l4Cp5wPCm"; + const INPUT_WITH_PADDING: &str = "im9+knCkMNQNh9o6sbdcZw=="; + Base64::::parse(INPUT).unwrap(); + Base64::::parse(INPUT_WITH_PADDING) + .expect("We should be able to decode padded Base64"); } }