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 Session based CSRF Protection #5201

Merged
merged 5 commits into from
Oct 23, 2021
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
11 changes: 11 additions & 0 deletions app/Config/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@

class Security extends BaseConfig
{
/**
* --------------------------------------------------------------------------
* CSRF Protection Method
* --------------------------------------------------------------------------
*
* Protection Method for Cross Site Request Forgery protection.
*
* @var string 'cookie' or 'session'
*/
public $csrfProtection = 'cookie';

/**
* --------------------------------------------------------------------------
* CSRF Token Name
Expand Down
1 change: 1 addition & 0 deletions depfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ ruleset:
- HTTP
Security:
- Cookie
- Session
- HTTP
Session:
- Cookie
Expand Down
1 change: 1 addition & 0 deletions env
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
# SECURITY
#--------------------------------------------------------------------

# security.csrfProtection = 'cookie'
# security.tokenName = 'csrf_token_name'
# security.headerName = 'X-CSRF-TOKEN'
# security.cookieName = 'csrf_cookie_name'
Expand Down
78 changes: 67 additions & 11 deletions system/Security/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Cookie\Cookie;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\Security\Exceptions\SecurityException;
use CodeIgniter\Session\Session;
use Config\App;
use Config\Cookie as CookieConfig;
use Config\Security as SecurityConfig;
Expand All @@ -27,6 +28,18 @@
*/
class Security implements SecurityInterface
{
public const CSRF_PROTECTION_COOKIE = 'cookie';
public const CSRF_PROTECTION_SESSION = 'session';

/**
* CSRF Protection Method
*
* Protection Method for Cross Site Request Forgery protection.
*
* @var string 'cookie' or 'session'
*/
protected $csrfProtection = self::CSRF_PROTECTION_COOKIE;

/**
* CSRF Hash
*
Expand Down Expand Up @@ -128,6 +141,13 @@ class Security implements SecurityInterface
*/
private $rawCookieName;

/**
* Session instance.
*
* @var Session
*/
private $session;

/**
* Constructor.
*
Expand All @@ -141,11 +161,12 @@ public function __construct(App $config)

// Store CSRF-related configurations
if ($security instanceof SecurityConfig) {
$this->tokenName = $security->tokenName ?? $this->tokenName;
$this->headerName = $security->headerName ?? $this->headerName;
$this->regenerate = $security->regenerate ?? $this->regenerate;
$this->rawCookieName = $security->cookieName ?? $this->rawCookieName;
$this->expires = $security->expires ?? $this->expires;
$this->csrfProtection = $security->csrfProtection ?? $this->csrfProtection;
$this->tokenName = $security->tokenName ?? $this->tokenName;
$this->headerName = $security->headerName ?? $this->headerName;
$this->regenerate = $security->regenerate ?? $this->regenerate;
$this->rawCookieName = $security->cookieName ?? $this->rawCookieName;
$this->expires = $security->expires ?? $this->expires;
} else {
// `Config/Security.php` is absence
$this->tokenName = $config->CSRFTokenName ?? $this->tokenName;
Expand All @@ -155,13 +176,28 @@ public function __construct(App $config)
$this->expires = $config->CSRFExpire ?? $this->expires;
}

$this->configureCookie($config);
if ($this->isCSRFCookie()) {
$this->configureCookie($config);
} else {
// Session based CSRF protection
$this->configureSession();
}

$this->request = Services::request();

$this->generateHash();
}

private function isCSRFCookie(): bool
{
return $this->csrfProtection === self::CSRF_PROTECTION_COOKIE;
}

private function configureSession(): void
{
$this->session = Services::session();
}

private function configureCookie(App $config): void
{
/** @var CookieConfig|null $cookie */
Expand Down Expand Up @@ -253,7 +289,12 @@ public function verify(RequestInterface $request)

if ($this->regenerate) {
$this->hash = null;
unset($_COOKIE[$this->cookieName]);
if ($this->isCSRFCookie()) {
unset($_COOKIE[$this->cookieName]);
} else {
// Session based CSRF protection
$this->session->remove($this->tokenName);
}
}

$this->generateHash();
Expand Down Expand Up @@ -409,13 +450,23 @@ protected function generateHash(): string
// We don't necessarily want to regenerate it with
// each page load since a page could contain embedded
// sub-pages causing this feature to fail
if ($this->isHashInCookie()) {
return $this->hash = $_COOKIE[$this->cookieName];
if ($this->isCSRFCookie()) {
if ($this->isHashInCookie()) {
return $this->hash = $_COOKIE[$this->cookieName];
}
} elseif ($this->session->has($this->tokenName)) {
// Session based CSRF protection
return $this->hash = $this->session->get($this->tokenName);
}

$this->hash = bin2hex(random_bytes(16));

$this->saveHashInCookie();
if ($this->isCSRFCookie()) {
$this->saveHashInCookie();
} else {
// Session based CSRF protection
$this->saveHashInSession();
}
}

return $this->hash;
Expand All @@ -428,7 +479,7 @@ private function isHashInCookie(): bool
&& preg_match('#^[0-9a-f]{32}$#iS', $_COOKIE[$this->cookieName]) === 1;
}

private function saveHashInCookie()
private function saveHashInCookie(): void
{
$this->cookie = new Cookie(
$this->rawCookieName,
Expand Down Expand Up @@ -467,4 +518,9 @@ protected function doSendCookie(): void
{
cookies([$this->cookie], false)->dispatch();
}

private function saveHashInSession(): void
{
$this->session->set($this->tokenName, $this->hash);
}
}
Loading