Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message corruption when displaying a base64 encoded message #9290

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions program/lib/Roundcube/rcube_imap_generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -3049,21 +3049,12 @@ protected static function decodeContent($chunk, $mode, $is_last = false, &$prev
{
// BASE64
if ($mode == 1) {
$chunk = $prev . preg_replace('|[^a-zA-Z0-9+=/]|', '', $chunk);

// create chunks with proper length for base64 decoding
$length = strlen($chunk);

if ($length % 4) {
$length = floor($length / 4) * 4;
$prev = substr($chunk, $length);
$chunk = substr($chunk, 0, $length);
$decoded_chunk = '';
foreach(explode('=', preg_replace('~[^a-zA-Z0-9+=/]~', '', $chunk)) as $line) {
$decoded_chunk .= base64_decode($line);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the &$prev should probably be still supported

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It must be supported. Also the code with $length % 4 is also important. You should not decode incomplete sequences.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we are basically back to the code I posted, right?

        if ($mode == 1) {
            $chunk = $prev . preg_replace('|[^a-zA-Z0-9+=/]|', '', $chunk);
            $length = strlen($chunk);

            if ($length % 4) {
                $length = floor($length / 4) * 4;
                $prev = substr($chunk, $length);
                $chunk = substr($chunk, 0, $length);
            } else {
                $prev = '';
            }
          
            $decoded_chunk = '';
            foreach(explode('=', $chunk) as $line) {
                $decoded_chunk  .= base64_decode($line);
            }
            return $decoded_chunk;
        }


}
else {
$prev = '';
}

return base64_decode($chunk);
return $decoded_chunk;
}

// QUOTED-PRINTABLE
Expand Down