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

Do not raise exception on header retrival for invalid header lines #147

Merged
merged 3 commits into from
Apr 26, 2018
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
24 changes: 17 additions & 7 deletions src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Countable;
use Iterator;
use Traversable;
use Zend\Http\Header\Exception;
use Zend\Http\Header\GenericHeader;
use Zend\Loader\PluginClassLocator;

/**
Expand Down Expand Up @@ -257,11 +259,11 @@ public function clearHeaders()
public function get($name)
{
$key = static::createKey($name);
if (! in_array($key, $this->headersKeys)) {
if (! $this->has($name)) {
return false;
}

$class = ($this->getPluginClassLoader()->load($key)) ?: 'Zend\Http\Header\GenericHeader';
$class = ($this->getPluginClassLoader()->load(str_replace('-', '', $key))) ?: 'Zend\Http\Header\GenericHeader';

if (in_array('Zend\Http\Header\MultipleHeaderInterface', class_implements($class, true))) {
$headers = [];
Expand Down Expand Up @@ -295,7 +297,7 @@ public function get($name)
*/
public function has($name)
{
return (in_array(static::createKey($name), $this->headersKeys));
return in_array(static::createKey($name), $this->headersKeys);
}

/**
Expand Down Expand Up @@ -433,17 +435,25 @@ public function forceLoading()

/**
* @param $index
* @param bool $isGeneric
* @return mixed|void
*/
protected function lazyLoadHeader($index)
protected function lazyLoadHeader($index, $isGeneric = false)
{
$current = $this->headers[$index];

$key = $this->headersKeys[$index];
/* @var $class Header\HeaderInterface */
$class = ($this->getPluginClassLoader()->load($key)) ?: 'Zend\Http\Header\GenericHeader';
$class = $this->getPluginClassLoader()->load(str_replace('-', '', $key));
if ($isGeneric || ! $class) {
$class = GenericHeader::class;
}

$headers = $class::fromString($current['line']);
try {
$headers = $class::fromString($current['line']);
} catch (Exception\InvalidArgumentException $exception) {
return $this->lazyLoadHeader($index, true);
}
if (is_array($headers)) {
$this->headers[$index] = $current = array_shift($headers);
foreach ($headers as $header) {
Expand All @@ -465,6 +475,6 @@ protected function lazyLoadHeader($index)
*/
protected static function createKey($name)
{
return str_replace(['-', '_', ' ', '.'], '', strtolower($name));
return str_replace(['_', ' ', '.'], '-', strtolower($name));
}
}
8 changes: 7 additions & 1 deletion test/HeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,16 @@ public function testHeadersFromStringFactoryCreatesMultipleObjects()
$this->assertEquals('Fake', $header->getFieldName());
$this->assertEquals('foo-bar', $header->getFieldValue());

$header = $headers->get('anotherfake');
$this->assertFalse($headers->get('anotherfake'));

$header = $headers->get('another-fake');
$this->assertInstanceOf(GenericHeader::class, $header);
$this->assertEquals('Another-Fake', $header->getFieldName());
$this->assertEquals('boo-baz', $header->getFieldValue());

$this->assertSame($header, $headers->get('another fake'));
$this->assertSame($header, $headers->get('another_fake'));
$this->assertSame($header, $headers->get('another.fake'));
}

public function testHeadersFromStringMultiHeaderWillAggregateLazyLoadedHeaders()
Expand Down
15 changes: 15 additions & 0 deletions test/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,19 @@ public function testCRLFAttack()
"GET /foo HTTP/1.1\r\nHost: example.com\r\nX-Foo: This\ris\r\n\r\nCRLF\nInjection"
);
}

public function testGetHeadersDoesNotRaiseExceptionForInvalidHeaderLines()
{
$request = Request::fromString("GET /foo HTTP/1.1\r\nHost: example.com\r\nUseragent: h4ckerbot");

$headers = $request->getHeaders();
$this->assertFalse($headers->has('User-Agent'));
$this->assertFalse($headers->get('User-Agent'));
$this->assertSame('bar-baz', $request->getHeader('User-Agent', 'bar-baz'));

$this->assertTrue($headers->has('useragent'));
$this->assertInstanceOf(GenericHeader::class, $headers->get('useragent'));
$this->assertSame('h4ckerbot', $headers->get('useragent')->getFieldValue());
$this->assertSame('h4ckerbot', $request->getHeader('useragent')->getFieldValue());
}
}