Repository navigation
Expand file tree
/
Copy pathFormProtectionComponent.php
More file actions
166 lines (149 loc) · 5.48 KB
/
Copy pathFormProtectionComponent.php
File metadata and controls
166 lines (149 loc) · 5.48 KB
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
<?php
declare(strict_types=1);
/**
* 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 4.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Controller\Component;
use Cake\Controller\Component;
use Cake\Core\Configure;
use Cake\Event\EventInterface;
use Cake\Form\FormProtector;
use Cake\Http\Exception\BadRequestException;
use Cake\Http\Response;
use Cake\Routing\Router;
use Closure;
/**
* Protects against form tampering. It ensures that:
*
* - Form's action (URL) is not modified.
* - Unknown / extra fields are not added to the form.
* - Existing fields have not been removed from the form.
* - Values of hidden inputs have not been changed.
*
* @psalm-property array{validate:bool, unlockedFields:array, unlockedActions:array, validationFailureCallback:?\Closure} $_config
*/
class FormProtectionComponent extends Component
{
/**
* Default message used for exceptions thrown.
*
* @var string
*/
public const DEFAULT_EXCEPTION_MESSAGE = 'Form tampering protection token validation failed.';
/**
* Default config
*
* - `validate` - Whether to validate request body / data. Set to false to disable
* for data coming from 3rd party services, etc.
* - `unlockedFields` - Form fields to exclude from validation. Fields can
* be unlocked either in the Component, or with FormHelper::unlockField().
* Fields that have been unlocked are not required to be part of the POST
* and hidden unlocked fields do not have their values checked.
* - `unlockedActions` - Actions to exclude from POST validation checks.
* - `validationFailureCallback` - Callback to call in case of validation
* failure. Must be a valid Closure. Unset by default in which case
* exception is thrown on validation failure.
*
* @var array
*/
protected $_defaultConfig = [
'validate' => true,
'unlockedFields' => [],
'unlockedActions' => [],
'validationFailureCallback' => null,
];
/**
* Component startup.
*
* Token check happens here.
*
* @param \Cake\Event\EventInterface $event An Event instance
* @return \Cake\Http\Response|null
*/
public function startup(EventInterface $event): ?Response
{
$request = $this->getController()->getRequest();
$data = $request->getParsedBody();
$hasData = ($data || $request->is(['put', 'post', 'delete', 'patch']));
if (
!in_array($request->getParam('action'), $this->_config['unlockedActions'], true)
&& $hasData
&& $this->_config['validate']
) {
$session = $request->getSession();
$session->start();
$url = Router::url($request->getRequestTarget());
$formProtector = new FormProtector($this->_config);
$isValid = $formProtector->validate($data, $url, $session->id());
if (!$isValid) {
return $this->validationFailure($formProtector);
}
}
$token = [
'unlockedFields' => $this->_config['unlockedFields'],
];
$request = $request->withAttribute('formTokenData', [
'unlockedFields' => $token['unlockedFields'],
]);
if (is_array($data)) {
unset($data['_Token']);
$request = $request->withParsedBody($data);
}
$this->getController()->setRequest($request);
return null;
}
/**
* Events supported by this component.
*
* @return array
*/
public function implementedEvents(): array
{
return [
'Controller.startup' => 'startup',
];
}
/**
* Throws a 400 - Bad request exception or calls custom callback.
*
* If `validationFailureCallback` config is specified, it will use this
* callback by executing the method passing the argument as exception.
*
* @param \Cake\Form\FormProtector $formProtector Form Protector instance.
* @return \Cake\Http\Response|null If specified, validationFailureCallback's response, or no return otherwise.
* @throws \Cake\Http\Exception\BadRequestException
*/
protected function validationFailure(FormProtector $formProtector): ?Response
{
if (Configure::read('debug')) {
$exception = new BadRequestException($formProtector->getError());
} else {
$exception = new BadRequestException(static::DEFAULT_EXCEPTION_MESSAGE);
}
if ($this->_config['validationFailureCallback']) {
return $this->executeCallback($this->_config['validationFailureCallback'], $exception);
}
throw $exception;
}
/**
* Execute callback.
*
* @param \Closure $callback A valid callable
* @param \Cake\Http\Exception\BadRequestException $exception Exception instance.
* @return \Cake\Http\Response|null
*/
protected function executeCallback(Closure $callback, BadRequestException $exception): ?Response
{
return $callback($exception);
}
}