Commit d8dbd130 authored by Côme Chilliet's avatar Côme Chilliet
Browse files

:sparkels: feat(core) Add CSRF protection token

issue #5840
Showing with 55 additions and 3 deletions
+55 -3
<?php <?php
/* /*
This code is part of FusionDirectory (http://www.fusiondirectory.org/) This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2003-2010 Cajus Pollmeier Copyright (C) 2003-2010 Cajus Pollmeier
Copyright (C) 2011-2016 FusionDirectory Copyright (C) 2011-2018 FusionDirectory
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -54,6 +53,8 @@ if (!session::global_is_set('connected')) { ...@@ -54,6 +53,8 @@ if (!session::global_is_set('connected')) {
exit; exit;
} }
CSRFProtection::check();
$ui = session::global_get('ui'); $ui = session::global_get('ui');
$config = session::global_get('config'); $config = session::global_get('config');
...@@ -324,7 +325,8 @@ if (session::is_set('errors') && session::get('errors') != "") { ...@@ -324,7 +325,8 @@ if (session::is_set('errors') && session::get('errors') != "") {
$focus = '<script type="text/javascript">'; $focus = '<script type="text/javascript">';
$focus .= 'next_msg_dialog();'; $focus .= 'next_msg_dialog();';
$focus .= '</script>'; $focus .= '</script>';
$smarty->assign("focus", $focus); $smarty->assign('focus', $focus);
$smarty->assign('CSRFtoken', CSRFProtection::getToken());
/* Set channel if needed */ /* Set channel if needed */
//TODO: * move all global session calls to global_ //TODO: * move all global session calls to global_
......
...@@ -49,6 +49,8 @@ session::start(); ...@@ -49,6 +49,8 @@ session::start();
session::global_set('DEBUGLEVEL', 0); session::global_set('DEBUGLEVEL', 0);
session::set('errorsAlreadyPosted', array()); session::set('errorsAlreadyPosted', array());
CSRFProtection::check();
/* Attribute initialization, reset errors */ /* Attribute initialization, reset errors */
reset_errors(); reset_errors();
...@@ -123,6 +125,7 @@ $smarty->assign("navigation", $setup->get_navigation_html()); ...@@ -123,6 +125,7 @@ $smarty->assign("navigation", $setup->get_navigation_html());
$smarty->assign("headline_image", $setup->get_header_image()); $smarty->assign("headline_image", $setup->get_header_image());
$smarty->assign("headline", $setup->get_header_text()); $smarty->assign("headline", $setup->get_header_text());
$smarty->assign("focus", $focus); $smarty->assign("focus", $focus);
$smarty->assign('CSRFtoken', CSRFProtection::getToken());
$smarty->assign("msg_dialogs", msg_dialog::get_dialogs()); $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
if ($error_collector != "") { if ($error_collector != "") {
......
...@@ -55,6 +55,7 @@ ...@@ -55,6 +55,7 @@
{$errors} {$errors}
{$focus} {$focus}
<input type="hidden" name="php_c_check" value="1"/> <input type="hidden" name="php_c_check" value="1"/>
<input type="hidden" name="CSRFtoken" value="{$CSRFtoken}"/>
</form> </form>
......
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2017-2018 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class CSRFProtection
{
public static function check()
{
if (empty($_POST)) {
return;
}
if (empty($_POST['CSRFtoken'])) {
throw new Exception('CSRF protection token missing');
  • :warning: Define and throw a dedicated exception instead of using a generic one. :blue_book:

    By Ghost User on 2018-06-04T12:45:42 (imported from GitLab)

Please register or sign in to reply
}
$validToken = static::getToken();
  • :warning: Remove this unused "$validToken" local variable. :blue_book:

    By Ghost User on 2018-06-04T12:45:41 (imported from GitLab)

Please register or sign in to reply
if ($_POST['CSRFtoken'] !== static::getToken()) {
throw new Exception('CSRF protection token invalid');
  • :warning: Define and throw a dedicated exception instead of using a generic one. :blue_book:

    By Ghost User on 2018-06-04T12:45:42 (imported from GitLab)

Please register or sign in to reply
}
}
public static function getToken()
{
if (!session::is_set('CSRFtoken')) {
session::set('CSRFtoken', standAlonePage::generateRandomHash());
}
return session::get('CSRFtoken');
}
}
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
{$errors} {$errors}
{$focus} {$focus}
<input type="hidden" name="setup_goto_step" value=""/> <input type="hidden" name="setup_goto_step" value=""/>
<input type="hidden" name="CSRFtoken" value="{$CSRFtoken}"/>
</form> </form>
</body> </body>
......
  • SonarQube analysis reported 3 issues

    • :warning: 3 major

    Watch the comments in this conversation to review them.

    By Ghost User on 2018-06-04T12:45:43 (imported from GitLab)

  • bmortier @bmortier

    mentioned in commit 62ffce88

    By Côme Chilliet on 2018-06-13T12:46:14 (imported from GitLab)

    ·

    mentioned in commit 62ffce88

    By Côme Chilliet on 2018-06-13T12:46:14 (imported from GitLab)

    Toggle commit list
  • bmortier @bmortier

    mentioned in merge request !296

    By Côme Chilliet on 2018-06-13T12:46:41 (imported from GitLab)

    ·

    mentioned in merge request !296

    By Côme Chilliet on 2018-06-13T12:46:41 (imported from GitLab)

    Toggle commit list
  • bmortier @bmortier

    mentioned in commit fd690297

    By Côme Chilliet on 2018-06-13T13:13:58 (imported from GitLab)

    ·

    mentioned in commit fd690297

    By Côme Chilliet on 2018-06-13T13:13:58 (imported from GitLab)

    Toggle commit list
  • bmortier @bmortier

    mentioned in merge request !298

    By Côme Chilliet on 2018-06-13T13:14:19 (imported from GitLab)

    ·

    mentioned in merge request !298

    By Côme Chilliet on 2018-06-13T13:14:19 (imported from GitLab)

    Toggle commit list
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment