-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
CsrfComponent.php
189 lines (171 loc) · 6.33 KB
/
CsrfComponent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Controller\Component;
use Cake\Controller\Component;
use Cake\Event\Event;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Exception\InvalidCsrfTokenException;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\I18n\Time;
use Cake\Utility\Security;
/**
* Provides CSRF protection & validation.
*
* This component adds a CSRF token to a cookie. The cookie value is compared to
* request data, or the X-CSRF-Token header on each PATCH, POST,
* PUT, or DELETE request.
*
* If the request data is missing or does not match the cookie data,
* an InvalidCsrfTokenException will be raised.
*
* This component integrates with the FormHelper automatically and when
* used together your forms will have CSRF tokens automatically added
* when `$this->Form->create(...)` is used in a view.
*
* @deprecated 3.5.0 Use Cake\Http\Middleware\CsrfProtectionMiddleware instead.
*/
class CsrfComponent extends Component
{
/**
* Default config for the CSRF handling.
*
* - cookieName = The name of the cookie to send.
* - expiry = How long the CSRF token should last. Defaults to browser session.
* - secure = Whether or not the cookie will be set with the Secure flag. Defaults to false.
* - httpOnly = Whether or not the cookie will be set with the HttpOnly flag. Defaults to false.
* - field = The form field to check. Changing this will also require configuring
* FormHelper.
*
* @var array
*/
protected $_defaultConfig = [
'cookieName' => 'csrfToken',
'expiry' => 0,
'secure' => false,
'httpOnly' => false,
'field' => '_csrfToken',
];
/**
* Warn if CsrfComponent is used together with CsrfProtectionMiddleware
*
* @param array $config The config data.
* @return void
*/
public function initialize(array $config)
{
if ($this->getController()->getRequest()->getParam('_csrfToken') !== false) {
deprecationWarning('Loading CsrfComponent while CsrfProtectionMiddleware is active ' .
'will corrupt CSRF data and form submitting will fail.');
}
}
/**
* Startup callback.
*
* Validates the CSRF token for POST data. If
* the request is a GET request, and the cookie value is absent a cookie will be set.
*
* Once a cookie is set it will be copied into request->getParam('_csrfToken')
* so that application and framework code can easily access the csrf token.
*
* RequestAction requests do not get checked, nor will
* they set a cookie should it be missing.
*
* @param \Cake\Event\Event $event Event instance.
* @return void
*/
public function startup(Event $event)
{
/** @var \Cake\Controller\Controller $controller */
$controller = $event->getSubject();
$request = $controller->getRequest();
$response = $controller->getResponse();
$cookieName = $this->_config['cookieName'];
$cookieData = $request->getCookie($cookieName);
if ($cookieData) {
$request = $request->withParam('_csrfToken', $cookieData);
}
if ($request->is('requested')) {
$controller->setRequest($request);
return;
}
if ($request->is('get') && $cookieData === null) {
list($request, $response) = $this->_setCookie($request, $response);
$controller->setResponse($response);
}
if ($request->is(['put', 'post', 'delete', 'patch']) || $request->getData()) {
$this->_validateToken($request);
$request = $request->withoutData($this->_config['field']);
}
$controller->setRequest($request);
}
/**
* Events supported by this component.
*
* @return array
*/
public function implementedEvents()
{
return [
'Controller.startup' => 'startup',
];
}
/**
* Set the cookie in the response.
*
* Also sets the request->params['_csrfToken'] so the newly minted
* token is available in the request data.
*
* @param \Cake\Http\ServerRequest $request The request object.
* @param \Cake\Http\Response $response The response object.
* @return array An array of the modified request, response.
*/
protected function _setCookie(ServerRequest $request, Response $response)
{
$expiry = new Time($this->_config['expiry']);
$value = hash('sha512', Security::randomBytes(16), false);
$request = $request->withParam('_csrfToken', $value);
$cookie = new Cookie(
$this->_config['cookieName'],
$value,
$expiry,
$request->getAttribute('webroot'),
'',
(bool)$this->_config['secure'],
(bool)$this->_config['httpOnly']
);
$response = $response->withCookie($cookie);
return [$request, $response];
}
/**
* Validate the request data against the cookie token.
*
* @param \Cake\Http\ServerRequest $request The request to validate against.
* @throws \Cake\Http\Exception\InvalidCsrfTokenException when the CSRF token is invalid or missing.
* @return void
*/
protected function _validateToken(ServerRequest $request)
{
$cookie = $request->getCookie($this->_config['cookieName']);
$post = $request->getData($this->_config['field']);
$header = $request->getHeaderLine('X-CSRF-Token');
if (!$cookie) {
throw new InvalidCsrfTokenException(__d('cake', 'Missing CSRF token cookie'));
}
if (!Security::constantEquals($post, $cookie) && !Security::constantEquals($header, $cookie)) {
throw new InvalidCsrfTokenException(__d('cake', 'CSRF token mismatch.'));
}
}
}