diff --git a/src/Headers.php b/src/Headers.php index e3897c1a0f..ab797a4b7f 100644 --- a/src/Headers.php +++ b/src/Headers.php @@ -472,6 +472,10 @@ protected function lazyLoadHeader($index, $isGeneric = false) try { $headers = $class::fromString($current['line']); } catch (Exception\InvalidArgumentException $exception) { + if ($isGeneric) { + throw $exception; + } + return $this->lazyLoadHeader($index, true); } if (is_array($headers)) { diff --git a/test/HeadersTest.php b/test/HeadersTest.php index 7437d5ecf4..3ebe2a518b 100644 --- a/test/HeadersTest.php +++ b/test/HeadersTest.php @@ -315,4 +315,14 @@ public function testCRLFAttack() $this->expectException(RuntimeException::class); Headers::fromString("Fake: foo-bar\r\n\r\nevilContent"); } + + public function testThrowExceptionOnInvalidHeader() + { + $headers = new Headers(); + $headers->addHeaderLine('Location', "/mail\r\ntest"); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid header value detected'); + $headers->get('Location'); + } }