From ad57a6ff6f683f1bd1090f274efab85698f598bd Mon Sep 17 00:00:00 2001 From: webimpress Date: Fri, 27 Sep 2019 20:51:28 +0100 Subject: [PATCH] Throw exception when header is invalid Fixes #180 - infinite recursion --- src/Headers.php | 4 ++++ test/HeadersTest.php | 10 ++++++++++ 2 files changed, 14 insertions(+) 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'); + } }