Skip to content

Commit e3d77f9

Browse files
authored
Merge pull request #4736 from paulbalandan/cookie-bc
Fix setting of value in Cookie's flag attributes
2 parents 94e0913 + 5516434 commit e3d77f9

3 files changed

Lines changed: 18 additions & 14 deletions

File tree

system/Cookie/Cookie.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,19 @@ final public function __construct(string $name, string $value = '', array $optio
220220
unset($options['max-age']);
221221
}
222222

223-
// to retain backward compatibility with previous versions' fallback
224-
$prefix = $options['prefix'] ?: self::$defaults['prefix'];
225-
$path = $options['path'] ?: self::$defaults['path'];
226-
$domain = $options['domain'] ?: self::$defaults['domain'];
227-
$secure = $options['secure'] ?: self::$defaults['secure'];
228-
$httponly = $options['httponly'] ?: self::$defaults['httponly'];
223+
// to preserve backward compatibility with array-based cookies in previous CI versions
224+
$prefix = $options['prefix'] ?: self::$defaults['prefix'];
225+
$path = $options['path'] ?: self::$defaults['path'];
226+
$domain = $options['domain'] ?: self::$defaults['domain'];
227+
228+
// empty string SameSite should use the default for browsers
229229
$samesite = $options['samesite'] ?: self::$defaults['samesite'];
230230

231-
$this->validateName($name, $options['raw']);
231+
$raw = $options['raw'];
232+
$secure = $options['secure'];
233+
$httponly = $options['httponly'];
234+
235+
$this->validateName($name, $raw);
232236
$this->validatePrefix($prefix, $secure, $path, $domain);
233237
$this->validateSameSite($samesite, $secure);
234238

@@ -241,7 +245,7 @@ final public function __construct(string $name, string $value = '', array $optio
241245
$this->secure = $secure;
242246
$this->httponly = $httponly;
243247
$this->samesite = ucfirst(strtolower($samesite));
244-
$this->raw = $options['raw'];
248+
$this->raw = $raw;
245249
}
246250

247251
//=========================================================================

tests/system/HTTP/ResponseCookieTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ public function testCookieHTTPOnly()
117117

118118
$response->setCookie('foo', 'bar');
119119
$cookie = $response->getCookie('foo');
120-
$this->assertTrue($cookie->isHTTPOnly());
120+
$this->assertFalse($cookie->isHTTPOnly());
121121

122-
$response->setCookie(['name' => 'bee', 'value' => 'bop', 'httponly' => false]);
122+
$response->setCookie(['name' => 'bee', 'value' => 'bop', 'httponly' => true]);
123123
$cookie = $response->getCookie('bee');
124124
$this->assertTrue($cookie->isHTTPOnly());
125125
}

user_guide_src/source/libraries/cookies.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ There are currently four (4) ways to create a new ``Cookie`` value object.
3333
use CodeIgniter\Cookie\Cookie;
3434
use DateTime;
3535

36-
// Throw the constructor
36+
// Using the constructor
3737
$cookie = new Cookie(
3838
'remember_token',
3939
'f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6',
@@ -79,7 +79,7 @@ instance or an array of defaults to the static ``Cookie::setDefaults()`` method.
7979
use CodeIgniter\Cookie\Cookie;
8080
use Config\Cookie as CookieConfig;
8181

82-
// pass in an App instance before constructing a Cookie class
82+
// pass in an Config\Cookie instance before constructing a Cookie class
8383
Cookie::setDefaults(new CookieConfig());
8484
$cookie = new Cookie('login_token');
8585

@@ -456,11 +456,11 @@ Class Reference
456456
457457
.. php:staticmethod:: setDefaults([$config = []])
458458
459-
:param App|array $config: The configuration array or instance
459+
:param \Config\Cookie|array $config: The configuration array or instance
460460
:rtype: array<string, mixed>
461461
:returns: The old defaults
462462

463-
Set the default attributes to a Cookie instance by injecting the values from the ``App`` config or an array.
463+
Set the default attributes to a Cookie instance by injecting the values from the ``\Config\Cookie`` config or an array.
464464

465465
.. php:staticmethod:: fromHeaderString(string $header[, bool $raw = false])
466466

0 commit comments

Comments
 (0)