From e171655bfb83eeec56948fc77e8bb7a62ebe666d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Jensen?= Date: Tue, 9 Jan 2024 21:27:44 +0100 Subject: [PATCH] Additional header checks --- docs/Changelog.md | 3 ++- src/Http/Message.php | 13 +++++++------ tests/suites/http/RequestTest.php | 3 +++ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/docs/Changelog.md b/docs/Changelog.md index 90ea885..8354e99 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -8,7 +8,8 @@ ### `2.1.1` - * Fix for HTTP headers (@zgrguric, @sirn-se) + * Fix issue with falsy but valid HTTP headers (@zgrguric) + * Additional check for HTTP headers (@sirn-se) ### `2.1.0` diff --git a/src/Http/Message.php b/src/Http/Message.php index d2cd018..1a4ccca 100644 --- a/src/Http/Message.php +++ b/src/Http/Message.php @@ -98,7 +98,7 @@ public function getHeaderLine(string $name): string * @return static * @throws \InvalidArgumentException for invalid header names or values. */ - public function withHeader(string $name, $value): self + public function withHeader(string $name, mixed $value): self { $new = clone $this; if ($this->hasHeader($name)) { @@ -116,7 +116,7 @@ public function withHeader(string $name, $value): self * @throws \InvalidArgumentException for invalid header names. * @throws \InvalidArgumentException for invalid header values. */ - public function withAddedHeader(string $name, $value): self + public function withAddedHeader(string $name, mixed $value): self { $new = clone $this; $new->handleHeader($name, $value); @@ -164,19 +164,20 @@ public function getAsArray(): array return $lines; } - private function handleHeader(string $name, string|array $value): void + private function handleHeader(string $name, mixed $value): void { // @todo: Add all available characters, these are just some of them. if (!preg_match('|^[0-9a-zA-Z#_-]+$|', $name)) { throw new InvalidArgumentException("'{$name}' is not a valid header field name."); } - $value = array_map(function (string|array $item) { - return trim($item); - }, is_array($value) ? $value : [$value]); + $value = is_array($value) ? $value : [$value]; if (empty($value)) { throw new InvalidArgumentException("Invalid header value(s) provided."); } foreach ($value as $content) { + if (!is_string($content) && !is_numeric($content)) { + throw new InvalidArgumentException("Invalid header value(s) provided."); + } $content = trim($content); if ('' === $content) { throw new InvalidArgumentException("Invalid header value(s) provided."); diff --git a/tests/suites/http/RequestTest.php b/tests/suites/http/RequestTest.php index d04ee07..ac6b2b2 100644 --- a/tests/suites/http/RequestTest.php +++ b/tests/suites/http/RequestTest.php @@ -242,6 +242,8 @@ public static function provideInvalidHeaderValues(): Generator yield ['']; yield [' ']; yield [['0', '']]; + yield [[null]]; + yield [[[0]]]; yield [[]]; } @@ -262,5 +264,6 @@ public static function provideValidHeaderValues(): Generator yield ['0 ', ['name' => ['0']]]; yield [' 0', ['name' => ['0']]]; yield [['0', '1'], ['name' => ['0', '1']]]; + yield [0, ['name' => ['0']]]; } }