Skip to content

Commit

Permalink
Security: Add form->protect() to validate token when submitting a form.
Browse files Browse the repository at this point in the history
Function called in user_edit.php
  • Loading branch information
jmontoyaa committed Apr 24, 2020
1 parent 42d1a5d commit bf50545
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions main/admin/user_edit.php
Expand Up @@ -99,6 +99,7 @@ function confirmation(name) {
api_get_self().'?user_id='.$user_id,
''
);
$form->protect();
$form->addElement('header', $tool_name);
$form->addElement('hidden', 'user_id', $user_id);

Expand Down
35 changes: 35 additions & 0 deletions main/inc/lib/pear/HTML/QuickForm.php
@@ -1,5 +1,7 @@
<?php

use ChamiloSession as Session;

/**
* Create, validate and process HTML forms
*
Expand Down Expand Up @@ -64,6 +66,7 @@ class HTML_QuickForm extends HTML_Common
{
const MAX_ELEMENT_ARGUMENT = 10;
private $dateTimePickerLibraryAdded;
private $token;

/**
* Array containing the form fields
Expand Down Expand Up @@ -227,7 +230,9 @@ public function __construct(
$attributes = null,
$trackSubmit = false
) {
$this->token = null;
parent::__construct($attributes);

$method = (strtoupper($method) == 'GET') ? 'get' : 'post';
$action = ($action == '') ? api_get_self() : $action;
$target = empty($target) ? array() : array('target' => $target);
Expand Down Expand Up @@ -270,6 +275,28 @@ public function __construct(
}
}

public function protect()
{
$token = $this->getSubmitValue('protect_token');
if (null === $token) {
$token = Security::get_token();
} else {
$token = Security::get_existing_token();
}
$this->addHidden('protect_token', $token);
$this->setToken($token);
}

public function setToken($token)
{
$this->token = $token;
}

public function getToken()
{
return $this->token;
}

/**
* Returns the current API version
*
Expand Down Expand Up @@ -1401,6 +1428,14 @@ public function validate()
return false;
}

if (null !== $this->getToken()) {
$check = Security::check_token('form', $this);
Security::clear_token();
if (false === $check) {
return false;
}
}

$registry =& HTML_QuickForm_RuleRegistry::singleton();

foreach ($this->_rules as $target => $rules) {
Expand Down
11 changes: 10 additions & 1 deletion main/inc/lib/security.lib.php
@@ -1,4 +1,5 @@
<?php

/* For licensing terms, see /license.txt */

use Chamilo\CoreBundle\Component\HTMLPurifier\Filter\AllowIframes;
Expand Down Expand Up @@ -143,7 +144,7 @@ public static function getTokenFromSession()
*
* @return bool True if it's the right token, false otherwise
*/
public static function check_token($request_type = 'post')
public static function check_token($request_type = 'post', FormValidator $form = null)
{
$sessionToken = Session::read('sec_token');
switch ($request_type) {
Expand All @@ -164,6 +165,14 @@ public static function check_token($request_type = 'post')
return true;
}

return false;
case 'form':
$token = $form->getSubmitValue('protect_token');

if (!empty($sessionToken) && !empty($token) && $sessionToken === $token) {
return true;
}

return false;
default:
if (!empty($sessionToken) && isset($request_type) && $sessionToken === $request_type) {
Expand Down

0 comments on commit bf50545

Please sign in to comment.