From 4c55a28392ab65ca9a7ff0f1725436b55f90be0d Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 15 Nov 2023 06:56:38 +0900 Subject: [PATCH] feat: add error check to getHeaderLine() --- system/HTTP/Message.php | 9 +++++++++ tests/system/HTTP/MessageTest.php | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/system/HTTP/Message.php b/system/HTTP/Message.php index 9d0b517778a4..6de3835ddcdd 100644 --- a/system/HTTP/Message.php +++ b/system/HTTP/Message.php @@ -11,6 +11,8 @@ namespace CodeIgniter\HTTP; +use InvalidArgumentException; + /** * An HTTP message * @@ -112,6 +114,13 @@ public function hasHeader(string $name): bool */ public function getHeaderLine(string $name): string { + if ($this->hasMultipleHeaders($name)) { + throw new InvalidArgumentException( + 'The header "' . $name . '" already has multiple headers.' + . ' You cannot use getHeaderLine().' + ); + } + $origName = $this->getHeaderName($name); if (! array_key_exists($origName, $this->headers)) { diff --git a/tests/system/HTTP/MessageTest.php b/tests/system/HTTP/MessageTest.php index c6cfb4d874cd..1403605a0287 100644 --- a/tests/system/HTTP/MessageTest.php +++ b/tests/system/HTTP/MessageTest.php @@ -355,4 +355,23 @@ public function testAppendHeaderWithMultipleHeaders(): void $this->message->appendHeader('Set-Cookie', 'HttpOnly'); } + + public function testGetHeaderLineWithMultipleHeaders(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'The header "Set-Cookie" already has multiple headers. You cannot use getHeaderLine().' + ); + + $this->message->addHeader( + 'Set-Cookie', + 'logged_in=no; Path=/' + ); + $this->message->addHeader( + 'Set-Cookie', + 'sessid=123456; Path=/' + ); + + $this->message->getHeaderLine('Set-Cookie'); + } }