Skip to content

Commit f8ae5a1

Browse files
authored
Merge pull request #7221 from kenjis/remove-config-app-cookie-4.4
refactor: remove Cookie config items in Config\App
2 parents 7ea9372 + 01f7a6b commit f8ae5a1

File tree

17 files changed

+168
-202
lines changed

17 files changed

+168
-202
lines changed

app/Config/App.php

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -238,85 +238,6 @@ class App extends BaseConfig
238238
*/
239239
public ?string $sessionDBGroup = null;
240240

241-
/**
242-
* --------------------------------------------------------------------------
243-
* Cookie Prefix
244-
* --------------------------------------------------------------------------
245-
*
246-
* Set a cookie name prefix if you need to avoid collisions.
247-
*
248-
* @deprecated use Config\Cookie::$prefix property instead.
249-
*/
250-
public string $cookiePrefix = '';
251-
252-
/**
253-
* --------------------------------------------------------------------------
254-
* Cookie Domain
255-
* --------------------------------------------------------------------------
256-
*
257-
* Set to `.your-domain.com` for site-wide cookies.
258-
*
259-
* @deprecated use Config\Cookie::$domain property instead.
260-
*/
261-
public string $cookieDomain = '';
262-
263-
/**
264-
* --------------------------------------------------------------------------
265-
* Cookie Path
266-
* --------------------------------------------------------------------------
267-
*
268-
* Typically will be a forward slash.
269-
*
270-
* @deprecated use Config\Cookie::$path property instead.
271-
*/
272-
public string $cookiePath = '/';
273-
274-
/**
275-
* --------------------------------------------------------------------------
276-
* Cookie Secure
277-
* --------------------------------------------------------------------------
278-
*
279-
* Cookie will only be set if a secure HTTPS connection exists.
280-
*
281-
* @deprecated use Config\Cookie::$secure property instead.
282-
*/
283-
public bool $cookieSecure = false;
284-
285-
/**
286-
* --------------------------------------------------------------------------
287-
* Cookie HttpOnly
288-
* --------------------------------------------------------------------------
289-
*
290-
* Cookie will only be accessible via HTTP(S) (no JavaScript).
291-
*
292-
* @deprecated use Config\Cookie::$httponly property instead.
293-
*/
294-
public bool $cookieHTTPOnly = true;
295-
296-
/**
297-
* --------------------------------------------------------------------------
298-
* Cookie SameSite
299-
* --------------------------------------------------------------------------
300-
*
301-
* Configure cookie SameSite setting. Allowed values are:
302-
* - None
303-
* - Lax
304-
* - Strict
305-
* - ''
306-
*
307-
* Alternatively, you can use the constant names:
308-
* - `Cookie::SAMESITE_NONE`
309-
* - `Cookie::SAMESITE_LAX`
310-
* - `Cookie::SAMESITE_STRICT`
311-
*
312-
* Defaults to `Lax` for compatibility with modern browsers. Setting `''`
313-
* (empty string) means default SameSite attribute set by browsers (`Lax`)
314-
* will be set on cookies. If set to `None`, `$cookieSecure` must also be set.
315-
*
316-
* @deprecated use Config\Cookie::$samesite property instead.
317-
*/
318-
public ?string $cookieSameSite = 'Lax';
319-
320241
/**
321242
* --------------------------------------------------------------------------
322243
* Reverse Proxy IPs

app/Config/Cookie.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class Cookie extends BaseConfig
8484
* Defaults to `Lax` for compatibility with modern browsers. Setting `''`
8585
* (empty string) means default SameSite attribute set by browsers (`Lax`)
8686
* will be set on cookies. If set to `None`, `$secure` must also be set.
87+
*
88+
* @phpstan-var 'None'|'Lax'|'Strict'|''
8789
*/
8890
public string $samesite = 'Lax';
8991

system/HTTP/Response.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
use CodeIgniter\Cookie\Cookie;
1515
use CodeIgniter\Cookie\CookieStore;
16-
use CodeIgniter\Cookie\Exceptions\CookieException;
1716
use CodeIgniter\HTTP\Exceptions\HTTPException;
1817
use Config\App;
18+
use Config\Cookie as CookieConfig;
1919
use Config\Services;
2020

2121
/**
@@ -156,31 +156,12 @@ public function __construct($config)
156156

157157
$this->CSPEnabled = $config->CSPEnabled;
158158

159-
// DEPRECATED COOKIE MANAGEMENT
160-
161-
$this->cookiePrefix = $config->cookiePrefix;
162-
$this->cookieDomain = $config->cookieDomain;
163-
$this->cookiePath = $config->cookiePath;
164-
$this->cookieSecure = $config->cookieSecure;
165-
$this->cookieHTTPOnly = $config->cookieHTTPOnly;
166-
$this->cookieSameSite = $config->cookieSameSite ?? Cookie::SAMESITE_LAX;
159+
$this->cookieStore = new CookieStore([]);
167160

168-
$config->cookieSameSite ??= Cookie::SAMESITE_LAX;
161+
/** @var CookieConfig $cookie */
162+
$cookie = config('Cookie');
169163

170-
if (! in_array(strtolower($config->cookieSameSite ?: Cookie::SAMESITE_LAX), Cookie::ALLOWED_SAMESITE_VALUES, true)) {
171-
throw CookieException::forInvalidSameSite($config->cookieSameSite);
172-
}
173-
174-
$this->cookieStore = new CookieStore([]);
175-
Cookie::setDefaults(config('Cookie') ?? [
176-
// @todo Remove this fallback when deprecated `App` members are removed
177-
'prefix' => $config->cookiePrefix,
178-
'path' => $config->cookiePath,
179-
'domain' => $config->cookieDomain,
180-
'secure' => $config->cookieSecure,
181-
'httponly' => $config->cookieHTTPOnly,
182-
'samesite' => $config->cookieSameSite ?? Cookie::SAMESITE_LAX,
183-
]);
164+
Cookie::setDefaults($cookie);
184165

185166
// Default to an HTML Content-Type. Devs can override if needed.
186167
$this->setContentType('text/html');

system/Helpers/cookie_helper.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* the LICENSE file that was distributed with this source code.
1010
*/
1111

12-
use Config\App;
1312
use Config\Cookie;
1413
use Config\Services;
1514

@@ -68,11 +67,10 @@ function set_cookie(
6867
function get_cookie($index, bool $xssClean = false, ?string $prefix = '')
6968
{
7069
if ($prefix === '') {
71-
/** @var Cookie|null $cookie */
70+
/** @var Cookie $cookie */
7271
$cookie = config('Cookie');
7372

74-
// @TODO Remove Config\App fallback when deprecated `App` members are removed.
75-
$prefix = $cookie instanceof Cookie ? $cookie->prefix : config('App')->cookiePrefix;
73+
$prefix = $cookie->prefix;
7674
}
7775

7876
$request = Services::request();

system/Security/Security.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ public function __construct(App $config)
195195
}
196196

197197
if ($this->isCSRFCookie()) {
198-
$this->configureCookie($config);
198+
/** @var CookieConfig $cookie */
199+
$cookie = config('Cookie');
200+
201+
$this->configureCookie($cookie);
199202
} else {
200203
// Session based CSRF protection
201204
$this->configureSession();
@@ -220,20 +223,11 @@ private function configureSession(): void
220223
$this->session = Services::session();
221224
}
222225

223-
private function configureCookie(App $config): void
226+
private function configureCookie(CookieConfig $cookie): void
224227
{
225-
/** @var CookieConfig|null $cookie */
226-
$cookie = config('Cookie');
227-
228-
if ($cookie instanceof CookieConfig) {
229-
$cookiePrefix = $cookie->prefix;
230-
$this->cookieName = $cookiePrefix . $this->rawCookieName;
231-
Cookie::setDefaults($cookie);
232-
} else {
233-
// `Config/Cookie.php` is absence
234-
$cookiePrefix = $config->cookiePrefix;
235-
$this->cookieName = $cookiePrefix . $this->rawCookieName;
236-
}
228+
$cookiePrefix = $cookie->prefix;
229+
$this->cookieName = $cookiePrefix . $this->rawCookieName;
230+
Cookie::setDefaults($cookie);
237231
}
238232

239233
/**

system/Session/Handlers/BaseHandler.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,22 +122,13 @@ public function __construct(AppConfig $config, string $ipAddress)
122122
$this->savePath = $config->sessionSavePath;
123123
}
124124

125-
/** @var CookieConfig|null $cookie */
125+
/** @var CookieConfig $cookie */
126126
$cookie = config('Cookie');
127127

128-
if ($cookie instanceof CookieConfig) {
129-
// Session cookies have no prefix.
130-
$this->cookieDomain = $cookie->domain;
131-
$this->cookiePath = $cookie->path;
132-
$this->cookieSecure = $cookie->secure;
133-
} else {
134-
// @TODO Remove this fallback when deprecated `App` members are removed.
135-
// `Config/Cookie.php` is absence
136-
// Session cookies have no prefix.
137-
$this->cookieDomain = $config->cookieDomain;
138-
$this->cookiePath = $config->cookiePath;
139-
$this->cookieSecure = $config->cookieSecure;
140-
}
128+
// Session cookies have no prefix.
129+
$this->cookieDomain = $cookie->domain;
130+
$this->cookiePath = $cookie->path;
131+
$this->cookieSecure = $cookie->secure;
141132

142133
$this->ipAddress = $ipAddress;
143134
}

system/Session/Session.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,22 +188,16 @@ public function __construct(SessionHandlerInterface $driver, App $config)
188188
$this->sessionRegenerateDestroy = $config->sessionRegenerateDestroy ?? $this->sessionRegenerateDestroy;
189189
}
190190

191-
// DEPRECATED COOKIE MANAGEMENT
192-
$this->cookiePath = $config->cookiePath ?? $this->cookiePath;
193-
$this->cookieDomain = $config->cookieDomain ?? $this->cookieDomain;
194-
$this->cookieSecure = $config->cookieSecure ?? $this->cookieSecure;
195-
$this->cookieSameSite = $config->cookieSameSite ?? $this->cookieSameSite;
196-
197191
/** @var CookieConfig $cookie */
198192
$cookie = config('Cookie');
199193

200194
$this->cookie = (new Cookie($this->sessionCookieName, '', [
201195
'expires' => $this->sessionExpiration === 0 ? 0 : Time::now()->getTimestamp() + $this->sessionExpiration,
202-
'path' => $cookie->path ?? $config->cookiePath,
203-
'domain' => $cookie->domain ?? $config->cookieDomain,
204-
'secure' => $cookie->secure ?? $config->cookieSecure,
196+
'path' => $cookie->path,
197+
'domain' => $cookie->domain,
198+
'secure' => $cookie->secure,
205199
'httponly' => true, // for security
206-
'samesite' => $cookie->samesite ?? $config->cookieSameSite ?? Cookie::SAMESITE_LAX,
200+
'samesite' => $cookie->samesite ?? Cookie::SAMESITE_LAX,
207201
'raw' => $cookie->raw ?? false,
208202
]))->withPrefix(''); // Cookie prefix should be ignored.
209203

system/Test/Mock/MockAppConfig.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ class MockAppConfig extends App
1717
{
1818
public string $baseURL = 'http://example.com/';
1919
public string $uriProtocol = 'REQUEST_URI';
20-
public string $cookiePrefix = '';
21-
public string $cookieDomain = '';
22-
public string $cookiePath = '/';
23-
public bool $cookieSecure = false;
24-
public bool $cookieHTTPOnly = false;
25-
public ?string $cookieSameSite = 'Lax';
2620
public array $proxyIPs = [];
2721
public string $CSRFTokenName = 'csrf_test_name';
2822
public string $CSRFHeaderName = 'X-CSRF-TOKEN';

system/Test/Mock/MockCLIConfig.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ class MockCLIConfig extends App
1717
{
1818
public string $baseURL = 'http://example.com/';
1919
public string $uriProtocol = 'REQUEST_URI';
20-
public string $cookiePrefix = '';
21-
public string $cookieDomain = '';
22-
public string $cookiePath = '/';
23-
public bool $cookieSecure = false;
24-
public bool $cookieHTTPOnly = false;
25-
public ?string $cookieSameSite = 'Lax';
2620
public array $proxyIPs = [];
2721
public string $CSRFTokenName = 'csrf_test_name';
2822
public string $CSRFCookieName = 'csrf_cookie_name';

tests/system/API/ResponseTraitTest.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\API;
1313

14+
use CodeIgniter\Config\Factories;
1415
use CodeIgniter\Format\FormatterInterface;
1516
use CodeIgniter\Format\JSONFormatter;
1617
use CodeIgniter\Format\XMLFormatter;
@@ -20,6 +21,7 @@
2021
use CodeIgniter\Test\Mock\MockIncomingRequest;
2122
use CodeIgniter\Test\Mock\MockResponse;
2223
use Config\App;
24+
use Config\Cookie;
2325
use stdClass;
2426

2527
/**
@@ -59,17 +61,25 @@ protected function makeController(array $userConfig = [], string $uri = 'http://
5961
'negotiateLocale' => false,
6062
'supportedLocales' => ['en'],
6163
'CSPEnabled' => false,
62-
'cookiePrefix' => '',
63-
'cookieDomain' => '',
64-
'cookiePath' => '/',
65-
'cookieSecure' => false,
66-
'cookieHTTPOnly' => false,
6764
'proxyIPs' => [],
68-
'cookieSameSite' => 'Lax',
6965
] as $key => $value) {
7066
$config->{$key} = $value;
7167
}
7268

69+
$cookie = new Cookie();
70+
71+
foreach ([
72+
'prefix' => '',
73+
'domain' => '',
74+
'path' => '/',
75+
'secure' => false,
76+
'httponly' => false,
77+
'samesite' => 'Lax',
78+
] as $key => $value) {
79+
$cookie->{$key} = $value;
80+
}
81+
Factories::injectMock('config', 'Cookie', $cookie);
82+
7383
if ($this->request === null) {
7484
$this->request = new MockIncomingRequest($config, new URI($uri), null, new UserAgent());
7585
$this->response = new MockResponse($config);
@@ -532,17 +542,25 @@ public function testFormatByRequestNegotiateIfFormatIsNotJsonOrXML()
532542
'negotiateLocale' => false,
533543
'supportedLocales' => ['en'],
534544
'CSPEnabled' => false,
535-
'cookiePrefix' => '',
536-
'cookieDomain' => '',
537-
'cookiePath' => '/',
538-
'cookieSecure' => false,
539-
'cookieHTTPOnly' => false,
540545
'proxyIPs' => [],
541-
'cookieSameSite' => 'Lax',
542546
] as $key => $value) {
543547
$config->{$key} = $value;
544548
}
545549

550+
$cookie = new Cookie();
551+
552+
foreach ([
553+
'prefix' => '',
554+
'domain' => '',
555+
'path' => '/',
556+
'secure' => false,
557+
'httponly' => false,
558+
'samesite' => 'Lax',
559+
] as $key => $value) {
560+
$cookie->{$key} = $value;
561+
}
562+
Factories::injectMock('config', 'Cookie', $cookie);
563+
546564
$request = new MockIncomingRequest($config, new URI($config->baseURL), null, new UserAgent());
547565
$response = new MockResponse($config);
548566

0 commit comments

Comments
 (0)