common: Allow padding when decoding the Base64 type from a string

This commit is contained in:
Damir Jelić 2023-09-20 12:28:57 +02:00 committed by GitHub
parent 5040aa2a93
commit 1b6240286f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 4 deletions

View File

@ -17,6 +17,7 @@ Breaking changes:
Improvements: Improvements:
- Allow padding when decoding the `Base64` type from a string
- Add convenience methods for `push::Ruleset`: - Add convenience methods for `push::Ruleset`:
- To update the server-default push rules - To update the server-default push rules
- To remove a user-defined push rule - To remove a user-defined push rule

View File

@ -3,7 +3,7 @@
use std::{fmt, marker::PhantomData}; use std::{fmt, marker::PhantomData};
use base64::{ use base64::{
engine::{general_purpose, GeneralPurpose, GeneralPurposeConfig}, engine::{general_purpose, DecodePaddingMode, GeneralPurpose, GeneralPurposeConfig},
Engine, Engine,
}; };
use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
@ -56,9 +56,10 @@ impl Base64Config for UrlSafe {
} }
impl<C: Base64Config, B> Base64<C, B> { impl<C: Base64Config, B> Base64<C, B> {
const CONFIG: GeneralPurposeConfig = general_purpose::NO_PAD
// See https://github.com/matrix-org/matrix-spec/issues/838 // See https://github.com/matrix-org/matrix-spec/issues/838
const CONFIG: GeneralPurposeConfig = .with_decode_allow_trailing_bits(true)
general_purpose::NO_PAD.with_decode_allow_trailing_bits(true); .with_decode_padding_mode(DecodePaddingMode::Indifferent);
const ENGINE: GeneralPurpose = GeneralPurpose::new(&C::CONF.0, Self::CONFIG); const ENGINE: GeneralPurpose = GeneralPurpose::new(&C::CONF.0, Self::CONFIG);
} }
@ -155,6 +156,10 @@ mod tests {
fn slightly_malformed_base64() { fn slightly_malformed_base64() {
const INPUT: &str = "3UmJnEIzUr2xWyaUnJg5fXwRybwG5FVC6Gq\ const INPUT: &str = "3UmJnEIzUr2xWyaUnJg5fXwRybwG5FVC6Gq\
MHverEUn0ztuIsvVxX89JXX2pvdTsOBbLQx+4TVL02l4Cp5wPCm"; MHverEUn0ztuIsvVxX89JXX2pvdTsOBbLQx+4TVL02l4Cp5wPCm";
const INPUT_WITH_PADDING: &str = "im9+knCkMNQNh9o6sbdcZw==";
Base64::<Standard>::parse(INPUT).unwrap(); Base64::<Standard>::parse(INPUT).unwrap();
Base64::<Standard>::parse(INPUT_WITH_PADDING)
.expect("We should be able to decode padded Base64");
} }
} }