From 9cb2631b8ac5fd7c0de6df67d259209eece25305 Mon Sep 17 00:00:00 2001 From: Jerome Jutteau Date: Tue, 1 May 2018 12:17:22 +0200 Subject: [PATCH] [BUGFIX] Add CSRF protection for admin Signed-off-by: Jerome Jutteau --- admin.php | 116 +++++++++++++++++++++------------------------- lib/functions.php | 29 ++++++++++++ 2 files changed, 83 insertions(+), 62 deletions(-) diff --git a/admin.php b/admin.php index 4500a19..d3a069c 100644 --- a/admin.php +++ b/admin.php @@ -53,72 +53,58 @@ if (php_sapi_name() == "cli") { } /* Unlog if asked. */ - if (isset($_POST['action']) && (strcmp($_POST['action'], 'logout') == 0)) { - $_SESSION['admin_auth'] = false; + if (jirafeau_admin_session_logged() && isset($_POST['action']) && (strcmp($_POST['action'], 'logout') == 0)) { + jirafeau_admin_session_end(); } - /* Check classic admin password authentification. */ - if (isset($_POST['admin_password']) && empty($cfg['admin_http_auth_user'])) { - if ($cfg['admin_password'] === $_POST['admin_password'] || - $cfg['admin_password'] === hash('sha256', $_POST['admin_password'])) { - $_SESSION['admin_auth'] = true; - } else { - $_SESSION['admin_auth'] = false; - require(JIRAFEAU_ROOT . 'lib/template/header.php'); - echo '

'. - t('BAD_PSW') . '

'; + if (!jirafeau_admin_session_logged()) { + /* Test HTTP authentification. */ + if (!empty($cfg['admin_http_auth_user']) && + $cfg['admin_http_auth_user'] == $_SERVER['PHP_AUTH_USER']) { + jirafeau_admin_session_start(); + } + /* Test web password authentification. */ + else if (!empty($cfg['admin_password']) && isset($_POST['admin_password'])) { + if ($cfg['admin_password'] === $_POST['admin_password'] || + $cfg['admin_password'] === hash('sha256', $_POST['admin_password'])) { + jirafeau_admin_session_start(); + } else { + require(JIRAFEAU_ROOT . 'lib/template/header.php'); + echo '

'. t('BAD_PSW') . '

'; + require(JIRAFEAU_ROOT.'lib/template/footer.php'); + exit; + } + } + /* Admin password prompt form. */ + else { + require(JIRAFEAU_ROOT . 'lib/template/header.php'); ?> + + - -

'. - t('NO_ADMIN_AUTH') . - '

'; - require(JIRAFEAU_ROOT.'lib/template/footer.php'); - exit; - } /* Operations may take a long time. * Be sure PHP's safe mode is off. @@ -138,6 +124,7 @@ if (php_sapi_name() == "cli") {
+ @@ -150,6 +137,7 @@ if (php_sapi_name() == "cli") { + @@ -162,6 +150,7 @@ if (php_sapi_name() == "cli") { + @@ -174,6 +163,7 @@ if (php_sapi_name() == "cli") { + @@ -188,6 +178,7 @@ if (php_sapi_name() == "cli") { + @@ -202,8 +193,9 @@ if (php_sapi_name() == "cli") { + - + @@ -216,10 +208,10 @@ if (php_sapi_name() == "cli") { +
' . '' . '' . + jirafeau_admin_csrf_field() . '' . '' . '
' . '' . '' . + jirafeau_admin_csrf_field() . '' . '
' . '
' . '' . '' . + jirafeau_admin_csrf_field() . '' . '
' . ''; @@ -1248,3 +1251,29 @@ function jirafeau_escape($string) { return htmlspecialchars($string, ENT_QUOTES); } + +function jirafeau_admin_session_start() +{ + $_SESSION['admin_auth'] = true; + $_SESSION['admin_csrf'] = md5(uniqid(mt_rand(), true)); +} + +function jirafeau_admin_session_end() +{ + $_SESSION = array(); + session_destroy(); +} + +function jirafeau_admin_session_logged() +{ + return isset($_SESSION['admin_auth']) && + isset($_SESSION['admin_csrf']) && + isset($_POST['admin_csrf']) && + $_SESSION['admin_auth'] === true && + $_SESSION['admin_csrf'] === $_POST['admin_csrf']; +} + +function jirafeau_admin_csrf_field() +{ + return ""; +} -- 2.34.1