Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fallback for Config\Cookie #4625

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions system/Cookie/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use ArrayAccess;
use CodeIgniter\Cookie\Exceptions\CookieException;
use Config\App;
use Config\Cookie as CookieConfig;
use DateTimeInterface;
use InvalidArgumentException;
Expand Down Expand Up @@ -128,7 +127,6 @@ class Cookie implements ArrayAccess, CloneableCookieInterface
public static function setDefaults($config = [])
{
$oldDefaults = self::$defaults;

$newDefaults = [];

if ($config instanceof CookieConfig)
Expand Down Expand Up @@ -222,16 +220,15 @@ final public function __construct(string $name, string $value = '', array $optio
unset($options['max-age']);
}

// to retain BC
// to retain backward compatibility with previous versions' fallback
$prefix = $options['prefix'] ?: self::$defaults['prefix'];
$path = $options['path'] ?: self::$defaults['path'];
$domain = $options['domain'] ?: self::$defaults['domain'];
$secure = $options['secure'] ?: self::$defaults['secure'];
$httponly = $options['httponly'] ?: self::$defaults['httponly'];
$samesite = $options['samesite'] ?: self::$defaults['samesite'];
$raw = $options['raw'] ?: self::$defaults['raw'];

$this->validateName($name, $raw);
$this->validateName($name, $options['raw']);
$this->validatePrefix($prefix, $secure, $path, $domain);
$this->validateSameSite($samesite, $secure);

Expand All @@ -244,7 +241,7 @@ final public function __construct(string $name, string $value = '', array $optio
$this->secure = $secure;
$this->httponly = $httponly;
$this->samesite = ucfirst(strtolower($samesite));
$this->raw = $raw;
$this->raw = $options['raw'];
}

//=========================================================================
Expand Down Expand Up @@ -289,9 +286,7 @@ public function getPrefixedName(): string
else
{
$search = str_split(self::$reservedCharsList);
$replace = array_map(static function (string $char): string {
return rawurlencode($char);
}, $search);
$replace = array_map('rawurlencode', $search);

$name .= str_replace($search, $replace, $this->getName());
}
paulbalandan marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
10 changes: 9 additions & 1 deletion system/HTTP/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,15 @@ public function __construct($config)
}

$this->cookieStore = new CookieStore([]);
Cookie::setDefaults(config('Cookie'));
Cookie::setDefaults(config('Cookie') ?? [
// @todo Remove this fallback when deprecated `App` members are removed
'prefix' => $config->cookiePrefix,
'path' => $config->cookiePath,
'domain' => $config->cookieDomain,
'secure' => $config->cookieSecure,
'httponly' => $config->cookieHTTPOnly,
'samesite' => $config->cookieSameSite ?? Cookie::SAMESITE_LAX,
]);

// Default to an HTML Content-Type. Devs can override if needed.
$this->setContentType('text/html');
Expand Down
8 changes: 2 additions & 6 deletions system/Security/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ class Security implements SecurityInterface
*/
public function __construct(App $config)
{
/**
* @var SecurityConfig
*/
/** @var SecurityConfig */
$security = config('Security');

// Store CSRF-related configurations
Expand All @@ -138,9 +136,7 @@ public function __construct(App $config)
$this->regenerate = $security->regenerate ?? $config->CSRFRegenerate ?? $this->regenerate;
$rawCookieName = $security->cookieName ?? $config->CSRFCookieName ?? $this->cookieName;

/**
* @var CookieConfig
*/
/** @var CookieConfig */
$cookie = config('Cookie');

$cookiePrefix = $cookie->prefix ?? $config->cookiePrefix;
Expand Down
16 changes: 7 additions & 9 deletions system/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,17 @@ public function __construct(SessionHandlerInterface $driver, App $config)
$this->cookieSecure = $config->cookieSecure ?? $this->cookieSecure;
$this->cookieSameSite = $config->cookieSameSite ?? $this->cookieSameSite;

/**
* @var CookieConfig
*/
$config = config('Cookie');
/** @var CookieConfig */
$cookie = config('Cookie');

$this->cookie = new Cookie($this->sessionCookieName, '', [
'expires' => $this->sessionExpiration === 0 ? 0 : time() + $this->sessionExpiration,
'path' => $config->path,
'domain' => $config->domain,
'secure' => $config->secure,
'path' => $cookie->path ?? $config->cookiePath,
'domain' => $cookie->domain ?? $config->cookieDomain,
'secure' => $cookie->secure ?? $config->cookieSecure,
'httponly' => true, // for security
'samesite' => $config->samesite,
'raw' => $config->raw,
'samesite' => $cookie->samesite ?? $config->cookieSameSite ?? Cookie::SAMESITE_LAX,
'raw' => $cookie->raw ?? false,
]);

helper('array');
Expand Down
40 changes: 12 additions & 28 deletions tests/system/Cookie/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,40 +174,24 @@ public function testSetCookieHeaderCreation(string $header, array $changed): voi
public static function setCookieHeaderProvider(): iterable
{
yield 'basic' => [
'test=value',
[
'name' => 'test',
'value' => 'value',
],
];
'test=value',
['name' => 'test', 'value' => 'value'],
];

yield 'empty-value' => [
'test',
[
'name' => 'test',
'value' => '',
],
];
'test',
['name' => 'test', 'value' => ''],
];

yield 'with-other-attrs' => [
'test=value; Max-Age=3600; Path=/web',
[
'name' => 'test',
'value' => 'value',
'path' => '/web',
],
];
'test=value; Max-Age=3600; Path=/web',
['name' => 'test', 'value' => 'value', 'path' => '/web'],
];

yield 'with-flags' => [
'test=value; Secure; HttpOnly; SameSite=Lax',
[
'name' => 'test',
'value' => 'value',
'secure' => true,
'httponly' => true,
'samesite' => 'Lax',
],
];
'test=value; Secure; HttpOnly; SameSite=Lax',
['name' => 'test', 'value' => 'value', 'secure' => true, 'httponly' => true, 'samesite' => 'Lax'],
];
}

public function testValidNamePerRfcYieldsSameNameRegardlessOfRawParam(): void
Expand Down