diff --git a/src/Cookie.php b/src/Cookie.php index e9ad899b5..b4a38e617 100644 --- a/src/Cookie.php +++ b/src/Cookie.php @@ -491,13 +491,19 @@ public static function parse($cookie_header, $name = '', $reference_time = null) * @param \WpOrg\Requests\Iri|null $origin URI for comparing cookie origins * @param int|null $time Reference time for expiration calculation * @return array + * + * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $origin argument is not null or an instance of the Iri class. */ - public static function parse_from_headers(Headers $headers, Iri $origin = null, $time = null) { + public static function parse_from_headers(Headers $headers, $origin = null, $time = null) { $cookie_headers = $headers->getValues('Set-Cookie'); if (empty($cookie_headers)) { return []; } + if ($origin !== null && !($origin instanceof Iri)) { + throw InvalidArgument::create(2, '$origin', Iri::class . ' or null', gettype($origin)); + } + $cookies = []; foreach ($cookie_headers as $header) { $parsed = self::parse($header, '', $time); diff --git a/tests/Cookie/ParseTest.php b/tests/Cookie/ParseTest.php index 3e29dd713..068ba502b 100644 --- a/tests/Cookie/ParseTest.php +++ b/tests/Cookie/ParseTest.php @@ -76,6 +76,37 @@ public function testParseInvalidReferenceTime() { Cookie::parse('test', 'test', 'now'); } + /** + * Verify parsing of cookies fails with an exception if the $origin parameter is passed anything but `null` + * or an instance of Iri. + * + * @dataProvider dataParseFromHeadersInvalidOrigin + * + * @covers ::parse_from_headers + * + * @param mixed $input Invalid parameter input. + * + * @return void + */ + public function testParseFromHeadersInvalidOrigin($input) { + $this->expectException(InvalidArgument::class); + $this->expectExceptionMessage('Argument #2 ($origin) must be of type WpOrg\Requests\Iri or null'); + + $headers = new Headers(); + $headers['Set-Cookie'] = 'name=value'; + + Cookie::parse_from_headers($headers, $input); + } + + /** + * Data Provider. + * + * @return array + */ + public static function dataParseFromHeadersInvalidOrigin() { + return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_NULL); + } + /** * Tests receiving an exception when the parse_from_headers() method received an invalid input type as `$reference_time`. *