Skip to content

Commit bf50545

Browse files
committed
Security: Add form->protect() to validate token when submitting a form.
Function called in user_edit.php
1 parent 42d1a5d commit bf50545

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

main/admin/user_edit.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ function confirmation(name) {
9999
api_get_self().'?user_id='.$user_id,
100100
''
101101
);
102+
$form->protect();
102103
$form->addElement('header', $tool_name);
103104
$form->addElement('hidden', 'user_id', $user_id);
104105

main/inc/lib/pear/HTML/QuickForm.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use ChamiloSession as Session;
4+
35
/**
46
* Create, validate and process HTML forms
57
*
@@ -64,6 +66,7 @@ class HTML_QuickForm extends HTML_Common
6466
{
6567
const MAX_ELEMENT_ARGUMENT = 10;
6668
private $dateTimePickerLibraryAdded;
69+
private $token;
6770

6871
/**
6972
* Array containing the form fields
@@ -227,7 +230,9 @@ public function __construct(
227230
$attributes = null,
228231
$trackSubmit = false
229232
) {
233+
$this->token = null;
230234
parent::__construct($attributes);
235+
231236
$method = (strtoupper($method) == 'GET') ? 'get' : 'post';
232237
$action = ($action == '') ? api_get_self() : $action;
233238
$target = empty($target) ? array() : array('target' => $target);
@@ -270,6 +275,28 @@ public function __construct(
270275
}
271276
}
272277

278+
public function protect()
279+
{
280+
$token = $this->getSubmitValue('protect_token');
281+
if (null === $token) {
282+
$token = Security::get_token();
283+
} else {
284+
$token = Security::get_existing_token();
285+
}
286+
$this->addHidden('protect_token', $token);
287+
$this->setToken($token);
288+
}
289+
290+
public function setToken($token)
291+
{
292+
$this->token = $token;
293+
}
294+
295+
public function getToken()
296+
{
297+
return $this->token;
298+
}
299+
273300
/**
274301
* Returns the current API version
275302
*
@@ -1401,6 +1428,14 @@ public function validate()
14011428
return false;
14021429
}
14031430

1431+
if (null !== $this->getToken()) {
1432+
$check = Security::check_token('form', $this);
1433+
Security::clear_token();
1434+
if (false === $check) {
1435+
return false;
1436+
}
1437+
}
1438+
14041439
$registry =& HTML_QuickForm_RuleRegistry::singleton();
14051440

14061441
foreach ($this->_rules as $target => $rules) {

main/inc/lib/security.lib.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/* For licensing terms, see /license.txt */
34

45
use Chamilo\CoreBundle\Component\HTMLPurifier\Filter\AllowIframes;
@@ -143,7 +144,7 @@ public static function getTokenFromSession()
143144
*
144145
* @return bool True if it's the right token, false otherwise
145146
*/
146-
public static function check_token($request_type = 'post')
147+
public static function check_token($request_type = 'post', FormValidator $form = null)
147148
{
148149
$sessionToken = Session::read('sec_token');
149150
switch ($request_type) {
@@ -164,6 +165,14 @@ public static function check_token($request_type = 'post')
164165
return true;
165166
}
166167

168+
return false;
169+
case 'form':
170+
$token = $form->getSubmitValue('protect_token');
171+
172+
if (!empty($sessionToken) && !empty($token) && $sessionToken === $token) {
173+
return true;
174+
}
175+
167176
return false;
168177
default:
169178
if (!empty($sessionToken) && isset($request_type) && $sessionToken === $request_type) {

0 commit comments

Comments
 (0)