-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
SessionCsrfProtectionMiddleware.php
270 lines (242 loc) · 8.87 KB
/
SessionCsrfProtectionMiddleware.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 4.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Http\Middleware;
use ArrayAccess;
use Cake\Http\Exception\InvalidCsrfTokenException;
use Cake\Http\Session;
use Cake\Utility\Hash;
use Cake\Utility\Security;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use RuntimeException;
/**
* Provides CSRF protection via session based tokens.
*
* This middleware adds a CSRF token to the session. Each request must
* contain a token in request data, or the X-CSRF-Token header on each PATCH, POST,
* PUT, or DELETE request. This follows a 'synchronizer token' pattern.
*
* If the request data is missing or does not match the session data,
* an InvalidCsrfTokenException will be raised.
*
* This middleware 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.
*
* If you use this middleware *do not* also use CsrfProtectionMiddleware.
*
* @see https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#sychronizer-token-pattern
*/
class SessionCsrfProtectionMiddleware implements MiddlewareInterface
{
/**
* Config for the CSRF handling.
*
* - `key` The session key to use. Defaults to `csrfToken`
* - `field` The form field to check. Changing this will also require configuring
* FormHelper.
*
* @var array
*/
protected $_config = [
'key' => 'csrfToken',
'field' => '_csrfToken',
];
/**
* Callback for deciding whether or not to skip the token check for particular request.
*
* CSRF protection token check will be skipped if the callback returns `true`.
*
* @var callable|null
*/
protected $skipCheckCallback;
/**
* @var int
*/
public const TOKEN_VALUE_LENGTH = 32;
/**
* Constructor
*
* @param array $config Config options. See $_config for valid keys.
*/
public function __construct(array $config = [])
{
$this->_config = $config + $this->_config;
}
/**
* Checks and sets the CSRF token depending on the HTTP verb.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
* @param \Psr\Http\Server\RequestHandlerInterface $handler The request handler.
* @return \Psr\Http\Message\ResponseInterface A response.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$method = $request->getMethod();
$hasData = in_array($method, ['PUT', 'POST', 'DELETE', 'PATCH'], true)
|| $request->getParsedBody();
if (
$hasData
&& $this->skipCheckCallback !== null
&& call_user_func($this->skipCheckCallback, $request) === true
) {
$request = $this->unsetTokenField($request);
return $handler->handle($request);
}
$session = $request->getAttribute('session');
if (!$session || !($session instanceof Session)) {
throw new RuntimeException('You must have a `session` attribute to use session based CSRF tokens');
}
$token = $session->read($this->_config['key']);
if ($token === null) {
$token = $this->createToken();
$session->write($this->_config['key'], $token);
}
$request = $request->withAttribute('csrfToken', $this->saltToken($token));
if ($method === 'GET') {
return $handler->handle($request);
}
if ($hasData) {
$this->validateToken($request, $session);
$request = $this->unsetTokenField($request);
}
return $handler->handle($request);
}
/**
* Set callback for allowing to skip token check for particular request.
*
* The callback will receive request instance as argument and must return
* `true` if you want to skip token check for the current request.
*
* @param callable $callback A callable.
* @return $this
*/
public function skipCheckCallback(callable $callback)
{
$this->skipCheckCallback = $callback;
return $this;
}
/**
* Apply entropy to a CSRF token
*
* To avoid BREACH apply a random salt value to a token
* When the token is compared to the session the token needs
* to be unsalted.
*
* @param string $token The token to salt.
* @return string The salted token with the salt appended.
*/
public function saltToken(string $token): string
{
$decoded = base64_decode($token);
$length = strlen($decoded);
$salt = Security::randomBytes($length);
$salted = '';
for ($i = 0; $i < $length; $i++) {
// XOR the token and salt together so that we can reverse it later.
$salted .= chr(ord($decoded[$i]) ^ ord($salt[$i]));
}
return base64_encode($salted . $salt);
}
/**
* Remove the salt from a CSRF token.
*
* If the token is not TOKEN_VALUE_LENGTH * 2 it is an old
* unsalted value that is supported for backwards compatibility.
*
* @param string $token The token that could be salty.
* @return string An unsalted token.
*/
protected function unsaltToken(string $token): string
{
$decoded = base64_decode($token, true);
if ($decoded === false || strlen($decoded) !== static::TOKEN_VALUE_LENGTH * 2) {
return $token;
}
$salted = substr($decoded, 0, static::TOKEN_VALUE_LENGTH);
$salt = substr($decoded, static::TOKEN_VALUE_LENGTH);
$unsalted = '';
for ($i = 0; $i < static::TOKEN_VALUE_LENGTH; $i++) {
// Reverse the XOR to desalt.
$unsalted .= chr(ord($salted[$i]) ^ ord($salt[$i]));
}
return base64_encode($unsalted);
}
/**
* Remove CSRF protection token from request data.
*
* This ensures that the token does not cause failures during
* form tampering protection.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request object.
* @return \Psr\Http\Message\ServerRequestInterface
*/
protected function unsetTokenField(ServerRequestInterface $request): ServerRequestInterface
{
$body = $request->getParsedBody();
if (is_array($body)) {
unset($body[$this->_config['field']]);
$request = $request->withParsedBody($body);
}
return $request;
}
/**
* Create a new token to be used for CSRF protection
*
* This token is a simple unique random value as the compare
* value is stored in the session where it cannot be tampered with.
*
* @return string
*/
public function createToken(): string
{
return base64_encode(Security::randomBytes(static::TOKEN_VALUE_LENGTH));
}
/**
* Validate the request data against the cookie token.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request to validate against.
* @param \Cake\Http\Session $session The session instance.
* @return void
* @throws \Cake\Http\Exception\InvalidCsrfTokenException When the CSRF token is invalid or missing.
*/
protected function validateToken(ServerRequestInterface $request, Session $session): void
{
$token = $session->read($this->_config['key']);
if (!$token || !is_string($token)) {
throw new InvalidCsrfTokenException(__d('cake', 'Missing or incorrect CSRF session key'));
}
$body = $request->getParsedBody();
if (is_array($body) || $body instanceof ArrayAccess) {
$post = (string)Hash::get($body, $this->_config['field']);
$post = $this->unsaltToken($post);
if (hash_equals($post, $token)) {
return;
}
}
$header = $request->getHeaderLine('X-CSRF-Token');
$header = $this->unsaltToken($header);
if (hash_equals($header, $token)) {
return;
}
throw new InvalidCsrfTokenException(__d(
'cake',
'CSRF token from either the request body or request headers did not match or is missing.'
));
}
}