Skip to content

Commit 481e426

Browse files
committed
Backport security fixes
1 parent b0fe0a9 commit 481e426

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

system/core/Security.php

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,32 @@ class CI_Security {
9898

9999
/**
100100
* Constructor
101+
*
102+
* @return void
101103
*/
102104
public function __construct()
103105
{
104-
// CSRF config
105-
foreach(array('csrf_expire', 'csrf_token_name', 'csrf_cookie_name') as $key)
106+
// Is CSRF protection enabled?
107+
if (config_item('csrf_protection') === TRUE)
106108
{
107-
if (FALSE !== ($val = config_item($key)))
109+
// CSRF config
110+
foreach (array('csrf_expire', 'csrf_token_name', 'csrf_cookie_name') as $key)
108111
{
109-
$this->{'_'.$key} = $val;
112+
if (FALSE !== ($val = config_item($key)))
113+
{
114+
$this->{'_'.$key} = $val;
115+
}
110116
}
111-
}
112117

113-
// Append application specific cookie prefix
114-
if (config_item('cookie_prefix'))
115-
{
116-
$this->_csrf_cookie_name = config_item('cookie_prefix').$this->_csrf_cookie_name;
117-
}
118+
// Append application specific cookie prefix
119+
if (config_item('cookie_prefix'))
120+
{
121+
$this->_csrf_cookie_name = config_item('cookie_prefix').$this->_csrf_cookie_name;
122+
}
118123

119-
// Set the CSRF hash
120-
$this->_csrf_set_hash();
124+
// Set the CSRF hash
125+
$this->_csrf_set_hash();
126+
}
121127

122128
log_message('debug', "Security Class Initialized");
123129
}
@@ -131,15 +137,14 @@ public function __construct()
131137
*/
132138
public function csrf_verify()
133139
{
134-
// If no POST data exists we will set the CSRF cookie
135-
if (count($_POST) == 0)
140+
// If it's not a POST request we will set the CSRF cookie
141+
if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST')
136142
{
137143
return $this->csrf_set_cookie();
138144
}
139145

140146
// Do the tokens exist in both the _POST and _COOKIE arrays?
141-
if ( ! isset($_POST[$this->_csrf_token_name]) OR
142-
! isset($_COOKIE[$this->_csrf_cookie_name]))
147+
if ( ! isset($_POST[$this->_csrf_token_name], $_COOKIE[$this->_csrf_cookie_name]))
143148
{
144149
$this->csrf_show_error();
145150
}
@@ -159,7 +164,7 @@ public function csrf_verify()
159164
$this->_csrf_set_hash();
160165
$this->csrf_set_cookie();
161166

162-
log_message('debug', "CSRF token verified ");
167+
log_message('debug', 'CSRF token verified');
163168

164169
return $this;
165170
}
@@ -176,14 +181,9 @@ public function csrf_set_cookie()
176181
$expire = time() + $this->_csrf_expire;
177182
$secure_cookie = (config_item('cookie_secure') === TRUE) ? 1 : 0;
178183

179-
if ($secure_cookie)
184+
if ($secure_cookie && (empty($_SERVER['HTTPS']) OR strtolower($_SERVER['HTTPS']) === 'off'))
180185
{
181-
$req = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : FALSE;
182-
183-
if ( ! $req OR $req == 'off')
184-
{
185-
return FALSE;
186-
}
186+
return FALSE;
187187
}
188188

189189
setcookie($this->_csrf_cookie_name, $this->_csrf_hash, $expire, config_item('cookie_path'), config_item('cookie_domain'), $secure_cookie);
@@ -871,7 +871,6 @@ protected function _csrf_set_hash()
871871
}
872872

873873
}
874-
// END Security Class
875874

876875
/* End of file Security.php */
877-
/* Location: ./system/libraries/Security.php */
876+
/* Location: ./system/libraries/Security.php */

user_guide/changelog.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ <h3>Bug fixes for 2.1.3:</h3>
6868
<li>Fixed a bug (#1699) - <a href="libraries/migration.html">Migration Library</a> ignored the <samp>$config['migration_path']</samp> setting.</li>
6969
<li>Fixed a bug (#227) - <a href="libraries/input.html">Input Library</a> allowed unconditional spoofing of HTTP clients' IP addresses through the HTTP_CLIENT_IP header.</li>
7070
<li>Fixed a bug (#907) - <a href="libraries/input.html">Input Library</a> ignored HTTP_X_CLUSTER_CLIENT_IP and HTTP_X_CLIENT_IP headers when checking for proxies.</li>
71+
<li>Fixed a bug (#940) - <samp>csrf_verify()</samp> used to set the CSRF cookie while processing a POST request with no actual POST data, which resulted in validating a request that should be considered invalid.</li>
72+
<li>Fixed a bug in the <a href="libraries/security.html">Security Library</a> where a CSRF cookie was created even if <samp>$config['csrf_protection']</samp> is set tot FALSE.</li>
7173
</ul>
7274

7375
<h2>Version 2.1.2</h2>

0 commit comments

Comments
 (0)