Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Avoid triggering a notice with special crafted accept headers #58

Merged
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
11 changes: 8 additions & 3 deletions src/Header/AbstractAccept.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ protected function getParametersFromFieldValuePart($fieldValuePart)
foreach ($paramsStrings as $param) {
$explode = explode('=', $param, 2);

$value = trim($explode[1]);
if (count($explode) === 2) {
$value = trim($explode[1]);
} else {
$value = null;
}

if (isset($value[0]) && $value[0] == '"' && substr($value, -1) == '"') {
$value = substr(substr($value, 1), 0, -1);
}
Expand Down Expand Up @@ -226,9 +231,9 @@ function ($v) {
);

if ($escaped == $value && !array_intersect(str_split($value), $separators)) {
$value = $key . '=' . $value;
$value = $key . ($value ? '=' . $value : '');
} else {
$value = $key . '="' . $escaped . '"';
$value = $key . ($value ? '="' . $escaped . '"' : '');
}

return $value;
Expand Down
12 changes: 12 additions & 0 deletions test/Header/AcceptCharsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ public function testAcceptCharsetGetFieldValueReturnsProperValue()
$this->assertEquals('xxx', $acceptCharsetHeader->getFieldValue());
}

public function testAcceptCharsetGetFieldValueReturnsProperValueWithTrailingSemicolon()
{
$acceptCharsetHeader = AcceptCharset::fromString('Accept-Charset: xxx;');
$this->assertEquals('xxx', $acceptCharsetHeader->getFieldValue());
}

public function testAcceptCharsetGetFieldValueReturnsProperValueWithSemicolonWithoutEqualSign()
{
$acceptCharsetHeader = AcceptCharset::fromString('Accept-Charset: xxx;yyy');
$this->assertEquals('xxx;yyy', $acceptCharsetHeader->getFieldValue());
}

public function testAcceptCharsetToStringReturnsHeaderFormattedString()
{
$acceptCharsetHeader = new AcceptCharset();
Expand Down
12 changes: 12 additions & 0 deletions test/Header/AcceptEncodingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ public function testAcceptEncodingGetFieldValueReturnsProperValue()
$this->assertEquals('xxx', $acceptEncodingHeader->getFieldValue());
}

public function testAcceptEncodingGetFieldValueReturnsProperValueWithTrailingSemicolon()
{
$acceptEncodingHeader = AcceptEncoding::fromString('Accept-Encoding: xxx;');
$this->assertEquals('xxx', $acceptEncodingHeader->getFieldValue());
}

public function testAcceptEncodingGetFieldValueReturnsProperValueWithSemicolonWithoutEqualSign()
{
$acceptEncodingHeader = AcceptEncoding::fromString('Accept-Encoding: xxx;yyy');
$this->assertEquals('xxx;yyy', $acceptEncodingHeader->getFieldValue());
}

public function testAcceptEncodingToStringReturnsHeaderFormattedString()
{
$acceptEncodingHeader = new AcceptEncoding();
Expand Down
12 changes: 12 additions & 0 deletions test/Header/AcceptLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ public function testAcceptLanguageGetFieldValueReturnsProperValue()
$this->assertEquals('xxx', $acceptLanguageHeader->getFieldValue());
}

public function testAcceptLanguageGetFieldValueReturnsProperValueWithTrailingSemicolon()
{
$acceptLanguageHeader = AcceptLanguage::fromString('Accept-Language: xxx;');
$this->assertEquals('xxx', $acceptLanguageHeader->getFieldValue());
}

public function testAcceptLanguageGetFieldValueReturnsProperValueWithSemicolonWithoutEqualSign()
{
$acceptLanguageHeader = AcceptLanguage::fromString('Accept-Language: xxx;yyy');
$this->assertEquals('xxx;yyy', $acceptLanguageHeader->getFieldValue());
}

public function testAcceptLanguageToStringReturnsHeaderFormattedString()
{
$acceptLanguageHeader = new AcceptLanguage();
Expand Down