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: binary files as part are corrupted when stripping carriage returns #26

Open
wants to merge 2 commits into
base: 2.9.x
Choose a base branch
from
Open
Show file tree
Hide file tree
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
24 changes: 10 additions & 14 deletions src/Decode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use const E_NOTICE;
use const E_WARNING;
use const ICONV_MIME_DECODE_CONTINUE_ON_ERROR;
use const PREG_OFFSET_CAPTURE;

class Decode
{
Expand All @@ -37,36 +38,31 @@ class Decode
*/
public static function splitMime($body, $boundary)
{
// TODO: we're ignoring \r for now - is this function fast enough and is it safe to assume noone needs \r?
$body = str_replace("\r", '', $body);

$start = 0;
$res = [];
$res = [];
// find every mime part limiter and cut out the
// string before it.
// the part before the first boundary string is discarded:
$p = strpos($body, '--' . $boundary . "\n", $start);
if ($p === false) {
if (preg_match('/--' . $boundary . '(\r?\n)/', $body, $matches, PREG_OFFSET_CAPTURE) === 0) {
// no parts found!
return [];
}

// position after first boundary line
$start = $p + 3 + strlen($boundary);
$start = $matches[0][1] + strlen($matches[0][0]);
$serverEOL = $matches[1][0];

while (($p = strpos($body, '--' . $boundary . "\n", $start)) !== false) {
$res[] = substr($body, $start, $p - $start);
$start = $p + 3 + strlen($boundary);
while (preg_match('/--' . $boundary . '\r?\n/', $body, $matches, PREG_OFFSET_CAPTURE, $start) === 1) {
$res[] = substr($body, $start, $matches[0][1] - $start - strlen($serverEOL));
$start = $matches[0][1] + strlen($matches[0][0]);
}

// no more parts, find end boundary
$p = strpos($body, '--' . $boundary . '--', $start);
if ($p === false) {
if (preg_match('/--' . $boundary . '--/', $body, $matches, PREG_OFFSET_CAPTURE, $start) !== 1) {
throw new Exception\RuntimeException('Not a valid Mime Message: End Missing');
}

// the remaining part also needs to be parsed:
$res[] = substr($body, $start, $p - $start);
$res[] = substr($body, $start, $matches[0][1] - $start - strlen($serverEOL));
Copy link
Member

Choose a reason for hiding this comment

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

What does $matches[0][1] represent? Can you memoize it into a variable with a descriptive name, please?

return $res;
}

Expand Down
14 changes: 7 additions & 7 deletions test/MimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public static function dataTestFromMessageDecode(): array
'Alle meine Entchen schwimmen in dem See, schwimmen in dem See, K=C3=B6pfche=
n in das Wasser, Schw=C3=A4nzchen in die H=C3=B6h!',
],
['foobar', 'base64', 'Zm9vYmFyCg=='],
['foobar', 'base64', 'Zm9vYmFy'],
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
];
// phpcs:enable
}
Expand All @@ -257,15 +257,15 @@ public static function dataTestFromMessageDecode(): array
public function testFromMessageDecode(string $input, string $encoding, string $result): void
{
$parts = Mime\Message::createFromMessage(
'--089e0141a1902f83ee04e0a07b7a' . "\r\n"
. 'Content-Type: text/plain; charset=UTF-8' . "\r\n"
. 'Content-Transfer-Encoding: ' . $encoding . "\r\n"
. "\r\n"
. $result . "\r\n"
'--089e0141a1902f83ee04e0a07b7a' . "\n"
. 'Content-Type: text/plain; charset=UTF-8' . "\n"
. 'Content-Transfer-Encoding: ' . $encoding . "\n"
. "\n"
. $result . "\n"
Copy link
Member

Choose a reason for hiding this comment

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

Same as above: changes to pre-existing test probably hiding a BC break 🤔

Copy link
Author

Choose a reason for hiding this comment

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

I understand your concern, but don't have a clear answer.
Ripping-off carriage returns is harmful to binaries but people might have built solutions of their own to mitigate it and this fix might break them.

Copy link
Member

Choose a reason for hiding this comment

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

TBH, if that's how things are, that would be an https://xkcd.com/1172/ :D

workflow_2x

I just want a second review by somebody else, but if it's really like that, the bugfix is valid, and to be merged with the test changes too 👍

. '--089e0141a1902f83ee04e0a07b7a--',
'089e0141a1902f83ee04e0a07b7a'
)->getParts();
$this->assertSame($input . "\n", $parts[0]->getRawContent());
$this->assertSame($input, $parts[0]->getRawContent());
}

/**
Expand Down
16 changes: 16 additions & 0 deletions test/PartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,20 @@ public function testSetContentRaisesInvalidArgumentExceptionForInvalidContentTyp
$this->expectException(Mime\Exception\InvalidArgumentException::class);
$part->setContent($content);
}

public function testBinaryPart()
Copy link
Member

Choose a reason for hiding this comment

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

Give this a better name: testBinaryPartsAreCreatedAndParsedWithoutLoss() or similar.

{
$content = file_get_contents(__DIR__ . '/TestAsset/laminas.png');
$inputMessage = new Mime\Message();
$inputMessage->addPart(new Mime\Part('Hello World'));
$inputMessage->addPart(new Mime\Part($content));

$outputMessage = Mime\Message::createFromMessage(
$inputMessage->generateMessage(),
$inputMessage->getMime()->boundary()
);
$parts = $outputMessage->getParts();
$this->assertCount(2, $parts);
$this->assertEquals($content, $parts[1]->getContent());
}
}
Binary file added test/TestAsset/laminas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.