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

Merge release 2.12.5 into 2.13.x #125

Merged
merged 11 commits into from
Jan 17, 2021
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 2.12.5 - 2020-12-31


-----

### Release Notes for [2.12.5](https://github.com/laminas/laminas-mail/milestone/9)

2.12.x bugfix release (patch)

### 2.12.5

- Total issues resolved: **0**
- Total pull requests resolved: **1**
- Total contributors: **1**

#### Bug

- [108: Fix Invalid header line for Content-Disposition string - incomplete continuation](https://github.com/laminas/laminas-mail/pull/108) thanks to @glensc

## 2.12.3 - TBD

### Added
Expand Down
19 changes: 18 additions & 1 deletion src/Header/ContentDisposition.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ public static function fromString($headerLine)

if (strpos($name, '*')) {
list($name, $count) = explode('*', $name);
// allow optional count:
// Content-Disposition: attachment; filename*=UTF-8''%64%61%61%6D%69%2D%6D%C3%B5%72%76%2E%6A%70%67
if ($count === "") {
$count = 0;
}

if (! is_numeric($count)) {
$type = gettype($count);
$value = var_export($count, 1);
throw new Exception\InvalidArgumentException(sprintf(
"Invalid header line for Content-Disposition string".
" - count expected to be numeric, got %s with value %s",
$type,
$value
));
}
if (! isset($continuedValues[$name])) {
$continuedValues[$name] = [];
}
Expand All @@ -82,7 +98,8 @@ public static function fromString($headerLine)
for ($i = 0; $i < count($values); $i++) {
if (! isset($values[$i])) {
throw new Exception\InvalidArgumentException(
'Invalid header line for Content-Disposition string - incomplete continuation'
'Invalid header line for Content-Disposition string - incomplete continuation'.
'; HeaderLine: '.$headerLine
);
}
$value .= $values[$i];
Expand Down
60 changes: 60 additions & 0 deletions test/Header/ContentDispositionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,30 @@ public function testFromStringHandlesContinuations()
$this->assertEquals(['level' => '1'], $header->getParameters());
}

/**
* Should not throw if the optional count is missing
*
* @see https://tools.ietf.org/html/rfc2231
* @dataProvider parameterWrappingProvider
*/
public function testParameterWrapping(string $input, string $disposition, array $parameters): void
{
$header = ContentDisposition::fromString($input);

$this->assertEquals($disposition, $header->getDisposition());
$this->assertEquals($parameters, $header->getParameters());
}

/**
* @dataProvider parameterWrappingProviderExceptions
*/
public function testParameterWrappingExceptions(string $input, string $exception, string $message): void
{
$this->expectException($exception);
$this->expectExceptionMessage($message);
ContentDisposition::fromString($input);
}

/**
* @dataProvider invalidParametersProvider
*/
Expand Down Expand Up @@ -246,4 +270,40 @@ public function getParameterProvider()
];
// @codingStandardsIgnoreEnd
}

public function parameterWrappingProvider(): iterable
{
// @codingStandardsIgnoreStart
yield 'Without sequence number' => [
"Content-Disposition: attachment; filename*=UTF-8''%64%61%61%6D%69%2D%6D%C3%B5%72%76%2E%6A%70%67",
'attachment',
['filename' => "UTF-8''%64%61%61%6D%69%2D%6D%C3%B5%72%76%2E%6A%70%67"]
];
yield 'With two ordered items' => [
"Content-Disposition: attachment;" .
"filename*0*=UTF-8''%76%C3%A4%6C%6A%61%70%C3%A4%C3%A4%73%75%2D%65%69%2D%6F;" .
"filename*1*=%6C%65%2E%6A%70%67",
'attachment',
['filename' => "UTF-8''%76%C3%A4%6C%6A%61%70%C3%A4%C3%A4%73%75%2D%65%69%2D%6F%6C%65%2E%6A%70%67"]
];
yield 'With two ordered items' => [
"Content-Disposition: attachment; filename*=utf-8''Capture%20d%E2%80%99e%CC%81cran%202020%2D05%2D13%20a%CC%80%2017.13.47.png",
'attachment',
['filename' => "utf-8''Capture%20d%E2%80%99e%CC%81cran%202020%2D05%2D13%20a%CC%80%2017.13.47.png"]
];
// @codingStandardsIgnoreEnd
}

public function parameterWrappingProviderExceptions(): iterable
{
// @codingStandardsIgnoreStart
yield 'With non-numeric-sequence' => [
"Content-Disposition: attachment;" .
"filename*0*=UTF-8''%76%C3%A4%6C%6A%61%70%C3%A4%C3%A4%73%75%2D%65%69%2D%6F;" .
"filename*a*=%6C%65%2E%6A%70%67",
InvalidArgumentException::class,
"Invalid header line for Content-Disposition string - count expected to be numeric, got string with value 'a'"
];
// @codingStandardsIgnoreEnd
}
}
21 changes: 21 additions & 0 deletions test/Header/ContentTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ public function testFromStringHandlesContinuations()
$this->assertEquals(['level' => '1'], $header->getParameters());
}

/**
* Should not throw if the optional count is missing
*
* @see https://tools.ietf.org/html/rfc2231
* @dataProvider parameterWrappingProvider
*/
public function testParameterWrapping(string $input, array $parameters): void
{
$header = ContentType::fromString($input);

$this->assertEquals($parameters, $header->getParameters());
}

/**
* @dataProvider invalidParametersProvider
*/
Expand Down Expand Up @@ -245,4 +258,12 @@ public function testRemoveParameterNotExists()
$header = ContentType::fromString('content-type: text/plain');
$this->assertFalse($header->removeParameter('level'));
}

public function parameterWrappingProvider(): iterable
{
yield 'Example from RFC2231' => [
"Content-Type: application/x-stuff; title*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A",
['title*' => "us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A"]
];
}
}