Skip to content

Commit 47ab7c5

Browse files
committed
fix: set_cookie() does not use Config\Cookie values
1 parent 1f7cc3f commit 47ab7c5

3 files changed

Lines changed: 45 additions & 9 deletions

File tree

system/HTTP/ResponseTrait.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\HTTP\Exceptions\HTTPException;
1818
use CodeIgniter\Pager\PagerInterface;
1919
use CodeIgniter\Security\Exceptions\SecurityException;
20+
use Config\Cookie as CookieConfig;
2021
use Config\Services;
2122
use DateTime;
2223
use DateTimeZone;
@@ -544,8 +545,8 @@ public function redirect(string $uri, string $method = 'auto', ?int $code = null
544545
* @param string $domain Cookie domain (e.g.: '.yourdomain.com')
545546
* @param string $path Cookie path (default: '/')
546547
* @param string $prefix Cookie name prefix ('': the default prefix)
547-
* @param bool $secure Whether to only transfer cookies via SSL
548-
* @param bool $httponly Whether only make the cookie accessible via HTTP (no javascript)
548+
* @param bool|null $secure Whether to only transfer cookies via SSL
549+
* @param bool|null $httponly Whether only make the cookie accessible via HTTP (no javascript)
549550
* @param string|null $samesite
550551
*
551552
* @return $this
@@ -557,8 +558,8 @@ public function setCookie(
557558
$domain = '',
558559
$path = '/',
559560
$prefix = '',
560-
$secure = false,
561-
$httponly = false,
561+
$secure = null,
562+
$httponly = null,
562563
$samesite = null
563564
) {
564565
if ($name instanceof Cookie) {
@@ -567,8 +568,17 @@ public function setCookie(
567568
return $this;
568569
}
569570

571+
/** @var CookieConfig|null $cookieConfig */
572+
$cookieConfig = config('Cookie');
573+
574+
if ($cookieConfig) {
575+
$secure ??= $cookieConfig->secure;
576+
$httponly ??= $cookieConfig->httponly;
577+
$samesite ??= $cookieConfig->samesite;
578+
}
579+
570580
if (is_array($name)) {
571-
// always leave 'name' in last place, as the loop will break otherwise, due to $$item
581+
// always leave 'name' in last place, as the loop will break otherwise, due to ${$item}
572582
foreach (['samesite', 'value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name'] as $item) {
573583
if (isset($name[$item])) {
574584
${$item} = $name[$item];

system/Helpers/cookie_helper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
* @param string $domain For site-wide cookie. Usually: .yourdomain.com
3131
* @param string $path The cookie path
3232
* @param string $prefix The cookie prefix ('': the default prefix)
33-
* @param bool $secure True makes the cookie secure
34-
* @param bool $httpOnly True makes the cookie accessible via http(s) only (no javascript)
33+
* @param bool|null $secure True makes the cookie secure
34+
* @param bool|null $httpOnly True makes the cookie accessible via http(s) only (no javascript)
3535
* @param string|null $sameSite The cookie SameSite value
3636
*
3737
* @see \CodeIgniter\HTTP\Response::setCookie()
@@ -43,8 +43,8 @@ function set_cookie(
4343
string $domain = '',
4444
string $path = '/',
4545
string $prefix = '',
46-
bool $secure = false,
47-
bool $httpOnly = false,
46+
?bool $secure = null,
47+
?bool $httpOnly = null,
4848
?string $sameSite = null
4949
) {
5050
$response = Services::response();

tests/system/Helpers/CookieHelperTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use CodeIgniter\Test\CIUnitTestCase;
2121
use CodeIgniter\Test\Mock\MockResponse;
2222
use Config\App;
23+
use Config\Cookie;
2324
use Config\Cookie as CookieConfig;
2425
use Config\Services;
2526

@@ -89,6 +90,31 @@ public function testSetCookieByArrayParameters()
8990
delete_cookie($this->name);
9091
}
9192

93+
public function testSetCookieConfigCookieIsUsed()
94+
{
95+
/** @var Cookie $config */
96+
$config = config('Cookie');
97+
$config->secure = true;
98+
$config->httponly = true;
99+
$config->samesite = 'None';
100+
Factories::injectMock('config', 'Cookie', $config);
101+
102+
$cookieAttr = [
103+
'name' => $this->name,
104+
'value' => $this->value,
105+
'expire' => $this->expire,
106+
];
107+
set_cookie($cookieAttr);
108+
109+
$cookie = $this->response->getCookie($this->name);
110+
$options = $cookie->getOptions();
111+
$this->assertTrue($options['secure']);
112+
$this->assertTrue($options['httponly']);
113+
$this->assertSame('None', $options['samesite']);
114+
115+
delete_cookie($this->name);
116+
}
117+
92118
public function testSetCookieSecured()
93119
{
94120
$pre = 'Hello, I try to';

0 commit comments

Comments
 (0)