marshallpierce / rust-base64

base64, in rust
Apache License 2.0
617 stars 115 forks source link

DecoderReader accepts incorrect input #226

Closed mina86 closed 1 year ago

mina86 commented 1 year ago

When parts read from underlying reader include valid padding the reader accepts the input even if there’s more data to follow. In this example the reader produces string AA==AA==AA== which should result in an error but instead generates three zero bytes.

struct Foo(usize);

impl std::io::Read for Foo {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        if self.0 == 0 {
            return Ok(0);
        }
        self.0 -= 1;
        buf[..4].copy_from_slice(b"AA==");
        Ok(4)
    }
}

fn main() {
    use std::io::Read;
    use base64::engine::general_purpose::STANDARD;

    let mut decoder = base64::read::DecoderReader::new(Foo(3), &STANDARD);
    let mut vec = Vec::new();
    let res = decoder.read_to_end(&mut vec);
    print!("{res:?} {vec:?}");
}
marshallpierce commented 1 year ago

Good find, thanks for the heads up.

marshallpierce commented 1 year ago

This will be fixed in 0.21.1 momentarily.