Skip to content
Open
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
24 changes: 6 additions & 18 deletions system/HTTP/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
namespace CodeIgniter\HTTP;

use CodeIgniter\Cookie\Cookie;
use CodeIgniter\Cookie\CookieStore;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use Config\Cookie as CookieConfig;

/**
* Representation of an outgoing, server-side response.
Expand All @@ -38,7 +36,7 @@ class Response extends Message implements ResponseInterface
/**
* HTTP status codes
*
* @var array
* @var array<int, string>
*/
protected static $statusCodes = [
// 1xx: Informational
Expand Down Expand Up @@ -140,23 +138,14 @@ class Response extends Message implements ResponseInterface
*/
protected $pretend = false;

/**
* Construct a non-caching response with a default content type of `text/html`.
*/
public function __construct()
{
// Default to a non-caching page.
// Also ensures that a Cache-control header exists.
$this->noCache();

// We need CSP object even if not enabled to avoid calls to non existing methods
$this->CSP = service('csp');

$this->cookieStore = new CookieStore([]);

$cookie = config(CookieConfig::class);

Cookie::setDefaults($cookie);
Cookie::setDefaults(config('Cookie'));

// Default to an HTML Content-Type. Devs can override if needed.
$this->setContentType('text/html');
$this->noCache()->setContentType('text/html');
Comment thread
michalsn marked this conversation as resolved.
}

/**
Expand All @@ -168,7 +157,6 @@ public function __construct()
* @return $this
*
* @internal For testing purposes only.
* @testTag only available to test code
*/
public function pretend(bool $pretend = true)
{
Expand Down
107 changes: 89 additions & 18 deletions system/HTTP/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\HTTP;

use CodeIgniter\Config\Services;
use CodeIgniter\Cookie\Cookie;
use CodeIgniter\Cookie\CookieStore;
use CodeIgniter\Cookie\Exceptions\CookieException;
Expand All @@ -21,6 +22,8 @@
use CodeIgniter\I18n\Time;
use CodeIgniter\Pager\PagerInterface;
use CodeIgniter\Security\Exceptions\SecurityException;
use Config\App;
use Config\ContentSecurityPolicy as ContentSecurityPolicyConfig;
use Config\Cookie as CookieConfig;
use DateTime;
use DateTimeZone;
Expand All @@ -31,21 +34,30 @@
* Additional methods to make a PSR-7 Response class
* compliant with the framework's own ResponseInterface.
*
* @property array<int, string> $statusCodes
* @property string|null $body
*
* @see https://github.com/php-fig/http-message/blob/master/src/ResponseInterface.php
*/
trait ResponseTrait
{
/**
* Content security policy handler
* Content security policy handler.
*
* Lazily instantiated on first use via `self::getCSP()` so that the
* ContentSecurityPolicy class is not loaded on requests that do not use CSP.
*
* @var ContentSecurityPolicy
* @var ContentSecurityPolicy|null
*/
protected $CSP;

/**
* CookieStore instance.
*
* @var CookieStore
* Lazily instantiated on first cookie-related call so that the Cookie and
* CookieStore classes are not loaded on requests that do not use cookies.
*
* @var CookieStore|null
*/
protected $cookieStore;

Expand Down Expand Up @@ -77,19 +89,17 @@ trait ResponseTrait
*/
public function setStatusCode(int $code, string $reason = '')
{
// Valid range?
if ($code < 100 || $code > 599) {
throw HTTPException::forInvalidStatusCode($code);
}

// Unknown and no message?
if (! array_key_exists($code, static::$statusCodes) && ($reason === '')) {
if (! array_key_exists($code, static::$statusCodes) && $reason === '') {
throw HTTPException::forUnkownStatusCode($code);
}

$this->statusCode = $code;

$this->reason = ($reason !== '') ? $reason : static::$statusCodes[$code];
$this->reason = $reason !== '' ? $reason : static::$statusCodes[$code];

return $this;
}
Expand Down Expand Up @@ -366,8 +376,10 @@ public function setLastModified($date)
public function send()
{
// If we're enforcing a Content Security Policy,
// we need to give it a chance to build out it's headers.
$this->CSP->finalize($this);
// we need to give it a chance to build out its headers.
if ($this->shouldFinalizeCsp()) {
$this->getCSP()->finalize($this);
}

$this->sendHeaders();
$this->sendCookies();
Expand All @@ -376,6 +388,44 @@ public function send()
return $this;
}

/**
* Decides whether {@see ContentSecurityPolicy::finalize()} should run for
* this response. Keeping the CSP class unloaded on requests that do not
* need it avoids the cost of constructing a 1000+ line service on every
* request.
*/
private function shouldFinalizeCsp(): bool
{
// Developer already touched CSP through getCSP(); respect it.
if ($this->CSP !== null) {
return true;
}

// A CSP instance has been registered (e.g., via Services::injectMock()
// or any earlier service('csp') call) — reuse it instead of skipping.
if (Services::has('csp')) {
return true;
}

if (config(App::class)->CSPEnabled) {
return true;
}

// Placeholders in the body still need to be stripped even when CSP
// is disabled, so the body is scanned for the configured nonce tags
// before committing to loading the full CSP class.
$body = (string) $this->body;

if ($body === '') {
return false;
}

$cspConfig = config(ContentSecurityPolicyConfig::class);

return str_contains($body, $cspConfig->scriptNonceTag)
|| str_contains($body, $cspConfig->styleNonceTag);
}

/**
* Sends the headers of this HTTP response to the browser.
*
Expand Down Expand Up @@ -518,8 +568,10 @@ public function setCookie(
$httponly = null,
$samesite = null,
) {
$store = $this->initializeCookieStore();

if ($name instanceof Cookie) {
$this->cookieStore = $this->cookieStore->put($name);
$this->cookieStore = $store->put($name);

return $this;
}
Expand Down Expand Up @@ -553,7 +605,7 @@ public function setCookie(
'samesite' => $samesite ?? '',
]);

$this->cookieStore = $this->cookieStore->put($cookie);
$this->cookieStore = $store->put($cookie);

return $this;
}
Expand All @@ -565,17 +617,18 @@ public function setCookie(
*/
public function getCookieStore()
{
return $this->cookieStore;
return $this->initializeCookieStore();
}

/**
* Checks to see if the Response has a specified cookie or not.
*/
public function hasCookie(string $name, ?string $value = null, string $prefix = ''): bool
{
$store = $this->initializeCookieStore();
$prefix = $prefix !== '' ? $prefix : Cookie::setDefaults()['prefix']; // to retain BC

return $this->cookieStore->has($name, $prefix, $value);
return $store->has($name, $prefix, $value);
}

/**
Expand All @@ -588,14 +641,16 @@ public function hasCookie(string $name, ?string $value = null, string $prefix =
*/
public function getCookie(?string $name = null, string $prefix = '')
{
$store = $this->initializeCookieStore();

if ((string) $name === '') {
return $this->cookieStore->display();
return $store->display();
}

try {
$prefix = $prefix !== '' ? $prefix : Cookie::setDefaults()['prefix']; // to retain BC

return $this->cookieStore->get($name, $prefix);
return $store->get($name, $prefix);
} catch (CookieException $e) {
log_message('error', (string) $e);

Expand All @@ -614,10 +669,10 @@ public function deleteCookie(string $name = '', string $domain = '', string $pat
return $this;
}

$store = $this->initializeCookieStore();
$prefix = $prefix !== '' ? $prefix : Cookie::setDefaults()['prefix']; // to retain BC

$prefixed = $prefix . $name;
$store = $this->cookieStore;
$found = false;

/** @var Cookie $cookie */
Expand Down Expand Up @@ -653,6 +708,10 @@ public function deleteCookie(string $name = '', string $domain = '', string $pat
*/
public function getCookies()
{
if ($this->cookieStore === null) {
return [];
}

return $this->cookieStore->display();
}

Expand All @@ -663,7 +722,7 @@ public function getCookies()
*/
protected function sendCookies()
{
if ($this->pretend) {
if ($this->pretend || $this->cookieStore === null) {
return;
}

Expand Down Expand Up @@ -753,6 +812,18 @@ public function download(string $filename = '', $data = '', bool $setMime = fals

public function getCSP(): ContentSecurityPolicy
{
return $this->CSP;
return $this->CSP ??= service('csp');
}

/**
* Lazily initializes the cookie store and the Cookie class defaults.
* Called by every cookie-related method so cookie machinery is only
* loaded when the developer actually interacts with cookies.
*/
private function initializeCookieStore(): CookieStore
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small clarification: for initialization, it is not necessary to return something (void). If you use return for the call chain, perhaps you should rename it to getCookieStore()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, you're right!

{
$this->cookieStore ??= new CookieStore([]);

return $this->cookieStore;
}
}
Loading
Loading