Skip to content

Commit 6fd466d

Browse files
committed
fix: Security class does not send cookies
Add Response::setCookieStore()
1 parent bd50a53 commit 6fd466d

File tree

3 files changed

+17
-77
lines changed

3 files changed

+17
-77
lines changed

system/HTTP/ResponseTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,14 @@ public function getCookieStore()
596596
return $this->cookieStore;
597597
}
598598

599+
/**
600+
* Sets the CookieStore.
601+
*/
602+
public function setCookieStore(CookieStore $cookieStore)
603+
{
604+
$this->cookieStore = $cookieStore;
605+
}
606+
599607
/**
600608
* Checks to see if the Response has a specified cookie or not.
601609
*/

system/Security/Security.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -528,32 +528,12 @@ private function saveHashInCookie(): void
528528
'expires' => $this->expires === 0 ? 0 : time() + $this->expires,
529529
]
530530
);
531-
$this->sendCookie($this->request);
532-
}
533-
534-
/**
535-
* CSRF Send Cookie
536-
*
537-
* @return false|Security
538-
*/
539-
protected function sendCookie(RequestInterface $request)
540-
{
541-
542-
$this->doSendCookie();
543-
log_message('info', 'CSRF cookie sent.');
544531

545-
return $this;
546-
}
532+
$response = Services::response();
533+
$cookieStore = $response->getCookieStore();
534+
$cookieStore = $cookieStore->put($this->cookie);
547535

548-
/**
549-
* Actual dispatching of cookies.
550-
* Extracted for this to be unit tested.
551-
*
552-
* @codeCoverageIgnore
553-
*/
554-
protected function doSendCookie(): void
555-
{
556-
cookies([$this->cookie], false)->dispatch();
536+
$response->setCookieStore($cookieStore);
557537
}
558538

559539
private function saveHashInSession(): void

tests/system/Security/SecurityTest.php

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
use CodeIgniter\Test\CIUnitTestCase;
2121
use CodeIgniter\Test\Mock\MockAppConfig;
2222
use CodeIgniter\Test\Mock\MockSecurity;
23-
use Config\Cookie as CookieConfig;
2423
use Config\Security as SecurityConfig;
24+
use Config\Services;
2525

2626
/**
2727
* @backupGlobals enabled
@@ -41,11 +41,7 @@ protected function setUp(): void
4141

4242
public function testBasicConfigIsSaved()
4343
{
44-
$config = new MockAppConfig();
45-
$security = $this->getMockBuilder(Security::class)
46-
->setConstructorArgs([$config])
47-
->onlyMethods(['doSendCookie'])
48-
->getMock();
44+
$security = new MockSecurity(new MockAppConfig());
4945

5046
$hash = $security->getHash();
5147

@@ -57,11 +53,7 @@ public function testHashIsReadFromCookie()
5753
{
5854
$_COOKIE['csrf_cookie_name'] = '8b9218a55906f9dcc1dc263dce7f005a';
5955

60-
$config = new MockAppConfig();
61-
$security = $this->getMockBuilder(Security::class)
62-
->setConstructorArgs([$config])
63-
->onlyMethods(['doSendCookie'])
64-
->getMock();
56+
$security = new MockSecurity(new MockAppConfig());
6557

6658
$this->assertSame('8b9218a55906f9dcc1dc263dce7f005a', $security->getHash());
6759
}
@@ -74,7 +66,8 @@ public function testGetHashSetsCookieWhenGETWithoutCSRFCookie()
7466

7567
$security->verify(new Request(new MockAppConfig()));
7668

77-
$this->assertSame($_COOKIE['csrf_cookie_name'], $security->getHash());
69+
$cookie = Services::response()->getCookie('csrf_cookie_name');
70+
$this->assertSame($security->getHash(), $cookie->getValue());
7871
}
7972

8073
public function testGetHashReturnsCSRFCookieWhenGETWithCSRFCookie()
@@ -238,45 +231,4 @@ public function testGetters(): void
238231
$this->assertIsString($security->getCookieName());
239232
$this->assertIsBool($security->shouldRedirect());
240233
}
241-
242-
public function testSendingCookiesFalse(): void
243-
{
244-
$request = $this->createMock(IncomingRequest::class);
245-
$request->method('isSecure')->willReturn(false);
246-
247-
$config = new CookieConfig();
248-
249-
$config->secure = true;
250-
Factories::injectMock('config', CookieConfig::class, $config);
251-
252-
$security = $this->getMockBuilder(Security::class)
253-
->setConstructorArgs([new MockAppConfig()])
254-
->onlyMethods(['doSendCookie'])
255-
->getMock();
256-
257-
$sendCookie = $this->getPrivateMethodInvoker($security, 'sendCookie');
258-
259-
$security->expects($this->never())->method('doSendCookie');
260-
$this->assertFalse($sendCookie($request));
261-
}
262-
263-
public function testSendingGoodCookies(): void
264-
{
265-
$request = $this->createMock(IncomingRequest::class);
266-
$request->method('isSecure')->willReturn(true);
267-
268-
$config = new MockAppConfig();
269-
270-
$config->cookieSecure = true;
271-
272-
$security = $this->getMockBuilder(Security::class)
273-
->setConstructorArgs([$config])
274-
->onlyMethods(['doSendCookie'])
275-
->getMock();
276-
277-
$sendCookie = $this->getPrivateMethodInvoker($security, 'sendCookie');
278-
279-
$security->expects($this->once())->method('doSendCookie');
280-
$this->assertInstanceOf(Security::class, $sendCookie($request));
281-
}
282234
}

0 commit comments

Comments
 (0)