Skip to content

Commit 5c4e905

Browse files
nosilver4uschlesserajrfnl
authored
Allow integers as cookie names (#847)
Modify data validation checks, exception messages, and docblocks to allow cookie names to be integers. This affects both the constructor and the parse() method. Also modified unit tests to allow strings and integers, and to validate the updated exception messages. --------- Co-authored-by: Alain Schlesser <alain.schlesser@gmail.com> Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
1 parent c4a34ad commit 5c4e905

3 files changed

Lines changed: 46 additions & 16 deletions

File tree

src/Cookie.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,23 @@ class Cookie {
6767
/**
6868
* Create a new cookie object
6969
*
70-
* @param string $name The name of the cookie.
70+
* @param int|string $name The name of the cookie.
7171
* @param string $value The value for the cookie.
7272
* @param array|\WpOrg\Requests\Utility\CaseInsensitiveDictionary $attributes Associative array of attribute data
7373
* @param array $flags The flags for the cookie.
7474
* Valid keys are `'creation'`, `'last-access'`,
7575
* `'persistent'` and `'host-only'`.
7676
* @param int|null $reference_time Reference time for relative calculations.
7777
*
78-
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $name argument is not a string.
78+
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $name argument is not an integer or string that conforms to RFC 2616.
7979
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $value argument is not a string.
8080
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $attributes argument is not an array or iterable object with array access.
8181
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $flags argument is not an array.
8282
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $reference_time argument is not an integer or null.
8383
*/
8484
public function __construct($name, $value, $attributes = [], $flags = [], $reference_time = null) {
85-
if (is_string($name) === false) {
86-
throw InvalidArgument::create(1, '$name', 'string', gettype($name));
85+
if ($name !== '' && InputValidator::is_valid_rfc2616_token($name) === false) {
86+
throw InvalidArgument::create(1, '$name', 'integer|string and conform to RFC 2616', gettype($name));
8787
}
8888

8989
if (is_string($value) === false) {
@@ -102,7 +102,7 @@ public function __construct($name, $value, $attributes = [], $flags = [], $refer
102102
throw InvalidArgument::create(5, '$reference_time', 'integer|null', gettype($reference_time));
103103
}
104104

105-
$this->name = $name;
105+
$this->name = (string) $name;
106106
$this->value = $value;
107107
$this->attributes = $attributes;
108108
$default_flags = [
@@ -426,9 +426,9 @@ public function format_for_set_cookie() {
426426
* is an intentional deviation from RFC 2109 and RFC 2616. RFC 6265
427427
* specifies some of this handling, but not in a thorough manner.
428428
*
429-
* @param string $cookie_header Cookie header value (from a Set-Cookie header)
430-
* @param string $name
431-
* @param int|null $reference_time
429+
* @param int|string $cookie_header Cookie header value (from a Set-Cookie header)
430+
* @param string $name
431+
* @param int|null $reference_time
432432
* @return \WpOrg\Requests\Cookie Parsed cookie object
433433
*
434434
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $cookie_header argument is not a string.
@@ -439,8 +439,12 @@ public static function parse($cookie_header, $name = '', $reference_time = null)
439439
throw InvalidArgument::create(1, '$cookie_header', 'string', gettype($cookie_header));
440440
}
441441

442-
if (is_string($name) === false) {
443-
throw InvalidArgument::create(2, '$name', 'string', gettype($name));
442+
if (is_string($name)) {
443+
$name = trim($name);
444+
}
445+
446+
if ($name !== '' && InputValidator::is_valid_rfc2616_token($name) === false) {
447+
throw InvalidArgument::create(2, '$name', 'integer|string and conform to RFC 2616', gettype($name));
444448
}
445449

446450
$parts = explode(';', $cookie_header);
@@ -463,6 +467,10 @@ public static function parse($cookie_header, $name = '', $reference_time = null)
463467
$name = trim($name);
464468
$value = trim($value);
465469

470+
if ($name !== '' && InputValidator::is_valid_rfc2616_token($name) === false) {
471+
throw InvalidArgument::create(2, '$name', 'integer|string and conform to RFC 2616', gettype($name));
472+
}
473+
466474
// Attribute keys are handled case-insensitively
467475
$attributes = new CaseInsensitiveDictionary();
468476

tests/Cookie/ConstructorTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,30 @@ final class ConstructorTest extends TestCase {
1616
/**
1717
* Tests receiving an exception when the constructor received an invalid input type as `$name`.
1818
*
19-
* @dataProvider dataInvalidStringInput
19+
* @dataProvider dataInvalidName
2020
*
2121
* @param mixed $input Invalid parameter input.
2222
*
2323
* @return void
2424
*/
2525
public function testInvalidName($input) {
2626
$this->expectException(InvalidArgument::class);
27-
$this->expectExceptionMessage('Argument #1 ($name) must be of type string');
27+
$this->expectExceptionMessage('Argument #1 ($name) must be of type integer|string and conform to RFC 2616');
2828

2929
new Cookie($input, 'value');
3030
}
3131

32+
/**
33+
* Data Provider.
34+
*
35+
* @return array
36+
*/
37+
public static function dataInvalidName() {
38+
$data = TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_INT, TypeProviderHelper::GROUP_STRING);
39+
$data['Valid string, but not a valid RFC 2616 token'] = ["some\ntext\rwith\tcontrol\echaracters\fin\vit"];
40+
return $data;
41+
}
42+
3243
/**
3344
* Tests receiving an exception when the constructor received an invalid input type as `$value`.
3445
*

tests/Cookie/ParseTest.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,19 @@ public function testParseInvalidCookieHeader($input) {
3232
Cookie::parse($input);
3333
}
3434

35+
/**
36+
* Data Provider.
37+
*
38+
* @return array
39+
*/
40+
public static function dataInvalidStringInput() {
41+
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_STRING);
42+
}
43+
3544
/**
3645
* Tests receiving an exception when the parse() method received an invalid input type as `$name`.
3746
*
38-
* @dataProvider dataInvalidStringInput
47+
* @dataProvider dataParseInvalidName
3948
*
4049
* @covers ::parse
4150
*
@@ -45,7 +54,7 @@ public function testParseInvalidCookieHeader($input) {
4554
*/
4655
public function testParseInvalidName($input) {
4756
$this->expectException(InvalidArgument::class);
48-
$this->expectExceptionMessage('Argument #2 ($name) must be of type string');
57+
$this->expectExceptionMessage('Argument #2 ($name) must be of type integer|string and conform to RFC 2616');
4958

5059
Cookie::parse('test', $input);
5160
}
@@ -55,8 +64,10 @@ public function testParseInvalidName($input) {
5564
*
5665
* @return array
5766
*/
58-
public static function dataInvalidStringInput() {
59-
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_STRING);
67+
public static function dataParseInvalidName() {
68+
$data = TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_INT, TypeProviderHelper::GROUP_STRING);
69+
$data['Valid string, but not a valid RFC 2616 token'] = ["some\ntext\rwith\tcontrol\echaracters\fin\vit"];
70+
return $data;
6071
}
6172

6273
/**

0 commit comments

Comments
 (0)